From b34c7fbd0fa703feb7b5689795664e944dceb0a3 Mon Sep 17 00:00:00 2001
From: Alexey Shpakovsky <alexey@shpakovsky.ru>
Date: Tue, 24 Jun 2014 06:25:55 +0200
Subject: make rebuild_tags() optionally accept 2 lists (of filenames and tags)

---
 bb.sh | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)

(limited to 'bb.sh')

diff --git a/bb.sh b/bb.sh
index 2317575..664add4 100755
--- a/bb.sh
+++ b/bb.sh
@@ -769,14 +769,37 @@ rebuild_index() {
     chmod 644 "$index_file"
 }
 
-# Rebuilds all tag_*.html files
+# Rebuilds tag_*.html files
+# if no arguments given, rebuilds all of them
+# if arguments given, they should have this format:
+# "FILE1 [FILE2 [...]]" "TAG1 [TAG2 [...]]"
+# where FILEn are files with posts which should be used for rebuilding tags,
+# and TAGn are names of tags which should be rebuilt.
+# example:
+# rebuild_tags "one_post.html another_atricle.html" "example-tag another-tag"
+# mind the tags!
 rebuild_tags() {
+    if [ "$#" -lt 2 ]; then
+        # will process all files and tags
+        files="$(ls -t ./*.html)"
+        all_tags="yes"
+    else
+        # will process only given files and tags
+        files="$(ls -t $1)"
+        tags="$2"
+    fi
     echo -n "Rebuilding tag pages "
     n=0
-    rm ./$prefix_tags*.html &> /dev/null
+    if [ $all_tags ]; then
+        rm ./$prefix_tags*.html &> /dev/null
+    else
+        for i in $tags; do
+            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 $(ls -t ./*.html); do
+    for i in $files; do
         is_boilerplate_file "$i" && continue;
         echo -n "."
         tmpfile="$(mktemp tmp.XXX)"
@@ -792,7 +815,9 @@ rebuild_tags() {
                     for dirty_tag in "${tags[@]}"; do # extract html around it
                         tag="$(expr "$dirty_tag" : ".*>\(.*\)</a" | tr " " "_")"
                         # Add the content of this post to the tag file
-                        cat "$tmpfile" >> "$prefix_tags$tag".tmp.html
+                        if [ "$all_tags" ] || [[ " $tags " == *" $tag "* ]]; then
+                            cat "$tmpfile" >> "$prefix_tags$tag".tmp.html
+                        fi
                     done
                 done
             fi
-- 
cgit v1.2.3


From a52f2e6758ce35ec1a0be5107ccf89d7897acd9c Mon Sep 17 00:00:00 2001
From: Alexey Shpakovsky <alexey@shpakovsky.ru>
Date: Tue, 24 Jun 2014 06:51:27 +0200
Subject: tags_in_post() to list all tags referenced in one post file

---
 bb.sh | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

(limited to 'bb.sh')

diff --git a/bb.sh b/bb.sh
index 664add4..f6ccd83 100755
--- a/bb.sh
+++ b/bb.sh
@@ -769,6 +769,13 @@ rebuild_index() {
     chmod 644 "$index_file"
 }
 
+# Finds all tags referenced in one post.
+# 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
+}
+
 # Rebuilds tag_*.html files
 # if no arguments given, rebuilds all of them
 # if arguments given, they should have this format:
@@ -808,20 +815,11 @@ rebuild_tags() {
         else
             get_html_file_content 'entry' 'entry' <$i >> "$tmpfile"
         fi
-        while IFS='' read line; do
-            if [[ "$line" = "<p>$template_tags_line_header"* ]]; then
-                # 'split' tags by commas
-                echo "$line" | cut -c 10- | while IFS="," read -a tags; do
-                    for dirty_tag in "${tags[@]}"; do # extract html around it
-                        tag="$(expr "$dirty_tag" : ".*>\(.*\)</a" | tr " " "_")"
-                        # Add the content of this post to the tag file
-                        if [ "$all_tags" ] || [[ " $tags " == *" $tag "* ]]; then
-                            cat "$tmpfile" >> "$prefix_tags$tag".tmp.html
-                        fi
-                    done
-                done
+        for tag in $(tags_in_post $i); do
+            if [ "$all_tags" ] || [[ " $tags " == *" $tag "* ]]; then
+                cat "$tmpfile" >> "$prefix_tags$tag".tmp.html
             fi
-        done < "$i"
+        done
         rm "$tmpfile"
     done
     # Now generate the tag files with headers, footers, etc
-- 
cgit v1.2.3


From 4558f43f4a4bc543c4848b3c7c1049d0cbaf9b60 Mon Sep 17 00:00:00 2001
From: Alexey Shpakovsky <alexey@shpakovsky.ru>
Date: Tue, 24 Jun 2014 07:16:57 +0200
Subject: posts_with_tags() to list all posts referenced by several tag files

---
 bb.sh | 8 ++++++++
 1 file changed, 8 insertions(+)

(limited to 'bb.sh')

diff --git a/bb.sh b/bb.sh
index f6ccd83..5a17e4a 100755
--- a/bb.sh
+++ b/bb.sh
@@ -776,6 +776,14 @@ tags_in_post() {
     sed -n "/^<p>$template_tags_line_header/{s/^<p>$template_tags_line_header//;s/<[^>]*>//g;s/[ ,]\+/ /g;p}" $1
 }
 
+# Finds all posts referenced in a number of tags.
+# Arguments are tags
+# Prints one line with space-separated tags to stdout
+posts_with_tags() {
+    tag_files="$(echo "$@" | sed "s/\S\+/tag_&.html/g")"
+    sed -n '/^<h3><a class="ablack" href="[^"]*">/{s/.*href="\([^"]*\)">.*/\1/;p}' $tag_files
+}
+
 # Rebuilds tag_*.html files
 # if no arguments given, rebuilds all of them
 # if arguments given, they should have this format:
