home · contact · privacy
Add HTML templating, extend tests.
authorChristian Heller <c.heller@plomlompom.de>
Sat, 24 Dec 2016 01:47:41 +0000 (02:47 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sat, 24 Dec 2016 01:47:41 +0000 (02:47 +0100)
12 files changed:
README.md
processor/default.html.do
processor/helpers.sh
processor/index.html.do
processor/metadata/article.tmpl.do [new file with mode: 0644]
processor/metadata/default.index_snippet.do
processor/metadata/index.tmpl.do [new file with mode: 0644]
processor/metadata/index_snippet.tmpl.do [new file with mode: 0644]
test/test_files/feed.xml.ignoring
test/test_files/foo.html
test/test_files/foo.rst
test/test_files/index.html

index b7f03dd4ccb5cc319f40028e113dffa4a0ccb9ce..9d6a05b17e7cbffe92d6f62d90315fa5f8724102 100644 (file)
--- a/README.md
+++ b/README.md
@@ -25,11 +25,13 @@ run ./add_dir.sh DIRECTORY.
 
 You can then enter the directory and run redo there. This will generate article
 .html files from all .md and .rst files, plus a ./index.html, and a ./feed.xml.
-These files will be linked to symbolically in a directory ./public/. (Some
-metadata files will also be generated below ./metadata/: for each article, there
-will be generated a .uuid and a .intermediate file; furthermore, files for data
-used in ./feed.xml and ./index.html will be built and can be edited to customize
-the blog: ./metadata/url, ./metadata/author, ./metadata/uuid, ./metadata/title.) 
+These files will be linked to symbolically in a directory ./public/.
+
+Some metadata files will also be generated below ./metadata/: For each article,
+there will be generated a .uuid and a .intermediate file; furthermore, files for
+data used in ./feed.xml and ./index.html will be built there and can be edited
+to customize the blog – namely the files url, author, uuid, title, index.tmpl,
+index_snippet.tmpl, article.tmpl.
 
 bugs
 ----
index fcef3efe6b778e72d24f434afc6b51ca7c6d6325..99d784df28884f9816f08305099b04cc3338211e 100644 (file)
@@ -4,25 +4,25 @@
 . ./helpers.sh
 metadata_dir=metadata
 intermediate_file="${metadata_dir}/${1%.html}.intermediate"
+redo-ifchange "$intermediate_file"
 title_file="${metadata_dir}"/title
 redo-ifchange "$title_file" 
-redo-ifchange "$intermediate_file"
+template_file="${metadata_dir}"/article.tmpl
+redo-ifchange "$template_file"
 
 # Build entry data.
-blog_title=`read_and_escape_file "$title_file" | head -1`
-title_html=`cat "$intermediate_file" | head -1`
+blog_title=$(read_and_escape_file "$title_file" | head -1 | prep_sed)
+title_html=$(cat "$intermediate_file" | head -1)
 title_plaintext=`echo "$title_html" | html2text`
-title_plaintext_escaped=`escape_html "$title_plaintext"`
-body=`cat "$intermediate_file" | sed 1d`
-
-# Write first part of entry head.
-cat << EOF
-<!DOCTYPE html>
-<html>
-<head>
-EOF
+title_html=$(printf "%s" "$title_html" | prep_sed)
+title_plaintext=$(escape_html "$title_plaintext" | prep_sed)
+body=$(cat "$intermediate_file" | sed 1d | prep_sed)
 
-# Write remaining entry head and body. 
-printf "<title>%s – %s</title>\n</head>\n<body>\n" "$blog_title" "$title_plaintext_escaped"
-printf "<h1>%s</h1>\n" "$title_html"
-printf "<section>\n%s\n</section>\n</body>\n</html>" "$body"
+# Put data into template.
+template=$(cat "$template_file")
+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/%BODY%/'"$body"'/g' | \
+tr '\a' '%'
index 20905ab6a9e5f06eb01763088ad2e4310784fabb..2869a29a3d414db7066c71e17ff1439b0b7c6c92 100644 (file)
@@ -23,7 +23,7 @@ get_basepath() {
   url_basepath=`echo $base_url | cut -d '/' -f 3-`
   url_basepath_escaped=`escape_url "$url_basepath"`
   basepath="$url_protocol""://""$url_basepath_escaped"
-  echo "$basepath"
+  printf "%s" "$basepath"
 }
 
 get_source_file() {
@@ -37,5 +37,15 @@ get_source_file() {
     exit 1
   fi
   redo-ifchange "$src_file"
-  printf "$src_file"
+  printf "%s" "$src_file"
+}
+
+prep_sed () {
+  # Escape characters that confuse sed in a replacement string. Also replace
+  # occurences of % (which the templating uses as a variable marker) with
+  # non-printable placeholder \a (clear input of it first), to be replaced by
+  # % again when the templating has finished (so that no replacement string gets
+  # interpreted by the templating).
+  sedsafe_pattern='s/\\/\\\\/g; s/\//\\\//g; s/&/\\\&/g; $!s/$/\\/g; '
+  sed "$sedsafe_pattern" | tr -d '\a' | tr '%' '\a'
 }
index 86369692cad6f9070110a04d644e29baa60fad20..105bc74309cc2e177fafd508fb52da4829c70e71 100644 (file)
@@ -6,16 +6,11 @@ metadata_dir=metadata
 srcdir=`pwd`
 title_file="$metadata_dir"/title
 redo-ifchange "$title_file"
+template_file="${metadata_dir}"/index.tmpl
+redo-ifchange "$template_file"
 
-# Write index head.
-cat << EOF
-<!DOCTYPE html>
-<html>
-<head>
-EOF
-blog_title=`read_and_escape_file "$title_file" | head -1`
-printf "<title>%s</title>\n</head>\n<body>\n" "$blog_title"
-printf "<h1>%s</h1>\n<ul>\n" "$blog_title"
+# Build blog title.
+title=$(read_and_escape_file "$title_file" | head -1 | prep_sed)
 
 # Generate link list entries.
 tmp_snippets_dir=.tmp_index_snippets
@@ -39,9 +34,13 @@ for file in ./${tmp_snippets_dir}/*; do
   mv ./${tmp_snippets_dir}/tmp ./${tmp_snippets_dir}/list
 done
 if [ -e "./${tmp_snippets_dir}/list" ]; then
-  cat ./${tmp_snippets_dir}/list
+  list=$(cat ./${tmp_snippets_dir}/list | prep_sed)
 fi
 rm -rf "${tmp_snippets_dir}"
 
-# Write index footer.
-printf "</ul>\n</body>\n</html>"
+# Put data into template.
+template=$(cat "$template_file")
+printf "%s" "$template" | \
+sed 's/%TITLE%/'"$title"'/g' | \
+sed 's/%LIST%/'"$list"'/g' | \
+tr '\a' '%'
diff --git a/processor/metadata/article.tmpl.do b/processor/metadata/article.tmpl.do
new file mode 100644 (file)
index 0000000..7878a3e
--- /dev/null
@@ -0,0 +1,18 @@
+#!/bin/sh
+
+if [ ! -f "$1" ]; then
+cat << EOF
+<!DOCTYPE html>
+<html>
+<head>
+<title>%BLOG_TITLE% – %ARTICLE_TITLE_ESCAPED%</title>
+</head>
+<body>
+<h1>%ARTICLE_TITLE_HTML%</h1>
+<section>
+%BODY%
+</section>
+</body>
+</html>
+EOF
+fi
index e332350f23fe81f6b1b27dd5ac1208e5d4930053..c65f1a66cdf54a045d8d34ead2c56d4b1075b48f 100644 (file)
@@ -7,8 +7,16 @@ intermediate_file="${1%.index_snippet}.intermediate"
 redo-ifchange "$intermediate_file"
 html_file="${src_file%.*}.html"
 redo-ifchange "$html_file"
+template_file=index_snippet.tmpl
+redo-ifchange "$template_file"
 
-# Get variables, write entry.
-title_html=$(cat "$intermediate_file" | head -1)
-html_file_escaped=$(escape_url "${1%.index_snippet}.html")
-printf "<li><a href=\"%s\" />%s</a></li>\n" "$html_file_escaped" "$title_html"
+# Build entry data.
+title=$(cat "$intermediate_file" | head -1 | prep_sed)
+link=$(escape_url "${1%.index_snippet}.html" | prep_sed)
+
+# Put data into template.
+template=$(cat "$template_file")
+printf "%s\n" "$template" | \
+sed 's/%TITLE%/'"$title"'/g' | \
+sed 's/%LINK%/'"$link"'/g' | \
+tr '\a' '%'
diff --git a/processor/metadata/index.tmpl.do b/processor/metadata/index.tmpl.do
new file mode 100644 (file)
index 0000000..c2a9c21
--- /dev/null
@@ -0,0 +1,18 @@
+#!/bin/sh
+
+if [ ! -f "$1" ]; then
+cat << EOF
+<!DOCTYPE html>
+<html>
+<head>
+<title>%TITLE%</title>
+</head>
+<body>
+<h1>%TITLE%</h1>
+<ul>
+%LIST%
+</ul>
+</body>
+</html>
+EOF
+fi
diff --git a/processor/metadata/index_snippet.tmpl.do b/processor/metadata/index_snippet.tmpl.do
new file mode 100644 (file)
index 0000000..8846342
--- /dev/null
@@ -0,0 +1,7 @@
+#!/bin/sh
+
+if [ ! -f "$1" ]; then
+cat << EOF
+<li><a href="%LINK%">%TITLE%</a></li>
+EOF
+fi
index 0d6ea0d4d9112db220ec32d2c88ccb4caaadaa3a..dde4369ca848fb1b29bcf89bd5da70968f8663a6 100644 (file)
 </entry>
 
 <entry>
-<title type="html">a title with some nasty characters: &amp;amp;&amp;lt;&amp;gt;&amp;quot;&#x27;</title>
+<title type="html">a title with some nasty characters: &amp;amp;&amp;lt;&amp;gt;&amp;quot;&#x27; %BODY%</title>
 <id>urn:uuid:IGNORE</id>
 <updated>IGNORE</updated>
 <published>IGNORE</published>
 <link href="http://example.org/foo.html" />
 <content type="html">
-&lt;p&gt;this text contains some special characters: &#x27;&amp;quot;&amp;gt;&amp;lt;&amp;amp;&lt;/p&gt;
+&lt;p&gt;this text contains some special characters: /;%&#x27;&amp;quot;&amp;gt;&amp;lt;&amp;amp;äöüß&lt;/p&gt;
 &lt;p&gt;and more than one paragraph&lt;/p&gt;
+&lt;p&gt;note that should be ignored but \ should give a backlash&lt;/p&gt;
 </content>
 </entry>
 
index aa3575d5eca148881a24cb7ea2c1a4ed1c1da7aa..dd25b702183c8d115a3516eff446be82411a70a1 100644 (file)
@@ -1,13 +1,14 @@
 <!DOCTYPE html>
 <html>
 <head>
-<title>Yet another blog – a title with some nasty characters: &amp;&lt;&gt;&quot;&#x27;</title>
+<title>Yet another blog – a title with some nasty characters: &amp;&lt;&gt;&quot;&#x27; %BODY%</title>
 </head>
 <body>
-<h1>a title with some nasty characters: &amp;&lt;&gt;&quot;'</h1>
+<h1>a title with some nasty characters: &amp;&lt;&gt;&quot;' %BODY%</h1>
 <section>
-<p>this text contains some special characters: '&quot;&gt;&lt;&amp;</p>
+<p>this text contains some special characters: /;%'&quot;&gt;&lt;&amp;äöüß</p>
 <p>and more than one paragraph</p>
+<p>note that should be ignored but \ should give a backlash</p>
 </section>
 </body>
 </html>
\ No newline at end of file
index 951861b35323e4b1bf2ec2c63e75eeb0dfce5508..cf4c2dff9af78b558b4dc5daac9262542389693a 100644 (file)
@@ -1,6 +1,8 @@
-a title with some nasty characters: &<>"'
-=========================================
+a title with some nasty characters: &<>"' %BODY%
+================================================
 
-this text contains some special characters: '"><&
+this text contains some special characters: /;%'"><&äöüß
 
 and more than one paragraph
+
+note that \ should be ignored but \\ should give a backlash
index 3210fef4d4d8597c80a4201b516fe1ee207c7aa4..84fdfb34c2f341c52f48a689993428f1c3af839f 100644 (file)
@@ -6,9 +6,9 @@
 <body>
 <h1>Yet another blog</h1>
 <ul>
-<li><a href="foo.html" />a title with some nasty characters: &amp;&lt;&gt;&quot;'</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></li>
+<li><a href="bar%20baz.html">foo</a></li>
+<li><a href="test.html">foo <em>bar</em> <strong>baz</strong></a></li>
 </ul>
 </body>
 </html>
\ No newline at end of file