Compare commits

..

2 Commits

Author SHA1 Message Date
c7b708da77 added a get method 2024-01-15 10:11:22 -06:00
59bd42f185 Finished working on getter methods 2024-01-05 14:23:44 -06:00

85
utils/libs/get.sh Normal file
View File

@ -0,0 +1,85 @@
#!/usr/bin/bash
function get_category_names () {
local -n OUTPUT_CATEGORY_NAMES_VAR="${1:?"get_category_names : Output variable not provided"}"
local CATEGORY_NAME
for category_path in $BLOG_DIR/posts/*; do
CATEGORY_NAME="${category_path/*\//}"
OUTPUT_CATEGORY_NAMES_VAR+=( "$CATEGORY_NAME" )
done
}
function get_category_paths () {
local -a CATEGORY_PATHS=( $BLOG_DIR/posts/* ) CATEGORY_NAMES
local -n OUTPUT_CATEGORY_VAR="${1:?"get_categories : No category variable provided"}"
for path in ${CATEGORY_PATHS[@]}; do
OUTPUT_CATEGORY_VAR[${path/*\//}]=$path
done
}
function get_posts_in_category () {
local -A CATEGORIES
local CATEGORY_NAME="${1:?"get_posts_in_category : Category name not provided"}"
local -n OUTPUT_POST_VAR="${2:?"get_posts_in_category : Output variable not provided"}"
get_category_paths CATEGORIES
if [[ "${!CATEGORIES[@]}" != *$CATEGORY_NAME* ]]; then
echo "get_posts_in_category : There is no category by that name"
exit
fi
CATEGORY_PATH="${CATEGORIES[$CATEGORY_NAME]}"
for post in ${CATEGORY_PATH}/*.wiki; do
FULLPATH=$post
FILENAME="${post/*\/}"
TIMESTAMP=`cut -d '-' -f 1 <<< "$FILENAME"`
DATE=`date -d @$TIMESTAMP +"$DATE_FORMAT"`
WIKI_PATH=/posts/$CATEGORY_NAME/$FILENAME
POST_TITLE="`grep '%title' $FULLPATH | cut -d ' ' -f 2-` ($DATE)"
DATA="$FULLPATH:$WIKI_PATH:$POST_TITLE"
OUTPUT_POST_VAR+=( "$DATA" )
done
}
function get_posts_by_category () {
local -a CATEGORY_NAMES POSTS
local -A POST_BY_CATEGORY
local post_string
local -n OUTPUT_POST_ASSOCIATIVE_ARRAY="${1:?"get_posts_by_category : Output variable not provided"}"
get_category_names CATEGORY_NAMES
for name in ${CATEGORY_NAMES[@]}; do
get_posts_in_category $name POSTS
for i in ${!POSTS[@]}; do
post="${POSTS[$i]}"
if [[ "$post_string" ]]; then
post_string+="${POST_DELIMITER}${post}"
else
post_string="$post"
fi
done
OUTPUT_POST_ASSOCIATIVE_ARRAY[$name]="$post_string"
post_string=""
unset POSTS
done
}
function deserialize_posts () {
local -n OUTPUT_POST_ARRAY="${1:?"deserialize_posts : Output variable not provided"}"
local SERIALIZED_POSTS="${2:?"deserialize_posts : Serialized posts not provided"}"
OIFS=$IFS
IFS="$POST_DELIMITER"
read -a OUTPUT_POST_ARRAY <<< "$SERIALIZED_POSTS"
}