-- 
cgit v1.2.3


From 3c31fd3f608e3b800751ca17a011130a8ca4754c Mon Sep 17 00:00:00 2001
From: Alexey Shpakovsky <alexey@shpakovsky.ru>
Date: Tue, 24 Jun 2014 08:01:02 +0200
Subject: use new functions

also bugfix: leave only unique filenames in rebuild_tags()
---
 bb.sh | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

(limited to 'bb.sh')

diff --git a/bb.sh b/bb.sh
index 5a17e4a..9730d32 100755
--- a/bb.sh
+++ b/bb.sh
@@ -369,6 +369,7 @@ 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
         $EDITOR "$1"
         filename="$1"
@@ -405,6 +406,10 @@ edit() {
     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 ' ' '\n' | sort -u | tr '\n' ' ')"
+    relevant_posts="$(posts_with_tags $relevant_tags) $filename"
+    rebuild_tags "$relevant_posts" "$relevant_tags"
 }
 
 # Adds the code needed by the twitter button
@@ -674,6 +679,9 @@ EOF
     fi
     chmod 644 "$filename"
     echo "Posted $filename"
+    relevant_tags="$(tags_in_post $filename)"
+    relevant_posts="$(posts_with_tags $relevant_tags) $filename"
+    rebuild_tags "$relevant_posts" "$relevant_tags"
 }
 
 # Create an index page with all the posts
