From: Christian Heller <c.heller@plomlompom.de>
Date: Sun, 11 Dec 2016 23:06:49 +0000 (+0100)
Subject: Move snippet generation for index/feed into separate .do files.
X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/static/%7B%7Bdb.prefix%7D%7D/task?a=commitdiff_plain;h=4373137de8ac6ad8731e6828a9876de9efe0a82e;p=redo-blog

Move snippet generation for index/feed into separate .do files.
---

diff --git a/processor/default.feed_snippet.do b/processor/default.feed_snippet.do
new file mode 100644
index 0000000..9d33ad9
--- /dev/null
+++ b/processor/default.feed_snippet.do
@@ -0,0 +1,27 @@
+#!/bin/sh
+
+# Pull in dependencies. 
+. ./helpers.sh
+src_file=$(get_source_file "$1")
+uuid_file="${1%.feed_snippet}.uuid"
+redo-ifchange "$uuid_file"
+intermediate_file="${src_file%.*}.intermediate"
+redo-ifchange "$intermediate_file"
+
+# Get variables, write entry.
+html_file=`escape_url "${src_file%.*}.html"`
+lastmod=`stat -c%y "$src_file"`
+lastmod_rfc3339=`date -u "+%Y-%m-%dT%TZ" -d "$lastmod"`
+published=`stat -c%y "$uuid_file"`
+published_rfc3339=`date -u "+%Y-%m-%dT%TZ" -d "${published}"`
+title=`read_and_escape_file "$intermediate_file" | head -1`
+uuid=`read_and_escape_file "$uuid_file" | head -1`
+body=`read_and_escape_file "$intermediate_file" | sed 1d`
+printf "<entry>\n"
+printf "<title type=\"html\">%s</title>\n" "$title"
+printf "<id>urn:uuid:%s</id>\n" "$uuid"
+printf "<updated>%s</updated>\n" "$lastmod_rfc3339"
+printf "<published>%s</published>\n" "$published_rfc3339"
+printf "<link href=\"%s%s\" />\n" "$(get_basepath)" "${html_file#\./}"
+printf "<content type=\"html\">\n%s\n</content>\n" "$body"
+printf "</entry>\n"
diff --git a/processor/default.index_snippet.do b/processor/default.index_snippet.do
new file mode 100644
index 0000000..759a510
--- /dev/null
+++ b/processor/default.index_snippet.do
@@ -0,0 +1,14 @@
+#!/bin/sh
+
+# Pull in dependencies.
+. ./helpers.sh
+src_file=$(get_source_file "$1")
+intermediate_file="${src_file%.*}.intermediate"
+redo-ifchange "$intermediate_file"
+html_file="${src_file%.*}.html"
+redo-ifchange "$html_file"
+
+# Get variables, write entry.
+title_html=`cat "$intermediate_file" | head -1`
+html_file_escaped=`escape_url "${html_file#\./}"`
+printf "<li><a href=\"%s\" />%s</a></li>\n" "$html_file_escaped" "$title_html"
diff --git a/processor/feed.xml.do b/processor/feed.xml.do
index a034911..16011f3 100644
--- a/processor/feed.xml.do
+++ b/processor/feed.xml.do
@@ -1,32 +1,7 @@
 #!/bin/sh
 
-build_entry () {
-  file="${1}"
-  uuidfile="${2}"
-  published="${3}"
-  intermediate_file="${file%.*}.intermediate"
-  htmlfile=`escape_url "${file%.*}.html"`
-  redo-ifchange "$uuid_file"
-  redo-ifchange "$intermediate_file"
-  lastmod=`stat -c%y "$file"`
-  lastmod_rfc3339=`date -u "+%Y-%m-%dT%TZ" -d "$lastmod"`
-  title=`read_and_escape_file "$intermediate_file" | head -1`
-  uuid=`read_and_escape_file "$uuid_file" | head -1`
-  body=`read_and_escape_file "$intermediate_file" | sed 1d`
-  published_rfc3339=`date -u "+%Y-%m-%dT%TZ" -d "${published}"`
-  printf "<entry>\n"
-  printf "<title type=\"html\">%s</title>\n" "$title"
-  printf "<id>urn:uuid:%s</id>\n" "$uuid"
-  printf "<updated>%s</updated>\n" "$lastmod_rfc3339"
-  printf "<published>%s</published>\n" "$published_rfc3339"
-  printf "<link href=\"%s%s\" />\n" "$basepath" "${htmlfile#\./}"
-  printf "<content type=\"html\">\n%s\n</content>\n" "$body"
-  printf "</entry>"
-}
-
 # Pull in global dependencies.
 . ./helpers.sh
-url_file=url.meta
 author_file=author.meta
 uuid_file=uuid.meta
 title_file=title.meta
