From 1ef57d8807fcf015951e4be4f1e485fa08324935 Mon Sep 17 00:00:00 2001 From: Simon Petit Date: Sat, 16 Nov 2024 00:40:52 +0100 Subject: [PATCH] upgrading publish and unpublish functions --- bob | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/bob b/bob index 829e7af..f0e8b11 100755 --- a/bob +++ b/bob @@ -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 "" }') + # 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() -{ - - post=$(awk -f ${BOB_LIB}/markdown.awk ./drafts/$1.md) - 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 +{ + # Storing the path of the post/article to publish + 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) + + # 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" + + # 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 }