From 59bd42f1859e32a60f502aa012b055adb11c4fe9 Mon Sep 17 00:00:00 2001 From: Tristan Ancelet Date: Fri, 5 Jan 2024 14:23:44 -0600 Subject: [PATCH] Finished working on getter methods --- utils/libs/get.sh | 85 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 utils/libs/get.sh diff --git a/utils/libs/get.sh b/utils/libs/get.sh new file mode 100644 index 0000000..eddc4a7 --- /dev/null +++ b/utils/libs/get.sh @@ -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/$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" +}