diff --git a/bb.sh b/bb.sh index eacf90b..dbeb198 100755 --- a/bb.sh +++ b/bb.sh @@ -143,16 +143,16 @@ 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=$(which Markdown.pl || which markdown) } # Check for the validity of some variables # DO NOT EDIT THIS FUNCTION unless you know what you're doing global_variables_check() { - [[ "$header_file" == ".header.html" ]] && + [[ $header_file == .header.html ]] && echo "Please check your configuration. '.header.html' is not a valid value for the setting 'header_file'" && exit - [[ "$footer_file" == ".footer.html" ]] && + [[ $footer_file == .footer.html ]] && echo "Please check your configuration. '.footer.html' is not a valid value for the setting 'footer_file'" && exit } @@ -160,17 +160,17 @@ global_variables_check() { # Test if the markdown script is working correctly test_markdown() { - [[ -z "$markdown_bin" ]] && return 1 - [[ -z "$(which diff)" ]] && return 1 + [[ -z $markdown_bin ]] && return 1 + [[ -z $(which diff) ]] && return 1 - in="/tmp/md-in-${RANDOM}.md" - out="/tmp/md-out-${RANDOM}.html" - good="/tmp/md-good-${RANDOM}.html" + in=/tmp/md-in-${RANDOM}.md + out=/tmp/md-out-${RANDOM}.html + good=/tmp/md-good-${RANDOM}.html echo -e "line 1\n\nline 2" > "$in" echo -e "

line 1

\n\n

line 2

" > "$good" - $markdown_bin $in > $out 2> /dev/null + "$markdown_bin" "$in" > "$out" 2> /dev/null diff $good $out &> /dev/null # output is irrelevant, we'll check $? - if [[ $? -ne 0 ]]; then + if (($? != 0)); then rm -f "$in" "$good" "$out" return 1 fi @@ -182,8 +182,8 @@ test_markdown() { # Parse a Markdown file into HTML and return the generated file markdown() { - out="$(echo $1 | sed 's/md$/html/g')" - while [ -f "$out" ]; do out="$(echo $out | sed 's/\.html$/\.'$RANDOM'\.html/')"; done + out=${1%.md}.html + while [[ -f $out ]]; do out=${out%.html}.$RANDOM.html; done $markdown_bin "$1" > "$out" echo "$out" } @@ -191,9 +191,9 @@ markdown() { # Prints the required google analytics code google_analytics() { - [[ -z "$global_analytics" ]] && [[ -z "$global_analytics_file" ]] && return + [[ -z $global_analytics && -z $global_analytics_file ]] && return - if [[ -z "$global_analytics_file" ]]; then + if [[ -z $global_analytics_file ]]; then echo "' + echo "" return; else echo "

$template_comments "; @@ -387,14 +387,16 @@ 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`" - if [[ "$name" == "$index_file" ]] || [[ "$name" == "$archive_index" ]] || [[ "$name" == "$tags_index" ]] || [[ "$name" == "$footer_file" ]] || [[ "$name" == "$header_file" ]] || [[ "$name" == "$global_analytics_file" ]] || [[ "$name" = "$prefix_tags"* ]] ; then return 0 - else # Check for exclded - for excl in ${html_exclude[*]}; do - [[ "$name" == "$excl" ]] && return 0 + name=$(clean_filename "$1") + case $name in + ( "$index_file" | "$archive_index" | "$tags_index" | "$footer_file" | "$header_file" | "$global_analytics_file" | "$prefix_tags"* ) + return 0 ;; + ( * ) # Check for excluded + for excl in "${html_exclude[@]}"; do + [[ $name == "$excl" ]] && return 0 done - return 1 - fi + return 1 ;; + esac } # Filenames sometimes have leading './' or other oddities which need to be cleaned @@ -402,9 +404,7 @@ is_boilerplate_file() { # $1 the file name # returns the clean file name clean_filename() { - name="$1" - [[ "${name:0:2}" == "./" ]] && name=${name:2} # Delete leading './' - echo $name + echo "${1#./}" # Delete leading './' } # Adds all the bells and whistles to format the html page @@ -419,67 +419,69 @@ clean_filename() { # $4 title for the html header # $5 original blog timestamp create_html_page() { - content="$1" - filename="$2" - index="$3" - title="$4" - timestamp="$5" + content=$1 + filename=$2 + index=$3 + title=$4 + timestamp=$5 # Create the actual blog post # html, head - cat ".header.html" > "$filename" - echo "$title" >> "$filename" - google_analytics >> "$filename" - twitter_card "$content" "$title" >> "$filename" - echo "" >> "$filename" - # stuff to add before the actual body content - [[ -n "$body_begin_file" ]] && cat "$body_begin_file" >> "$filename" - # body divs - echo '