@@ -800,7 +808,8 @@ rebuild_tags() {
         all_tags="yes"
     else
         # will process only given files and tags
-        files="$(ls -t $1)"
+        files="$(echo "$1" | tr ' ' '\n' | sort -u | tr '\n' ' ')"
+        files="$(ls -t $files)"
         tags="$2"
     fi
     echo -n "Rebuilding tag pages "
@@ -1124,8 +1133,8 @@ do_main() {
     create_css
     create_includes
     [[ "$1" == "post" ]] && write_entry "$@"
-    [[ "$1" == "rebuild" ]] && rebuild_all_entries
-    [[ "$1" == "delete" ]] && rm "$2" &> /dev/null 
+    [[ "$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"
@@ -1137,7 +1146,6 @@ do_main() {
     fi
     rebuild_index
     all_posts
-    rebuild_tags
     all_tags
     make_rss
     delete_includes
-- 
cgit v1.2.3


From e648b82250a95f8393bc2dcdaf202f6aad0cd712 Mon Sep 17 00:00:00 2001
From: Alexey Shpakovsky <alexey@shpakovsky.ru>
Date: Wed, 25 Jun 2014 06:38:17 +0200
Subject: don't rebuild tags if they were not involved at all

(otherwise posts_with_tags would endlessly freeze)
---
 bb.sh | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

(limited to 'bb.sh')

diff --git a/bb.sh b/bb.sh
index 9730d32..d28bf17 100755
--- a/bb.sh
+++ b/bb.sh
@@ -408,8 +408,10 @@ edit() {
     echo "Posted $filename"
     tags_after="$(tags_in_post $filename)"
     relevant_tags="$(echo "$tags_before $tags_after" | tr ' ' '\n' | sort -u | tr '\n' ' ')"
-    relevant_posts="$(posts_with_tags $relevant_tags) $filename"
-    rebuild_tags "$relevant_posts" "$relevant_tags"
+    if [ "$relevant_tags" ]; then
+        relevant_posts="$(posts_with_tags $relevant_tags) $filename"
+        rebuild_tags "$relevant_posts" "$relevant_tags"
+    fi
 }
 
 # Adds the code needed by the twitter button
@@ -680,8 +682,10 @@ EOF
     chmod 644 "$filename"
     echo "Posted $filename"
     relevant_tags="$(tags_in_post $filename)"
-    relevant_posts="$(posts_with_tags $relevant_tags) $filename"
-    rebuild_tags "$relevant_posts" "$relevant_tags"
+    if [ "$relevant_tags" ]; then
+        relevant_posts="$(posts_with_tags $relevant_tags) $filename"
+        rebuild_tags "$relevant_posts" "$relevant_tags"
+    fi
 }
 
 # Create an index page with all the posts
-- 
cgit v1.2.3


From ff978ec1018bf759efb0c7541c61507e937985b1 Mon Sep 17 00:00:00 2001
From: Alexey Shpakovsky <alexey@shpakovsky.ru>
Date: Thu, 26 Jun 2014 07:11:02 +0200
Subject: proper check for empty variable

---
 bb.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

(limited to 'bb.sh')

diff --git a/bb.sh b/bb.sh
index d28bf17..01c1c6d 100755
--- a/bb.sh
+++ b/bb.sh
@@ -408,7 +408,7 @@ edit() {
     echo "Posted $filename"
     tags_after="$(tags_in_post $filename)"
     relevant_tags="$(echo "$tags_before $tags_after" | tr ' ' '\n' | sort -u | tr '\n' ' ')"
-    if [ "$relevant_tags" ]; then
+    if [ ! -z "$relevant_tags" ]; then
         relevant_posts="$(posts_with_tags $relevant_tags) $filename"
         rebuild_tags "$relevant_posts" "$relevant_tags"
     fi
@@ -682,7 +682,7 @@ EOF
     chmod 644 "$filename"
     echo "Posted $filename"
     relevant_tags="$(tags_in_post $filename)"
-    if [ "$relevant_tags" ]; then
+    if [ ! -z "$relevant_tags" ]; then
         relevant_posts="$(posts_with_tags $relevant_tags) $filename"
         rebuild_tags "$relevant_posts" "$relevant_tags"
     fi
-- 
cgit v1.2.3


From 0b75439f416122ac21ee6d4f8b50805938512bf5 Mon Sep 17 00:00:00 2001
From: Alexey Shpakovsky <alexey@shpakovsky.ru>
Date: Thu, 26 Jun 2014 04:25:48 +0200
Subject: fix typos in comments

---
 bb.sh | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

(limited to 'bb.sh')

diff --git a/bb.sh b/bb.sh
index 01c1c6d..e77baf4 100755
--- a/bb.sh
+++ b/bb.sh
@@ -110,7 +110,7 @@
 # with merges in VCS
 global_config=".config"
 
-# This function will load all the variables defined here. They might be overriden
+# This function will load all the variables defined here. They might be overridden
 # by the 'global_config' file contents
 global_variables() {
     global_software_name="BashBlog"
@@ -803,8 +803,8 @@ posts_with_tags() {
 # where FILEn are files with posts which should be used for rebuilding tags,
 # and TAGn are names of tags which should be rebuilt.
 # example:
-# rebuild_tags "one_post.html another_atricle.html" "example-tag another-tag"
-# mind the tags!
+# rebuild_tags "one_post.html another_article.html" "example-tag another-tag"
+# mind the quotes!
 rebuild_tags() {
     if [ "$#" -lt 2 ]; then
         # will process all files and tags
-- 
cgit v1.2.3