#!/usr/bin/awk -f
BEGIN {
state = "NORMAL"
}
function format_inline(marker, tag, text,
escaped, pattern,
start, len,
inner, before, after) {
escaped = marker
gsub(/[][\\.^$*+?(){}|]/, "\\\\&", escaped)
pattern = escaped "([^" substr(marker,1,1) "]+" ")" escaped
while (match(text, pattern)) {
start = RSTART
len = RLENGTH
before = substr(text, 1, start - 1)
inner = substr(text, start + length(marker), len - 2 * length(marker))
after = substr(text, start + len)
text = before "<" tag ">" inner "" tag ">" after
}
return text
}
function format_footnotes(text,
start, end,
before, note, id,
ref) {
while (match(text, /\{\^[^}]+\}/)) {
start = RSTART
end = RLENGTH
before = substr(text, 1, start - 1)
note = substr(text, start + 2, end - 3)
fn_count++
id = fn_count
fn_text[id] = note
ref = "" \
"[" id "] "
text = before ref substr(text, start + end)
}
return text
}
function format_links(text,
start, end,
before, inside,
label, url, mid) {
while (match(text, /\[[^]]+\]\([^)]*\)/)) {
start = RSTART
end = RLENGTH
before = substr(text, 1, start - 1)
inside = substr(text, start, end)
match(inside, /\[[^]]+\]/)
label = substr(inside, RSTART + 1, RLENGTH - 2)
match(inside, /\([^)]*\)/)
url = substr(inside, RSTART + 1, RLENGTH - 2)
text = before "" label " " \
substr(text, start + end)
}
return text
}
function escape_html(text) {
gsub(/&/, "\\&", text);
gsub(/, "\\<", text);
gsub(/>/, "\\>", text);
return text
}
function apply_inline(text) {
text = escape_html(text)
text = format_footnotes(text)
text = format_links(text)
text = format_inline("'''", "code", text)
text = format_inline("```", "code", text)
text = format_inline("`", "code", text)
text = format_inline("**", "strong", text)
text = format_inline("*", "em", text)
return text
}
function open_block(type) {
state = type
if (type == "PARAGRAPH")
printf "
"
else if (type == "LIST")
print "
"
else if (state == "BLOCKQUOTE")
print ""
else if (state == "CODEBLOCK")
print ""
state = "NORMAL"
}
function close_if_open() {
if (state != "NORMAL")
close_block()
if (list_level > 0) {
while (list_level > 0) {
print ""
list_level--
}
}
}
function list_indent_level(line, indent) {
match(line, /^ */)
indent = RLENGTH
return int(indent / 2)
}
function list_open(target, i) {
for (i = list_level; i < target; i++) {
print ""
list_level++
}
}
function list_close(target, i) {
for (i = list_level; i > target; i--) {
print " "
list_level--
}
}
function write_line(line) {
if (state == "PARAGRAPH") {
if (paragraph_has_content)
printf " "
printf "%s", apply_inline(line)
paragraph_has_content = 1
}
else if (state == "BLOCKQUOTE") {
print apply_inline(line)
}
else if (state == "CODEBLOCK") {
print escape_html(line)
}
}
function print_header(line,
level, text) {
match(line, /^#+/)
level = RLENGTH
if (level > 6)
level = 6
text = substr(line, level + 2)
printf "%s \n",
level,
apply_inline(text),
level
}
function handle_image(line,
alt, src, cap,
is_banner, class) {
is_banner = 0
if (line ~ /^!\[/) {
is_banner = 1
class = "banner"
} else {
class = ""
}
match(line, /\[[^]]+\]/)
alt = substr(line, RSTART + 1, RLENGTH - 2)
match(line, /\([^)]*\)/)
src = substr(line, RSTART + 1, RLENGTH - 2)
cap = ""
if (match(line, /\{[^}]+\}/)) {
cap = substr(line, RSTART + 1, RLENGTH - 2)
}
printf ""
printf " ", src, alt
if (cap != "")
printf "%s ", apply_inline(cap)
print " "
}
function print_footnotes( i) {
if (fn_count == 0)
return
print ""
}
{
line = $0
if (line ~ /^;/)
next
if (state == "CODEBLOCK") {
if (line ~ /^'''/ || line ~ /^```/)
close_block()
else
write_line(line)
next
}
if (state == "BLOCKQUOTE") {
if (line == "\"\"\"")
close_block()
else
write_line(line)
next
}
if (line == "") {
close_if_open()
paragraph_has_content = 0
next
}
if (line ~ /^'''/ || line ~ /^```/) {
close_if_open()
open_block("CODEBLOCK")
next
}
if (line == "\"\"\"") {
close_if_open()
open_block("BLOCKQUOTE")
next
}
if (line ~ /^) {
close_if_open()
print line
next
}
if (line ~ /^!?!\[/) {
close_if_open()
handle_image(line)
next
}
if (line ~ /^#{1,6} /) {
close_if_open()
print_header(line)
next
}
if (line ~ /^ *- /) {
if (state == "PARAGRAPH")
close_block()
level = list_indent_level(line) + 1
list_open(level)
list_close(level)
sub(/^ *- /, "", line)
printf "%s \n",
apply_inline(line)
next
}
if (state != "PARAGRAPH") {
close_if_open()
paragraph_has_content = 0
open_block("PARAGRAPH")
}
write_line(line)
}
END {
close_if_open()
print_footnotes()
}