From 2abc8068347254b6ea8463e2541bcf757374317a Mon Sep 17 00:00:00 2001 From: Carlos Bautista Date: Thu, 7 Mar 2013 23:30:32 +0100 Subject: [PATCH 1/9] Changed 'date' calls so they work now on BSD Systems (Lost locale date translation on the way :( ) --- bb.sh | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/bb.sh b/bb.sh index 3380c98..67fc3ab 100755 --- a/bb.sh +++ b/bb.sh @@ -147,6 +147,8 @@ global_variables() { # The locale to use for the dates displayed on screen (not for the timestamps) date_format="%B %d, %Y" date_locale="C" + # Date options which work on both BSD and GNU date variants + date_R='+%a, %d %h %Y %H:%M:%S %z' } # Prints the required google analytics code @@ -177,7 +179,8 @@ google_analytics() { # # $1 the file to edit edit() { - timestamp="$(date -r $1 +'%Y%m%d%k%M')" + # timestamp="$(date -r $1 +'%Y%m%d%k%M')" + timestamp="$(stat -f "%Sm" -t '%Y%m%d%k%M' $1)" $EDITOR "$1" touch -t $timestamp "$1" } @@ -238,9 +241,11 @@ create_html_page() { echo "$title" >> "$filename" echo '' >> "$filename" if [ "$timestamp" == "" ]; then - echo '
'$(LC_ALL=date_locale date +"$date_format")' — ' >> "$filename" + # echo '
'$(LC_ALL=date_locale date +"$date_format")' — ' >> "$filename" + echo '
'$(date "$date_R")' — ' >> "$filename" else - echo '
'$(LC_ALL=date_locale date +"$date_format" --date="$timestamp") ' — ' >> "$filename" + # echo '
'$(LC_ALL=date_locale date +"$date_format" --date="$timestamp") ' — ' >> "$filename" + echo '
'"$timestamp"' — ' >> "$filename" fi echo "$global_author
" >> "$filename" echo '' >> "$filename" # This marks the text body, after the title, date... @@ -371,7 +376,8 @@ all_posts() { title="$(awk '/

/, /<\/a><\/h3>/{if (!/

/ && !/<\/a><\/h3>/) print}' $i)" echo -n '
  • '$title' —' >> "$contentfile" # Date - date="$(LC_ALL=date_locale date -r "$i" +"$date_format")" + #date="$(LC_ALL=date_locale date -r "$i" +"$date_format")" + date="$(stat -f "%Sm" -t "%a, %d %h %Y %H:%M:%S %z")" echo " $date
  • " >> "$contentfile" done echo "" @@ -424,7 +430,7 @@ list_posts() { n=1 for i in $(ls -t *.html); do if [ "$i" == "$index_file" ] || [ "$i" == "$archive_index" ]; then continue; fi - line="$n # $(awk '/

    /, /<\/a><\/h3>/{if (!/

    / && !/<\/a><\/h3>/) print}' $i) # $(LC_ALL=date_locale date -r $i +"date_format")" + line="$n # $(awk '/

    /, /<\/a><\/h3>/{if (!/

    / && !/<\/a><\/h3>/) print}' $i) # $(stat -f "%Sm" -t "%a, %d %h %Y %H:%M:%S %z" $i)" lines="${lines}""$line""\n" # Weird stuff needed for the newlines n=$(( $n + 1 )) done @@ -443,8 +449,8 @@ make_rss() { echo '' >> "$rssfile" echo ''$global_title''$global_url'' >> "$rssfile" echo ''$global_description'en' >> "$rssfile" - echo ''$(date -R)'' >> "$rssfile" - echo ''$(date -R)'' >> "$rssfile" + echo ''$(date "$date_R")'' >> "$rssfile" + echo ''$(date "$date_R")'' >> "$rssfile" echo '' >> "$rssfile" n=0 @@ -460,8 +466,9 @@ make_rss() { echo "]]>$global_url/$i" >> "$rssfile" echo "$global_url/$i" >> "$rssfile" echo "$global_author" >> "$rssfile" - echo ''$(date -r "$i" -R)'' >> "$rssfile" + #echo ''$(date -r "$i" -R)'' >> "$rssfile" + echo ''$(stat -f "%Sm" -t "%a, %d %h %Y %H:%M:%S %z" "$i" )'' >> "$rssfile" n=$(( $n + 1 )) done @@ -557,11 +564,13 @@ rebuild_all_entries() { awk '//, //{if (!// && !//) print}' "$i" >> "$contentfile" # Original post timestamp - timestamp="$(date -R -r $i)" + #timestamp="$(date -R -r $i)" + timestamp="$(stat -f "%Sm" -t "%a, %d %h %Y %H:%M:%S %z" $i)" create_html_page "$contentfile" "$i.rebuilt" no "$title" "$timestamp" # keep the original timestamp! - timestamp="$(date -r $i +'%Y%m%d%k%M')" + #timestamp="$(date -r $i +'%Y%m%d%k%M')" + timestamp="$(stat -f "%Sm" -t '%Y%m%d%k%M' $i)" mv "$i.rebuilt" "$i" chmod 644 "$i" touch -t $timestamp "$i" @@ -663,3 +672,4 @@ do_main() { # Do not change anything here. If you want to modify the code, edit do_main() # do_main $* + From cccb1547c90f3fd2e9689e5ef26e7deb79c58f15 Mon Sep 17 00:00:00 2001 From: Carlos Bautista Date: Wed, 13 Mar 2013 14:48:44 +0100 Subject: [PATCH 2/9] Added config file support and check for files existing prior to backup --- bb.sh | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/bb.sh b/bb.sh index 1e04a7c..6d6fc32 100755 --- a/bb.sh +++ b/bb.sh @@ -85,6 +85,10 @@ # Global variables # It is recommended to perform a 'rebuild' after changing any of this in the code + +# Config file. Use instead of this function if you want to avoid merges in VCS +global_config=".config" + global_variables() { global_software_name="BashBlog" global_software_version="1.5.1" @@ -424,6 +428,12 @@ rebuild_index() { # Displays a list of the posts list_posts() { + ls *.html &> /dev/null + if [[ $? -ne 0 ]]; then + echo "No posts yet. Use 'bb.sh post' to create one" + return + fi + lines="" n=1 for i in $(ls -t *.html); do @@ -599,8 +609,10 @@ reset() { echo "Are you sure you want to delete all blog entries? Please write \"Yes, I am!\" " read line if [ "$line" == "Yes, I am!" ]; then - rm *.html *.css *.rss + rm .*.html *.html *.css *.rss &> /dev/null + echo echo "Deleted all posts, stylesheets and feeds." + echo "Kept your old '.backup.tar.gz' just in case, please delete it manually if needed." else echo "Phew! You dodged a bullet there. Nothing was modified." fi @@ -612,7 +624,8 @@ reset() { # $1 command to run # $2 file name of a draft to continue editing (optional) do_main() { - global_variables + # Use config file or fallback to inline configuration + source "$global_config" || global_variables # Check for $EDITOR if [[ -z "$EDITOR" ]]; then @@ -645,8 +658,11 @@ do_main() { fi # We're going to back up just in case + ls *.html &> /dev/null + if [[ $? -eq 0 ]]; then tar cfz ".backup.tar.gz" *.html chmod 600 ".backup.tar.gz" + fi if [ "$1" == "reset" ]; then reset @@ -669,5 +685,4 @@ do_main() { # MAIN # Do not change anything here. If you want to modify the code, edit do_main() # -do_main $* - +do_main $* \ No newline at end of file From cae7d16376ec190cb122a2a7e65a46ef1345c203 Mon Sep 17 00:00:00 2001 From: Carlos Bautista Date: Tue, 2 Apr 2013 16:40:10 +0200 Subject: [PATCH 3/9] Reverted date commands to GNU Date commands Added detection of date version. Tries to use gdate --- bb.sh | 52 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/bb.sh b/bb.sh index 67c5179..1b9f5e6 100755 --- a/bb.sh +++ b/bb.sh @@ -152,8 +152,6 @@ global_variables() { # The locale to use for the dates displayed on screen (not for the timestamps) date_format="%B %d, %Y" date_locale="C" - # Date options which work on both BSD and GNU date variants - date_R='+%a, %d %h %Y %H:%M:%S %z' } # Prints the required google analytics code @@ -184,8 +182,7 @@ google_analytics() { # # $1 the file to edit edit() { - # timestamp="$(date -r $1 +'%Y%m%d%k%M')" - timestamp="$(stat -f "%Sm" -t '%Y%m%d%k%M' $1)" + timestamp="$(date -r $1 +'%Y%m%d%k%M')" $EDITOR "$1" touch -t $timestamp "$1" } @@ -246,11 +243,9 @@ create_html_page() { echo "$title" >> "$filename" echo '

    ' >> "$filename" if [ "$timestamp" == "" ]; then - # echo '
    '$(LC_ALL=date_locale date +"$date_format")' — ' >> "$filename" - echo '
    '$(date "$date_R")' — ' >> "$filename" + echo '
    '$(LC_ALL=date_locale date +"$date_format")' — ' >> "$filename" else - # echo '
    '$(LC_ALL=date_locale date +"$date_format" --date="$timestamp") ' — ' >> "$filename" - echo '
    '"$timestamp"' — ' >> "$filename" + echo '
    '$(LC_ALL=date_locale date +"$date_format" --date="$timestamp") ' — ' >> "$filename" fi echo "$global_author
    " >> "$filename" echo '' >> "$filename" # This marks the text body, after the title, date... @@ -381,8 +376,7 @@ all_posts() { title="$(awk '/

    /, /<\/a><\/h3>/{if (!/

    / && !/<\/a><\/h3>/) print}' $i)" echo -n '
  • '$title' —' >> "$contentfile" # Date - #date="$(LC_ALL=date_locale date -r "$i" +"$date_format")" - date="$(stat -f "%Sm" -t "%a, %d %h %Y %H:%M:%S %z")" + date="$(LC_ALL=date_locale date -r "$i" +"$date_format")" echo " $date
  • " >> "$contentfile" done echo "" @@ -441,7 +435,7 @@ list_posts() { n=1 for i in $(ls -t *.html); do if [ "$i" == "$index_file" ] || [ "$i" == "$archive_index" ]; then continue; fi - line="$n # $(awk '/

    /, /<\/a><\/h3>/{if (!/

    / && !/<\/a><\/h3>/) print}' $i) # $(stat -f "%Sm" -t "%a, %d %h %Y %H:%M:%S %z" $i)" + line="$n # $(awk '/

    /, /<\/a><\/h3>/{if (!/

    / && !/<\/a><\/h3>/) print}' $i) # $(LC_ALL=date_locale date -r $i +"date_format")" lines="${lines}""$line""\n" # Weird stuff needed for the newlines n=$(( $n + 1 )) done @@ -460,8 +454,8 @@ make_rss() { echo '' >> "$rssfile" echo ''$global_title''$global_url'' >> "$rssfile" echo ''$global_description'en' >> "$rssfile" - echo ''$(date "$date_R")'' >> "$rssfile" - echo ''$(date "$date_R")'' >> "$rssfile" + echo ''$(date -R)'' >> "$rssfile" + echo ''$(date -R)'' >> "$rssfile" echo '' >> "$rssfile" n=0 @@ -477,9 +471,8 @@ make_rss() { echo "]]>$global_url/$i" >> "$rssfile" echo "$global_url/$i" >> "$rssfile" echo "$global_author" >> "$rssfile" + echo ''$(date -r "$i" -R)'' >> "$rssfile" - #echo ''$(date -r "$i" -R)'' >> "$rssfile" - echo ''$(stat -f "%Sm" -t "%a, %d %h %Y %H:%M:%S %z" "$i" )'' >> "$rssfile" n=$(( $n + 1 )) done @@ -575,13 +568,11 @@ rebuild_all_entries() { awk '//, //{if (!// && !//) print}' "$i" >> "$contentfile" # Original post timestamp + timestamp="$(date -R -r $i)" - #timestamp="$(date -R -r $i)" - timestamp="$(stat -f "%Sm" -t "%a, %d %h %Y %H:%M:%S %z" $i)" create_html_page "$contentfile" "$i.rebuilt" no "$title" "$timestamp" # keep the original timestamp! - #timestamp="$(date -r $i +'%Y%m%d%k%M')" - timestamp="$(stat -f "%Sm" -t '%Y%m%d%k%M' $i)" + timestamp="$(date -r $i +'%Y%m%d%k%M')" mv "$i.rebuilt" "$i" chmod 644 "$i" touch -t $timestamp "$i" @@ -621,14 +612,31 @@ reset() { fi } +date_version_detect() { + if !(date --version >/dev/null 2>&1) ; then + # date utility is BSD. Test if gdate is installed + if gdate --version >/dev/null 2>&1 ; then + alias date=gdate + echo Using gdate. + else + echo ERROR: Not GNU date found. + echo Try installing gdate utility or coreutils. + echo Exiting... + exit + fi + fi +} + # Main function # Encapsulated on its own function for readability purposes # # $1 command to run # $2 file name of a draft to continue editing (optional) do_main() { + # Detect if using BSD date or GNU date + date_version_detect # Use config file or fallback to inline configuration - source "$global_config" || global_variables + source "$global_config" &> /dev/null || global_variables # Check for $EDITOR if [[ -z "$EDITOR" ]]; then @@ -663,8 +671,8 @@ do_main() { # We're going to back up just in case ls *.html &> /dev/null if [[ $? -eq 0 ]]; then - tar cfz ".backup.tar.gz" *.html - chmod 600 ".backup.tar.gz" + tar cfz ".backup.tar.gz" *.html + chmod 600 ".backup.tar.gz" fi if [ "$1" == "reset" ]; then From cb5e388f1e554280d11615b7866a6b79669ea6eb Mon Sep 17 00:00:00 2001 From: Carlos Bautista Date: Tue, 2 Apr 2013 16:52:11 +0200 Subject: [PATCH 4/9] Added disqus support --- bb.sh | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/bb.sh b/bb.sh index 1b9f5e6..fa308f3 100755 --- a/bb.sh +++ b/bb.sh @@ -90,6 +90,8 @@ global_config=".config" global_variables() { + if [[ -z $EDITOR ]]; then EDITOR=vim; fi + global_software_name="BashBlog" global_software_version="1.5.1" @@ -191,8 +193,8 @@ edit() { # # $1 the post URL twitter() { - echo "

    $template_comments  " - echo " $template_comments  " + echo "' >> "$filename" # content + echo ${filename%.*.*} + if [[ ${filename%.*.*} != "index" && ${filename%.*.*} != "all_posts" ]]; then + cat disqus >> "$filename" + fi # page footer cat .footer.html >> "$filename" # close divs echo '

    ' >> "$filename" # divbody and divbodyholder + cat disqus_footer >> "$filename" echo '' >> "$filename" } From 2ca8e59bd0e11dd47734bce3ebe0794c9ba72383 Mon Sep 17 00:00:00 2001 From: Carlos Bautista Date: Tue, 2 Apr 2013 23:35:33 +0200 Subject: [PATCH 5/9] Configuration settings for disqus variables --- bb.sh | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/bb.sh b/bb.sh index fa308f3..961eda9 100755 --- a/bb.sh +++ b/bb.sh @@ -90,10 +90,10 @@ global_config=".config" global_variables() { - if [[ -z $EDITOR ]]; then EDITOR=vim; fi + echo Loading inline configuration global_software_name="BashBlog" - global_software_version="1.5.1" + global_software_version="1.6.0" # Blog title global_title="My fancy blog" @@ -124,6 +124,10 @@ global_variables() { global_twitter="true" global_twitter_username="example" + # Leave these empty if you don't want to use disqus for comments + global_disqus="true" + global_disqus_username="disqus_undefined" + # Blog generated files # index page of blog (it is usually good to use "index.html" here) @@ -175,6 +179,45 @@ google_analytics() { " } +# Prints the required code for disqus comments +disqus_body() { +if [ "$global_disqus" != "" ]; then + echo '
    + +
    + comments powered by Disqus' +fi + +} +# Prints the required code for disqus in the footer +disqus_footer() { +if [ "$global_disqus" != "" ]; then + echo '' + + fi +} + # 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'. @@ -266,13 +309,13 @@ create_html_page() { echo '
    ' >> "$filename" # content echo ${filename%.*.*} if [[ ${filename%.*.*} != "index" && ${filename%.*.*} != "all_posts" ]]; then - cat disqus >> "$filename" + disqus_body >> "$filename" fi # page footer cat .footer.html >> "$filename" # close divs echo '
    ' >> "$filename" # divbody and divbodyholder - cat disqus_footer >> "$filename" + disqus_footer >> "$filename" echo '' >> "$filename" } @@ -623,7 +666,7 @@ date_version_detect() { if !(date --version >/dev/null 2>&1) ; then # date utility is BSD. Test if gdate is installed if gdate --version >/dev/null 2>&1 ; then - alias date=gdate + DATE_COMMAND=gdate echo Using gdate. else echo ERROR: Not GNU date found. @@ -631,9 +674,14 @@ date_version_detect() { echo Exiting... exit fi + + else + DATE_COMMAND=date fi } - +date() { +$DATE_COMMAND "$@" +} # Main function # Encapsulated on its own function for readability purposes # @@ -643,6 +691,7 @@ do_main() { # Detect if using BSD date or GNU date date_version_detect # Use config file or fallback to inline configuration + echo Loading configuration source "$global_config" &> /dev/null || global_variables # Check for $EDITOR From 7c9d9c655f1769bca5ca18d8f023fd2d92c6a3fa Mon Sep 17 00:00:00 2001 From: Carlos Bautista Date: Tue, 9 Apr 2013 17:09:23 +0200 Subject: [PATCH 6/9] Fixed detection of GNU date so it works on Linux --- bb.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bb.sh b/bb.sh index 961eda9..087ae79 100755 --- a/bb.sh +++ b/bb.sh @@ -307,7 +307,8 @@ create_html_page() { fi echo '

    ' >> "$filename" # content - echo ${filename%.*.*} + + # Add disqus commments except for index and all_posts pages if [[ ${filename%.*.*} != "index" && ${filename%.*.*} != "all_posts" ]]; then disqus_body >> "$filename" fi @@ -663,7 +664,8 @@ reset() { } date_version_detect() { - if !(date --version >/dev/null 2>&1) ; then + date --version >/dev/null 2>&1 + if [ $? -ne 0 ]; then # date utility is BSD. Test if gdate is installed if gdate --version >/dev/null 2>&1 ; then DATE_COMMAND=gdate From 5ff62fefe906bf9795a8a1e4845863a73c098bd0 Mon Sep 17 00:00:00 2001 From: Carlos Bautista Date: Tue, 9 Apr 2013 17:25:35 +0200 Subject: [PATCH 7/9] Fixed use of date() function --- bb.sh | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/bb.sh b/bb.sh index 087ae79..2bfdb29 100755 --- a/bb.sh +++ b/bb.sh @@ -668,7 +668,9 @@ date_version_detect() { if [ $? -ne 0 ]; then # date utility is BSD. Test if gdate is installed if gdate --version >/dev/null 2>&1 ; then - DATE_COMMAND=gdate + date() { + gdate "$@" + } echo Using gdate. else echo ERROR: Not GNU date found. @@ -676,14 +678,9 @@ date_version_detect() { echo Exiting... exit fi - - else - DATE_COMMAND=date fi } -date() { -$DATE_COMMAND "$@" -} + # Main function # Encapsulated on its own function for readability purposes # From 35e5843273239e730bd9c359883decc86bea30c4 Mon Sep 17 00:00:00 2001 From: Carlos Bautista Date: Tue, 9 Apr 2013 17:50:03 +0200 Subject: [PATCH 8/9] date: changed %k switch to %H. The former inserts padding spaces when hour is under 10 --- bb.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bb.sh b/bb.sh index 2bfdb29..3cd0c65 100755 --- a/bb.sh +++ b/bb.sh @@ -227,7 +227,7 @@ if [ "$global_disqus" != "" ]; then # # $1 the file to edit edit() { - timestamp="$(date -r $1 +'%Y%m%d%k%M')" + timestamp="$(date -r $1 +'%Y%m%d%H%M')" $EDITOR "$1" touch -t $timestamp "$1" } @@ -623,7 +623,7 @@ rebuild_all_entries() { create_html_page "$contentfile" "$i.rebuilt" no "$title" "$timestamp" # keep the original timestamp! - timestamp="$(date -r $i +'%Y%m%d%k%M')" + timestamp="$(date -r $i +'%Y%m%d%H%M')" mv "$i.rebuilt" "$i" chmod 644 "$i" touch -t $timestamp "$i" From 5e4215eecf2646fc22793d5cf1dbe0f266a50ab3 Mon Sep 17 00:00:00 2001 From: Carlos Bautista Date: Tue, 9 Apr 2013 19:41:37 +0200 Subject: [PATCH 9/9] Updated comments --- README.md | 1 - bb.sh | 6 ++++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7069dee..2ffc7bd 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,5 @@ Features Non features (not planned) -------------------------- -- Comments. Would need a CAPTCHA or another antispam mechanism. Comments are handled through twitter, with a Twitter button Read the CHANGELOG section of the script header for more updates diff --git a/bb.sh b/bb.sh index 3cd0c65..f126832 100755 --- a/bb.sh +++ b/bb.sh @@ -15,7 +15,7 @@ # Basically it asks the user to create a text file, then converts it into a .html file # and then rebuilds the index.html and feed.rss. # -# Comments are not supported. +# Comments are supported via external service (Disqus). # # This script is standalone, it doesn't require any other file to run # @@ -60,6 +60,8 @@ # ######################################################################################### # +# 1.6.1 'date' fix when hours are 1 digit. +# 1.6.0 Disqus comments. External configuration file. Check of 'date' command version. # 1.5.1 Misc bugfixes and parameter checks # 1.5 Durad Radojicic refactored some code and added flexibility and i18n # 1.4.2 Now issues are handled at Github @@ -93,7 +95,7 @@ global_variables() { echo Loading inline configuration global_software_name="BashBlog" - global_software_version="1.6.0" + global_software_version="1.6.1" # Blog title global_title="My fancy blog"