diff options
Diffstat (limited to 'build.sh')
| -rwxr-xr-x | build.sh | 235 |
1 files changed, 235 insertions, 0 deletions
diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..166eeba --- /dev/null +++ b/build.sh @@ -0,0 +1,235 @@ +#!/bin/sh + +header='<meta charset="UTF-8"> +<meta name="viewport" content="width=device-width, initial-scale=1"> +<style> + body { + max-width: 45rem; + margin: auto; + padding: 0.5em; + text-align: justify; + } + h1 { text-align: center } + pre { + padding: 1ch; + background-color: #f5f5f5; + overflow: auto; + } + img { + display: block; + margin: auto; + } + .footer { + text-align: center; + font-size: 0.8em; + } +</style> +' + +md2html() { +# Usage: +# md2html file.md > file.html +# Options: -v esc=false to not escape html +awk ' + function newblock(nblock) { + if (text) + print "<" block ">" text "</" block ">" + text = "" + block = nblock ? nblock : "p" + } + + function subinline(tgl, inl) { + while (match($0, tgl)){ + if (inline[ni] == inl) + ni -= sub(tgl, "</" inl ">") + else if (sub(tgl, "<" inl ">")) + inline[++ni] = inl + } + } + + function dolink(href, lnk) { + # Undo escaped html in uris + gsub(/&/, "\\&", href) + gsub(/</, "<", href) + gsub(/>/, ">", href) + # & can be tricky, and not standard: + gsub(/&/, "\\\\\\&", href) + gsub(/&/, "\\\\\\&", lnk) + return "<a href=\"" href "\">" lnk "</a>" + } + + BEGIN { + ni = 0; # inlines + nl = 0; # nested lists + text = "" + block = "p" + } + + # Escape html + esc != "false" { + gsub("&", "\\&") + gsub("<", "\\<") + gsub(">", "\\>") + } + + # Horizontal rules (_ is not in markdown) + /^[ ]*([-*_] ?)+[ ]*$/ && text == "" { + print "<hr>" + next + } + + # Tables (not in markdown) + # Syntax: + # Right Align| Center Align |Left Align + /([ ]\|)|(\|[ ])/ { + if (block != "table") + newblock("table") + nc = split($0, cells, "|") + $0 = "<tr>\n" + for (i = 1; i <= nc; i++){ + align = "left" + if (sub(/^[ ]+/, "", cells[i])){ + if (sub(/[ ]+$/, "", cells[i])) + align = "center" + else + align = "right" + } + sub(/[ ]+$/,"", cells[i]) + $0 = $0 "<td align=\"" align "\">" cells[i] "</td>\n" + } + $0 = $0 "</tr>" + } + + # Ordered and unordered (possibly nested) lists + /^[ ]*([*+-]|(([0-9]+[\.-]?)+))[ ]/ { + newblock("li") + nnl = 1 + while (match($0, /^[ ]/)){ + sub(/^[ ]/,"") + nnl++ + } + while (nl > nnl) + print "</" list[nl--] ">" + while (nl < nnl){ + list[++nl] = "ol" + if (match($0, /^[*+-]/)) + list[nl] = "ul" + print "<" list[nl] ">" + } + sub(/^([*+-]|(([0-9]+[\.-]?)+))[ ]/,"") + } + + # Multi line list items + block == "li" { + sub(/^( *)|( *)/,"") + } + + # Code blocks + /^( | )/ { + if (block != "code") + newblock("code") + sub(/^( | )/, "") + text = text $0 "\n" + next + } + + # Paragraphs + /^$/ { + newblock() + while (nl > 0) + print "</" list[nl--] ">" + } + + # Headers + /^#/ { + newblock() + match($0, /#+/) + n = RLENGTH + if (n > 6) + n = 6 + text = substr($0, RLENGTH + 1) + block = "h" n + next + } + + # Alternate headers (underlined) + /^=+$/ { + block = "h" 1 + next + } + + /^-+$/ { + block = "h" 2 + next + } + + { + # Images + while (match($0, /!\[[^\]]+\]\([^\)]+\)/)){ + split(substr($0, RSTART, RLENGTH), a, /(!\[)|\)|(\]\()/) + sub(/!\[[^\]]+\]\([^\)]+\)/, "<img src=\"" a[3] "\" alt=\"" a[2] "\">") + } + # Links + while (match($0, /\[[^\]]+\]\([^\)]+\)/)){ + split(substr($0, RSTART, RLENGTH), a, /[\[\)]|(\]\()/) + sub(/\[[^\]]+\]\([^\)]+\)/, dolink(a[3], a[2])) + } + # Auto links (uri matching is poor) + na = split($0, a, /(^\()|[ ]|([,\.\)]([ ]|$))/) + for (i = 1; i <= na; i++) + if (match(a[i], /^(((https?|ftp|file|news|irc):\/\/)|(mailto:)).+$/)) + sub(a[i], dolink(a[i], a[i])) + # Inline + subinline("(\\*\\*)|(__)", "strong") + subinline("\\*", "em") + subinline("`", "code") + text = text (text ? " " : "") $0 + } + + END { + while (ni > 0) + text = text "</" inline[ni--] ">" + newblock() + while (nl > 0) + print "</" list[nl--] ">" + }' "$1" +} + +genhtml() ( + cd "$1" || return + + . ./meta.sh + + exec 1>index.html + + # Header + echo "<!DOCTYPE html>" + echo "<!-- generated by build.sh. DO NOT EDIT. -->" + echo "<html lang=\"${lang:-en}\">" + echo "<title>$title</title>" + [ "$description" ] && echo "<meta name=\"description\" content=\"$description\">" + echo "$header" + [ "$1" != . ] && echo "<a href=\"..\">$blog_title</a><hr>" + + # Body + # pandoc *.md + for f in *.md; do + [ -f "$f" ] && md2html "$f" + done + + # Footer + [ "$1" != . ] && echo "<hr>From: $author, $date" +) + +for d in *; do + [ -d "$d" ] && genhtml "$d" +done +genhtml . + +# Fix for mastodon. +sed '/mstdn/s/href=/rel="me" href=/' index.html >xx && mv xx index.html +# Put a license in index footer. +echo '<hr><div class="footer"> +<a href="feed.xml">RSS feed</a>.     Licensed under +<a href="http://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a>. +</div>' >>index.html |
