upgrading publish and unpublish functions

This commit is contained in:
Simon Petit 2024-11-16 00:40:52 +01:00
parent dc591b29bb
commit 1ef57d8807

41
bob
View File

@ -58,6 +58,7 @@ 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>"
@ -72,28 +73,56 @@ update_index()
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"
}
publish()
{
# Storing the path of the post/article to publish
article_path=$1
post=$(awk -f ${BOB_LIB}/markdown.awk ./drafts/$1.md)
# from the relative path, only retrieving the name of the article (without file extension)
article_name=$(echo $article_path | cut -d '/' -f 2 | cut -d '.' -f 1)
# Convert the markdown draft into an html article and storing it locally
post=$(awk -f ${BOB_LIB}/markdown.awk ./$article_path)
# Retrieving the html article template
template="${BOB_LIB}/template/post.html"
escaped_post=$(echo "$post" | sed 's/&/\\&/g')
awk -v content="$escaped_post" '{gsub(/\{\{article\}\}/, content); print}' "$template" > "./posts/$1.html"
mv ./drafts/$1.md ./drafts/published/$1.md
# Escaping the & for next step to not confuse awk
escaped_post=$(echo "$post" | sed 's/&/\\&/g')
# In the template, replacing the string {{article}} by the actual content parsed above
awk -v content="$escaped_post" '{gsub(/\{\{article\}\}/, content); print}' "$template" > "./posts/$article_name.html"
# moving the draft into the "published" folder
mv ./$article_path ./drafts/published/$article_name.md
# updating the index.html as a new article shall appear in the list
update_index
}
unpublish()
{
rm ./posts/$1.html
mv ./drafts/published/$1.md ./drafts/$1.md
# storing the path of the article
article_path=$1
# from the relative path, only retrieving the name of the article (without file extension)
article_name=$(echo $article_path | cut -d '/' -f 2 | cut -d '.' -f 1)
# removing the html article
rm ./$article_path
# moving the ex-published article into the draft folder
mv ./drafts/published/$article_name.md ./drafts/$article_name.md
# updating index as a article is to be removed from the list
update_index
}