Browse Source

Merge pull request #37 from Lex-2008/small-fixes/2

Small fixes/2
Carlos Fenollosa 11 years ago
parent
commit
44eea21cf4
1 changed files with 33 additions and 26 deletions
  1. 33 26
      bb.sh

+ 33 - 26
bb.sh

@@ -362,15 +362,15 @@ edit() {
         # Title
         # Title
         echo "$(get_post_title $1)" > "$TMPFILE"
         echo "$(get_post_title $1)" > "$TMPFILE"
         # Post text with plaintext tags
         # Post text with plaintext tags
-        get_html_file_content 'text' 'text' <$1 | sed "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"
         rm $1
         rm $1
         $EDITOR "$TMPFILE"
         $EDITOR "$TMPFILE"
-        parse_file "$TMPFILE" "$edit_timestamp" # this command sets $filename as the html processed file
-        rm "$TMPFILE"
         if [ "$2" = "keep" ]; then
         if [ "$2" = "keep" ]; then
-            mv $filename $1
-            filename="$1"
+            parse_file "$TMPFILE" "$edit_timestamp" "$1"
+        else
+            parse_file "$TMPFILE" "$edit_timestamp" # this command sets $filename as the html processed file
         fi
         fi
+        rm "$TMPFILE"
     fi
     fi
     touch -t "$touch_timestamp" "$filename"
     touch -t "$touch_timestamp" "$filename"
     chmod 644 "$filename"
     chmod 644 "$filename"
@@ -487,31 +487,39 @@ create_html_page() {
 
 
 # Parse the plain text file into an html file
 # Parse the plain text file into an html file
 #
 #
-# $1    file name
+# $1    source file name
 # $2    (optional) timestamp for the file
 # $2    (optional) timestamp for the file
+# $3    (optional) destination file name
+# note that although timestamp is optional, something must be provided at its
+# place if destination file name is provided, i.e:
+# parse_file source.txt "" destination.html
 parse_file() {
 parse_file() {
     # Read for the title and check that the filename is ok
     # Read for the title and check that the filename is ok
     title=""
     title=""
-    while IFS='' read line; do
+    while IFS='' read -r line; do
         if [[ "$title" == "" ]]; then
         if [[ "$title" == "" ]]; then
             # set title and
             # set title and
             # remove extra <p> and </p> added by markdown
             # remove extra <p> and </p> added by markdown
             title=$(echo "$line" | sed 's/<\/*p>//g')
             title=$(echo "$line" | sed 's/<\/*p>//g')
-            filename="$(echo $title | tr [:upper:] [:lower:])"
-            filename="$(echo $filename | sed 's/\ /-/g')"
-            filename="$(echo $filename | sed 'y/йцукенгшщзхъфывапролджэячсмитьбю/jcukengsszh-fyvaproldzeahsmit-by/')"
-            filename="$(echo $filename | sed 'y/ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ/jcukengsszh-fyvaproldzeahsmit-by/')"
-            filename="$(echo $filename | tr -dc '[:alnum:]-')" # html likes alphanumeric
-            filename="$(echo $filename | sed 's/^-*//')" # unix utilities are unhappy if filename starts with -
-            [ "$filename" ] || filename=$RANDOM # if filename gets empty, put something in it
-            filename="$filename.html"
+            if [ "$3" ]; then
+                filename=$3
+            else
+                filename="$(echo $title | tr [:upper:] [:lower:])"
+                filename="$(echo $filename | sed 's/\ /-/g')"
+                filename="$(echo $filename | sed 'y/йцукенгшщзхъфывапролджэячсмитьбю/jcukengsszh-fyvaproldzeahsmit-by/')"
+                filename="$(echo $filename | sed 'y/ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ/jcukengsszh-fyvaproldzeahsmit-by/')"
+                filename="$(echo $filename | tr -dc '[:alnum:]-')" # html likes alphanumeric
+                filename="$(echo $filename | sed 's/^-*//')" # unix utilities are unhappy if filename starts with -
+                [ "$filename" ] || filename=$RANDOM # if filename gets empty, put something in it
+                filename="$filename.html"
+
+                # Check for duplicate file names
+                while [ -f "$filename" ]; do
+                    suffix="$RANDOM"
+                    filename="$(echo $filename | sed 's/\.html/'$suffix'\.html/g')"
+                done
+            fi
             content="$filename.tmp"
             content="$filename.tmp"
-
-            # Check for duplicate file names
-            while [ -f "$filename" ]; do
-                suffix="$RANDOM"
-                filename="$(echo $filename | sed 's/\.html/'$suffix'\.html/g')"
-            done
         # Parse possible tags
         # Parse possible tags
         elif [[ "$line" = "<p>$template_tags_line_header"* ]]; then
         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')"
             tags="$(echo "$line" | cut -d ":" -f 2- | sed -e 's/<\/p>//g' -e 's/^ *//' -e 's/ *$//' -e 's/, /,/g')"
@@ -576,7 +584,9 @@ EOF
     chmod 600 "$TMPFILE"
     chmod 600 "$TMPFILE"
 
 
     post_status="E"
     post_status="E"
+    filename=""
     while [ "$post_status" != "p" ] && [ "$post_status" != "P" ]; do
     while [ "$post_status" != "p" ] && [ "$post_status" != "P" ]; do
+        [ "$filename" ] && rm "$filename" # Delete the generated html file, if any
         $EDITOR "$TMPFILE"
         $EDITOR "$TMPFILE"
         if [[ "$fmt" == "md" ]]; then
         if [[ "$fmt" == "md" ]]; then
             html_from_md="$(markdown "$TMPFILE")"
             html_from_md="$(markdown "$TMPFILE")"
@@ -614,9 +624,6 @@ EOF
             echo "Saved your draft as '$draft'"
             echo "Saved your draft as '$draft'"
             exit
             exit
         fi
         fi
-        if [[ "$post_status" == "e" ]] || [[ "$post_status" == "E" ]]; then
-            rm "$filename" # Delete the html file as it will be generated again
-        fi
     done
     done
 
 
     rm "$TMPFILE"
     rm "$TMPFILE"
@@ -737,7 +744,7 @@ rebuild_tags() {
                 # 'split' tags by commas
                 # 'split' tags by commas
                 echo "$line" | cut -c 10- | while IFS="," read -a tags; do
                 echo "$line" | cut -c 10- | while IFS="," read -a tags; do
                     for dirty_tag in "${tags[@]}"; do # extract html around it
                     for dirty_tag in "${tags[@]}"; do # extract html around it
-                        tag="$(echo $dirty_tag | grep -o ">.*</a>" | awk '{print substr($0, 2, length($0)-5)}' | tr " " "_")"
+                        tag="$(expr "$dirty_tag" : ".*>\(.*\)</a" | tr " " "_")"
                         # Add the content of this post to the tag file
                         # Add the content of this post to the tag file
                         cat "$tmpfile" >> "$prefix_tags$tag".tmp.html
                         cat "$tmpfile" >> "$prefix_tags$tag".tmp.html
                     done
                     done
@@ -1066,4 +1073,4 @@ do_main() {
 #
 #
 do_main $*
 do_main $*
 
 
-# vim: set shiftwidth=4 expandtab:
+# vim: set shiftwidth=4 tabstop=4 expandtab: