buildpage (1633B) [raw]
1 #!/bin/sh 2 # buildpage -- small markdown wrapper with frontmatter <title> support 3 set -e 4 die() { 5 echo "$*" 1>&2 6 exit 1 7 } 8 9 [ -z "$1" ] && die "usage: buildpage FILE" 10 11 # Print the header 12 cat <<EOM 13 <!DOCTYPE html> 14 <html lang="en"> 15 <head> 16 <meta charset="utf-8"/> 17 <meta name="viewport" content="width=device-width,initial-scale=1"> 18 <!-- Inspired by https://www.swyx.io/css-100-bytes/ --> 19 <style> 20 html { 21 max-width: 70ch; 22 padding: 3em 1em; 23 margin: auto; 24 font-size: 1em; 25 font-family: sans-serif; 26 } 27 footer { 28 font-size: .8em; 29 } 30 code { 31 font-family: consolas, courier, monospace; 32 } 33 EOM 34 35 if [ "$(basename "$1")" != "root-index.md" ]; then 36 cat <<EOM 37 blockquote, pre { 38 background: #f2f2f2; 39 overflow: auto; 40 padding: 10px; 41 border: 2px solid black; 42 } 43 EOM 44 fi 45 46 echo "</style>" 47 48 # Print the title, if we find it in the frontmatter 49 if [ "$(head -n1 "$1")" = "---" ]; then 50 title="$(tail +2 "$1" | sed '/^---$/q' | grep '^title:' | sed 's/title: *//')" 51 nofooter="$(tail +2 "$1" | sed '/^---$/q' | grep '^nofooter:' | sed 's/nofooter: *//')" 52 [ -n "$title" ] && printf "<title>$title</title>\n" 53 start=$(awk '/^---$/ { c++; if (c == 2) { printf "%d\n", NR + 1; exit } }' "$1") 54 else 55 start=1 56 fi 57 printf "</head>\n<body>\n" 58 59 # Render the markdown starting from the end of the frontmatter! 60 tail +$start "$1" | cmark --unsafe 61 62 # Print a nav footer 63 if [ -z "$nofooter" ]; then 64 cat <<EOM 65 <br><br> 66 <footer> 67 <a href="/">alexkarle.com</a> | <a href="/garbash/~alex">Garbash Home</a> | <a href="https://git.alexkarle.com/garbash-www">Source</a> 68 </footer> 69 </body> 70 </html> 71 EOM 72 else 73 printf "</body>\n</html>\n" 74 fi