diff --git a/bb.sh b/bb.sh index a86c138..1ade8cf 100755 --- a/bb.sh +++ b/bb.sh @@ -61,6 +61,8 @@ # ######################################################################################### # +# 2.0 Added Markdown support +# Fully support BSD date # 1.6.4 Fixed bug in localized dates # 1.6.3 Now supporting BSD date # 1.6.2 Simplified some functions and variables to avoid duplicated information @@ -101,7 +103,7 @@ global_config=".config" # by the 'global_config' file contents global_variables() { global_software_name="BashBlog" - global_software_version="1.6.4" + global_software_version="2.0" # Blog title global_title="My fancy blog" @@ -164,11 +166,46 @@ global_variables() { # The locale to use for the dates displayed on screen (not for the timestamps) date_format="%B %d, %Y" date_locale="C" + + # Markdown location. Trying to autodetect by default, but change this + # to the script location + markdown_bin="$(which Markdown.pl)" } +# Test if the markdown script is working correctly +test_markdown() { + [[ -z "$markdown_bin" ]] && echo "Markdown binary not found" && return 1 + + in="/tmp/md-in-$(echo $RANDOM).md" + out="/tmp/md-out-$(echo $RANDOM).html" + good="/tmp/md-good-$(echo $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 + diff $good $out &> /dev/null # output is irrelevant, we'll check $? + if [[ $? -ne 0 ]]; then + rm -f $in $good $out + echo "Markdown binary not converting properly" + return 1 + fi + + rm -f $in $good $out + return 0 +} + + +# 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 + $markdown_bin $1 > $out + echo $out +} + + # Prints the required google analytics code google_analytics() { - if [ "$global_analytics" == "" ]; then return; fi + [[ -z "$global_analytics" ]] && return echo "' + /* * * DONT EDIT BELOW THIS LINE * * */ + (function () { + var s = document.createElement("script"); s.async = true; + s.type = "text/javascript"; + s.src = "//" + disqus_shortname + ".disqus.com/count.js"; + (document.getElementsByTagName("HEAD")[0] || document.getElementsByTagName("BODY")[0]).appendChild(s); + }()); + ' } # Edit an existing, published .html file while keeping its original timestamp @@ -240,9 +277,9 @@ edit() { # # $1 the post URL twitter() { - if [[ "$global_twitter_username" == "" ]]; then return; fi + [[ -z "$global_twitter_username" ]] && return - if [[ "$global_disqus_username" == "" ]]; then + if [[ "$global_disqus_username" ]]; then echo "

$template_comments " else echo "

$template_comments  " @@ -291,12 +328,13 @@ create_html_page() { file_url="$(sed 's/.rebuilt//g' <<< $filename)" # Get the correct URL when rebuilding # one blog entry - if [ "$index" == "no" ]; then + if [[ "$index" == "no" ]]; then echo '' >> "$filename" # marks the beginning of the whole post echo '

' >> "$filename" - echo "$title" >> "$filename" + # remove possible

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

' >> "$filename" - if [ "$timestamp" == "" ]; then + if [[ "$timestamp" == "" ]]; then echo '
'$(LC_ALL=$date_locale date +"$date_format")' — ' >> "$filename" else echo '
'$(LC_ALL=$date_locale date +"$date_format" --date="$timestamp") ' — ' >> "$filename" @@ -305,7 +343,7 @@ create_html_page() { echo '' >> "$filename" # This marks the text body, after the title, date... fi cat "$content" >> "$filename" # Actual content - if [ "$index" == "no" ]; then + if [[ "$index" == "no" ]]; then echo '' >> "$filename" twitter "$global_url/$file_url" >> "$filename" @@ -332,9 +370,10 @@ parse_file() { # Read for the title and check that the filename is ok title="" while read line; do - if [ "$title" == "" ]; then + if [[ "$title" == "" ]]; then title="$line" - filename="$(echo $title | tr [:upper:] [:lower:])" + # remove extra

and

