#!/usr/bin/awk -f BEGIN { indent = 0 prefix = "html/body/" print("") level("html/head") } function headers() { printf("%0" indent "s\n", "") if (meta["title"]) printf("%0" indent "s
\\1
", "g", text)
text = gensub(/\[(.*)\]\((.*)\)/, "\\1", "g", text)
return text
}
/^---$/ {
parse_meta = !parse_meta
next
}
{
if(parse_meta) {
split($0, r, ": ")
gsub(/(^"|"$)/, "", r[2])
meta[r[1]]=r[2]
next
}
}
/^```/ {
if (old == prefix "pre") {
level(prefix)
} else {
level(prefix "pre")
}
next
}
{
if (old == prefix "pre") {
printf("%s\n", escape($0))
next
}
}
/^- / {
level(prefix "ul")
level(prefix "ul/li")
gsub(/^- /, "", $0)
printf("%0" indent "s%s\n", "", inline($0))
next
}
/^\s*\* / {
level(prefix "ul")
level(prefix "ul/li")
gsub(/^\s*\* /, "", $0)
printf("%0" indent "s%s\n", "", inline($0))
next
}
/^> / {
level(prefix "quote")
gsub(/^> /, "", $0)
printf("%0" indent "s%s\n", "", inline($0))
}
/^ / {
level(prefix "blockquote")
printf("%0" indent "s%s\n", "", inline($0))
}
/^ / {
if (old == prefix) level(prefix "p")
printf("%0" indent "s%s\n", "", inline($0))
next
}
/^# / {
gsub(/^#* /, "", $0)
if (!meta["title"]) meta["title"] = $0
level(prefix)
oneliner("h1", $0)
next
}
/^## / {
gsub(/^#* /, "", $0)
level(prefix)
oneliner("h2", $0)
next
}
/^### / {
gsub(/^#* /, "", $0)
level(prefix)
oneliner("h3", $0)
next
}
/^$/ {
if (old != "html/head") level(prefix)
next
}
/^!\[.*\]\(.*\)$/ {
txt = $0
filenam = $0
gsub(/(^!\[|\].*$)/, "", txt)
gsub(/(^!.*\(|\)$)/, "", filenam)
level(prefix "figure")
oneliner("img",""," src=\"" escape(filenam) "\" alt=\"" escape(txt) "\"")
oneliner("figcaption", txt, "")
next
}
{
level(prefix "p")
printf("%0" indent "s%s\n", "", inline($0))
next
}
END {
level("")
}