' >> "$filename" - echo '
' >> "$filename" - # blog title - echo '
' >> "$filename" - cat .title.html >> "$filename" - echo '
' >> "$filename" # title, header, headerholder - echo '
' >> "$filename" + { + cat ".header.html" + echo "$title" + google_analytics + twitter_card "$content" "$title" + echo "" + # stuff to add before the actual body content + [[ -n $body_begin_file ]] && cat "$body_begin_file" + # body divs + echo '
' + echo '
' + # blog title + echo '
' + cat .title.html + echo '
' # title, header, headerholder + echo '
' - file_url="`clean_filename $filename`" - file_url="$(sed 's/.rebuilt//g' <<< $file_url)" # Get the correct URL when rebuilding - # one blog entry - if [[ "$index" == "no" ]]; then - echo '' >> "$filename" # marks the beginning of the whole post - echo '

' >> "$filename" - # remove possible

's on the title because of markdown conversion - echo "$(echo "$title" | sed 's/<\/*p>//g')" >> "$filename" - echo '

' >> "$filename" - if [[ "$timestamp" == "" ]]; then - echo '
'$(LC_ALL=$date_locale date +"$date_format")' — ' >> "$filename" - else - echo '
'$(LC_ALL=$date_locale date +"$date_format" --date="$timestamp") ' — ' >> "$filename" + file_url=$(clean_filename "$filename") + file_url=$(sed 's/.rebuilt//g' <<< "$file_url") # 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' + echo '

' + if [[ -z $timestamp ]]; then + echo "
$(LC_ALL=$date_locale date +"$date_format") — " + else + echo "
$(LC_ALL=$date_locale date +"$date_format" --date="$timestamp") — " + fi + echo "$global_author
" + echo '' # This marks the text body, after the title, date... fi - echo "$global_author
" >> "$filename" - echo '' >> "$filename" # This marks the text body, after the title, date... - fi - cat "$content" >> "$filename" # Actual content - if [[ "$index" == "no" ]]; then - echo -e '\n' >> "$filename" + cat "$content" # Actual content + if [[ $index == no ]]; then + echo -e '\n' - twitter "$global_url/$file_url" >> "$filename" + twitter "$global_url/$file_url" - echo '' >> "$filename" # absolute end of the post - fi + echo '' # absolute end of the post + fi - echo '
' >> "$filename" # content + echo '
' # content - # Add disqus commments except for index and all_posts pages - [[ "$index" == "no" ]] && disqus_body >> "$filename" + # Add disqus commments except for index and all_posts pages + [[ $index == no ]] && disqus_body - # page footer - cat .footer.html >> "$filename" - # close divs - echo '
' >> "$filename" # divbody and divbodyholder - disqus_footer >> "$filename" - echo '' >> "$filename" + # page footer + cat .footer.html + # close divs + echo '
' # divbody and divbodyholder + disqus_footer + echo '' + } > "$filename" } # Parse the plain text file into an html file @@ -494,36 +496,35 @@ parse_file() { # Read for the title and check that the filename is ok title="" while IFS='' read -r line; do - if [[ "$title" == "" ]]; then + if [[ -z $title ]]; then # remove extra

and

added by markdown title=$(echo "$line" | sed 's/<\/*p>//g') - if [ "$3" ]; then + if [[ -n $3 ]]; then filename=$3 else filename=$title - [[ "$convert_filename" ]] && - filename="$(echo $title | eval $convert_filename)" - [[ "$filename" ]] || + [[ -n $convert_filename ]] && + filename=$(echo "$title" | eval "$convert_filename") + [[ -n $filename ]] || filename=$RANDOM # don't allow empty filenames - filename="$filename.html" + filename=$filename.html # Check for duplicate file names - while [ -f "$filename" ]; do - suffix="$RANDOM" - filename="$(echo $filename | sed 's/\.html/'$suffix'\.html/g')" + while [[ -f $filename ]]; do + filename=${filename%.html}$RANDOM.html done fi - content="$filename.tmp" + content=$filename.tmp # Parse possible tags - 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')" + 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 + for item in "${array[@]}"; do echo -n "$item, " - done ) | sed 's/, $/<\/p>/g' >> "$content" + done | sed 's/, $/<\/p>/g' >> "$content" else echo "$line" >> "$content" fi @@ -537,41 +538,41 @@ parse_file() { # Manages the creation of the text file and the parsing to html file # also the drafts write_entry() { - test_markdown && fmt="md" || fmt="html" - f="$2" - [[ "$2" == "-html" ]] && fmt="html" && f="$3" + test_markdown && fmt=md || fmt=html + f=$2 + [[ $2 == -html ]] && fmt=html && f=$3 - if [[ "$f" != "" ]]; then - TMPFILE="$f" - if [[ ! -f "$TMPFILE" ]]; then + if [[ -n $f ]]; then + TMPFILE=$f + if [[ ! -f $TMPFILE ]]; then echo "The file doesn't exist" delete_includes exit fi # guess format from TMPFILE - extension="${TMPFILE##*.}" - [[ "$extension" == "md" || "$extension" == "html" ]] && fmt="$extension" + extension=${TMPFILE##*.} + [[ $extension == md || $extension == html ]] && fmt=$extension # but let user override it (`bb.sh post -html file.md`) - [[ "$2" == "-html" ]] && fmt="html" + [[ $2 == -html ]] && fmt=html # Test if Markdown is working before re-posting a .md file - if [[ "$extension" == "md" ]]; then + if [[ $extension == md ]]; then test_markdown - if [[ "$?" -ne 0 ]]; then + if (($? != 0)); then echo "Markdown is not working, please edit HTML file directly." exit fi fi else - TMPFILE=".entry-$RANDOM.$fmt" + TMPFILE=.entry-$RANDOM.$fmt echo -e "Title on this line\n" >> "$TMPFILE" - [[ "$fmt" == "html" ]] && cat << EOF >> "$TMPFILE" + [[ $fmt == html ]] && cat << EOF >> "$TMPFILE"

The rest of the text file is an html blog post. The process will continue as soon as you exit your editor.

$template_tags_line_header keep-this-tag-format, tags-are-optional, example

EOF - [[ "$fmt" == "md" ]] && cat << EOF >> "$TMPFILE" + [[ $fmt == md ]] && cat << EOF >> "$TMPFILE" The rest of the text file is a **Markdown** blog post. The process will continue as soon as you exit your editor. @@ -582,11 +583,11 @@ EOF post_status="E" filename="" - while [ "$post_status" != "p" ] && [ "$post_status" != "P" ]; do - [ "$filename" ] && rm "$filename" # Delete the generated html file, if any + while [[ $post_status != "p" && $post_status != "P" ]]; do + [[ -n $filename ]] && rm "$filename" # Delete the generated html file, if any $EDITOR "$TMPFILE" - if [[ "$fmt" == "md" ]]; then - html_from_md="$(markdown "$TMPFILE")" + if [[ $fmt == md ]]; then + html_from_md=$(markdown "$TMPFILE") parse_file "$html_from_md" rm "$html_from_md" else @@ -594,20 +595,20 @@ EOF fi chmod 644 "$filename" - [ "$preview_url" ] || preview_url="$global_url" + [[ -n $preview_url ]] || preview_url=$global_url echo "To preview the entry, open $preview_url/$filename in your browser" echo -n "[P]ost this entry, [E]dit again, [D]raft for later? (p/E/d) " - read post_status - if [[ "$post_status" == "d" ]] || [[ "$post_status" == "D" ]]; then + read -r post_status + if [[ $post_status == d || $post_status == D ]]; then mkdir -p "drafts/" chmod 700 "drafts/" - title="$(head -n 1 $TMPFILE)" - [[ "$convert_filename" ]] && title="$(echo $title | eval $convert_filename)" - [[ "$title" ]] || title=$RANDOM + title=$(head -n 1 $TMPFILE) + [[ -n $convert_filename ]] && title=$(echo "$title" | eval "$convert_filename") + [[ -n $title ]] || title=$RANDOM - draft="drafts/$title.$fmt" + draft=drafts/$title.$fmt mv "$TMPFILE" "$draft" chmod 600 "$draft" rm "$filename" @@ -617,15 +618,15 @@ EOF fi done - if [[ "$fmt" == "md" && "$save_markdown" ]]; then + if [[ $fmt == md && -n $save_markdown ]]; then mv "$TMPFILE" "${filename%%.*}.md" else rm "$TMPFILE" fi chmod 644 "$filename" echo "Posted $filename" - relevant_tags="$(tags_in_post $filename)" - if [ ! -z "$relevant_tags" ]; then + 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" fi @@ -634,34 +635,36 @@ EOF # Create an index page with all the posts all_posts() { echo -n "Creating an index page with all the posts " - contentfile="$archive_index.$RANDOM" - while [ -f "$contentfile" ]; do - contentfile="$archive_index.$RANDOM" + contentfile=$archive_index.$RANDOM + while [[ -f $contentfile ]]; do + contentfile=$archive_index.$RANDOM done - echo "

$template_archive_title

" >> "$contentfile" - prev_month="" - for i in $(ls -t ./*.html); do - is_boilerplate_file "$i" && continue - echo -n "." - # Month headers - month="$(LC_ALL=$date_locale date -r "$i" +"$date_allposts_header")" - if [[ "$month" != "$prev_month" ]]; then - [[ "$prev_month" ]] && echo "" >> "$contentfile" # Don't close ul before first header - echo "

$month

" >> "$contentfile" - echo "" >> "$contentfile" - echo '' >> "$contentfile" + { + echo "

$template_archive_title

" + prev_month="" + while IFS='' read -r i; do + is_boilerplate_file "$i" && continue + echo -n "." 1>&3 + # Month headers + month=$(LC_ALL=$date_locale date -r "$i" +"$date_allposts_header") + if [[ $month != "$prev_month" ]]; then + [[ -n $prev_month ]] && echo "" # Don't close ul before first header + echo "

$month

" + echo "" + echo "" + } 3>&1 >"$contentfile" create_html_page "$contentfile" "$archive_index.tmp" yes "$global_title — $template_archive_title" mv "$archive_index.tmp" "$archive_index" @@ -672,23 +675,26 @@ all_posts() { # Create an index page with all the tags all_tags() { echo -n "Creating an index page with all the tags " - contentfile="$tags_index.$RANDOM" - while [ -f "$contentfile" ]; do - contentfile="$tags_index.$RANDOM" + contentfile=$tags_index.$RANDOM + while [[ -f $contentfile ]]; do + contentfile=$tags_index.$RANDOM done - echo "

$template_tags_title

" >> "$contentfile" - echo "" >> "$contentfile" - echo '' >> "$contentfile" + { + echo "

$template_tags_title

" + echo "" + echo "" + } 3>&1 > "$contentfile" create_html_page "$contentfile" "$tags_index.tmp" yes "$global_title — $template_tags_title" mv "$tags_index.tmp" "$tags_index" @@ -699,30 +705,32 @@ all_tags() { # Generate the index.html with the content of the latest posts rebuild_index() { echo -n "Rebuilding the index " - newindexfile="$index_file.$RANDOM" - contentfile="$newindexfile.content" - while [ -f "$newindexfile" ]; do - newindexfile="$index_file.$RANDOM" - contentfile="$newindexfile.content" + newindexfile=$index_file.$RANDOM + contentfile=$newindexfile.content + while [[ -f $newindexfile ]]; do + newindexfile=$index_file.$RANDOM + contentfile=$newindexfile.content done # Create the content file - n=0 - for i in $(ls -t ./*.html); do # sort by date, newest first - is_boilerplate_file "$i" && continue; - if [[ "$n" -ge "$number_of_index_articles" ]]; then break; fi - if [ "$cut_do" ]; then - get_html_file_content 'entry' 'entry' 'cut' <$i | awk '/'"$cut_line"'/ { print "

'"$template_read_more"'

" ; next } 1' >> "$contentfile" - else - get_html_file_content 'entry' 'entry' <$i >> "$contentfile" - fi - echo -n "." - n=$(( n + 1 )) - done + { + n=0 + while IFS='' read -r i; do + is_boilerplate_file "$i" && continue; + if ((n >= number_of_index_articles)); then break; fi + if [[ -n $cut_do ]]; then + get_html_file_content 'entry' 'entry' 'cut' <"$i" | awk "/$cut_line/ { print \"

$template_read_more

\" ; next } 1" + else + get_html_file_content 'entry' 'entry' <"$i" + fi + echo -n "." 1>&3 + n=$(( n + 1 )) + done < <(ls -t ./*.html) # sort by date, newest first - feed="$blog_feed" - if [[ "$global_feedburner" != "" ]]; then feed="$global_feedburner"; fi - echo '' >> "$contentfile" + feed=$blog_feed + if [[ -n $global_feedburner ]]; then feed=$global_feedburner; fi + echo "" + } 3>&1 >"$contentfile" echo "" @@ -736,16 +744,17 @@ rebuild_index() { # Accepts either filename as first argument, or post content at stdin # Prints one line with space-separated tags to stdout tags_in_post() { - sed -n "/^

$template_tags_line_header/{s/^

$template_tags_line_header//;s/<[^>]*>//g;s/[ ,]\+/ /g;p;}" $1 | tr ', ' ' ' + sed -n "/^

$template_tags_line_header/{s/^

$template_tags_line_header//;s/<[^>]*>//g;s/[ ,]\+/ /g;p;}" "$1" | tr ', ' ' ' } # Finds all posts referenced in a number of tags. # Arguments are tags # Prints one line with space-separated tags to stdout posts_with_tags() { - [ $# -lt 1 ] && return - tag_files="$(for i in $@; do echo -n $prefix_tags""$i.html" "; done)" - sed -n '/^

/{s/.*href="\([^"]*\)">.*/\1/;p;}' $tag_files 2> /dev/null + (($# < 1)) && return + set -- "${@/#/$prefix_tags}" + set -- "${@/%/.html}" + sed -n '/^

