From f58e6d66bc1228640227e9a95a305f8213a7103a Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Mon, 2 Jan 2017 18:58:03 +0100
Subject: [PATCH] Display creation dates in index.html and article html files.

---
 README.md                                     |  2 +-
 processor/default.html.do                     |  7 ++++
 processor/metadata/article.tmpl.do            |  3 ++
 processor/metadata/default.index_snippet.do   |  5 +++
 processor/metadata/index_snippet.tmpl.do      |  2 +-
 test.sh                                       | 37 ++++++++++++-------
 .../{bar baz.html => bar baz.html.ignoring}   |  3 ++
 .../{foo.html => foo.html.ignoring}           |  3 ++
 .../{index.html => index.html.ignoring}       |  6 +--
 .../{test.html => test.html.ignoring}         |  3 ++
 10 files changed, 52 insertions(+), 19 deletions(-)
 rename test/test_files/{bar baz.html => bar baz.html.ignoring} (76%)
 rename test/test_files/{foo.html => foo.html.ignoring} (89%)
 rename test/test_files/{index.html => index.html.ignoring} (68%)
 rename test/test_files/{test.html => test.html.ignoring} (78%)

diff --git a/README.md b/README.md
index c247230..bfb1174 100644
--- a/README.md
+++ b/README.md
@@ -84,7 +84,7 @@ bugs and peculiarities
 Don't create a index.rst or index.md in the redo-managed directory, that will
 break things.
 
-The article title is derived in .md files from a first line prefixed with `% `,
+The article title is derived in .md files from a first line prefixed with `%`,
 while all other headings are treated as sub-headings. In .rst files, the title
 is derived from a heading that must be at the top of the document, and be of an
 adornment style (such as underlining with `=`) that must be used only once in
diff --git a/processor/default.html.do b/processor/default.html.do
index 99d784d..c9bfbdb 100644
--- a/processor/default.html.do
+++ b/processor/default.html.do
@@ -3,6 +3,8 @@
 # Pull in global dependencies.
 . ./helpers.sh
 metadata_dir=metadata
+uuid_file="${metadata_dir}/${1%.html}.uuid"
+redo-ifchange "$uuid_file"
 intermediate_file="${metadata_dir}/${1%.html}.intermediate"
 redo-ifchange "$intermediate_file"
 title_file="${metadata_dir}"/title
@@ -17,6 +19,9 @@ title_plaintext=`echo "$title_html" | html2text`
 title_html=$(printf "%s" "$title_html" | prep_sed)
 title_plaintext=$(escape_html "$title_plaintext" | prep_sed)
 body=$(cat "$intermediate_file" | sed 1d | prep_sed)
+datetime_created_unix=$(stat -c%y "${uuid_file}")
+datetime_created_rfc3339=$(date -u "+%Y-%m-%dT%TZ" -d "${datetime_created_unix}")
+datetime_created_friendly=$(date -u "+%Y-%m-%d %T (UTC)" -d "${datetime_created_unix}")
 
 # Put data into template.
 template=$(cat "$template_file")
@@ -24,5 +29,7 @@ printf "%s" "$template" | \
 sed 's/%BLOG_TITLE%/'"$blog_title"'/g' | \
 sed 's/%ARTICLE_TITLE_ESCAPED%/'"$title_plaintext"'/g' | \
 sed 's/%ARTICLE_TITLE_HTML%/'"$title_html"'/g' | \
+sed 's/%DATETIME_CREATED_RFC3339%/'"$datetime_created_rfc3339"'/g' | \
+sed 's/%DATETIME_CREATED_FRIENDLY%/'"$datetime_created_friendly"'/g' | \
 sed 's/%BODY%/'"$body"'/g' | \
 tr '\a' '%'
diff --git a/processor/metadata/article.tmpl.do b/processor/metadata/article.tmpl.do
index 7a560ec..9f08b03 100644
--- a/processor/metadata/article.tmpl.do
+++ b/processor/metadata/article.tmpl.do
@@ -10,7 +10,10 @@ cat << EOF
 <body>
 <a href="index.html">%BLOG_TITLE%</a>
 <article>
+<header>
 <h1>%ARTICLE_TITLE_HTML%</h1>
+<p><time datetime="%DATETIME_CREATED_RFC3339%">%DATETIME_CREATED_FRIENDLY%</time></p>
+</header>
 %BODY%
 </article>
 </body>
