diff options
-rwxr-xr-x | bb.sh | 683 |
1 files changed, 349 insertions, 334 deletions
@@ -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 "<p>line 1</p>\n\n<p>line 2</p>" > "$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 "<script type=\"text/javascript\"> var _gaq = _gaq || []; @@ -214,7 +214,7 @@ google_analytics() { # Prints the required code for disqus comments disqus_body() { - [[ -z "$global_disqus_username" ]] && return + [[ -z $global_disqus_username ]] && return echo '<div id="disqus_thread"></div> <script type="text/javascript"> @@ -234,7 +234,7 @@ disqus_body() { # Prints the required code for disqus in the footer disqus_footer() { - [[ -z "$global_disqus_username" ]] && return + [[ -z $global_disqus_username ]] && return echo '<script type="text/javascript"> /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */ var disqus_shortname = '\'$global_disqus_username\''; // required: replace example with your forum shortname @@ -256,15 +256,15 @@ disqus_footer() { # note that this does not remove <hr /> line itself, # so you can see if text was cut or not get_html_file_content() { - awk '/<!-- '$1' begin -->/, /<!-- '$2' end -->/{ - if (!/<!-- '$1' begin -->/ && !/<!-- '$2' end -->/) print - if ("'$3'" == "cut" && /'"$cut_line"'/){ - if ("'$2'" == "text") exit # no need to read further + awk "/<!-- $1 begin -->/, /<!-- $2 end -->/{ + if (!/<!-- $1 begin -->/ && !/<!-- $2 end -->/) print + if (\"$3\" == \"cut\" && /$cut_line/){ + if (\"$2\" == \"text\") exit # no need to read further while (getline > 0 && !/<!-- text end -->/) { - if ("'$cut_tags'" == "no" && /^'"<p>$template_tags_line_header"'/ ) print + if (\"$cut_tags\" == \"no\" && /^<p>$template_tags_line_header/ ) print } } - }' + }" } # Edit an existing, published .html file while keeping its original timestamp @@ -281,48 +281,48 @@ get_html_file_content() { # leave empty for default behavior (edit only text part and change name) edit() { # Original post timestamp - edit_timestamp="$(LC_ALL=C date -r "${1%%.*}.html" +"%a, %d %b %Y %H:%M:%S %z" )" - touch_timestamp="$(LC_ALL=C date -r "${1%%.*}.html" +'%Y%m%d%H%M')" - tags_before="$(tags_in_post "${1%%.*}.html")" - if [ "$2" = "full" ]; then + edit_timestamp=$(LC_ALL=C date -r "${1%%.*}.html" +"%a, %d %b %Y %H:%M:%S %z" ) + touch_timestamp=$(LC_ALL=C date -r "${1%%.*}.html" +'%Y%m%d%H%M') + tags_before=$(tags_in_post "${1%%.*}.html") + if [[ $2 == full ]]; then $EDITOR "$1" - filename="$1" + filename=$1 else - if [[ "${1##*.}" == "md" ]]; then + if [[ ${1##*.} == md ]]; then test_markdown - if [[ "$?" -ne 0 ]]; then + if (($? != 0)); then echo "Markdown is not working, please edit HTML file directly." exit fi # editing markdown file $EDITOR "$1" - TMPFILE="$(markdown "$1")" - filename="${1%%.*}.html" + TMPFILE=$(markdown "$1") + filename=${1%%.*}.html else # Create the content file - TMPFILE="$(basename $1).$RANDOM.html" + TMPFILE=$(basename "$1").$RANDOM.html # Title - echo "$(get_post_title $1)" > "$TMPFILE" + get_post_title "$1" > "$TMPFILE" # Post text with plaintext tags - get_html_file_content 'text' 'text' <$1 | sed "/^<p>$template_tags_line_header/s|<a href='$prefix_tags\([^']*\).html'>\\1</a>|\\1|g" >> "$TMPFILE" + get_html_file_content 'text' 'text' <"$1" | sed "/^<p>$template_tags_line_header/s|<a href='$prefix_tags\([^']*\).html'>\\1</a>|\\1|g" >> "$TMPFILE" $EDITOR "$TMPFILE" - filename="$1" + filename=$1 fi rm "$filename" - if [ "$2" = "keep" ]; then + if [[ $2 == keep ]]; then parse_file "$TMPFILE" "$edit_timestamp" "$filename" else parse_file "$TMPFILE" "$edit_timestamp" # this command sets $filename as the html processed file - [[ "${1##*.}" == "md" ]] && mv "$1" "${filename%%.*}.md" 2>/dev/null + [[ ${1##*.} == md ]] && mv "$1" "${filename%%.*}.md" 2>/dev/null fi rm "$TMPFILE" fi touch -t "$touch_timestamp" "$filename" 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 + 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" fi @@ -333,16 +333,16 @@ edit() { # $1 the post file # $2 the title twitter_card() { - [[ -z "$global_twitter_username" ]] && return + [[ -z $global_twitter_username ]] && return echo "<meta name='twitter:card' content='summary' />" echo "<meta name='twitter:site' content='@$global_twitter_username' />" echo "<meta name='twitter:title' content='$2' />" # Twitter truncates at 70 char - description=$(grep -v "^<p>$template_tags_line_header" $1 | sed -e 's/<[^>]*>//g' | head -c 250 | tr '\n' ' ' | sed "s/\"/'/g") + description=$(grep -v "^<p>$template_tags_line_header" "$1" | sed -e 's/<[^>]*>//g' | head -c 250 | tr '\n' ' ' | sed "s/\"/'/g") echo "<meta name='twitter:description' content=\"$description\" />" - image=$(sed -n 's/.*<img.*src="\([^"]*\)".*/\1/p' $1 | head -n 1) # First image is fine - [[ -z "$image" ]] && return - [[ $image =~ ^https?:\/\/ ]] || image="$global_url/$image" # Check that URL is absolute + image=$(sed -n 's/.*<img.*src="\([^"]*\)".*/\1/p' "$1" | head -n 1) # First image is fine + [[ -z $image ]] && return + [[ $image =~ ^https?:// ]] || image=$global_url/$image # Check that URL is absolute echo "<meta name='twitter:image' content='$image' />" } @@ -350,20 +350,20 @@ twitter_card() { # # $1 the post URL twitter() { - [[ -z "$global_twitter_username" ]] && return + [[ -z $global_twitter_username ]] && return - if [[ -z "$global_disqus_username" ]]; then - if [[ "$global_twitter_cookieless" == "true" ]]; then + if [[ -z $global_disqus_username ]]; then + if [[ $global_twitter_cookieless == true ]]; then id=$RANDOM search_engine="https://twitter.com/search?q=" - [[ "$global_twitter_search" == "topsy" ]] && search_engine="http://topsy.com/trackback?url=" + [[ $global_twitter_search == topsy ]] && search_engine="http://topsy.com/trackback?url=" echo "<p id='twitter'><a href='http://twitter.com/intent/tweet?url=$1&text=$template_twitter_comment&via=$global_twitter_username'>$template_comments $template_twitter_button</a> " echo "<a href='$search_engine""$1'><span id='count-$id'></span></a> </p>" # Get current tweet count - echo '<script type="text/javascript">$.ajax({type: "GET", url: "https://cdn.api.twitter.com/1/urls/count.json?url='$1'", - dataType: "jsonp", success: function(data){ $("#count-'$id'").html("(" + data.count + ")"); }}); </script>' + echo "<script type=\"text/javascript\">\$.ajax({type: \"GET\", url: \"https://cdn.api.twitter.com/1/urls/count.json?url=$1\", + dataType: \"jsonp\", success: function(data){ \$(\"#count-$id\").html(\"(\" + data.count + \")\"); }}); </script>" return; else echo "<p id='twitter'>$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>$title</title>" >> "$filename" - google_analytics >> "$filename" - twitter_card "$content" "$title" >> "$filename" - echo "</head><body>" >> "$filename" - # stuff to add before the actual body content - [[ -n "$body_begin_file" ]] && cat "$body_begin_file" >> "$filename" - # body divs - echo '<div id="divbodyholder">' >> "$filename" - echo '<div class="headerholder"><div class="header">' >> "$filename" - # blog title - echo '<div id="title">' >> "$filename" - cat .title.html >> "$filename" - echo '</div></div></div>' >> "$filename" # title, header, headerholder - echo '<div id="divbody"><div class="content">' >> "$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 '<!-- entry begin -->' >> "$filename" # marks the beginning of the whole post - echo '<h3><a class="ablack" href="'$file_url'">' >> "$filename" - # remove possible <p>'s on the title because of markdown conversion - echo "$(echo "$title" | sed 's/<\/*p>//g')" >> "$filename" - echo '</a></h3>' >> "$filename" - if [[ "$timestamp" == "" ]]; then - echo '<div class="subtitle">'$(LC_ALL=$date_locale date +"$date_format")' — ' >> "$filename" - else - echo '<div class="subtitle">'$(LC_ALL=$date_locale date +"$date_format" --date="$timestamp") ' — ' >> "$filename" + { + cat ".header.html" + echo "<title>$title</title>" + google_analytics + twitter_card "$content" "$title" + echo "</head><body>" + # stuff to add before the actual body content + [[ -n $body_begin_file ]] && cat "$body_begin_file" + # body divs + echo '<div id="divbodyholder">' + echo '<div class="headerholder"><div class="header">' + # blog title + echo '<div id="title">' + cat .title.html + echo '</div></div></div>' # title, header, headerholder + echo '<div id="divbody"><div class="content">' + + 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 '<!-- entry begin -->' # marks the beginning of the whole post + echo "<h3><a class=\"ablack\" href=\"$file_url\">" + # remove possible <p>'s on the title because of markdown conversion + echo "$title" | sed 's/<\/*p>//g' + echo '</a></h3>' + if [[ -z $timestamp ]]; then + echo "<div class=\"subtitle\">$(LC_ALL=$date_locale date +"$date_format") — " + else + echo "<div class=\"subtitle\">$(LC_ALL=$date_locale date +"$date_format" --date="$timestamp") — " + fi + echo "$global_author</div>" + echo '<!-- text begin -->' # This marks the text body, after the title, date... fi - echo "$global_author</div>" >> "$filename" - echo '<!-- text begin -->' >> "$filename" # This marks the text body, after the title, date... - fi - cat "$content" >> "$filename" # Actual content - if [[ "$index" == "no" ]]; then - echo -e '\n<!-- text end -->' >> "$filename" + cat "$content" # Actual content + if [[ $index == no ]]; then + echo -e '\n<!-- text end -->' - twitter "$global_url/$file_url" >> "$filename" + twitter "$global_url/$file_url" - echo '<!-- entry end -->' >> "$filename" # absolute end of the post - fi + echo '<!-- entry end -->' # absolute end of the post + fi - echo '</div>' >> "$filename" # content + echo '</div>' # 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 '</div></div>' >> "$filename" # divbody and divbodyholder - disqus_footer >> "$filename" - echo '</body></html>' >> "$filename" + # page footer + cat .footer.html + # close divs + echo '</div></div>' # divbody and divbodyholder + disqus_footer + echo '</body></html>' + } > "$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 <p> and </p> 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" = "<p>$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 == "<p>$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 "<p>$template_tags_line_header " >> "$content" - (for item in "${array[@]}"; do + for item in "${array[@]}"; do echo -n "<a href='$prefix_tags$item.html'>$item</a>, " - 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" <p>The rest of the text file is an <b>html</b> blog post. The process will continue as soon as you exit your editor.</p> <p>$template_tags_line_header keep-this-tag-format, tags-are-optional, example</p> 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 "<h3>$template_archive_title</h3>" >> "$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 "</ul>" >> "$contentfile" # Don't close ul before first header - echo "<h4 class='allposts_header'>$month</h4>" >> "$contentfile" - echo "<ul>" >> "$contentfile" - prev_month="$month" - fi - # Title - title="$(get_post_title "$i")" - echo -n '<li><a href="'$i'">'$title'</a> —' >> "$contentfile" - # Date - date="$(LC_ALL=$date_locale date -r "$i" +"$date_format")" - echo " $date</li>" >> "$contentfile" - done - echo "" - echo "</ul>" >> "$contentfile" - echo '<div id="all_posts"><a href="'./'">'$template_archive_index_page'</a></div>' >> "$contentfile" + { + echo "<h3>$template_archive_title</h3>" + 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 "</ul>" # Don't close ul before first header + echo "<h4 class='allposts_header'>$month</h4>" + echo "<ul>" + prev_month=$month + fi + # Title + title=$(get_post_title "$i") + echo -n "<li><a href=\"$i\">$title</a> —" + # Date + date=$(LC_ALL=$date_locale date -r "$i" +"$date_format") + echo " $date</li>" + done < <(ls -t ./*.html) + echo "" 1>&3 + echo "</ul>" + echo "<div id=\"all_posts\"><a href=\"./\">$template_archive_index_page</a></div>" + } 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 "<h3>$template_tags_title</h3>" >> "$contentfile" - echo "<ul>" >> "$contentfile" - for i in $(ls ./$prefix_tags*.html 2>/dev/null || echo ''); do - echo -n "." - nposts="$(grep -c "<\!-- text begin -->" $i)" - tagname="$(echo $i | cut -c $((${#prefix_tags}+3))- | sed 's/\.html//g')" - i="`clean_filename $i`" - echo "<li><a href=\"$i\">$tagname</a> — $nposts $template_tags_posts</li>" >> "$contentfile" - done - echo "" - echo "</ul>" >> "$contentfile" - echo '<div id="all_posts"><a href="'./'">'$template_archive_index_page'</a></div>' >> "$contentfile" + { + echo "<h3>$template_tags_title</h3>" + echo "<ul>" + 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") + echo "<li><a href=\"$i\">$tagname</a> — $nposts $template_tags_posts</li>" + done + echo "" 1>&3 + echo "</ul>" + echo "<div id=\"all_posts\"><a href=\"./\">$template_archive_index_page</a></div>" + } 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 "<p class=\"readmore\"><a href=\"'$i'\">'"$template_read_more"'</a></p>" ; 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 \"<p class=\\\"readmore\\\"><a href=\\\"$i\\\">$template_read_more</a></p>\" ; 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 '<div id="all_posts"><a href="'$archive_index'">'$template_archive'</a> — <a href="'$tags_index'">'$template_tags_title'</a> — <a href="'$feed'">'$template_subscribe'</a></div>' >> "$contentfile" + feed=$blog_feed + if [[ -n $global_feedburner ]]; then feed=$global_feedburner; fi + echo "<div id=\"all_posts\"><a href=\"$archive_index\">$template_archive</a> — <a href=\"$tags_index\">$template_tags_title</a> — <a href=\"$feed\">$template_subscribe</a></div>" + } 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 "/^<p>$template_tags_line_header/{s/^<p>$template_tags_line_header//;s/<[^>]*>//g;s/[ ,]\+/ /g;p;}" $1 | tr ', ' ' ' + sed -n "/^<p>$template_tags_line_header/{s/^<p>$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 '/^<h3><a class="ablack" href="[^"]*">/{s/.*href="\([^"]*\)">.*/\1/;p;}' $tag_files 2> /dev/null + (($# < 1)) && return + set -- "${@/#/$prefix_tags}" + set -- "${@/%/.html}" + sed -n '/^<h3><a class="ablack" href="[^"]*">/{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 "<p class=\"readmore\"><a href=\"'$i'\">'"$template_read_more"'</a></p>" ; next } 1' >> "$tmpfile" + if [[ -n $cut_do ]]; then + get_html_file_content 'entry' 'entry' 'cut' <"$i" | awk "/$cut_line/ { print \"<p class=\\\"readmore\\\"><a href=\\\"$i\\\">$template_read_more</a></p>\" ; 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 - - echo '<?xml version="1.0" encoding="UTF-8" ?>' >> "$rssfile" - echo '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">' >> "$rssfile" - echo '<channel><title>'$global_title'</title><link>'$global_url'</link>' >> "$rssfile" - echo '<description>'$global_description'</description><language>en</language>' >> "$rssfile" - echo '<lastBuildDate>'$(LC_ALL=C date +"%a, %d %b %Y %H:%M:%S %z")'</lastBuildDate>' >> "$rssfile" - echo '<pubDate>'$(LC_ALL=C date +"%a, %d %b %Y %H:%M:%S %z")'</pubDate>' >> "$rssfile" - echo '<atom:link href="'$global_url/$blog_feed'" rel="self" type="application/rss+xml" />' >> "$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 '<item><title>' >> "$rssfile" - echo "$(get_post_title "$i")" >> "$rssfile" - echo '</title><description><![CDATA[' >> "$rssfile" - echo "$(get_html_file_content 'text' 'entry' $cut_do <$i)" >> "$rssfile" - echo "]]></description><link>$global_url/$(clean_filename $i)</link>" >> "$rssfile" - echo "<guid>$global_url/$i</guid>" >> "$rssfile" - echo "<dc:creator>$global_author</dc:creator>" >> "$rssfile" - echo '<pubDate>'$(LC_ALL=C date -r "$i" +"%a, %d %b %Y %H:%M:%S %z")'</pubDate></item>' >> "$rssfile" - - n=$(( n + 1 )) - done - - echo '</channel></rss>' >> "$rssfile" + rssfile=$blog_feed.$RANDOM + while [[ -f $rssfile ]]; do rssfile=$blog_feed.$RANDOM; done + + { + pubdate=$(LC_ALL=C date +"%a, %d %b %Y %H:%M:%S %z") + echo '<?xml version="1.0" encoding="UTF-8" ?>' + echo '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">' + echo "<channel><title>$global_title</title><link>$global_url</link>" + echo "<description>$global_description</description><language>en</language>" + echo "<lastBuildDate>$pubdate</lastBuildDate>" + echo "<pubDate>$pubdate</pubDate>" + echo "<atom:link href=\"$global_url/$blog_feed\" rel=\"self\" type=\"application/rss+xml\" />" + + 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 '<item><title>' + get_post_title "$i" + echo '</title><description><![CDATA[' + get_html_file_content 'text' 'entry' $cut_do <"$i" + echo "]]></description><link>$global_url/$(clean_filename "$i")</link>" + echo "<guid>$global_url/$i</guid>" + echo "<dc:creator>$global_author</dc:creator>" + echo "<pubDate>$(LC_ALL=C date -r "$i" +"%a, %d %b %Y %H:%M:%S %z")</pubDate></item>" + + n=$(( n + 1 )) + done < <(ls -t ./*.html) + + echo '</channel></rss>' + } 3>&1 >"$rssfile" echo "" mv "$rssfile" "$blog_feed" @@ -869,30 +882,33 @@ make_rss() { # generate headers, footers, etc create_includes() { - echo '<h1 class="nomargin"><a class="ablack" href="'$global_url'">'$global_title'</a></h1>' > ".title.html" - echo '<div id="description">'$global_description'</div>' >> ".title.html" - - if [[ -f "$header_file" ]]; then cp "$header_file" .header.html - else - echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' > ".header.html" - echo '<html xmlns="http://www.w3.org/1999/xhtml"><head>' >> ".header.html" - echo '<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />' >> ".header.html" - echo '<meta name="viewport" content="width=device-width, initial-scale=1.0">' >> ".header.html" - for css_file in ${css_include[*]}; do - echo '<link rel="stylesheet" href="'$css_file'" type="text/css" />' >> ".header.html" - done - if [[ "$global_feedburner" == "" ]]; then - echo '<link rel="alternate" type="application/rss+xml" title="'$template_subscribe_browser_button'" href="'$blog_feed'" />' >> ".header.html" + { + echo "<h1 class=\"nomargin\"><a class=\"ablack\" href=\"$global_url\">$global_title</a></h1>" + echo "<div id=\"description\">$global_description</div>" + } > ".title.html" + + if [[ -f $header_file ]]; then cp "$header_file" .header.html + else { + echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' + echo '<html xmlns="http://www.w3.org/1999/xhtml"><head>' + echo '<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />' + echo '<meta name="viewport" content="width=device-width, initial-scale=1.0">' + printf '<link rel="stylesheet" href="%s" type="text/css" />\n' "${css_include[@]}" + if [[ -z $global_feedburner ]]; then + echo "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"$template_subscribe_browser_button\" href=\"$blog_feed\" />" else - echo '<link rel="alternate" type="application/rss+xml" title="'$template_subscribe_browser_button'" href="'$global_feedburner'" />' >> ".header.html" + echo "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"$template_subscribe_browser_button\" href=\"$global_feedburner\" />" 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 '<div id="footer">'$global_license '<a href="'$global_author_url'">'$global_author'</a> — <a href="mailto:'$protected_mail'">'$protected_mail'</a><br/>' >> ".footer.html" - echo 'Generated with <a href="https://github.com/cfenollosa/bashblog">bashblog</a>, a single bash script to easily create blogs like this one</div>' >> ".footer.html" + if [[ -f $footer_file ]]; then cp "$footer_file" .footer.html + else { + protected_mail=${global_email//@/@} + protected_mail=${protected_mail//./.} + echo "<div id=\"footer\">$global_license <a href=\"$global_author_url\">$global_author</a> — <a href=\"mailto:$protected_mail\">$protected_mail</a><br/>" + echo 'Generated with <a href="https://github.com/cfenollosa/bashblog">bashblog</a>, a single bash script to easily create blogs like this one</div>' + } >> ".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 |