WIP to add creation date + update date
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
simonpetit 2025-12-04 15:54:52 +00:00
parent b619837af2
commit b89e8de06f

60
bob
View File

@ -27,7 +27,6 @@ usage()
echo " unpublish <post> unpublish the post"
}
init()
{
echo Name of the author of the blog :
@ -58,29 +57,55 @@ init()
update_index()
{
# Listing all posts and making an html list (with there link) out of them
posts=$(ls -t ./posts | awk '
BEGIN {
print "<ul>"
}
# Build lines with creation/modification epochs and formatted timestamps, safely handling spaces.
sorted_posts=$(
find ./posts -maxdepth 1 -type f -name '*.html' -print0 |
while IFS= read -r -d '' file; do
# Try birth time; fallback to mtime if birth not available
creation_epoch=$(stat -c %W "$file" 2>/dev/null)
if [ -z "$creation_epoch" ] || [ "$creation_epoch" -le 0 ]; then
creation_epoch=$(stat -c %Y "$file")
fi
modification_epoch=$(stat -c %Y "$file")
# Human-readable timestamps
created_fmt=$(date -d @"$creation_epoch" '+%Y-%m-%d %H:%M:%S')
modified_fmt=$(date -d @"$modification_epoch" '+%Y-%m-%d %H:%M:%S')
name=$(basename "$file")
# Use pipe as delimiter (filename already basename so cannot contain '/'; could contain '|', but that's rare;
# if paranoid, choose NUL and handle differently). This is simple and readable.
printf '%s|%s|%s|%s|%s\n' "$creation_epoch" "$created_fmt" "$modification_epoch" "$modified_fmt" "$name"
done |
# sort by numeric creation epoch descending (field 1)
sort -t'|' -k1,1nr |
# produce HTML from each record using awk
awk -F'|' '
BEGIN { print "<ul>" }
{
ref=$0
gsub(".html","",ref)
gsub(/[_-]/, " ", ref)
print "<li><a href=\"./posts/" $0 "\">" ref "</a></li>"
}
END {
print "</ul>"
}')
filename = $5
display = filename
sub(/\.html$/, "", display)
gsub(/[_-]/, " ", display)
# escape filename/display if needed (not done here); keep simple
printf "<li><a href=\"./posts/%s\">%s</a><div><p>created at %s</p><p>updated at %s</p></div></li>\n", filename, display, $2, $4
}
END { print "</ul>" }'
)
# retrieving the template for the index.html
template="${BOB_LIB}/template/index.html"
# replacing {{articles}} in the template with the actual list of articles from above
awk -v content="$posts" '{gsub(/{{articles}}/, content); print}' "$template" > "./index.html"
awk -v articles="$sorted_posts" '
{
gsub(/{{articles}}/, articles);
print
}' "$template" > "./index.html"
}
publish()
{
# Storing the path of the post/article to publish
@ -194,3 +219,4 @@ elif [[ "$1" == "deploy" ]]; then
elif [[ "$1" == "help" ]]; then
usage
fi