# Status: # [.] Inline HTML # [ ] Autoescape # [.] Block elements # [x] Paragraph # [x] Line break # [x] Headers (atx and setex) # [x] Blockquotes # [ ] Lists # [x] Code blocks # [x] Horizontal rules # [x] Span elements # [x] Links # [x] Inline # [x] Reference # [x] Emphasis # [x] Code # [x] Images # [x] Inline # [x] Reference # [.] Other # [x] Autolinks # [ ] Escapes # # [ ] Non-standard features # [ ] Strikethrough ~~...~~ # [ ] Superscript ^...^ # [ ] Subscript ~...~ # [ ] Footnote [^...] (with user customizations) # [ ] Task list - [ ] (readonly) # [ ] Definition lists # [x] Single line # [ ] Multi-line # [ ] Fenced code blocks ```lang ``` # [ ] Tables # [ ] Links # [ ] Link destination ( inside anchors) # [ ] Nested links # No character classes, since they aren't supported on every system # # Limitations: # - definition lists always contain one element, i.e. multiple definitions # aren't merged into the same list # - emphasis which isn't surrounded by blanks is still treated as emphasis # e.g. "some_text_here" will become "sometexthere" # - emphasis inside inline code block is still converted to HTML # - backticks inside `` emphasis blocks aren't escaped function empty(value) { return value == "" } function _next() { __buffered_print() not_paragraph = 1 next } function char(string, ind) { if (1 > ind || ind > length(string)) { return ""; } return substr(string, ind, 1) } function backup_match() { backup_rstart = RSTART backup_rlength = RLENGTH } function restore_match() { RSTART = backup_rstart RLENGTH = backup_rlength } BEGIN { ## This is used to convert email autolinks into a series of hex values ## Escaped output of: awk 'BEGIN {for (i = 32; i <= 126; ++i){ printf("%c", i) } }' ascii = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" for (i = 1; i < 97; ++i) { char_to_dec[char(ascii, i)] = 31 + i } } { not_paragraph_p1 = not_paragraph not_paragraph = 0 empty0 = empty($0) emptyP1 = empty(P1) } # Output is buffered on multiple lines, held in these variables: # P2 = previous non-empty line # P1 = previous line # $0 = current line # N1 = next new line (N1 is always set by the user) # Inline HTML /^[ \t]*<\/?[0-9A-Za-z]+/ { _next() } # Link definition !inside_codeblock && /^\[[^\]]+\]:[ \t]+.*/ { next } # Automatic links /<[^ \t<>]+>/ { ## URL: while (match($0, /<[A-Za-z][-A-Za-z0-9+.]*:[^ \t<>]+>/)) { href = substr($0, RSTART + 1, RLENGTH - 2) sub_match("" href "") } ## Email addresses, with support for quoted local part "..."@... ## and ip address literal ...@[...] while (match($0, /<([^ \t<>]+|".+")@([-A-Za-z0-9]+\.[^ \t<>]+|\[[0-9A-Fa-f.:]+\])>/)) { href = substr($0, RSTART + 1, RLENGTH - 2) href_masked = "" ## The original markdown blogpost calls for convertions of all ## email characters into their hex values. Allegedly, this fools ## many autoharvesting spam bots. for (i = length(href); i > 0; --i) { href_masked = sprintf("&#x%X;", char_to_dec[char(href, i)]) href_masked } ## href="mailto:..." sub_match("" href_masked "") } } # Blockquotes blockquote_level > 0 && empty0 { for (P1 = P1 ORS; blockquote_level > 0; --blockquote_level) { P1 = P1 "" } not_paragraph_p1 = 1 } /^>[ \t]/ { match($0, /^[> \t]+[ \t]+/) level = substr($0, RSTART, RLENGTH) $0 = substr($0, RSTART + RLENGTH) gsub(/[ \t]/, "", level) level = length(level) for (;blockquote_level < level; ++blockquote_level) { P1 = P1 "
" } if (blockquote_level > level) { P2 = P2 ORS } for (;blockquote_level > level; --blockquote_level) { P2 = P2 "
" } not_paragraph = 1 } /^>$/ { $0 = ""; not_paragraph = 1 } # Code blocks /^ / { if (!inside_codeblock) { P1 = P1 "
"
		inside_codeblock = 1
	}
	_next()
}
inside_codeblock && ! /^[ \t]*$/ {
	if (emptyP1) {
		P2 = P2 ORS "
" } else { P1 = P1 ORS "" } inside_codeblock = 0 emptyP1 = 1 } # Line break / $/ { sub(/[ \t]* $/, "
") } # atx-style headers /^[ \t]*#+/ { match($0, /#+/) if (RLENGTH <= 6) { ## Remove prefix/suffix gsub(/^[ \t]*#+[ \t]*/, "") gsub(/[ \t]*#+[ \t]*$/, "") $0 = "" $0 "" not_paragraph = 1 } } # setex-style headers !emptyP1 && /^[ \t]*(=|-)+[ \t]*$/ { hlevel = 1 + ($0 ~ /-/) P1 = "" substr(P1, 4) "" $0 = "" not_paragraph = 1 } # Horizontal rule /^(---+|- - -( -)*|\*\*\*+|\* \* \*( \*)*|___+|_ _ _( _)*)$/ { $0 = "
" not_paragraph = 1 } # Emphasis function sub_match(replacement) { $0 = substr($0, 1, RSTART - 1) replacement substr($0, RSTART + RLENGTH) } function surround(brace, left, right, blen) { if (blen == "") { blen = length(brace) } if (match($0, brace (brace == "``" ? "." : "[^" brace "]") "+" brace)) { elem = left substr($0, RSTART + blen, RLENGTH - 2*blen) right if (brace == "``") { gsub("`", "\\`", elem) } sub_match(elem) return 1; } return 0 } function gsurround(brace, left, right, blen) { while (surround(brace, left, right, blen)) {} if (blen == 1) { gsub("\\\\", "", brace) ## Surrounding a single character span with spaces should make it a ## literal char. This is inelegant but functional. gsub(" " left " ", " " brace " ") gsub(" " right " ", " " brace " ") } } { ## Make sure is on the inside for long chains ## E.g. *****a***** will be the same as ****_a_**** surround("___" , "", "") surround("\\*\\*\\*", "", "", 3) gsurround("__" , "", "") gsurround("_" , "" , "") gsurround("\\*\\*", "", "", 2) gsurround("\\*" , "" , "", 1) gsurround("``" , "" , "") gsurround("`" , "" , "") } # Links /!?\[[^\]]*\] ?[(\[][^)\]]*[\])]/ { while (match($0, /!?\[[^\]]*\] ?[(\[][^)\]]*[\])]/)) { link = substr($0, RSTART, RLENGTH) backup_match() ## ](xxxxx) match(link, /\] ?[(\[][^)\]]*[\])]$/) has_space = char(link, RSTART + 1) == " " is_img = char(link, 1) == "!" ## ...[xxxxx](...)... text = substr(link, 2 + is_img, RSTART - 2 - is_img) ## ...[...](xxxxx)... href = substr(link, RSTART + has_space + 2, RLENGTH - has_space - 3) title = "\"\"" if (char(link, RSTART + RLENGTH - 1) == "]") { ## is reference link if (href == "") href = text # [...][] ## [...]: ... "xxxxx" title = reference[href, "title"] ## [...]: xxxxx "..." href = reference[href, "href"] } else if (match(href, /[ \t]"[^"]*"[ \t]*$/)) { ## has title ## ...[...](... "xxxxx")... title = substr(href, RSTART + 1, RLENGTH - 1) ## ...[...](xxxxx "...")... href = substr(href, 1, RSTART - 1) } if (is_img) elem = "\""" else elem = "" text "" restore_match() sub_match(elem) } } # Paragraph emptyP1 && !empty0 && !not_paragraph { $0 = "

" $0 } !emptyP1 && empty0 && !not_paragraph_p1 { P1 = P1 "

" } # After all $0 parsing { __buffered_print() } function __buffered_print() { empty0 = empty($0) emptyP1 = empty(P1) if (empty0 && emptyP1) { ++P1_repeats } else if (!empty0 && emptyP1) { P1 = $0 } else { print P2 while (P1_repeats > 0) { print "" --P1_repeats } P2 = P1 P1 = $0 if (empty0) { P1_repeats = 1 } } if (!empty(N1)) { if (emptyP1) { P1 = N1 } else { print P2 while (P1_repeats > 0) { print "" --P1_repeats } P2 = P1 P1 = N1 N1 = "" } } } END { print P2 while (P1_repeats > 0) { print "" --P1_repeats } if (!empty(P1)) { print P1 } print if (!empty(N1)) { print N1 } }