From 1c3a7ffdb338efef4e1c799fff45e61185a44b73 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Tue, 12 Oct 2021 11:11:04 -0700 Subject: [PATCH 1/4] plugin/jekyll: `shfmt` && `shellcheck` Use bashisms, remove suplerfous variable, use parameter default value replacement --- plugins/available/jekyll.plugin.bash | 520 ++++++++++++--------------- 1 file changed, 235 insertions(+), 285 deletions(-) diff --git a/plugins/available/jekyll.plugin.bash b/plugins/available/jekyll.plugin.bash index c340c432..7fef0065 100644 --- a/plugins/available/jekyll.plugin.bash +++ b/plugins/available/jekyll.plugin.bash @@ -1,367 +1,317 @@ +# shellcheck shell=bash cite about-plugin about-plugin 'manage your jekyll site' -editpost() { - about 'edit a post' - param '1: site directory' - group 'jekyll' +function editpost() { + about 'edit a post' + param '1: site directory' + group 'jekyll' - unset SITE - if [ -z "$1" ] - then - echo "Error: no site specified." - echo "The site is the name of the directory your project is in." - return 1 - fi + local SITE site POST DATE TITLE POSTS TMPFILE POST_TO_EDIT + local -i COUNTER + if [[ -z "${1:-}" ]]; then + echo "Error: no site specified." + echo "The site is the name of the directory your project is in." + return 1 + fi - for site in ${SITES[@]} - do - if [ "${site##*/}" = "$1" ] - then - SITE=$site - break - fi - done + for site in "${SITES[@]:-}"; do + if [[ "${site##*/}" == "$1" ]]; then + SITE="${site}" + break + fi + done - if [ -z "$SITE" ] - then - echo "No such site." - return 1 - fi + if [[ -z "${SITE:-}" ]]; then + echo "No such site." + return 1 + fi - builtin cd "$SITE/_posts" + builtin cd "${SITE}/_posts" || return - COUNTER=1 - NUMBER="$RANDOM" - TMPFILE="/tmp/editpost-$NUMBER" + COUNTER=1 + TMPFILE="/tmp/editpost-$RANDOM" - for POST in * - do - DATE=`echo $POST | grep -oE "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}"` - TITLE=`cat $POST | grep -oE "title: (.+)"` - TITLE=`echo $TITLE | sed 's/title: //'` - echo "$COUNTER) $DATE $TITLE" >> "$TMPFILE" - POSTS[$COUNTER]=$POST - COUNTER=`expr $COUNTER + 1` - done - less $TMPFILE - read -p "Number of post to edit: " POST_TO_EDIT - if [ -z "$JEKYLL_EDITOR" ] - then - nano "${POSTS[$POST_TO_EDIT]}" - else - "$JEKYLL_EDITOR" "${POSTS[$POST_TO_EDIT]}" - fi + for POST in *; do + DATE="$(echo "${POST}" | grep -oE "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}")" + TITLE="$(grep -oE "title: (.+)" < "${POST}")" + TITLE="${TITLE/title: /}" + echo "${COUNTER}) ${DATE} ${TITLE}" >> "${TMPFILE}" + POSTS[COUNTER]="$POST" + COUNTER="$((COUNTER + 1))" + done + less "${TMPFILE}" + read -rp "Number of post to edit: " POST_TO_EDIT + "${JEKYLL_EDITOR:-${VISUAL:-${EDITOR:-${ALTERNATE_EDITOR:-nano}}}}" "${POSTS[$POST_TO_EDIT]}" } -newpost() { - about 'create a new post' - param '1: site directory' - group 'jekyll' +function newpost() { + about 'create a new post' + param '1: site directory' + group 'jekyll' - unset SITE - if [ -z "$1" ] - then - echo "Error: no site specified." - echo "The site is the name of the directory your project is in." - return 1 - fi + local SITE site FNAME_POST_TITLE FNAME YAML_DATE + local JEKYLL_FORMATTING FNAME_DATE OPTIONS OPTION POST_TYPE POST_TITLE + if [ -z "$1" ]; then + echo "Error: no site specified." + echo "The site is the name of the directory your project is in." + return 1 + fi - if [ -z "$SITE" ] - then - echo "No such site." - return 1 - fi + if [ -z "$SITE" ]; then + echo "No such site." + return 1 + fi - loc=0 + loc=0 - for site in ${SITES[@]} - do - if [ "${site##*/}" = "$1" ] - then - SITE=$site - JEKYLL_FORMATTING=${MARKUPS[$loc]} - break - fi - loc=$(($loc+1)) - done + for site in "${SITES[@]}"; do + if [ "${site##*/}" = "$1" ]; then + SITE=$site + JEKYLL_FORMATTING=${MARKUPS[$loc]} + break + fi + loc=$((loc + 1)) + done - # 'builtin cd' into the local jekyll root + # 'builtin cd' into the local jekyll root - builtin cd "$SITE/_posts" + builtin cd "${SITE}/_posts" || return - # Get the date for the new post's filename + # Get the date for the new post's filename - FNAME_DATE=$(date "+%Y-%m-%d") + FNAME_DATE="$(date "+%Y-%m-%d")" - # If the user is using markdown or textile formatting, let them choose what type of post they want. Sort of like Tumblr. + # If the user is using markdown or textile formatting, let them choose what type of post they want. Sort of like Tumblr. - OPTIONS="Text Quote Image Audio Video Link" + OPTIONS=('Text' 'Quote' 'Image' 'Audio' 'Video' 'Link') - if [ $JEKYLL_FORMATTING = "markdown" -o $JEKYLL_FORMATTING = "textile" ] - then - select OPTION in $OPTIONS - do - if [[ $OPTION = "Text" ]] - then - POST_TYPE="Text" - break - fi + if [[ $JEKYLL_FORMATTING == "markdown" || $JEKYLL_FORMATTING == "textile" ]]; then + select OPTION in "${OPTIONS[@]}"; do + if [[ $OPTION = "Text" ]]; then + POST_TYPE="Text" + break + fi - if [[ $OPTION = "Quote" ]] - then - POST_TYPE="Quote" - break - fi + if [[ $OPTION = "Quote" ]]; then + POST_TYPE="Quote" + break + fi - if [[ $OPTION = "Image" ]] - then - POST_TYPE="Image" - break - fi + if [[ $OPTION = "Image" ]]; then + POST_TYPE="Image" + break + fi - if [[ $OPTION = "Audio" ]] - then - POST_TYPE="Audio" - break - fi + if [[ $OPTION = "Audio" ]]; then + POST_TYPE="Audio" + break + fi - if [[ $OPTION = "Video" ]] - then - POST_TYPE="Video" - break - fi + if [[ $OPTION = "Video" ]]; then + POST_TYPE="Video" + break + fi - if [[ $OPTION = "Link" ]] - then - POST_TYPE="Link" - break - fi - done - fi + if [[ $OPTION = "Link" ]]; then + POST_TYPE="Link" + break + fi + done + fi - # Get the title for the new post + # Get the title for the new post - read -p "Enter title of the new post: " POST_TITLE + read -rp "Enter title of the new post: " POST_TITLE - # Convert the spaces in the title to hyphens for use in the filename + # Convert the spaces in the title to hyphens for use in the filename - FNAME_POST_TITLE=`echo $POST_TITLE | tr ' ' "-"` + FNAME_POST_TITLE="${POST_TITLE/ /-}" - # Now, put it all together for the full filename + # Now, put it all together for the full filename - FNAME="$FNAME_DATE-$FNAME_POST_TITLE.$JEKYLL_FORMATTING" + FNAME="$FNAME_DATE-$FNAME_POST_TITLE.$JEKYLL_FORMATTING" - # And, finally, create the actual post file. But we're not done yet... + # And, finally, create the actual post file. But we're not done yet... + # Write a little stuff to the file for the YAML Front Matter - touch "$FNAME" + echo "---" >> "${FNAME}" - # Write a little stuff to the file for the YAML Front Matter + # Now we have to get the date, again. But this time for in the header (YAML Front Matter) of + # the file - echo "---" >> $FNAME + YAML_DATE="$(date "+%B %d %Y %X")" - # Now we have to get the date, again. But this time for in the header (YAML Front Matter) of - # the file + # Echo the YAML Formatted date to the post file - YAML_DATE=$(date "+%B %d %Y %X") + echo "date: $YAML_DATE" >> "${FNAME}" - # Echo the YAML Formatted date to the post file + # Echo the original post title to the YAML Front Matter header - echo "date: $YAML_DATE" >> $FNAME + echo "title: $POST_TITLE" >> "${FNAME}" - # Echo the original post title to the YAML Front Matter header + # And, now, echo the "post" layout to the YAML Front Matter header - echo "title: $POST_TITLE" >> $FNAME + echo "layout: post" >> "${FNAME}" - # And, now, echo the "post" layout to the YAML Front Matter header + # Close the YAML Front Matter Header - echo "layout: post" >> $FNAME + echo "---" >> "${FNAME}" + echo >> "${FNAME}" - # Close the YAML Front Matter Header + # Generate template text based on the post type - echo "---" >> $FNAME - echo >> $FNAME + if [[ $JEKYLL_FORMATTING == "markdown" ]]; then + if [[ $POST_TYPE == "Text" ]]; then + true + fi - # Generate template text based on the post type + if [[ $POST_TYPE == "Quote" ]]; then + echo "> Quote" >> "${FNAME}" + echo >> "${FNAME}" + echo "— Author" >> "${FNAME}" + fi - if [[ $JEKYLL_FORMATTING = "markdown" ]] - then - if [[ $POST_TYPE = "Text" ]] - then - true - fi + if [[ $POST_TYPE == "Image" ]]; then + echo "![Alternate Text](/path/to/image/or/url)" >> "${FNAME}" + fi - if [[ $POST_TYPE = "Quote" ]] - then - echo "> Quote" >> $FNAME - echo >> $FNAME - echo "— Author" >> $FNAME - fi + if [[ $POST_TYPE == "Audio" ]]; then + echo "" >> "${FNAME}" + fi - if [[ $POST_TYPE = "Image" ]] - then - echo "![Alternate Text](/path/to/image/or/url)" >> $FNAME - fi + if [[ $POST_TYPE == "Video" ]]; then + echo "" >> "${FNAME}" + fi - if [[ $POST_TYPE = "Audio" ]] - then - echo "" >> $FNAME - fi + if [[ $POST_TYPE == "Link" ]]; then + echo "[link][1]" >> "${FNAME}" + echo >> "${FNAME}" + echo "> Quote" >> "${FNAME}" + echo >> "${FNAME}" + echo "[1]: url" >> "${FNAME}" + fi + fi - if [[ $POST_TYPE = "Video" ]] - then - echo "" >> $FNAME - fi + if [[ $JEKYLL_FORMATTING == "textile" ]]; then + if [[ $POST_TYPE == "Text" ]]; then + true + fi - if [[ $POST_TYPE = "Link" ]] - then - echo "[link][1]" >> $FNAME - echo >> $FNAME - echo "> Quote" >> $FNAME - echo >> $FNAME - echo "[1]: url" >> $FNAME - fi - fi + if [[ $POST_TYPE == "Quote" ]]; then + echo "bq. Quote" >> "${FNAME}" + echo >> "${FNAME}" + echo "— Author" >> "${FNAME}" + fi - if [[ $JEKYLL_FORMATTING = "textile" ]] - then - if [[ $POST_TYPE = "Text" ]] - then - true - fi + if [[ $POST_TYPE == "Image" ]]; then + echo "!url(alt text)" >> "${FNAME}" + fi - if [[ $POST_TYPE = "Quote" ]] - then - echo "bq. Quote" >> $FNAME - echo >> $FNAME - echo "— Author" >> $FNAME - fi + if [[ $POST_TYPE == "Audio" ]]; then + echo "" >> "${FNAME}" + fi - if [[ $POST_TYPE = "Image" ]] - then - echo "!url(alt text)" >> $FNAME - fi + if [[ $POST_TYPE == "Video" ]]; then + echo "" >> "${FNAME}" + fi - if [[ $POST_TYPE = "Audio" ]] - then - echo "" >> $FNAME - fi + if [[ $POST_TYPE == "Link" ]]; then + echo "\"Site\":url" >> "${FNAME}" + echo >> "${FNAME}" + echo "bq. Quote" >> "${FNAME}" + fi + fi - if [[ $POST_TYPE = "Video" ]] - then - echo "" >> $FNAME - fi + # Open the file in your favorite editor - if [[ $POST_TYPE = "Link" ]] - then - echo "\"Site\":url" >> $FNAME - echo >> $FNAME - echo "bq. Quote" >> $FNAME - fi - fi - - # Open the file in your favorite editor - - "$JEKYLL_EDITOR" $FNAME + "${JEKYLL_EDITOR:-${VISUAL:-${EDITOR:-${ALTERNATE_EDITOR:-nano}}}}" "${FNAME}" } function testsite() { - about 'launches local jekyll server' - param '1: site directory' - group 'jekyll' + about 'launches local jekyll server' + param '1: site directory' + group 'jekyll' - unset SITE - if [ -z "$1" ] - then - echo "Error: no site specified." - echo "The site is the name of the directory your project is in." - return 1 - fi + local SITE site + if [[ -z "${1:-}" ]]; then + echo "Error: no site specified." + echo "The site is the name of the directory your project is in." + return 1 + fi - for site in ${SITES[@]} - do - if [ "${site##*/}" = "$1" ] - then - SITE=$site - break - fi - done + for site in "${SITES[@]}"; do + if [[ "${site##*/}" == "$1" ]]; then + SITE="$site" + break + fi + done - if [ -z "$SITE" ] - then - echo "No such site." - return 1 - fi + if [[ -z "${SITE}" ]]; then + echo "No such site." + return 1 + fi - builtin cd $SITE - jekyll --server --auto + builtin cd "${SITE}" || return + jekyll --server --auto } function buildsite() { - about 'builds site' - param '1: site directory' - group 'jekyll' + about 'builds site' + param '1: site directory' + group 'jekyll' - unset SITE - if [ -z "$1" ] - then - echo "Error: no site specified." - echo "The site is the name of the directory your project is in." - return 1 - fi + local SITE site + if [[ -z "${1:-}" ]]; then + echo "Error: no site specified." + echo "The site is the name of the directory your project is in." + return 1 + fi - for site in ${SITES[@]} - do - if [ "${site##*/}" = "$1" ] - then - SITE=$site - break - fi - done + for site in "${SITES[@]}"; do + if [[ "${site##*/}" == "$1" ]]; then + SITE="$site" + break + fi + done - if [ -z "$SITE" ] - then - echo "No such site." - return 1 - fi + if [[ -z "${SITE}" ]]; then + echo "No such site." + return 1 + fi - builtin cd $SITE - rm -rf _site - jekyll --no-server + builtin cd "${SITE}" || return + rm -rf _site + jekyll --no-server } function deploysite() { - about 'rsyncs site to remote host' - param '1: site directory' - group 'jekyll' + about 'rsyncs site to remote host' + param '1: site directory' + group 'jekyll' - unset SITE - if [ -z "$1" ] - then - echo "Error: no site specified." - echo "The site is the name of the directory your project is in." - return 1 - fi + local SITE site REMOTE + local -i loc=0 + if [[ -z "${1:-}" ]]; then + echo "Error: no site specified." + echo "The site is the name of the directory your project is in." + return 1 + fi - loc=0 + for site in "${SITES[@]}"; do + if [ "${site##*/}" == "$1" ]; then + SITE="$site" + REMOTE="${REMOTES[loc]}" + break + fi + loc=$((loc + 1)) + done - for site in ${SITES[@]} - do - if [ "${site##*/}" = "$1" ] - then - SITE=$site - REMOTE=${REMOTES[$loc]} - break - fi - loc=$(($loc+1)) - done + if [[ -z "${SITE}" ]]; then + echo "No such site." + return 1 + fi - if [ -z "$SITE" ] - then - echo "No such site." - return 1 - fi - - builtin cd $SITE - rsync -rz $REMOTE + builtin cd "${SITE}" || return + rsync -rz "${REMOTE?}" } From c0aad51afd2e2336c3eadc4bf71fb738398812ce Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sun, 10 Oct 2021 00:07:48 -0700 Subject: [PATCH 2/4] main: move Jekyll stuff to plugins/jekyll If the user doesn't load the Jekyll plugin, then don't load any Jeykll stuff. --- bash_it.sh | 7 ------- plugins/available/jekyll.plugin.bash | 5 +++++ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/bash_it.sh b/bash_it.sh index de655e81..bda3407e 100755 --- a/bash_it.sh +++ b/bash_it.sh @@ -137,13 +137,6 @@ elif [ -s /Applications/Preview.app ]; then PREVIEW="/Applications/Preview.app" fi -# Load all the Jekyll stuff - -if [ -e "$HOME/.jekyllconfig" ]; then - # shellcheck disable=SC1090 - . "$HOME/.jekyllconfig" -fi - # BASH_IT_RELOAD_LEGACY is set. if ! _command_exists reload && [[ -n "${BASH_IT_RELOAD_LEGACY:-}" ]]; then case $OSTYPE in diff --git a/plugins/available/jekyll.plugin.bash b/plugins/available/jekyll.plugin.bash index 7fef0065..f647d4a0 100644 --- a/plugins/available/jekyll.plugin.bash +++ b/plugins/available/jekyll.plugin.bash @@ -315,3 +315,8 @@ function deploysite() { builtin cd "${SITE}" || return rsync -rz "${REMOTE?}" } + +# Load the Jekyll config +if [[ -s "$HOME/.jekyllconfig" ]]; then + source "$HOME/.jekyllconfig" +fi From e38eeefc5ff44c58397a3b6a34a5842c98f24a6c Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sun, 10 Oct 2021 00:44:21 -0700 Subject: [PATCH 3/4] plugin/jekyll: second `shellcheck` pass --- clean_files.txt | 1 + plugins/available/jekyll.plugin.bash | 119 +++++++++++++-------------- 2 files changed, 56 insertions(+), 64 deletions(-) diff --git a/clean_files.txt b/clean_files.txt index 53765a5f..0e6c39f5 100644 --- a/clean_files.txt +++ b/clean_files.txt @@ -97,6 +97,7 @@ plugins/available/history-search.plugin.bash plugins/available/history-substring-search.plugin.bash plugins/available/history.plugin.bash plugins/available/hub.plugin.bash +plugins/available/jekyll.plugin.bash plugins/available/jump.plugin.bash plugins/available/less-pretty-cat.plugin.bash plugins/available/node.plugin.bash diff --git a/plugins/available/jekyll.plugin.bash b/plugins/available/jekyll.plugin.bash index f647d4a0..b613db2d 100644 --- a/plugins/available/jekyll.plugin.bash +++ b/plugins/available/jekyll.plugin.bash @@ -7,8 +7,8 @@ function editpost() { param '1: site directory' group 'jekyll' - local SITE site POST DATE TITLE POSTS TMPFILE POST_TO_EDIT - local -i COUNTER + local SITE site POST DATE TITLE POSTS + local -i COUNTER=1 POST_TO_EDIT if [[ -z "${1:-}" ]]; then echo "Error: no site specified." echo "The site is the name of the directory your project is in." @@ -29,20 +29,16 @@ function editpost() { builtin cd "${SITE}/_posts" || return - COUNTER=1 - TMPFILE="/tmp/editpost-$RANDOM" - for POST in *; do DATE="$(echo "${POST}" | grep -oE "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}")" TITLE="$(grep -oE "title: (.+)" < "${POST}")" TITLE="${TITLE/title: /}" - echo "${COUNTER}) ${DATE} ${TITLE}" >> "${TMPFILE}" + echo "${COUNTER}) ${DATE} ${TITLE}" POSTS[COUNTER]="$POST" COUNTER="$((COUNTER + 1))" - done - less "${TMPFILE}" + done | less read -rp "Number of post to edit: " POST_TO_EDIT - "${JEKYLL_EDITOR:-${VISUAL:-${EDITOR:-${ALTERNATE_EDITOR:-nano}}}}" "${POSTS[$POST_TO_EDIT]}" + "${JEKYLL_EDITOR:-${VISUAL:-${EDITOR:-${ALTERNATE_EDITOR:-nano}}}}" "${POSTS[POST_TO_EDIT]}" } function newpost() { @@ -52,23 +48,22 @@ function newpost() { local SITE site FNAME_POST_TITLE FNAME YAML_DATE local JEKYLL_FORMATTING FNAME_DATE OPTIONS OPTION POST_TYPE POST_TITLE - if [ -z "$1" ]; then + local -i loc=0 + if [[ -z "${1:-}" ]]; then echo "Error: no site specified." echo "The site is the name of the directory your project is in." return 1 fi - if [ -z "$SITE" ]; then + if [[ -z "${SITE}" ]]; then echo "No such site." return 1 fi - loc=0 - for site in "${SITES[@]}"; do - if [ "${site##*/}" = "$1" ]; then - SITE=$site - JEKYLL_FORMATTING=${MARKUPS[$loc]} + if [[ "${site##*/}" == "$1" ]]; then + SITE="$site" + JEKYLL_FORMATTING="${MARKUPS[loc]}" break fi loc=$((loc + 1)) @@ -88,32 +83,32 @@ function newpost() { if [[ $JEKYLL_FORMATTING == "markdown" || $JEKYLL_FORMATTING == "textile" ]]; then select OPTION in "${OPTIONS[@]}"; do - if [[ $OPTION = "Text" ]]; then + if [[ $OPTION == "Text" ]]; then POST_TYPE="Text" break fi - if [[ $OPTION = "Quote" ]]; then + if [[ $OPTION == "Quote" ]]; then POST_TYPE="Quote" break fi - if [[ $OPTION = "Image" ]]; then + if [[ $OPTION == "Image" ]]; then POST_TYPE="Image" break fi - if [[ $OPTION = "Audio" ]]; then + if [[ $OPTION == "Audio" ]]; then POST_TYPE="Audio" break fi - if [[ $OPTION = "Video" ]]; then + if [[ $OPTION == "Video" ]]; then POST_TYPE="Video" break fi - if [[ $OPTION = "Link" ]]; then + if [[ $OPTION == "Link" ]]; then POST_TYPE="Link" break fi @@ -133,31 +128,27 @@ function newpost() { FNAME="$FNAME_DATE-$FNAME_POST_TITLE.$JEKYLL_FORMATTING" # And, finally, create the actual post file. But we're not done yet... - # Write a little stuff to the file for the YAML Front Matter + { + # Write a little stuff to the file for the YAML Front Matter + echo "---" - echo "---" >> "${FNAME}" + # Now we have to get the date, again. But this time for in the header (YAML Front Matter) of the file + YAML_DATE="$(date "+%B %d %Y %X")" - # Now we have to get the date, again. But this time for in the header (YAML Front Matter) of - # the file + # Echo the YAML Formatted date to the post file + echo "date: $YAML_DATE" - YAML_DATE="$(date "+%B %d %Y %X")" + # Echo the original post title to the YAML Front Matter header + echo "title: $POST_TITLE" - # Echo the YAML Formatted date to the post file + # And, now, echo the "post" layout to the YAML Front Matter header + echo "layout: post" - echo "date: $YAML_DATE" >> "${FNAME}" + # Close the YAML Front Matter Header + echo "---" - # Echo the original post title to the YAML Front Matter header - - echo "title: $POST_TITLE" >> "${FNAME}" - - # And, now, echo the "post" layout to the YAML Front Matter header - - echo "layout: post" >> "${FNAME}" - - # Close the YAML Front Matter Header - - echo "---" >> "${FNAME}" - echo >> "${FNAME}" + echo + } > "${FNAME}" # Generate template text based on the post type @@ -167,31 +158,31 @@ function newpost() { fi if [[ $POST_TYPE == "Quote" ]]; then - echo "> Quote" >> "${FNAME}" - echo >> "${FNAME}" - echo "— Author" >> "${FNAME}" + echo "> Quote" + echo + echo "— Author" fi if [[ $POST_TYPE == "Image" ]]; then - echo "![Alternate Text](/path/to/image/or/url)" >> "${FNAME}" + echo "![Alternate Text](/path/to/image/or/url)" fi if [[ $POST_TYPE == "Audio" ]]; then - echo "" >> "${FNAME}" + echo "" fi if [[ $POST_TYPE == "Video" ]]; then - echo "" >> "${FNAME}" + echo "" fi if [[ $POST_TYPE == "Link" ]]; then - echo "[link][1]" >> "${FNAME}" - echo >> "${FNAME}" - echo "> Quote" >> "${FNAME}" - echo >> "${FNAME}" - echo "[1]: url" >> "${FNAME}" + echo "[link][1]" + echo + echo "> Quote" + echo + echo "[1]: url" fi - fi + fi >> "${FNAME}" if [[ $JEKYLL_FORMATTING == "textile" ]]; then if [[ $POST_TYPE == "Text" ]]; then @@ -199,29 +190,29 @@ function newpost() { fi if [[ $POST_TYPE == "Quote" ]]; then - echo "bq. Quote" >> "${FNAME}" - echo >> "${FNAME}" - echo "— Author" >> "${FNAME}" + echo "bq. Quote" + echo + echo "— Author" fi if [[ $POST_TYPE == "Image" ]]; then - echo "!url(alt text)" >> "${FNAME}" + echo "!url(alt text)" fi if [[ $POST_TYPE == "Audio" ]]; then - echo "" >> "${FNAME}" + echo "" fi if [[ $POST_TYPE == "Video" ]]; then - echo "" >> "${FNAME}" + echo "" fi if [[ $POST_TYPE == "Link" ]]; then - echo "\"Site\":url" >> "${FNAME}" - echo >> "${FNAME}" - echo "bq. Quote" >> "${FNAME}" + echo "\"Site\":url" + echo + echo "bq. Quote" fi - fi + fi >> "${FNAME}" # Open the file in your favorite editor @@ -299,7 +290,7 @@ function deploysite() { fi for site in "${SITES[@]}"; do - if [ "${site##*/}" == "$1" ]; then + if [[ "${site##*/}" == "$1" ]]; then SITE="$site" REMOTE="${REMOTES[loc]}" break From 3fe9c8d6bcd79949504de102efa09294754b1325 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sun, 10 Oct 2021 00:53:00 -0700 Subject: [PATCH 4/4] plugin/jekyll: try to shorten the flow MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Alsö, use `pushd`/`popd` instead of `builtin cd`. Alsö, SC2153 --- plugins/available/jekyll.plugin.bash | 187 ++++++++++++--------------- 1 file changed, 81 insertions(+), 106 deletions(-) diff --git a/plugins/available/jekyll.plugin.bash b/plugins/available/jekyll.plugin.bash index b613db2d..d818b076 100644 --- a/plugins/available/jekyll.plugin.bash +++ b/plugins/available/jekyll.plugin.bash @@ -8,7 +8,7 @@ function editpost() { group 'jekyll' local SITE site POST DATE TITLE POSTS - local -i COUNTER=1 POST_TO_EDIT + local -i COUNTER=1 POST_TO_EDIT ret if [[ -z "${1:-}" ]]; then echo "Error: no site specified." echo "The site is the name of the directory your project is in." @@ -27,7 +27,7 @@ function editpost() { return 1 fi - builtin cd "${SITE}/_posts" || return + pushd "${SITE}/_posts" > /dev/null || return for POST in *; do DATE="$(echo "${POST}" | grep -oE "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}")" @@ -36,9 +36,12 @@ function editpost() { echo "${COUNTER}) ${DATE} ${TITLE}" POSTS[COUNTER]="$POST" COUNTER="$((COUNTER + 1))" - done | less + done > >(less) read -rp "Number of post to edit: " POST_TO_EDIT "${JEKYLL_EDITOR:-${VISUAL:-${EDITOR:-${ALTERNATE_EDITOR:-nano}}}}" "${POSTS[POST_TO_EDIT]}" + ret="$?" + popd > /dev/null || return "$ret" + return "$ret" } function newpost() { @@ -48,7 +51,7 @@ function newpost() { local SITE site FNAME_POST_TITLE FNAME YAML_DATE local JEKYLL_FORMATTING FNAME_DATE OPTIONS OPTION POST_TYPE POST_TITLE - local -i loc=0 + local -i loc=0 ret if [[ -z "${1:-}" ]]; then echo "Error: no site specified." echo "The site is the name of the directory your project is in." @@ -69,62 +72,29 @@ function newpost() { loc=$((loc + 1)) done - # 'builtin cd' into the local jekyll root - - builtin cd "${SITE}/_posts" || return + # Change directory into the local jekyll root + pushd "${SITE}/_posts" > /dev/null || return # Get the date for the new post's filename - FNAME_DATE="$(date "+%Y-%m-%d")" # If the user is using markdown or textile formatting, let them choose what type of post they want. Sort of like Tumblr. - OPTIONS=('Text' 'Quote' 'Image' 'Audio' 'Video' 'Link') if [[ $JEKYLL_FORMATTING == "markdown" || $JEKYLL_FORMATTING == "textile" ]]; then select OPTION in "${OPTIONS[@]}"; do - if [[ $OPTION == "Text" ]]; then - POST_TYPE="Text" - break - fi - - if [[ $OPTION == "Quote" ]]; then - POST_TYPE="Quote" - break - fi - - if [[ $OPTION == "Image" ]]; then - POST_TYPE="Image" - break - fi - - if [[ $OPTION == "Audio" ]]; then - POST_TYPE="Audio" - break - fi - - if [[ $OPTION == "Video" ]]; then - POST_TYPE="Video" - break - fi - - if [[ $OPTION == "Link" ]]; then - POST_TYPE="Link" - break - fi + POST_TYPE="${OPTION}" + break done fi # Get the title for the new post - read -rp "Enter title of the new post: " POST_TITLE # Convert the spaces in the title to hyphens for use in the filename - FNAME_POST_TITLE="${POST_TITLE/ /-}" # Now, put it all together for the full filename - FNAME="$FNAME_DATE-$FNAME_POST_TITLE.$JEKYLL_FORMATTING" # And, finally, create the actual post file. But we're not done yet... @@ -151,72 +121,65 @@ function newpost() { } > "${FNAME}" # Generate template text based on the post type - if [[ $JEKYLL_FORMATTING == "markdown" ]]; then - if [[ $POST_TYPE == "Text" ]]; then - true - fi - - if [[ $POST_TYPE == "Quote" ]]; then - echo "> Quote" - echo - echo "— Author" - fi - - if [[ $POST_TYPE == "Image" ]]; then - echo "![Alternate Text](/path/to/image/or/url)" - fi - - if [[ $POST_TYPE == "Audio" ]]; then - echo "" - fi - - if [[ $POST_TYPE == "Video" ]]; then - echo "" - fi - - if [[ $POST_TYPE == "Link" ]]; then - echo "[link][1]" - echo - echo "> Quote" - echo - echo "[1]: url" - fi - fi >> "${FNAME}" - - if [[ $JEKYLL_FORMATTING == "textile" ]]; then - if [[ $POST_TYPE == "Text" ]]; then - true - fi - - if [[ $POST_TYPE == "Quote" ]]; then - echo "bq. Quote" - echo - echo "— Author" - fi - - if [[ $POST_TYPE == "Image" ]]; then - echo "!url(alt text)" - fi - - if [[ $POST_TYPE == "Audio" ]]; then - echo "" - fi - - if [[ $POST_TYPE == "Video" ]]; then - echo "" - fi - - if [[ $POST_TYPE == "Link" ]]; then - echo "\"Site\":url" - echo - echo "bq. Quote" - fi + case $POST_TYPE in + "Text") + true + ;; + "Quote") + echo "> Quote" + echo + echo "— Author" + ;; + "Image") + echo "![Alternate Text](/path/to/image/or/url)" + ;; + "Audio") + echo "" + ;; + "Video") + echo "" + ;; + "Link") + echo "[link][1]" + echo + echo "> Quote" + echo + echo "[1]: url" + ;; + esac + elif [[ $JEKYLL_FORMATTING == "textile" ]]; then + case $POST_TYPE in + "Text") + true + ;; + "Quote") + echo "bq. Quote" + echo + echo "— Author" + ;; + "Image") + echo "!url(alt text)" + ;; + "Audio") + echo "" + ;; + "Video") + echo "" + ;; + "Link") + echo "\"Site\":url" + echo + echo "bq. Quote" + ;; + esac fi >> "${FNAME}" # Open the file in your favorite editor - "${JEKYLL_EDITOR:-${VISUAL:-${EDITOR:-${ALTERNATE_EDITOR:-nano}}}}" "${FNAME}" + ret="$?" + popd > /dev/null || return "$ret" + return "$ret" } function testsite() { @@ -225,6 +188,7 @@ function testsite() { group 'jekyll' local SITE site + local -i ret if [[ -z "${1:-}" ]]; then echo "Error: no site specified." echo "The site is the name of the directory your project is in." @@ -243,8 +207,11 @@ function testsite() { return 1 fi - builtin cd "${SITE}" || return + pushd "${SITE}" > /dev/null || return jekyll --server --auto + ret="$?" + popd > /dev/null || return "$ret" + return "$ret" } function buildsite() { @@ -253,6 +220,7 @@ function buildsite() { group 'jekyll' local SITE site + local -i ret if [[ -z "${1:-}" ]]; then echo "Error: no site specified." echo "The site is the name of the directory your project is in." @@ -271,9 +239,12 @@ function buildsite() { return 1 fi - builtin cd "${SITE}" || return + pushd "${SITE}" > /dev/null || return rm -rf _site jekyll --no-server + ret="$?" + popd > /dev/null || return "$ret" + return "$ret" } function deploysite() { @@ -282,7 +253,7 @@ function deploysite() { group 'jekyll' local SITE site REMOTE - local -i loc=0 + local -i loc=0 ret if [[ -z "${1:-}" ]]; then echo "Error: no site specified." echo "The site is the name of the directory your project is in." @@ -292,6 +263,7 @@ function deploysite() { for site in "${SITES[@]}"; do if [[ "${site##*/}" == "$1" ]]; then SITE="$site" + # shellcheck disable=SC2153 # who knows REMOTE="${REMOTES[loc]}" break fi @@ -303,8 +275,11 @@ function deploysite() { return 1 fi - builtin cd "${SITE}" || return + pushd "${SITE}" > /dev/null || return rsync -rz "${REMOTE?}" + ret="$?" + popd > /dev/null || return "$ret" + return "$ret" } # Load the Jekyll config