blog/utils/libs/blog-page.sh

49 lines
1.4 KiB
Bash
Raw Normal View History

2023-09-01 03:54:17 +00:00
#!/usr/bin/bash
function get_blog_pages () {
2023-09-01 04:40:43 +00:00
## Getting the variable to set the list to externally
local VAR_NAME="${1:?"get_blog_pages : No variable was provided to lod the pages into"}"
2023-09-01 03:54:17 +00:00
local -n VAR="$VAR_NAME"
2023-09-01 04:40:43 +00:00
## Setting the list to the available blog files.
2023-09-03 01:22:34 +00:00
if ls -1 $BLOG_DIR/*.wiki >/dev/null 2>&1; then
VAR=( $(ls -1 $BLOG_DIR/*.wiki | grep -v 'index.wiki' ) )
fi
2023-09-01 03:54:17 +00:00
}
function get_last_5_pages (){
2023-09-01 04:40:43 +00:00
## Index to use in the iterrations later
local INDEX
## Local variable to store pages (locally)
2023-09-01 03:54:17 +00:00
local -a pages
2023-09-01 04:40:43 +00:00
## Variable name to dump list items into
local VAR_NAME="${1:?"get_last_5_pages : A variable name was not passed into the function"}"
## Reference variable to handle setting value of external variable
local -n VAR="$VAR_NAME"
## Page Count Variable
2023-09-01 03:54:17 +00:00
local PAGES
2023-09-01 04:40:43 +00:00
## Grabbing pages from above function
2023-09-01 03:54:17 +00:00
get_blog_pages pages
2023-09-01 04:40:43 +00:00
## Getting page count
2023-09-01 03:54:17 +00:00
PAGES=${#pages[@]};
2023-09-01 04:40:43 +00:00
# if the count is less than the minimum, then just set the list as the one grabbed above
2023-09-01 03:54:17 +00:00
if [[ $PAGES -lt 5 ]]; then
2023-09-01 04:40:43 +00:00
VAR=( ${pages[@]} )
2023-09-01 03:54:17 +00:00
else
2023-09-01 04:40:43 +00:00
## Otherwise, itterate through the last 5 blog posts
INDEX=-5
while [[ $INDEX -ne 0 ]]; do
VAR+=( "${pages[$INDEX]}" )
## Increment the index so that we get closer and closer to end of the list
((INDEX++))
done
2023-09-01 03:54:17 +00:00
fi
}