diff --git a/processor/metadata/default.index_snippet.do b/processor/metadata/default.index_snippet.do
index c65f1a6..a1ff6f9 100644
--- a/processor/metadata/default.index_snippet.do
+++ b/processor/metadata/default.index_snippet.do
@@ -3,6 +3,8 @@
 # Pull in dependencies.
 . ../helpers.sh
 src_file=$(get_source_file "$1")
+uuid_file="${1%.index_snippet}.uuid"
+redo-ifchange "$uuid_file"
 intermediate_file="${1%.index_snippet}.intermediate"
 redo-ifchange "$intermediate_file"
 html_file="${src_file%.*}.html"
@@ -13,10 +15,13 @@ redo-ifchange "$template_file"
 # Build entry data.
 title=$(cat "$intermediate_file" | head -1 | prep_sed)
 link=$(escape_url "${1%.index_snippet}.html" | prep_sed)
+datetime_created_unix=$(stat -c%y "${uuid_file}")
+date_created=$(date -u "+%Y-%m-%d" -d "${datetime_created_unix}")
 
 # Put data into template.
 template=$(cat "$template_file")
 printf "%s\n" "$template" | \
 sed 's/%TITLE%/'"$title"'/g' | \
 sed 's/%LINK%/'"$link"'/g' | \
+sed 's/%DATE_CREATED%/'"$date_created"'/g' | \
 tr '\a' '%'
diff --git a/processor/metadata/index_snippet.tmpl.do b/processor/metadata/index_snippet.tmpl.do
index 8846342..fbe3a45 100644
--- a/processor/metadata/index_snippet.tmpl.do
+++ b/processor/metadata/index_snippet.tmpl.do
@@ -2,6 +2,6 @@
 
 if [ ! -f "$1" ]; then
 cat << EOF
-<li><a href="%LINK%">%TITLE%</a></li>
+<li><a href="%LINK%">%TITLE%</a> (<time>%DATE_CREATED%</time>)</li>
 EOF
 fi
diff --git a/test.sh b/test.sh
index 89a817f..025ebc4 100755
--- a/test.sh
+++ b/test.sh
@@ -26,9 +26,7 @@ diff_test()
 
 # Set up test directory. 
 expected_files_dir="test/test_files"
-expected_files_dir_escaped="test\\/test_files"
 generated_files_dir="test/test_dir"
-generated_files_dir_escaped="test\\/test_dir"
 rm -rf "$generated_files_dir" 
 ./add_dir.sh "$generated_files_dir" 
 working_dir=$(pwd)
@@ -42,26 +40,37 @@ cp "$working_dir/$expected_files_dir"/foo.rst .
 redo
 cd "$working_dir"
 
-# Simple file comparison tests and UUID tests.
+# Compare metadata files.
 uuid_test "$generated_files_dir""/metadata/uuid"
