WIP to add creation date + update date
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
b619837af2
commit
b89e8de06f
60
bob
60
bob
@ -27,7 +27,6 @@ usage()
|
|||||||
echo " unpublish <post> unpublish the post"
|
echo " unpublish <post> unpublish the post"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
init()
|
init()
|
||||||
{
|
{
|
||||||
echo Name of the author of the blog :
|
echo Name of the author of the blog :
|
||||||
@ -58,29 +57,55 @@ init()
|
|||||||
|
|
||||||
update_index()
|
update_index()
|
||||||
{
|
{
|
||||||
# Listing all posts and making an html list (with there link) out of them
|
# Build lines with creation/modification epochs and formatted timestamps, safely handling spaces.
|
||||||
posts=$(ls -t ./posts | awk '
|
sorted_posts=$(
|
||||||
BEGIN {
|
find ./posts -maxdepth 1 -type f -name '*.html' -print0 |
|
||||||
print "<ul>"
|
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
|
filename = $5
|
||||||
gsub(".html","",ref)
|
display = filename
|
||||||
gsub(/[_-]/, " ", ref)
|
sub(/\.html$/, "", display)
|
||||||
print "<li><a href=\"./posts/" $0 "\">" ref "</a></li>"
|
gsub(/[_-]/, " ", display)
|
||||||
}
|
# escape filename/display if needed (not done here); keep simple
|
||||||
END {
|
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
|
||||||
print "</ul>"
|
}
|
||||||
}')
|
END { print "</ul>" }'
|
||||||
|
)
|
||||||
|
|
||||||
# retrieving the template for the index.html
|
# retrieving the template for the index.html
|
||||||
template="${BOB_LIB}/template/index.html"
|
template="${BOB_LIB}/template/index.html"
|
||||||
|
|
||||||
# replacing {{articles}} in the template with the actual list of articles from above
|
awk -v articles="$sorted_posts" '
|
||||||
awk -v content="$posts" '{gsub(/{{articles}}/, content); print}' "$template" > "./index.html"
|
{
|
||||||
|
gsub(/{{articles}}/, articles);
|
||||||
|
print
|
||||||
|
}' "$template" > "./index.html"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
publish()
|
publish()
|
||||||
{
|
{
|
||||||
# Storing the path of the post/article to publish
|
# Storing the path of the post/article to publish
|
||||||
@ -194,3 +219,4 @@ elif [[ "$1" == "deploy" ]]; then
|
|||||||
elif [[ "$1" == "help" ]]; then
|
elif [[ "$1" == "help" ]]; then
|
||||||
usage
|
usage
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user