diff --git a/bb.sh b/bb.sh index 6108eb0..b75a449 100755 --- a/bb.sh +++ b/bb.sh @@ -5,6 +5,18 @@ # https://github.com/carlesfe/bashblog/contributors # Check out README.md for more details +# Some shell settings for robustness by default. These help eliminate +# unexpected snags and security vulnerabilities in case someone forgets to +# quote a variable somewhere. They do require a few coding adaptations. + +IFS=$'\n' # Globally, we do word splitting only on newline (which also + # makes "$*" expand with newline separator instead of space). + +set -f # Disable globbing (pathname expansion). It can be re-enabled + # locally using 'set +f'; it's handy to do this in a subshell, + # for example in $(command substitution), as the globbing will + # be local to the subshell. + # Global variables # It is recommended to perform a 'rebuild' after changing any of this in the code @@ -257,6 +269,14 @@ get_html_file_content() { }" } +# Invoke the editor specified by the $EDITOR environment variable. Use a +# function for this as we need to locally word-split $EDITOR on spaces +# (in case it contains arguments, like EDITOR='joe -nobackups). +invoke_editor() { + local IFS=$' \t\n' + $EDITOR "$1" +} + # Edit an existing, published .html file while keeping its original timestamp # Please note that this function does not automatically republish anything, as # it is usually called from 'main'. @@ -276,7 +296,8 @@ edit() { touch_timestamp=$(LC_ALL=C date -r "${1%%.*}.html" +"$date_format_timestamp") tags_before=$(tags_in_post "${1%%.*}.html") if [[ $2 == full ]]; then - $EDITOR "$1" + invoke_editor "$1" + touch -t "$touch_timestamp" "$1" filename=$1 else if [[ ${1##*.} == md ]]; then @@ -286,7 +307,8 @@ edit() { exit fi # editing markdown file - $EDITOR "$1" + invoke_editor "$1" + touch -t "$touch_timestamp" "$1" TMPFILE=$(markdown "$1") filename=${1%%.*}.html else @@ -296,27 +318,29 @@ edit() { get_post_title "$1" > "$TMPFILE" # Post text with plaintext tags get_html_file_content 'text' 'text' <"$1" | sed "/^

$template_tags_line_header/s|\\1|\\1|g" >> "$TMPFILE" - $EDITOR "$TMPFILE" + invoke_editor "$TMPFILE" filename=$1 fi rm "$filename" if [[ $2 == keep ]]; then + old_filename='' parse_file "$TMPFILE" "$edit_timestamp" "$filename" else + old_filename=$filename # save old filename to exclude it from $relevant_posts parse_file "$TMPFILE" "$edit_timestamp" # this command sets $filename as the html processed file [[ ${1##*.} == md ]] && mv "$1" "${filename%%.*}.md" 2>/dev/null fi rm "$TMPFILE" + touch -t "$touch_timestamp" "$filename" fi - touch -t "$touch_timestamp" "$filename" - touch -t "$touch_timestamp" "$1" chmod 644 "$filename" echo "Posted $filename" tags_after=$(tags_in_post "$filename") - relevant_tags=$(echo "$tags_before $tags_after" | tr ',' ' ' | tr ' ' '\n' | sort -u | tr '\n' ' ') - if [[ ! -z $relevant_tags ]]; then - relevant_posts="$(posts_with_tags $relevant_tags) $filename" - rebuild_tags "$relevant_posts" "$relevant_tags" + relevant_tags=$(sort -u <<< "$tags_before"$'\n'"$tags_after") + if [[ -n $relevant_tags ]]; then + relevant_posts=$(posts_with_tags $relevant_tags)$'\n'$filename + [[ -n $old_filename ]] && relevant_posts=$(grep -vFx "$old_filename" <<<"$relevant_posts") + rebuild_tags $relevant_posts --tags $relevant_tags fi } @@ -488,10 +512,11 @@ create_html_page() { parse_file() { # Read for the title and check that the filename is ok title="" - while IFS='' read -r line; do + while read -r line; do if [[ -z $title ]]; then # remove extra

and

added by markdown - title=$(echo "$line" | sed 's/<\/*p>//g') + title=${line#

} + title=${title%

} if [[ -n $3 ]]; then filename=$3 else @@ -513,7 +538,6 @@ parse_file() { elif [[ $line == "

$template_tags_line_header"* ]]; then tags=$(echo "$line" | cut -d ":" -f 2- | sed -e 's/<\/p>//g' -e 's/^ *//' -e 's/ *$//' -e 's/, /,/g') IFS=, read -r -a array <<< "$tags" - echo -n "

$template_tags_line_header " >> "$content" for item in "${array[@]}"; do echo -n "$item, " @@ -578,7 +602,7 @@ EOF filename="" while [[ $post_status != "p" && $post_status != "P" ]]; do [[ -n $filename ]] && rm "$filename" # Delete the generated html file, if any - $EDITOR "$TMPFILE" + invoke_editor "$TMPFILE" if [[ $fmt == md ]]; then html_from_md=$(markdown "$TMPFILE") parse_file "$html_from_md" @@ -620,8 +644,8 @@ EOF echo "Posted $filename" relevant_tags=$(tags_in_post $filename) if [[ -n $relevant_tags ]]; then - relevant_posts="$(posts_with_tags $relevant_tags) $filename" - rebuild_tags "$relevant_posts" "$relevant_tags" + relevant_posts=$(posts_with_tags $relevant_tags)$'\n'$filename + rebuild_tags $relevant_posts --tags $relevant_tags fi } @@ -636,7 +660,7 @@ all_posts() { { echo "

$template_archive_title

" prev_month="" - while IFS='' read -r i; do + for i in $(set +f; ls -t ./*.html); do is_boilerplate_file "$i" && continue echo -n "." 1>&3 # Month headers @@ -653,7 +677,7 @@ all_posts() { # Date date=$(LC_ALL=$date_locale date -r "$i" +"$date_format") echo " $date" - done < <(ls -t ./*.html) + done echo "" 1>&3 echo "" echo "
$template_archive_index_page
" @@ -676,7 +700,7 @@ all_tags() { { echo "

$template_tags_title

" echo "