/{s/.*href="\([^"]*\)">.*/\1/;p;}' "$@" 2> /dev/null } # Rebuilds tag_*.html files @@ -758,49 +767,50 @@ posts_with_tags() { # rebuild_tags "one_post.html another_article.html" "example-tag another-tag" # mind the quotes! rebuild_tags() { - if [ "$#" -lt 2 ]; then + if (($# < 2)); then # will process all files and tags - files="$(ls -t ./*.html)" - all_tags="yes" + files=$(ls -t ./*.html) + all_tags=yes else # will process only given files and tags - files="$(echo "$1" | tr ' ' '\n' | sort -u | tr '\n' ' ')" - files="$(ls -t $files)" - tags="$2" + files=$(printf '%s\n' $1 | sort -u) + files=$(ls -t $files) + tags=$2 fi echo -n "Rebuilding tag pages " n=0 - if [ $all_tags ]; then - rm ./$prefix_tags*.html &> /dev/null + if [[ -n $all_tags ]]; then + rm ./"$prefix_tags"*.html &> /dev/null else for i in $tags; do - rm ./$prefix_tags$i.html &> /dev/null + rm "./$prefix_tags$i.html" &> /dev/null done fi # First we will process all files and create temporal tag files # with just the content of the posts - for i in $files; do + tmpfile=tmp.$RANDOM + while [[ -f $tmpfile ]]; do tmpfile=tmp.$RANDOM; done + while IFS='' read -r i; do is_boilerplate_file "$i" && continue; echo -n "." - tmpfile="$(mktemp tmp.XXX)" - if [ "$cut_do" ]; then - get_html_file_content 'entry' 'entry' 'cut' <$i | awk '/'"$cut_line"'/ { print "

