#!/usr/bin/env bash if [ -z "${BOB_LIB}" ]; then BOB_LIB=/usr/local/lib/bob else BOB_LIB=./lib fi usage() { echo echo "This script is made for those who want to blog and are also addicted to the command line" echo echo "Run the initiation for a start. After that, place all your future blog posts, written in markdown (.md or .markdown), in the draft folder." echo echo "Once you publish your blog, all the drafts in the said folder will be converted to html, added to the posts folder and append to the index.html" echo echo "To remove a post, just remove it from the draft folder and republish your blog" echo echo "bob commands :" echo " help display this help" echo " init initiate a blog" echo " publish publish the post" echo " unpublish unpublish the post" } init() { echo Name of the author of the blog : read author echo "author=$author" > .blog.conf echo Name of the blog : read blog echo "blog=$blog" >> .blog.conf echo "Language of the blog : [en]" read lang if [ -z $lang ]; then lang=en fi echo "lang=$lang" >> .blog.conf echo "Activate dark mode : (y/N)" read dark if [ -z $dark ]; then dark=n fi echo "dark=$dark" >> .blog.conf mkdir drafts # mkdir templates mkdir posts # mkdir css # _init_css # _index "$blog" "$lang" "$dark" } 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() { # 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 } publish_one() { # Storing the path of the post/article to publish # The path is supposed to have this format "./drafts/published/
.* 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 4 | 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" } publish_all() { # List all drafts to be published published=$(ls -1 ./drafts/published) # turning it into an array published_array=($published) # Remove all html articles in case a previously published one was removed rm ./posts/*.html # Publish them one by one (ie turning md into html) for file in "${published_array[@]}"; do publish_one ./drafts/published/$file done # updating the index.html as new articles are supposedly present and some may be removed update_index } unpublish() { # 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 } unpublish_all() { rm ./posts/* mv ./drafts/published/* ./drafts/ } deploy() { echo "TODO" } if [[ $# -eq 0 ]]; then usage elif [[ "$1" == "help" ]]; then usage elif [[ "$1" == "usage" ]]; then usage elif [[ "$1" == "init" ]]; then init elif [[ "$1" == "publish" ]]; then publish_all elif [[ "$1" == "deploy" ]]; then deploy elif [[ "$1" == "help" ]]; then usage fi