-for file in "$expected_files_dir"/*.html "$expected_files_dir"/metadata/*; do
-  sed_expression='s/'"$expected_files_dir_escaped"'/'"$generated_files_dir_escaped"'/'
-  cmp_file=`echo "$file" | sed "$sed_expression"`
-  if [ ! "$file" = "$expected_files_dir""/index.html" ] && \
-      echo "$file" | grep -q "\.html$"; then
-    basename=$(basename "$cmp_file")
-    uuid_test "${generated_files_dir}/metadata/${basename%.html}.uuid"
-  fi
+for file in "$expected_files_dir"/metadata/*; do
+  basename=$(basename "$file")
+  cmp_file="$generated_files_dir/metadata/$basename"
   diff_test "$file" "$cmp_file"
 done
 
-# To compare feed.xml, ignore all variable date and uuid strings.
+# Compare generated HTML files. Ignore variable dates.
+for file in "$expected_files_dir"/*.html.ignoring; do
+  basename=$(basename "$file")
+  cmp_file="$generated_files_dir/${basename%.ignoring}"
+  if [ ! "$file" = "$expected_files_dir""/index.html.ignoring" ]; then
+    uuid_test "${generated_files_dir}/metadata/${basename%.html.ignoring}.uuid"
+  fi
+  generated_file="$cmp_file".ignoring
+  cat "$cmp_file" | \
+    sed 's/[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}/IGNORE/g' | \
+    sed 's/IGNORET[0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}Z/IGNORE/g' | \
+    sed 's/IGNORE [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\} (UTC)/IGNORE/g' \
+    > "$generated_file"
+  diff_test "$file" "$generated_file"
+done
+
+# Compare feed files. Ignore variable dates and UUIDs.
 original_file="$generated_files_dir""/feed.xml"
 generated_file="$original_file".ignoring
 expected_file="$expected_files_dir""/feed.xml.ignoring"
 cat "$original_file" | \
-  sed 's/>[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}T[0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}Z</>IGNORE</' | \
-  sed 's/>urn:uuid:[0-9a-f]\{8\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{12\}</>urn:uuid:IGNORE</' \
+  sed 's/[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}T[0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}Z/IGNORE/g' | \
+  sed 's/urn:uuid:[0-9a-f]\{8\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{12\}/urn:uuid:IGNORE/g' \
   > "$generated_file"
 diff_test "$expected_file" "$generated_file"
 rm "$generated_file"
diff --git a/test/test_files/bar baz.html b/test/test_files/bar baz.html.ignoring
similarity index 76%
rename from test/test_files/bar baz.html
rename to test/test_files/bar baz.html.ignoring
index 418a91b..410dadf 100644
--- a/test/test_files/bar baz.html	
+++ b/test/test_files/bar baz.html.ignoring	
@@ -6,7 +6,10 @@
 <body>
 <a href="index.html">Yet another blog</a>
 <article>
+<header>
 <h1>foo</h1>
+<p><time datetime="IGNORE">IGNORE</time></p>
+</header>
 <h2 id="bar">bar</h2>
 <p>bar</p>
 <h3 id="bar-1">bar</h3>
diff --git a/test/test_files/foo.html b/test/test_files/foo.html.ignoring
similarity index 89%
rename from test/test_files/foo.html
rename to test/test_files/foo.html.ignoring
index 9eb95f5..72d1388 100644
--- a/test/test_files/foo.html
+++ b/test/test_files/foo.html.ignoring
@@ -6,7 +6,10 @@
 <body>
 <a href="index.html">Yet another blog</a>
 <article>
+<header>
 <h1>a title with some nasty characters: &amp;&lt;&gt;&quot;' %BODY%</h1>
+<p><time datetime="IGNORE">IGNORE</time></p>
+</header>
 <p>this text contains some special characters: /;%'&quot;&gt;&lt;&amp;äöüß</p>
 <h2 id="some-sub-heading">some sub-heading</h2>
 <p>and more than one paragraph</p>
diff --git a/test/test_files/index.html b/test/test_files/index.html.ignoring
similarity index 68%
rename from test/test_files/index.html
rename to test/test_files/index.html.ignoring
index a2660a1..3f80781 100644
--- a/test/test_files/index.html
+++ b/test/test_files/index.html.ignoring
@@ -7,9 +7,9 @@
 <body>
 <h1>Yet another blog</h1>
 <ul>
-<li><a href="foo.html">a title with some nasty characters: &amp;&lt;&gt;&quot;' %BODY%</a></li>
-<li><a href="bar%20baz.html">foo</a></li>
-<li><a href="test.html">foo <em>bar</em> <strong>baz</strong></a></li>
+<li><a href="foo.html">a title with some nasty characters: &amp;&lt;&gt;&quot;' %BODY%</a> (<time>IGNORE</time>)</li>
+<li><a href="bar%20baz.html">foo</a> (<time>IGNORE</time>)</li>
+<li><a href="test.html">foo <em>bar</em> <strong>baz</strong></a> (<time>IGNORE</time>)</li>
 </ul>
 </body>
 </html>
\ No newline at end of file
diff --git a/test/test_files/test.html b/test/test_files/test.html.ignoring
similarity index 78%
rename from test/test_files/test.html
rename to test/test_files/test.html.ignoring
index bd4b81f..31fa715 100644
--- a/test/test_files/test.html
+++ b/test/test_files/test.html.ignoring
@@ -6,7 +6,10 @@
 <body>
 <a href="index.html">Yet another blog</a>
 <article>
+<header>
 <h1>foo <em>bar</em> <strong>baz</strong></h1>
+<p><time datetime="IGNORE">IGNORE</time></p>
+</header>
 <p>foo</p>
 <h2 id="bar">bar</h2>
 <p>baz</p>
-- 
2.30.2