'"$template_read_more"'

" ; next } 1' >> "$tmpfile" + if [[ -n $cut_do ]]; then + get_html_file_content 'entry' 'entry' 'cut' <"$i" | awk "/$cut_line/ { print \"

$template_read_more

\" ; next } 1" else - get_html_file_content 'entry' 'entry' <$i >> "$tmpfile" - fi - for tag in $(tags_in_post $i); do - if [ "$all_tags" ] || [[ " $tags " == *" $tag "* ]]; then + get_html_file_content 'entry' 'entry' <"$i" + fi >"$tmpfile" + for tag in $(tags_in_post "$i"); do + if [[ -n $all_tags || " $tags " == *" $tag "* ]]; then cat "$tmpfile" >> "$prefix_tags$tag".tmp.html fi done - rm "$tmpfile" - done + done <<< "$files" + rm "$tmpfile" # Now generate the tag files with headers, footers, etc - for i in $(ls -t ./$prefix_tags*.tmp.html 2>/dev/null || echo ''); do - tagname="$(echo $i | cut -c $((${#prefix_tags}+3))- | sed 's/\.tmp\.html//g')" + while IFS='' read -r i; do + tagname=$(echo "$i" | cut -c "$((${#prefix_tags}+3))-" | sed 's/\.tmp\.html//g') create_html_page "$i" "$prefix_tags$tagname.html" yes "$global_title — $template_tag_title \"$tagname\"" rm "$i" - done + done < <(ls -t ./"$prefix_tags"*.tmp.html 2>/dev/null) echo } @@ -814,16 +824,16 @@ get_post_title() { # Displays a list of the posts list_posts() { ls ./*.html &> /dev/null - [[ $? -ne 0 ]] && echo "No posts yet. Use 'bb.sh post' to create one" && return + (($? != 0)) && echo "No posts yet. Use 'bb.sh post' to create one" && return lines="" n=1 - for i in $(ls -t ./*.html); do + while IFS='' read -r i; do is_boilerplate_file "$i" && continue - line="$n # $(get_post_title "$i") # $(LC_ALL=$date_locale date -r $i +"$date_format")" - lines="${lines}""$line""\n" # Weird stuff needed for the newlines + line="$n # $(get_post_title "$i") # $(LC_ALL=$date_locale date -r "$i" +"$date_format")" + lines+=$line\\n n=$(( n + 1 )) - done + done < <(ls -t ./*.html) echo -e "$lines" | column -t -s "#" } @@ -832,35 +842,38 @@ list_posts() { make_rss() { echo -n "Making RSS " - rssfile="$blog_feed.$RANDOM" - while [ -f "$rssfile" ]; do rssfile="$blog_feed.$RANDOM"; done + rssfile=$blog_feed.$RANDOM + while [[ -f $rssfile ]]; do rssfile=$blog_feed.$RANDOM; done - echo '' >> "$rssfile" - echo '' >> "$rssfile" - echo ''$global_title''$global_url'' >> "$rssfile" - echo ''$global_description'en' >> "$rssfile" - echo ''$(LC_ALL=C date +"%a, %d %b %Y %H:%M:%S %z")'' >> "$rssfile" - echo ''$(LC_ALL=C date +"%a, %d %b %Y %H:%M:%S %z")'' >> "$rssfile" - echo '' >> "$rssfile" - - n=0 - for i in $(ls -t ./*.html); do - is_boilerplate_file "$i" && continue - [[ "$n" -ge "$number_of_feed_articles" ]] && break # max 10 items - echo -n "." - echo '' >> "$rssfile" - echo "$(get_post_title "$i")" >> "$rssfile" - echo '> "$rssfile" - echo "$(get_html_file_content 'text' 'entry' $cut_do <$i)" >> "$rssfile" - echo "]]>$global_url/$(clean_filename $i)" >> "$rssfile" - echo "$global_url/$i" >> "$rssfile" - echo "$global_author" >> "$rssfile" - echo ''$(LC_ALL=C date -r "$i" +"%a, %d %b %Y %H:%M:%S %z")'' >> "$rssfile" - - n=$(( n + 1 )) - done - - echo '' >> "$rssfile" + { + pubdate=$(LC_ALL=C date +"%a, %d %b %Y %H:%M:%S %z") + echo '' + echo '' + echo "$global_title$global_url" + echo "$global_descriptionen" + echo "$pubdate" + echo "$pubdate" + echo "" + + n=0 + while IFS='' read -r i; do + is_boilerplate_file "$i" && continue + ((n >= number_of_feed_articles)) && break # max 10 items + echo -n "." 1>&3 + echo '' + get_post_title "$i" + echo '$global_url/$(clean_filename "$i")" + echo "$global_url/$i" + echo "$global_author" + echo "$(LC_ALL=C date -r "$i" +"%a, %d %b %Y %H:%M:%S %z")" + + n=$(( n + 1 )) + done < <(ls -t ./*.html) + + echo '' + } 3>&1 >"$rssfile" echo "" mv "$rssfile" "$blog_feed" @@ -869,30 +882,33 @@ make_rss() { # generate headers, footers, etc create_includes() { - echo '

'$global_title'

' > ".title.html" - echo '
'$global_description'
' >> ".title.html" + { + echo "

$global_title

" + echo "
$global_description
" + } > ".title.html" - if [[ -f "$header_file" ]]; then cp "$header_file" .header.html - else - echo '' > ".header.html" - echo '' >> ".header.html" - echo '' >> ".header.html" - echo '' >> ".header.html" - for css_file in ${css_include[*]}; do - echo '' >> ".header.html" - done - if [[ "$global_feedburner" == "" ]]; then - echo '' >> ".header.html" + if [[ -f $header_file ]]; then cp "$header_file" .header.html + else { + echo '' + echo '' + echo '' + echo '' + printf '\n' "${css_include[@]}" + if [[ -z $global_feedburner ]]; then + echo "" else - echo '' >> ".header.html" + echo "" fi + } > ".header.html" fi - if [[ -f "$footer_file" ]]; then cp "$footer_file" .footer.html - else - protected_mail="$(echo "$global_email" | sed 's/@/\@/g' | sed 's/\./\./g')" - echo '' >> ".footer.html" + if [[ -f $footer_file ]]; then cp "$footer_file" .footer.html + else { + protected_mail=${global_email//@/@} + protected_mail=${protected_mail//./.} + echo "
$global_license $global_author$protected_mail
" + echo 'Generated with bashblog, a single bash script to easily create blogs like this one
' + } >> ".footer.html" fi } @@ -905,8 +921,8 @@ 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 - [ $css_include ] && return || css_include=('main.css' 'blog.css') - if [[ ! -f "blog.css" ]]; then + [[ -n $css_include ]] && 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;} a.ablack{color:black !important;} @@ -925,9 +941,9 @@ create_css() { # If there is a style.css from the parent page (i.e. some landing page) # then use it. This directive is here for compatibility with my own # home page. Feel free to edit it out, though it doesn't hurt - if [[ -f "../style.css" ]] && [[ ! -f "main.css" ]]; then + if [[ -f ../style.css ]] && [[ ! -f main.css ]]; then ln -s "../style.css" "main.css" - elif [[ ! -f "main.css" ]]; then + elif [[ ! -f main.css ]]; then echo 'body{font-family:Georgia,"Times New Roman",Times,serif;margin:0;padding:0;background-color:#F3F3F3;} #divbodyholder{padding:5px;background-color:#DDD;width:100%;max-width:874px;margin:24px auto;} #divbody{border:solid 1px #ccc;background-color:#fff;padding:0px 48px 24px 48px;top:0;} @@ -954,20 +970,20 @@ rebuild_all_entries() { for i in ./*.html; do # no need to sort is_boilerplate_file "$i" && continue; - contentfile=".tmp.$RANDOM" - while [ -f "$contentfile" ]; do contentfile=".tmp.$RANDOM"; done + contentfile=.tmp.$RANDOM + while [[ -f $contentfile ]]; do contentfile=.tmp.$RANDOM; done echo -n "." # Get the title and entry, and rebuild the html structure from scratch (divs, title, description...) - title="$(get_post_title "$i")" - get_html_file_content 'text' 'text' <$i >> "$contentfile" + title=$(get_post_title "$i") + get_html_file_content 'text' 'text' <"$i" >> "$contentfile" # Original post timestamp - timestamp="$(LC_ALL=C date -r $i +"%a, %d %b %Y %H:%M:%S %z" )" + timestamp=$(LC_ALL=C date -r "$i" +"%a, %d %b %Y %H:%M:%S %z" ) create_html_page "$contentfile" "$i.rebuilt" no "$title" "$timestamp" # keep the original timestamp! - timestamp="$(LC_ALL=C date -r $i +'%Y%m%d%H%M')" + timestamp=$(LC_ALL=C date -r "$i" +'%Y%m%d%H%M') mv "$i.rebuilt" "$i" chmod 644 "$i" touch -t "$timestamp" "$i" @@ -1001,7 +1017,7 @@ usage() { reset() { echo "Are you sure you want to delete all blog entries? Please write \"Yes, I am!\" " read -r line - if [[ "$line" == "Yes, I am!" ]]; then + if [[ $line == "Yes, I am!" ]]; then rm .*.html ./*.html ./*.css ./*.rss &> /dev/null echo echo "Deleted all posts, stylesheets and feeds." @@ -1014,7 +1030,7 @@ reset() { # Detects if GNU date is installed date_version_detect() { date --version >/dev/null 2>&1 - if [[ $? -ne 0 ]]; then + if (($? != 0)); then # date utility is BSD. Test if gdate is installed if gdate --version >/dev/null 2>&1 ; then date() { @@ -1023,13 +1039,13 @@ date_version_detect() { else # BSD date date() { - if [[ "$1" == "-r" ]]; then + if [[ $1 == -r ]]; then # Fall back to using stat for 'date -r' - format=$(echo $3 | sed 's/\+//g') + format=${3//+/} stat -f "%Sm" -t "$format" "$2" - elif [[ $(echo "$@" | grep '\-\-date') ]]; then + elif [[ $2 == --date* ]]; then # convert between dates using BSD date syntax - command date -j -f "%a, %d %b %Y %H:%M:%S %z" "$(echo $2 | sed 's/\-\-date\=//g')" "$1" + command date -j -f "%a, %d %b %Y %H:%M:%S %z" "${2#--date=}" "$1" else # acceptable format for BSD date command date -j "$@" @@ -1049,54 +1065,53 @@ do_main() { date_version_detect # Load default configuration, then override settings with the config file global_variables - [[ -f "$global_config" ]] && source "$global_config" &> /dev/null + [[ -f $global_config ]] && source "$global_config" &> /dev/null global_variables_check # Check for $EDITOR - [[ -z "$EDITOR" ]] && + [[ -z $EDITOR ]] && echo "Please set your \$EDITOR environment variable" && exit # Check for validity of argument - [[ "$1" != "reset" ]] && [[ "$1" != "post" ]] && [[ "$1" != "rebuild" ]] && [[ "$1" != "list" ]] && [[ "$1" != "edit" ]] && [[ "$1" != "delete" ]] && + [[ $1 != "reset" && $1 != "post" && $1 != "rebuild" && $1 != "list" && $1 != "edit" && $1 != "delete" ]] && usage && exit - [[ "$1" == "list" ]] && + [[ $1 == list ]] && list_posts && exit - if [[ "$1" == "edit" ]]; then - if [[ $# -lt 2 ]] || [[ ! -f "${!#}" ]]; then + if [[ $1 == edit ]]; then + if (($# < 2)) || [[ ! -f ${!#} ]]; then echo "Please enter a valid .md or .html file to edit" exit fi fi # Test for existing html files - ls ./*.html &> /dev/null - [[ $? -ne 0 ]] && [[ "$1" == "rebuild" ]] && - echo "Can't find any html files, nothing to rebuild" && exit - - # We're going to back up just in case - ls ./*.html &> /dev/null - [[ $? -eq 0 ]] && + if ls ./*.html &> /dev/null; then + # We're going to back up just in case tar cfz ".backup.tar.gz" *.html && - chmod 600 ".backup.tar.gz" + chmod 600 ".backup.tar.gz" + elif [[ $1 == rebuild ]]; then + echo "Can't find any html files, nothing to rebuild" + exit + fi # Keep first backup of this day containing yesterday's version of the blog - [[ ! -f .yesterday.tar.gz ]] || [ "$(LC_ALL=$date_locale date -r .yesterday.tar.gz +'%d')" != "$(LC_ALL=$date_locale date +'%d')" ] && + [[ ! -f .yesterday.tar.gz || $(date -r .yesterday.tar.gz +'%d') != "$(date +'%d')" ]] && cp .backup.tar.gz .yesterday.tar.gz &> /dev/null - [[ "$1" == "reset" ]] && + [[ $1 == reset ]] && reset && exit create_css create_includes - [[ "$1" == "post" ]] && write_entry "$@" - [[ "$1" == "rebuild" ]] && rebuild_all_entries && rebuild_tags - [[ "$1" == "delete" ]] && rm "$2" &> /dev/null && rebuild_tags - if [[ "$1" == "edit" ]]; then - if [[ "$2" == "-n" ]]; then + [[ $1 == post ]] && write_entry "$@" + [[ $1 == rebuild ]] && rebuild_all_entries && rebuild_tags + [[ $1 == delete ]] && rm "$2" &> /dev/null && rebuild_tags + if [[ $1 == edit ]]; then + if [[ $2 == -n ]]; then edit "$3" - elif [[ "$2" == "-f" ]]; then + elif [[ $2 == -f ]]; then edit "$3" full else edit "$2" keep