render_file: allow page elements to be customised

The header, footer and <head> can now be customised using
either static or dynamic templates. This makes it possible
to use Wordpusher for more than one site without modifying
the source scripts.
This commit is contained in:
Gender Demon 2021-09-04 20:37:34 +01:00
parent e78f2866d8
commit 2ef8fe251d
2 changed files with 24 additions and 9 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
gentemplate/*
template/*

View File

@ -8,6 +8,23 @@ frontmatter=$(awk '/!startmeta/,/!endmeta/ {if ($0 != "!startmeta" && $0 != "!en
docbody=$(awk '/!endmeta/ {p=1; next} p' "$1") docbody=$(awk '/!endmeta/ {p=1; next} p' "$1")
title=$(echo "$frontmatter" | awk -F'=' '/title=/ {print $2}') title=$(echo "$frontmatter" | awk -F'=' '/title=/ {print $2}')
date=$(echo "$frontmatter" | awk -F'=' '/date=/ {print $2}') date=$(echo "$frontmatter" | awk -F'=' '/date=/ {print $2}')
# Attempt to dynamically generate the header and footer for this page from gentemplates
# (If no script for generation exists, skip the step for that page element)
if [ -f gentemplate/header ]
then
printf 'Generating page header...' >&2
title="$title" date="$date" gentemplate/header
fi
if [ -f gentemplate/footer ]
then
printf 'Generating page footer...' >&2
date="$date" gentemplate/footer
fi
if [ -f gentemplate/htmlhead ]
then
printf 'Generating page <head>...' >&2
title="$title" gentemplate/htmlhead
fi
# cmark doesn't create the <head>, doctype declaration or indeed most of the document # cmark doesn't create the <head>, doctype declaration or indeed most of the document
# structure for us, so we'll do it ourselves. # structure for us, so we'll do it ourselves.
# We're going to use single quotes for the strings here too because otherwise we'll have # We're going to use single quotes for the strings here too because otherwise we'll have
@ -15,17 +32,13 @@ date=$(echo "$frontmatter" | awk -F'=' '/date=/ {print $2}')
printf '<!doctype html>\n' printf '<!doctype html>\n'
printf '<html>\n' printf '<html>\n'
printf '<head>\n' printf '<head>\n'
printf "<title>%s | cren's webspace</title>\n" "$title" # At this point, insert the <head>
printf '<link rel="icon" href="favicon.ico" type="image/x-icon">\n' printf "%s\n" "$(cat template/htmlhead)"
printf '<link rel="stylesheet" href="cren.css">\n'
printf '<meta charset="utf8">\n'
printf '<meta name="viewport" content="width=device-wdith, initial-scale=1">\n'
printf '</head>\n' printf '</head>\n'
printf '<body>\n' printf '<body>\n'
printf '<header>\n' printf '<header>\n'
printf "<h1>cren's webspace</h1>\n" # Now insert the header
printf '<h2>%s</h2>\n' "$title" printf "%s\n" "$(cat template/header)"
printf '<date datetime=%s>%s</date>' "$date" "$date"
printf '</header>\n' printf '</header>\n'
printf '<main>\n' printf '<main>\n'
# Now we insert the main content of the page # Now we insert the main content of the page
@ -34,7 +47,7 @@ printf '%s' "$(echo "$docbody" | cmark)"
printf '</main>\n' printf '</main>\n'
# Finally, the footer # Finally, the footer
printf '<footer>\n' printf '<footer>\n'
printf '<p>Content on this webspace is made available under the <a href="https://creativecommons.org/publicdomain/zero/1.0/legalcode">CC0 licence</a>. You have the right to copy.</p>\n' printf '%s\n' "$(cat template/footer)"
printf '</footer>\n' printf '</footer>\n'
printf '</body>\n' printf '</body>\n'
printf '</html>\n' printf '</html>\n'