wordpusher/render_file

41 lines
1.7 KiB
Bash
Executable File

#!/bin/sh
# render_file.sh: render a markdown file as HTML
# arguments:
# $1: markdown file to render
# Separate metadata and actual markdown in the .md file
frontmatter=$(awk '/!startmeta/,/!endmeta/ {if ($0 != "!startmeta" && $0 != "!endmeta") print;}' "$1")
docbody=$(awk '/!endmeta/ {p=1; next} p' "$1")
title=$(echo "$frontmatter" | awk -F'=' '/title=/ {print $2}')
date=$(echo "$frontmatter" | awk -F'=' '/date=/ {print $2}')
# cmark doesn't create the <head>, doctype declaration or indeed most of the document
# 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
# to escape all the double quotes in the HTML
printf '<!doctype html>\n'
printf '<html>\n'
printf '<head>\n'
printf "<title>%s | cren's webspace</title>\n" "$title"
printf '<link rel="icon" href="favicon.ico" type="image/x-icon">\n'
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 '<body>\n'
printf '<header>\n'
printf "<h1>cren's webspace</h1>\n"
printf '<h2>%s</h2>\n' "$title"
printf '<date datetime=%s>%s</date>' "$date" "$date"
printf '</header>\n'
printf '<main>\n'
# Now we insert the main content of the page
# Also insert a <date> element in the header after the heading, based on the `date` metadata item
printf '%s' "$(echo "$docbody" | cmark)"
printf '</main>\n'
# Finally, the footer
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 '</footer>\n'
printf '</body>\n'
printf '</html>\n'