@@ -37,11 +12,8 @@ redo-ifchange "$title_file"
 
 # Build some variables. XML-escape even file contents that should not contain
 # dangerous characters, just to avoid any XML trouble.
-base_url=`cat "$url_file" | head -1`
-url_protocol=`echo $base_url | cut -d ':' -f 1`
-url_basepath=`echo $base_url | cut -d '/' -f 3-`
-url_basepath_escaped=`escape_url "$url_basepath"`
-basepath="$url_protocol""://""$url_basepath_escaped"
+srcdir=`pwd`
+basepath=$(get_basepath)
 title=`read_and_escape_file "$title_file" | head -1`
 author=`read_and_escape_file "$author_file" | head -1`
 uuid=`read_and_escape_file "$uuid_file" | head -1`
@@ -65,8 +37,9 @@ for file in ./*.rst ./*.md; do
     redo-ifchange "$uuid_file"
     published=`stat -c%y "${uuid_file}"`
     published_unix=$(date -u "+%s%N" -d "${published}")
-    entry=$(build_entry "${file}" "${uuid_file}" "${published}")
-    echo "${entry}" > ./feed_snippets/${published_unix}
+    snippet_file="${file%.*}.feed_snippet"
+    redo-ifchange "$snippet_file"
+    ln -s "$srcdir/$snippet_file" "./feed_snippets/${published_unix}"
   fi
 done
 
diff --git a/processor/helpers.sh b/processor/helpers.sh
index c90e077..cc12358 100644
--- a/processor/helpers.sh
+++ b/processor/helpers.sh
@@ -1,19 +1,41 @@
 #!/bin/sh
 
-escape_html()
-{
-out=`python3 -c 'import sys, html; print(html.escape(sys.argv[1]))' "$1"`
-printf "%s" "$out"
+escape_html() {
+  out=`python3 -c 'import sys, html; print(html.escape(sys.argv[1]))' "$1"`
+  printf "%s" "$out"
 }
 
-read_and_escape_file()
-{
-in=`cat "$1"`
-escape_html "$in"
+read_and_escape_file() {
+  in=`cat "$1"`
+  escape_html "$in"
 }
 
-escape_url()
-{
-out=`python3 -c 'import sys, urllib.parse; print(urllib.parse.quote(sys.argv[1]))' "$1"`
-printf "%s" "$out"
+escape_url() {
+  out=`python3 -c 'import sys, urllib.parse; print(urllib.parse.quote(sys.argv[1]))' "$1"`
+  printf "%s" "$out"
+}
+
+get_basepath() {
+  url_file=url.meta
+  redo-ifchange "$url_file"
+  base_url=`cat "$url_file" | head -1`
+  url_protocol=`echo $base_url | cut -d ':' -f 1`
+  url_basepath=`echo $base_url | cut -d '/' -f 3-`
+  url_basepath_escaped=`escape_url "$url_basepath"`
+  basepath="$url_protocol""://""$url_basepath_escaped"
+  echo "$basepath"
+}
+
+get_source_file() {
+  md_file="${1%.*}.md"
+  rst_file="${1%.*}.rst"
+  if [ -f "$rst_file" ]; then
+    src_file="$rst_file"
+  elif [ -f "$md_file" ]; then
+    src_file="$md_file"
+  else
+    exit 1
+  fi
+  redo-ifchange "$src_file"
+  printf "$src_file"
 }
diff --git a/processor/index.html.do b/processor/index.html.do
index dca7c33..af8ba85 100644
--- a/processor/index.html.do
+++ b/processor/index.html.do
@@ -2,6 +2,7 @@
 
 # Pull in global dependencies.
 . ./helpers.sh
+srcdir=`pwd`
 title_file=title.meta
 redo-ifchange "$title_file"
 
@@ -23,13 +24,9 @@ for file in ./*.rst ./*.md; do
     redo-ifchange "$uuid_file"
     published=`stat -c%y "${uuid_file}"`
     published_unix=$(date -u "+%s%N" -d "${published}")
-    intermediate_file="${file%.*}.intermediate"
-    html_file="${file%.*}.html"
-    redo-ifchange "$intermediate_file"
-    redo-ifchange "$html_file"
-    title_html=`cat "$intermediate_file" | head -1`
-    html_file_escaped=`escape_url "${html_file#\./}"`
-    printf "<li><a href=\"%s\" />%s</a></li>\n" "$html_file_escaped" "$title_html" > ./index_snippets/${published_unix}
+    snippet_file="${file%.*}.index_snippet"
+    redo-ifchange "$snippet_file"
+    ln -s "$srcdir/$snippet_file" "./index_snippets/${published_unix}"
   fi
 done