added by markdown + filename="$(echo $title | sed 's/\<\/*p\>//g' | tr [:upper:] [:lower:])" filename="$(echo $filename | sed 's/\ /-/g')" filename="$(echo $filename | tr -dc '[:alnum:]-')" # html likes alphanumeric filename="$filename.html" @@ -348,7 +387,7 @@ parse_file() { else echo "$line" >> "$content" fi - done < "$TMPFILE" + done < "$1" # Create the actual html page create_html_page "$content" "$filename" no "$title" @@ -358,38 +397,57 @@ parse_file() { # Manages the creation of the text file and the parsing to html file # also the drafts write_entry() { - if [ "$1" != "" ]; then - TMPFILE="$1" - if [ ! -f "$TMPFILE" ]; then + fmt="html"; f="$2" + [[ "$2" == "-m" ]] && fmt="md" && f="$3" + [[ "$fmt" == "md" ]] && [[ $(test_markdown) -ne 0 ]] && + echo "Markdown is not working, please use HTML. Press a key to continue..." && + fmt="html" && read + + if [[ "$f" != "" ]]; then + TMPFILE="$f" + if [[ ! -f "$TMPFILE" ]]; then echo "The file doesn't exist" delete_includes exit fi + # check if TMPFILE is markdown even though the user didn't specify it + extension="${TMPFILE##*.}" + [[ "$extension" == "md" ]] && fmt="md" else - TMPFILE=".entry-$RANDOM.html" + TMPFILE=".entry-$RANDOM.$fmt" echo "Title on this line" >> "$TMPFILE" echo "" >> "$TMPFILE" - echo "

The rest of the text file is an html blog post. The process" >> "$TMPFILE" - echo "will continue as soon as you exit your editor

" >> "$TMPFILE" + [[ "$fmt" == "html" ]] && echo -n "

" >> "$TMPFILE" + echo -n "The rest of the text file is " >> "$TMPFILE" + [[ "$fmt" == "html" ]] && echo -n "an html " >> "$TMPFILE" + [[ "$fmt" == "md" ]] && echo -n "a **Markdown** " >> "$TMPFILE" + echo -n "blog post. The process will continue as soon as you exit your editor" >> "$TMPFILE" + [[ "$fmt" == "html" ]] && echo "

" >> "$TMPFILE" fi chmod 600 "$TMPFILE" post_status="E" while [ "$post_status" != "p" ] && [ "$post_status" != "P" ]; do $EDITOR "$TMPFILE" - parse_file "$TMPFILE" # this command sets $filename as the html processed file + if [[ "$fmt" == "md" ]]; then + html_from_md="$(markdown "$TMPFILE")" + parse_file "$html_from_md" + rm "$html_from_md" + else + parse_file "$TMPFILE" # this command sets $filename as the html processed file + fi chmod 600 "$filename" echo -n "Preview? (Y/n) " read p - if [ "$p" != "n" ] && [ "$p" != "N" ]; then + if [[ "$p" != "n" ]] && [[ "$p" != "N" ]]; then chmod 644 "$filename" echo "Open $global_url/$filename in your browser" fi 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 + if [[ "$post_status" == "d" ]] || [[ "$post_status" == "D" ]]; then mkdir -p "drafts/" chmod 700 "drafts/" @@ -397,8 +455,8 @@ write_entry() { title="$(echo $title | tr [:upper:] [:lower:])" title="$(echo $title | sed 's/\ /-/g')" title="$(echo $title | tr -dc '[:alnum:]-')" - draft="drafts/$title.html" - while [ -f "$draft" ]; do draft="drafts/$title-$RANDOM.html"; done + draft="drafts/$title.$fmt" + while [ -f "$draft" ]; do draft="drafts/$title-$RANDOM.$fmt"; done mv "$TMPFILE" "$draft" chmod 600 "$draft" @@ -407,7 +465,7 @@ write_entry() { echo "Saved your draft as '$draft'" exit fi - if [ "$post_status" == "e" ] || [ "$post_status" == "E" ]; then + if [[ "$post_status" == "e" ]] || [[ "$post_status" == "E" ]]; then rm "$filename" # Delete the html file as it will be generated again fi done @@ -428,7 +486,7 @@ all_posts() { echo "

All posts

" >> "$contentfile" echo "