diff --git a/bob b/bob index 5630587..710d31e 100755 --- a/bob +++ b/bob @@ -1,493 +1,9 @@ #!/usr/bin/env bash -_get_blog_name() -{ - # Extract the configuration file - conf=`cat .blog.conf` - name_regex="blog\:([a-zA-Z ]+)" - - # Echo the name of the blog, if any was given - if [[ $conf =~ $name_regex ]]; then - echo ${BASH_REMATCH[1]} - fi -} - -_get_dark_param() -{ - # Extract the configuration file - conf=`cat .blog.conf` - dark_regex="dark:(y|n|N)" - - if [[ $conf =~ $dark_regex ]]; then - echo ${BASH_REMATCH[1]} - fi -} - -_get_blog_lang() -{ - # Extract the configuration file - conf=`cat .blog.conf` - lang_regex="lang:([a-z]+)" - - if [[ $conf =~ $lang_regex ]]; then - echo ${BASH_REMATCH[1]} - fi -} - -_add_header() -{ - post_name=$1 - blog_name=$2 - lang=$3 - dark=$4 - - css="../css/poststyle.css" - if [[ $dark =~ "y" ]]; then - css="../css/poststyle.dark.css" - fi - - cat > ./posts/$post_name.html << EOF -<!DOCTYPE html> -<html lang="$lang" dir="ltr"> - <head> - <meta charset="utf-8"> - <title>$post_name</title> - <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover"> - <link href="https://fonts.googleapis.com/css?family=Cutive+Mono|IBM+Plex+Mono&display=swap" rel="stylesheet"> - <link rel="stylesheet" type="text/css" href="$css"> - </head> - <body> - <article class='post'> - <header> - <h2><a href="../index.html">$blog_name</a></h2> - </header> -EOF -} - -_add_footer() -{ - post_name=$1 - cat >> ./posts/$post_name.html << EOF - </article> - <footer> - <span>generated by <a href="https://www.github.com/SiwonP/bob">bob</a></span> - </footer> - </body> -</html> -EOF - -} - - -_get_last_modif_date() -{ - # Extract the date of the last modification of the file $1 - # and echo it with format dd/mm/yyyy - date=`stat -f "%Sm" -t "%d/%m/%Y" $1` - echo $date -} - -publish() -{ - # delete_posts - - # Collect all configuration parameters so that the regex are only evaluted - # once per publication. Done to improve the speed of the publication - dark=$(_get_dark_param) - lang=$(_get_blog_lang) - blog_name=$(_get_blog_name) - - # list all the drafts stored as most recently modified to - # least recently modified, as it should appear on a blog - drafts=`ls -t ./bases/` - # echo $drafts - - # Tranform the string into an array - list=($(echo "$drafts" | tr ' ' '\n')) - - # date=$(_get_last_modif_date "./drafts/${list[0]}") - - # Regex to extract the name without the extension - name_regex="([a-zA-Z]+)\.(md|markdown)" - - # Array to story only the names of the posts - posts_names=() - - for (( i=0; i<${#list[@]}; i++ )); - do - if [[ ${list[i]} =~ $name_regex ]]; then - # Append the names to the array - posts_names+=(${BASH_REMATCH[1]}) - fi - _create_posts "./draft/${list[i]}" "$blog_name" "$lang" "$dark" - done - - # function to update the index.html - # Passing the drafts as argument since it's sorted - _update_index "$drafts" "$blog_name" "$lang" "$dark" -} - -_create_posts() -{ - draft=$1 - blog_name=$2 - lang=$3 - dark=$4 - - # Convert the markdown file $draft into html - # and store the result in $content - content=`multimarkdown --nolabels $draft` - - # Regex to extract the simple post name - md_regex="([a-zA-Z\-]+)\.(md|markdown)" - - if [[ $draft =~ $md_regex ]]; then - # If the file have md or markdown extension, cut it - post_name=${BASH_REMATCH[1]} - else - # if not and it has no extension, keep it this way - post_name=$draft - fi - # Integrate the html converted content into a proper html file - _build_post "$post_name" "$blog_name" "$content" "$lang" "$dark" -} - -_build_post() -{ - # Create the html file of the post $post_name with the - # previsouly converted markdown to html $content - post_name=$1 - blog_name=$2 - content=$3 - lang=$4 - dark=$5 - - _add_header "$post_name" "$blog_name" "$lang" "$dark" - echo $content >> ./posts/$post_name.html - _add_footer "$post_name" -} - -_update_index() -{ - title=$2 - lang=$3 - dark=$4 - - css="./css/indexstyle.css" - if [[ $dark =~ "y" ]]; then - css="./css/indexstyle.dark.css" - fi - - cat > index.html << EOF -<!DOCTYPE html> -<html lang="$lang" dir="ltr"> - <head> - <meta charset="utf-8"> - <title>$title</title> - <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover"> - <link href="https://fonts.googleapis.com/css?family=Cutive+Mono|IBM+Plex+Mono&display=swap" rel="stylesheet"> - <link rel="stylesheet" type="text/css" href="$css"> - </head> - <body> - <h1 class='title'>$title</h1> -EOF - -# Retrieve the string containing all drafts names and splitting it -# into an array -drafts=$1 -drafts_list=($(echo "$drafts" | tr ' ' '\n')) - -# Number of posts in the draft list, needed for the for loop -nbposts=${#drafts_list[@]} - -# If there are posts to post, begin the list -if [ $nbposts -gt 0 ] -then - echo "<ul class='posts_list'>" >> index.html -fi - -# Regex to extract just the name of the post -post_regex="([a-zA-Z\-]+)\.(md|markdown)" - -for (( i=0; i<${#drafts_list[@]}; i++ )); -do - # Retrieve the date of the last modification of the draft - date=$(_get_last_modif_date "./bases/${drafts_list[i]}") - - # In case the next regex doesn't work, even though it's supposed to - # work everytime - post_name="unknown" - - # Extract the strict name of the post in the same - # order of the drafts because they are sorted according - # to the last modification date - if [[ ${drafts_list[i]} =~ $post_regex ]]; then - post_name=${BASH_REMATCH[1]} - fi - - # Function to add items to the posts list - _add_post_link "./posts/$post_name.html" "$post_name" "$date" - -done - -# If there are posts posted, close the list -if [ $nbposts -gt 0 ]; then - echo "</ul>" >> index.html -fi - -cat >> index.html << EOF - </body> -</html> -EOF -} - -_add_post_link() -{ - url=$1 - post_name=$2 - date=$3 - post_name=${post_name//\-/ } - echo "<li class='post'>" >> index.html - echo "<a href="$url">$post_name</a>" >> index.html - echo "<span class='date'>$date</span>" >> index.html - echo "</li>" >> index.html -} - -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 bases - mkdir posts - mkdir css - _init_css - _index "$blog" "$lang" "$dark" -} - -_init_css() -{ - # Initiate css files for both the index and the posts, - # both dark and light themes - cat > ./css/indexstyle.css << EOF - .wrap { - width: 40vw; - margin-right: auto; - margin-left: auto; - } - .title { - margin-top: 5vh; - margin-bottom: 7vh; - text-align: center; - font-family: 'IBM Plex Mono', monospace; - } - .posts_list { - text-align: center; - padding-left: 0px; - list-style: none; - } - .post { - margin-bottom: 3vh; - font-family: 'IBM Plex Mono', monospace; - } - .post a:link { - text-decoration: none; - } - .post a:hover { - text-decoration: underline; - color: #AAA; - } - .post a { - color: black; - } - .date { - color: #777; - font-family: 'Cutive Mono', monospace; - } -EOF - -cat > ./css/indexstyle.dark.css << EOF -body { - background: #222; -} -.wrap { - width: 40vw; - margin-right: auto; - margin-left: auto; -} -.title { - margin-top: 5vh; - margin-bottom: 7vh; - color: #e1e1e1; - text-align: center; - font-family: 'IBM Plex Mono', monospace; -} -.posts_list { - text-align: center; - padding-left: 0px; - list-style: none; -} -.post { - margin-bottom: 3vh; - color: #e1e1e1; - font-family: 'IBM Plex Mono', monospace; -} -.post a:link { - color: #e1e1e1; - text-decoration: none; -} -.post a:hover { - text-decoration: underline; - color: #AAA; -} -.post a { - color: #e1e1e1; -} -.date { - color: #777; - font-family: 'Cutive Mono', monospace; -} -EOF - -cat > ./css/poststyle.css << EOF -body { - font-family: 'IBM Plex Mono', monospace; -} -header a { - text-decoration: none; -} -header a:hover { - text-decoration: underline; -} -header { - margin-bottom: 6vh; -} -.post { - width: 70vw; - margin-right: auto; - margin-left: auto; -} -footer { - width: 70vw; - margin-top: 6vh; - margin-right: auto; - margin-left: auto; - font-size: 1.3vh; -} -footer span a { - color: #FF8608; -} -EOF - -cat > ./css/poststyle.dark.css << EOF -body { - font-family: 'IBM Plex Mono', monospace; - background: #222; - color: #e1e1e1; -} -header a { - color: #777; - text-decoration: none; -} -header a:hover { - text-decoration: underline; - color: #e1e1e1; -} -header { - margin-bottom: 6vh; -} -.post { - width: 70vw; - margin-right: auto; - margin-left: auto; -} -footer { - width: 70vw; - margin-top: 6vh; - margin-right: auto; - margin-left: auto; - font-size: 1.3vh; -} -footer span a { - color: #FF8608; -} -EOF - -} - -_index() -{ - title=$1 - lang=$2 - dark=$3 - css="./css/indexstyle.css" - - if [[ $dark -eq "y" ]]; then - css="./css/indexstyle.dark.css" - fi - - # Create the first index file, empty of any posts - cat > index.html << EOF -<!DOCTYPE html> -<html lang="$lang" dir="ltr"> - <head> - <meta charset="utf-8"> - <title>$title</title> - <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover"> - <link href="https://fonts.googleapis.com/css?family=Cutive+Mono|IBM+Plex+Mono&display=swap" rel="stylesheet"> - <link rel="stylesheet" type="text/css" href="./css/indexstyle.css"> - </head> - <body> - <h1 class='title'>$title</h1> - </body> -</html> -EOF -} - -change_mode() -{ - # READ FILE LINE BY LINE SO THAT THE NEW CONF FILE IS NOT ON ONE LINE - echo "Activate dark mode : (y/N)" - read dark - if [ -z $lang ]; then - dark=N - fi - conf=`cat .blog.conf` - echo ${conf//dark:(y|n|N)/dark:$dark} > .blog.conf -} - -delete_posts() -{ - # posts=`ls ./posts/` - # echo $posts/ - # if [ "$posts" == "/" ]; then - # echo empty - # fi - - rm ./posts/*.html -} - usage() { echo - echo "This script is made for those who want to blog and are also addicted - to the command line" + 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." @@ -499,18 +15,112 @@ usage() your blog" echo echo "bob commands :" - echo " help display this help" - echo " init initiate the blog" - echo " publish publish the blog" + 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() +{ + 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>" + }') + + template="./lib/template/index.html" + awk -v content="$posts" '{gsub(/{{articles}}/, content); print}' "$template" > "./index.html" + +} + +publish() +{ + post=$(awk -f lib/markdown.awk ./drafts/$1.md) + template="./lib/template/post.html" + awk -v content="$post" '{gsub(/{{article}}/, content); print}' "$template" > "./posts/$1.html" + mv ./drafts/$1.md ./drafts/published/$1.md + + update_index +} + +unpublish() +{ + rm ./posts/$1.html + mv ./drafts/published/$1.md ./drafts/$1.md + + update_index +} + +unpublish_all() +{ + rm ./posts/* +} + +deploy() +{ + scp +} + 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 + 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 diff --git a/bob2 b/bob2 deleted file mode 100755 index 710d31e..0000000 --- a/bob2 +++ /dev/null @@ -1,126 +0,0 @@ -#!/usr/bin/env bash - -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() -{ - 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>" - }') - - template="./lib/template/index.html" - awk -v content="$posts" '{gsub(/{{articles}}/, content); print}' "$template" > "./index.html" - -} - -publish() -{ - post=$(awk -f lib/markdown.awk ./drafts/$1.md) - template="./lib/template/post.html" - awk -v content="$post" '{gsub(/{{article}}/, content); print}' "$template" > "./posts/$1.html" - mv ./drafts/$1.md ./drafts/published/$1.md - - update_index -} - -unpublish() -{ - rm ./posts/$1.html - mv ./drafts/published/$1.md ./drafts/$1.md - - update_index -} - -unpublish_all() -{ - rm ./posts/* -} - -deploy() -{ - scp -} - -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