From 699ecc60058106751d579c76156a7a08d7e5a85b Mon Sep 17 00:00:00 2001 From: Alexey Shpakovsky Date: Sat, 1 Mar 2014 12:30:08 +0100 Subject: [PATCH 1/6] move tag parsing to parse_file --- bb.sh | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/bb.sh b/bb.sh index 96d410a..b973a7e 100755 --- a/bb.sh +++ b/bb.sh @@ -476,6 +476,16 @@ parse_file() { suffix="$RANDOM" filename="$(echo $filename | sed 's/\.html/'$suffix'\.html/g')" done + # 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')" + IFS=, read -r -a array <<< "$tags" + + echo -n "

$template_tags_line_header " >> "$content" + (for item in "${array[@]}"; do + echo -n "$item, " + done ) | sed 's/, $//g' >> "$content" + echo -e "

" >> "$content" else echo "$line" >> "$content" fi @@ -574,23 +584,6 @@ EOF done rm "$TMPFILE" - # Parse possible tags - cp "$filename" "$filename.bak" - while read line; do - if [[ "$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 " - (for item in "${array[@]}"; do - echo -n "$item, " - done ) | sed 's/, $//g' - echo -e "

" - else echo "$line" - fi - done < "$filename.bak" > "$filename" - rm "$filename.bak" - chmod 644 "$filename" echo "Posted $filename" } From b365aec7978f663f21c3560e82ada7f0b49cdc1d Mon Sep 17 00:00:00 2001 From: Alexey Shpakovsky Date: Sat, 1 Mar 2014 13:55:55 +0100 Subject: [PATCH 2/6] show only text content in the editor --- bb.sh | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/bb.sh b/bb.sh index b973a7e..c2a0f77 100755 --- a/bb.sh +++ b/bb.sh @@ -338,14 +338,25 @@ get_html_file_content() { # Please note that this function does not automatically republish anything, as # it is usually called from 'main'. # -# 'edit' is kind of an advanced feature, as it leaves to the user the responsibility -# of editing an html file +# Note that it edits HTML file, even if you wrote the post as markdown originally +# Note that if you edit title then filename might also change # # $1 the file to edit edit() { - timestamp="$(date -r $1 +'%Y%m%d%H%M')" - $EDITOR "$1" - touch -t $timestamp "$1" + edit_timestamp="$(date -r $1 +'%Y%m%d%H%M')" + # Create the content file + TMPFILE="$1.$RANDOM" + # Title + echo "$(get_post_title $1)" > "$TMPFILE" + # Post text with plaintext tags + get_html_file_content 'text' 'text' <$1 | sed "s|\\1|\\1|g" >> "$TMPFILE" + rm $1 + $EDITOR "$TMPFILE" + parse_file "$TMPFILE" # this command sets $filename as the html processed file + touch -t $edit_timestamp "$filename" + rm "$TMPFILE" + chmod 644 "$filename" + echo "Posted $filename" } # Adds the code needed by the twitter button From 773c5fa363c3878c072bd5673821ee20cf2861bd Mon Sep 17 00:00:00 2001 From: Alexey Shpakovsky Date: Sat, 1 Mar 2014 14:34:52 +0100 Subject: [PATCH 3/6] add options to bring back old behavior and keep old filename --- bb.sh | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/bb.sh b/bb.sh index c2a0f77..32856d0 100755 --- a/bb.sh +++ b/bb.sh @@ -342,19 +342,32 @@ get_html_file_content() { # Note that if you edit title then filename might also change # # $1 the file to edit +# $2 (optional) edit mode: +# "keep" to keep old filename +# "full" to edit full HTML, and not only text part (keeps old filename) +# leave empty for default behavior (edit only text part and change name) edit() { edit_timestamp="$(date -r $1 +'%Y%m%d%H%M')" - # Create the content file - TMPFILE="$1.$RANDOM" - # Title - echo "$(get_post_title $1)" > "$TMPFILE" - # Post text with plaintext tags - get_html_file_content 'text' 'text' <$1 | sed "s|\\1|\\1|g" >> "$TMPFILE" - rm $1 - $EDITOR "$TMPFILE" - parse_file "$TMPFILE" # this command sets $filename as the html processed file + if [ "$2" = "full" ]; then + $EDITOR "$1" + filename="$1" + else + # Create the content file + TMPFILE="$1.$RANDOM" + # Title + echo "$(get_post_title $1)" > "$TMPFILE" + # Post text with plaintext tags + get_html_file_content 'text' 'text' <$1 | sed "s|\\1|\\1|g" >> "$TMPFILE" + rm $1 + $EDITOR "$TMPFILE" + parse_file "$TMPFILE" # this command sets $filename as the html processed file + rm "$TMPFILE" + if [ "$2" = "keep" ]; then + mv $filename $1 + filename="$1" + fi + fi touch -t $edit_timestamp "$filename" - rm "$TMPFILE" chmod 644 "$filename" echo "Posted $filename" } From 175810b7a3a882d18b9896716eb018de40d064f7 Mon Sep 17 00:00:00 2001 From: Alexey Shpakovsky Date: Sat, 1 Mar 2014 17:17:52 +0100 Subject: [PATCH 4/6] preserve date when editing post pass it through parse_file to create_html_page --- bb.sh | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/bb.sh b/bb.sh index 32856d0..cc1b688 100755 --- a/bb.sh +++ b/bb.sh @@ -347,7 +347,8 @@ get_html_file_content() { # "full" to edit full HTML, and not only text part (keeps old filename) # leave empty for default behavior (edit only text part and change name) edit() { - edit_timestamp="$(date -r $1 +'%Y%m%d%H%M')" + # Original post timestamp + edit_timestamp="$(LC_ALL=$date_locale date -r $1 +"%a, %d %b %Y %H:%M:%S %z" )" if [ "$2" = "full" ]; then $EDITOR "$1" filename="$1" @@ -360,14 +361,14 @@ edit() { get_html_file_content 'text' 'text' <$1 | sed "s|\\1|\\1|g" >> "$TMPFILE" rm $1 $EDITOR "$TMPFILE" - parse_file "$TMPFILE" # this command sets $filename as the html processed file + parse_file "$TMPFILE" "$edit_timestamp" # this command sets $filename as the html processed file rm "$TMPFILE" if [ "$2" = "keep" ]; then mv $filename $1 filename="$1" fi fi - touch -t $edit_timestamp "$filename" + touch -d "$edit_timestamp" "$filename" chmod 644 "$filename" echo "Posted $filename" } @@ -481,6 +482,9 @@ create_html_page() { } # Parse the plain text file into an html file +# +# $1 file name +# $2 (optional) timestamp for the file parse_file() { # Read for the title and check that the filename is ok title="" @@ -516,7 +520,7 @@ parse_file() { done < "$1" # Create the actual html page - create_html_page "$content" "$filename" no "$title" + create_html_page "$content" "$filename" no "$title" "$2" rm "$content" } From b3ea05ecd09e6839b2b93d009617409ec231efce Mon Sep 17 00:00:00 2001 From: Alexey Shpakovsky Date: Sat, 1 Mar 2014 21:02:13 +0100 Subject: [PATCH 5/6] keep .html extension when editing (to make synthax highlight work) --- bb.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bb.sh b/bb.sh index cc1b688..26829a5 100755 --- a/bb.sh +++ b/bb.sh @@ -354,7 +354,7 @@ edit() { filename="$1" else # Create the content file - TMPFILE="$1.$RANDOM" + TMPFILE="$(basename $1).$RANDOM.html" # Title echo "$(get_post_title $1)" > "$TMPFILE" # Post text with plaintext tags From 74c40baf643762cf188c2a5f9d7e00879aaaab99 Mon Sep 17 00:00:00 2001 From: Alexey Shpakovsky Date: Sun, 9 Mar 2014 11:49:18 +0100 Subject: [PATCH 6/6] add options to edit command --- bb.sh | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/bb.sh b/bb.sh index 26829a5..7794b8c 100755 --- a/bb.sh +++ b/bb.sh @@ -920,8 +920,10 @@ echo "" echo "Commands:" echo " post [-m] [filename] insert a new blog post, or the filename of a draft to continue editing it" echo " use '-m' to edit the post as Markdown text" -echo " edit [filename] edit an already published .html file. **NEVER** edit manually a published .html file," +echo " edit [-n|-f] [filename] edit an already published .html file. **NEVER** edit manually a published .html file," echo " always use this function as it keeps internal data and rebuilds the blog" +echo " use '-n' to give the file a new name, if title was changed" +echo " use '-f' to edit full html file, instead of just text part (also preserves name)" echo " delete [filename] deletes the post and rebuilds the blog" echo " rebuild regenerates all the pages and posts, preserving the content of the entries" echo " reset deletes everything except this script. Use with a lot of caution and back up first!" @@ -997,7 +999,7 @@ do_main() { list_posts && exit if [[ "$1" == "edit" ]]; then - if [[ $# -lt 2 ]] || [[ ! -f "$2" ]]; then + if [[ $# -lt 2 ]] || [[ ! -f "${!#}" ]]; then echo "Please enter a valid html file to edit" exit fi @@ -1021,8 +1023,16 @@ do_main() { create_css [[ "$1" == "post" ]] && write_entry "$@" [[ "$1" == "rebuild" ]] && rebuild_all_entries - [[ "$1" == "edit" ]] && edit "$2" [[ "$1" == "delete" ]] && rm "$2" &> /dev/null + if [[ "$1" == "edit" ]]; then + if [[ "$2" == "-n" ]]; then + edit "$3" + elif [[ "$2" == "-f" ]]; then + edit "$3" full + else + edit "$2" keep + fi + fi rebuild_index all_posts rebuild_tags