# Status: # [.] Inline HTML # [x] Autoescape # [ ] Block elements # [x] Paragraph # [x] Line break # [x] Headers (atx and setex) # [x] Blockquotes # [x] Lists # [ ] Code blocks # [x] Horizontal rules # [ ] Span elements # [.] Links # [x] Inline # [ ] Reference # [x] Emphasis # [x] Code # [ ] Images # [ ] Other # [ ] Autolinks # [ ] Escapes # # [ ] Non-standard features # [x] Strikethrough ~~...~~ # [x] Superscript ^...^ # [x] Subscript ~...~ # [ ] Footnote [^...] (with user customizations) # [ ] Task list - [ ] # [.] Definition lists # [x] Single line # [ ] Multi-line # [ ] Fenced code blocks ```lang ``` # [ ] Table alignment BEGIN { prev_blankline = blankline = 1 skipprint = 1 newStack("bq-level") newStack("list-level") } function println(line) { if (0 == line) line = $0 if (!skipprint) print prev prev = line skipprint = 0 } { prev_blankline = blankline blankline = 1 } /[^[:blank:]]/ { blankline = 0 } # Inline HTML /^[[:blank:]]*<\/?[[:alnum:]]+/ { println() next } # Auto-escape & /&/ { ampstart = 0 while (match(substr($0, ampstart + 1), /&/)) { ampstart += RSTART if (match(substr($0, ampstart), /&[[:alnum:]#]+;/)) { continue } $0 = substr($0, 1, ampstart - 1) "&" substr($0, ampstart + 1) } } # Auto-escape < / { gsub(/, "\\<") } # Blockquote Part 1/2 /^>[[:blank:]]*$/ { next } /^> / { level = 0 while (match($0, /^> [[:blank:]]*/)) { $0 = substr($0, RLENGTH + 1) level += RSTART while (topStack("bq-level") > level) { println("") popStack("bq-level") } if (topStack("bq-level") < level) { println("
") pushStack("bq-level", level) } } } # atx-style headers /^[[:blank:]]*#+/ { match($0, /#+/) if (RLENGTH <= 6) { gsub(/^[[:blank:]]*#+[[:blank:]]*/, "") gsub(/[[:blank:]]*#+[[:blank:]]*$/, "") println("") popStack("bq-level") } } # Links /\[[^\]]*\]\([^)]*\)/ { while (match($0, /\[[^\]]*\]\(/)) { astart = RSTART inner_anchor = substr($0, RSTART + 1, RLENGTH - 3) aend = astart + RLENGTH - 1 match(substr($0, astart), /\]\([^)]*)/) aend += RLENGTH - 2 href = substr($0, astart + RSTART + 1, RLENGTH - 3) title = "" if (match(href, /"[^"]*"[[:blank:]]*$/)) { title = " title=" substr(href, RSTART, RLENGTH) href = substr(href, 1, RSTART - 1) } $0 = substr($0, 1, astart - 1) "" inner_anchor "" substr($0, aend + 1) } } # Emphasis function surround(brace, opening, closing) { while ($0 ~ brace ".*" brace) { sub(brace, opening) sub(brace, closing) } } { surround("__", "", "") surround("_", "", "") surround("\\*\\*", "", "") surround("\\*", "", "") surround("~~", "" $0 " ") blankline = 1 next } } # setex-style headers !prev_blankline && /^[[:blank:]]*(=|-)+[[:blank:]]*$/ { sub(/^/, "", prev) hlevel = 1 + ($0 ~ /-/) print "
" prev " " prev = "" blankline = 1 skipprint = 1 next } # Horizontal rules /^(-|\*|_) ?(-|\*|_) ?(-|\*|_|[[:blank:]])+$/ { println("
") blankline = 1 next } # Line break / $/ { sub(/[[:blank:]]* $/, "") } # Code blocks /^( |\t)[[:blank:]]*/ { } # Unordered lists function abs(x) { return ((x < 0) ? -x : x) } !blankline && lengthStack("list-level") > 0 { match($0, /^ */) indent = RLENGTH # Compensating factor for list items match($0, /(\*|\+|-|[[:digit:]]+\.) /) if (RLENGTH == -1) RLENGTH = 0 while (indent + RLENGTH < abs(topStack("list-level"))) { if (topStack("list-level") < 0) println("") else println("") popStack("list-level") } } /^[[:blank:]]*(\*|\+|-|[[:digit:]]+\.) / { match($0, /(\*|\+|-|[[:digit:]]+\.) /) if (abs(topStack("list-level")) < RSTART) { if (RLENGTH > 2) { xl = ""; mult = -1 } else { xl = "
"; mult = 1 } println(xl "
- ") pushStack("list-level", mult * (RSTART + RLENGTH - 1)) } else { println("
- ") } $0 = substr($0, RSTART + RLENGTH) } # Paragraphs prev_blankline && !blankline && lengthStack("bq-level") == 0 && lengthStack("list-level") == 0 { $0 = "
" $0 } !prev_blankline && blankline && lengthStack("bq-level") == 0 && lengthStack("list-level") == 0 { prev = prev "
" } # Blockquote Part 2/2 blankline { while (lengthStack("bq-level") > 0) { println("
", "")
surround("`", "", "")
}
!prev_blankline && /^: / {
sub(/^/, "", prev) sub(/^: /, "") print "