From c683019d7c6b9762769e3c27f72ee54d07ae00fd Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Mon, 18 Jan 2016 20:08:52 +0100 Subject: [PATCH 1/5] XHTML compliance fix: self-close meta tag --- bb.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bb.sh b/bb.sh index 9fb3c07..18c18f4 100755 --- a/bb.sh +++ b/bb.sh @@ -923,7 +923,7 @@ create_includes() { echo '' echo '' echo '' - echo '' + echo '' printf '\n' "${css_include[@]}" if [[ -z $global_feedburner ]]; then echo "" From 7771fab8209fc0d434685b52a382da2595842b70 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Mon, 18 Jan 2016 20:18:23 +0100 Subject: [PATCH 2/5] Some more code and performance optimisations - Use builtin 'command -v' rather than external 'which'. - The clean_filename() function just removed the initial ./ from a file name; do this with a parameter substitution instead. This gets rid of the need to fork subshells for command substitutions, so is much faster. - Where convenient, replace 'echo ... | sed ...' with fast parameter substitutions. --- bb.sh | 49 +++++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/bb.sh b/bb.sh index 18c18f4..0132a78 100755 --- a/bb.sh +++ b/bb.sh @@ -144,7 +144,7 @@ global_variables() { # Markdown location. Trying to autodetect by default. # The invocation must support the signature 'markdown_bin in.md > out.html' - markdown_bin=$(which Markdown.pl || which markdown) + markdown_bin=$(command -v Markdown.pl || command -v markdown) } # Check for the validity of some variables @@ -162,7 +162,7 @@ global_variables_check() { # Test if the markdown script is working correctly test_markdown() { [[ -z $markdown_bin ]] && return 1 - [[ -z $(which diff) ]] && return 1 + command -v diff >/dev/null || return 1 in=/tmp/md-in-${RANDOM}.md out=/tmp/md-out-${RANDOM}.html @@ -388,7 +388,7 @@ twitter() { # Return 0 (bash return value 'true') if the input file is an index, feed, etc # or 1 (bash return value 'false') if it is a blogpost is_boilerplate_file() { - name=$(clean_filename "$1") + name=${1#./} case $name in ( "$index_file" | "$archive_index" | "$tags_index" | "$footer_file" | "$header_file" | "$global_analytics_file" | "$prefix_tags"* ) return 0 ;; @@ -400,14 +400,6 @@ is_boilerplate_file() { esac } -# Filenames sometimes have leading './' or other oddities which need to be cleaned -# -# $1 the file name -# returns the clean file name -clean_filename() { - echo "${1#./}" # Delete leading './' -} - # Adds all the bells and whistles to format the html page # Every blog post is marked with a and # which is parsed afterwards in the other functions. There is also a marker @@ -445,14 +437,16 @@ create_html_page() { echo '' # title, header, headerholder echo '
' - file_url=$(clean_filename "$filename") - file_url=$(sed 's/.rebuilt//g' <<< "$file_url") # Get the correct URL when rebuilding + file_url=${filename#./} + file_url=${file_url%.rebuilt} # Get the correct URL when rebuilding # one blog entry if [[ $index == no ]]; then echo '' # marks the beginning of the whole post echo "

" # remove possible

's on the title because of markdown conversion - echo "$title" | sed 's/<\/*p>//g' + title=${title//

/} + title=${title//<\/p>/} + echo "$title" echo '

' if [[ -z $timestamp ]]; then echo "
$(LC_ALL=$date_locale date +"$date_format") — " @@ -684,14 +678,13 @@ all_tags() { { echo "

$template_tags_title

" echo "
    " - for i in ./$prefix_tags*.html; do + for i in $prefix_tags*.html; do [[ -f "$i" ]] || break echo -n "." 1>&3 nposts=$(grep -c "<\!-- text begin -->" "$i") - tagname=$(echo "$i" | cut -c "$((${#prefix_tags}+3))-" | sed 's/\.html//g') - i=$(clean_filename "$i") - word=$template_tags_posts_singular - (($nposts > 1)) && word=$template_tags_posts + tagname=${i#"$prefix_tags"} + tagname=${tagname%.html} + ((nposts > 1)) && word=$template_tags_posts || word=$template_tags_posts_singular echo "
  • $tagname — $nposts $word
  • " done echo "" 1>&3 @@ -810,7 +803,8 @@ rebuild_tags() { rm "$tmpfile" # Now generate the tag files with headers, footers, etc while IFS='' read -r i; do - tagname=$(echo "$i" | cut -c "$((${#prefix_tags}+3))-" | sed 's/\.tmp\.html//g') + tagname=${i#./"$prefix_tags"} + tagname=${tagname%.tmp.html} create_html_page "$i" "$prefix_tags$tagname.html" yes "$global_title — $template_tag_title \"$tagname\"" rm "$i" done < <(ls -t ./"$prefix_tags"*.tmp.html 2>/dev/null) @@ -828,24 +822,23 @@ get_post_title() { # # $2 if "-n", tags will be sorted by number of posts list_tags() { - if [[ $2 ]] && [[ $2 == -n ]]; then do_sort=1; else do_sort=0; fi + if [[ $2 == -n ]]; then do_sort=1; else do_sort=0; fi ls ./$prefix_tags*.html &> /dev/null (($? != 0)) && echo "No posts yet. Use 'bb.sh post' to create one" && return lines="" - for i in ./$prefix_tags*.html; do + for i in $prefix_tags*.html; do [[ -f "$i" ]] || break nposts=$(grep -c "<\!-- text begin -->" "$i") - tagname=$(echo "$i" | cut -c "$((${#prefix_tags}+3))-" | sed 's/\.html//g') - i=$(clean_filename "$i") - word=$template_tags_posts_singular - (($nposts > 1)) && word=$template_tags_posts + tagname=${i#"$prefix_tags"} + tagname=${tagname#.html} + ((nposts > 1)) && word=$template_tags_posts || word=$template_tags_posts_singular line="$tagname # $nposts # $word" lines+=$line\\n done - if (( $do_sort == 1 )); then + if (( do_sort == 1 )); then echo -e "$lines" | column -t -s "#" | sort -nrk 2 else echo -e "$lines" | column -t -s "#" @@ -895,7 +888,7 @@ make_rss() { get_post_title "$i" echo '$global_url/$(clean_filename "$i")" + echo "]]>$global_url/${i#./}" echo "$global_url/$i" echo "$global_author" echo "$(LC_ALL=C date -r "$i" +"%a, %d %b %Y %H:%M:%S %z")" From c3e0572be6b8f191f60c3f6c29168e23159ce06b Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Mon, 18 Jan 2016 21:05:06 +0100 Subject: [PATCH 3/5] Get rid of a couple of more shellcheck.net warnings. --- bb.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bb.sh b/bb.sh index 0132a78..a0dee58 100755 --- a/bb.sh +++ b/bb.sh @@ -945,7 +945,7 @@ delete_includes() { create_css() { # To avoid overwriting manual changes. However it is recommended that # this function is modified if the user changes the blog.css file - [[ -n $css_include ]] && return || css_include=('main.css' 'blog.css') + (( ${#css_include[@]} > 0 )) && return || css_include=('main.css' 'blog.css') if [[ ! -f blog.css ]]; then # blog.css directives will be loaded after main.css and thus will prevail echo '#title{font-size: x-large;} @@ -1118,7 +1118,7 @@ do_main() { # Test for existing html files if ls ./*.html &> /dev/null; then # We're going to back up just in case - tar cfz ".backup.tar.gz" *.html && + tar -c -z -f ".backup.tar.gz" -- *.html && chmod 600 ".backup.tar.gz" elif [[ $1 == rebuild ]]; then echo "Can't find any html files, nothing to rebuild" From 9794f1635a44891c073d52976fb94e99f34353ec Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Mon, 18 Jan 2016 21:35:29 +0100 Subject: [PATCH 4/5] fix unquoted $global_disqus_username that shellcheck.net didn't catch (it was subject to field splitting and globbing) --- bb.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bb.sh b/bb.sh index a0dee58..870b91c 100755 --- a/bb.sh +++ b/bb.sh @@ -220,7 +220,7 @@ disqus_body() { echo '