bob/bob

165 lines
4.3 KiB
Bash
Executable File

#!/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 <post> publish the post"
echo " unpublish <post> 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 "<ul>"
}
{
ref=$0
gsub(".html","",ref)
gsub(/[_-]/, " ", ref)
print "<li><a href=\"./posts/" $0 "\">" ref "</a></li>"
}
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"
}
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
}
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
if [[ $# -eq 1 ]]; then
echo "Usage : bob publish <draft_name>"
else
publish $2
fi
elif [[ "$1" == "unpublish" ]]; then
if [[ $# -eq 1 ]]; then
echo "Usage : bob unpublish <post_name>"
else
unpublish $2
fi
elif [[ "$1" == "deploy" ]]; then
deploy
elif [[ "$1" == "help" ]]; then
usage
fi