#!/bin/sh # buildpage -- small markdown wrapper with frontmatter support set -e die() { echo "$*" 1>&2 exit 1 } [ -z "$1" ] && die "usage: buildpage FILE" # Print the header cat <<EOM <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"/> <meta name="viewport" content="width=device-width,initial-scale=1"> <!-- Inspired by https://www.swyx.io/css-100-bytes/ --> <style> html { max-width: 70ch; padding: 3em 1em; margin: auto; font-size: 1em; font-family: sans-serif; } footer { font-size: .8em; } code { font-family: consolas, courier, monospace; } EOM if [ "$(basename "$1")" != "root-index.md" ]; then cat <<EOM blockquote, pre { background: #f2f2f2; overflow: auto; padding: 10px; border: 2px solid black; } EOM fi echo "</style>" # Print the title, if we find it in the frontmatter if [ "$(head -n1 "$1")" = "---" ]; then title="$(tail +2 "$1" | sed '/^---$/q' | grep '^title:' | sed 's/title: *//')" nofooter="$(tail +2 "$1" | sed '/^---$/q' | grep '^nofooter:' | sed 's/nofooter: *//')" [ -n "$title" ] && printf "<title>$title\n" start=$(awk '/^---$/ { c++; if (c == 2) { printf "%d\n", NR + 1; exit } }' "$1") else start=1 fi printf "\n\n" # Render the markdown starting from the end of the frontmatter! tail +$start "$1" | cmark --unsafe # Print a nav footer if [ -z "$nofooter" ]; then cat <
EOM else printf "\n\n" fi