From b6f82bc119dd614862f61df3b1978cc5789ab31a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Fri, 30 Aug 2024 16:30:00 +0200 Subject: [PATCH] avoid recursion in _block function to increase compatibility --- markdown.awk | 756 ++++++++++++++++++++++++++------------------------- 1 file changed, 393 insertions(+), 363 deletions(-) diff --git a/markdown.awk b/markdown.awk index d1c8b5b..fc2f203 100755 --- a/markdown.awk +++ b/markdown.awk @@ -413,408 +413,438 @@ function _nblock( block, LOCAL, sec, n ) { return block sec; } -function _block( block, LOCAL, st, len, text, title, attrib, href, guard, code, indent, list, tmp ) { - gsub( "(^\n+|\n+$)", "", block ); - - if ( block == "" ) { - return ""; - - # HTML #2 #3 #4 $5 - } else if ( AllowHTML && match( block, /(^|\n) ? ? ?(|$)|<\?([^\?]|\?[^>])*(\?>|$)|]*(>|$)|])*(\]\]>|$))/) ) { - len = RLENGTH; st = RSTART; - return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len)); - - # HTML #6 - } else if ( AllowHTML && match( tolower(block), /(^|\n) ? ? ?<\/?(address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[123456]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)([[:space:]\n>]|\/>)([^\n]|\n[ \t]*[^\n])*(\n[[:space:]]*\n|$)/) ) { - len = RLENGTH; st = RSTART; - return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len)); - - # HTML #1 - } else if ( AllowHTML && match( tolower(block), /(^|\n) ? ? ?<(script|pre|style)([[:space:]\n>]).*(<\/script>|<\/pre>|<\/style>|$)/) ) { - len = RLENGTH; st = RSTART; - match( tolower(substr(block, st, len)), /(<\/script>|<\/pre>|<\/style>)/); - len = RSTART + RLENGTH; - return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len)); - - # HTML #7 - } else if ( AllowHTML && match( block, /^ ? ? ?(<\/[A-Za-z][A-Za-z0-9-]*[[:space:]]*>|<[A-Za-z][A-Za-z0-9-]*([[:space:]]+[A-Za-z_:][A-Za-z0-9_\.:-]*([[:space:]]*=[[:space:]]*([[:space:]"'=<>`]+|"[^"]*"|'[^']*'))?)*[[:space:]]*\/?>)([[:space:]]*\n)([^\n]|\n[ \t]*[^\n])*(\n[[:space:]]*\n|$)/) ) { - len = RLENGTH; st = RSTART; - return substr(block, st, len) _block(substr(block, st + len)); - - # Metadata (custom, block starting with %something) - # Metadata is ignored but can be interpreted externally - } else if ( match(block, /^%[a-zA-Z-]+([[:space:]][^\n]*)?(\n|$)(%[a-zA-Z-]+([[:space:]][^\n]*)?(\n|$)|%([[:space:]][^\n]*)?(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) { - len = RLENGTH; st = RSTART; - return _block( substr( block, len + 1) ); +function _block( block, LOCAL, st, len, text, title, attrib, href, guard, code, indent, list, tmp, ret) { + ret = ""; + while ( block != "" ) { + gsub( "(^\n+|\n+$)", "", block ); + + # HTML #2 #3 #4 $5 + if ( AllowHTML && match( block, /(^|\n) ? ? ?(|$)|<\?([^\?]|\?[^>])*(\?>|$)|]*(>|$)|])*(\]\]>|$))/) ) { + len = RLENGTH; st = RSTART; + ret = ret _block(substr(block, 1, st - 1)) substr(block, st, len); block = substr(block, st + len); + continue; + + # HTML #6 + } else if ( AllowHTML && match( tolower(block), /(^|\n) ? ? ?<\/?(address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[123456]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)([[:space:]\n>]|\/>)([^\n]|\n[ \t]*[^\n])*(\n[[:space:]]*\n|$)/) ) { + len = RLENGTH; st = RSTART; + ret = ret _block(substr(block, 1, st - 1)) substr(block, st, len); block = substr(block, st + len); + continue; + + # HTML #1 + } else if ( AllowHTML && match( tolower(block), /(^|\n) ? ? ?<(script|pre|style)([[:space:]\n>]).*(<\/script>|<\/pre>|<\/style>|$)/) ) { + len = RLENGTH; st = RSTART; + match( tolower(substr(block, st, len)), /(<\/script>|<\/pre>|<\/style>)/); + len = RSTART + RLENGTH; + ret = ret _block(substr(block, 1, st - 1)) substr(block, st, len); block = substr(block, st + len); + continue; + + # HTML #7 + } else if ( AllowHTML && match( block, /^ ? ? ?(<\/[A-Za-z][A-Za-z0-9-]*[[:space:]]*>|<[A-Za-z][A-Za-z0-9-]*([[:space:]]+[A-Za-z_:][A-Za-z0-9_\.:-]*([[:space:]]*=[[:space:]]*([[:space:]"'=<>`]+|"[^"]*"|'[^']*'))?)*[[:space:]]*\/?>)([[:space:]]*\n)([^\n]|\n[ \t]*[^\n])*(\n[[:space:]]*\n|$)/) ) { + len = RLENGTH; st = RSTART; + ret = ret substr(block, st, len); block = substr(block, st + len); + continue; + + # Metadata (custom, block starting with %something) + # Metadata is ignored but can be interpreted externally + } else if ( match(block, /^%[a-zA-Z-]+([[:space:]][^\n]*)?(\n|$)(%[a-zA-Z-]+([[:space:]][^\n]*)?(\n|$)|%([[:space:]][^\n]*)?(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) { + len = RLENGTH; st = RSTART; + block = substr( block, len + 1); + continue; - # Blockquote (leading >) - } else if ( match( block, /^> /) ) { - match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match(block, /$/); - len = RLENGTH; st = RSTART; - text = substr(block, 1, st - 1); gsub( /(^|\n)> /, "\n", text ); - text = _nblock( text ); gsub( /^\n|\n$/, "", text ) - return "
" text "
\n\n" _block( substr(block, st + len) ); - - # Pipe Tables (pandoc / php md / gfm ) - } else if ( match(block, "^((\\|)?([^\n]+\\|)+[^\n]+(\\|)?)\n" \ - "((\\|)?(:?-+:?[\\|+])+:?-+:?(\\|)?)\n" \ - "((\\|)?([^\n]+\\|)+[^\n]+(\\|)?(\n|$))+" ) ) { - len = RLENGTH; st = RSTART; - #initialize empty arrays - split("", talign); split("", tarray); - cols = 0; cnt=0; ttext = ""; - - # table header and alignment - tmp = substr(block, 1, match(block, /(\n|$)/)); - gsub( /(^|[^\\])\\\|/, "\\1\\|", tmp ); - gsub( /(^\||\|$)/, "", tmp) - split( tmp, tarray, /\|/); - block = substr(block, match(block, /(\n|$)/) + 1 ); - tmp = substr(block, 1, match(block, /(\n|$)/)); - gsub( /(^\||\|$)/, "", tmp ); - cols = split( tmp , talign, /[+\|]/); - block = substr(block, match(block, /(\n|$)/) + 1 ); - - for( cnt = 1; cnt < cols; cnt++ ) { - if (match(talign[cnt], /:-+:/)) talign[cnt]="center"; - else if (match(talign[cnt], /-+:/)) talign[cnt]="right"; - else if (match(talign[cnt], /:-+/)) talign[cnt]="left"; - else talign[cnt]=""; - } + # Blockquote (leading >) + } else if ( match( block, /^> /) ) { + match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match(block, /$/); + len = RLENGTH; st = RSTART; + text = substr(block, 1, st - 1); gsub( /(^|\n)> /, "\n", text ); + text = _nblock( text ); gsub( /^\n|\n$/, "", text ) + ret = ret "
" text "
\n\n"; block = substr(block, st + len); + continue; - ttext = "\n" - for (cnt = 1; cnt < cols; cnt++) - ttext = ttext "" inline(tarray[cnt]) "" - ttext = ttext "\n\n" + # Pipe Tables (pandoc / php md / gfm ) + } else if ( match(block, "^((\\|)?([^\n]+\\|)+[^\n]+(\\|)?)\n" \ + "((\\|)?(:?-+:?[\\|+])+:?-+:?(\\|)?)\n" \ + "((\\|)?([^\n]+\\|)+[^\n]+(\\|)?(\n|$))+" ) ) { + len = RLENGTH; st = RSTART; + #initialize empty arrays + split("", talign); split("", tarray); + cols = 0; cnt=0; ttext = ""; - while ( match(block, "^((\\|)?([^\n]+\\|)+[^\n]+(\\|)?(\n|$))+" ) ){ + # table header and alignment tmp = substr(block, 1, match(block, /(\n|$)/)); gsub( /(^|[^\\])\\\|/, "\\1\\|", tmp ); - gsub( /(^\||\|$)/, "", tmp ); + gsub( /(^\||\|$)/, "", tmp) split( tmp, tarray, /\|/); block = substr(block, match(block, /(\n|$)/) + 1 ); + tmp = substr(block, 1, match(block, /(\n|$)/)); + gsub( /(^\||\|$)/, "", tmp ); + cols = split( tmp , talign, /[+\|]/); + block = substr(block, match(block, /(\n|$)/) + 1 ); + + for( cnt = 1; cnt < cols; cnt++ ) { + if (match(talign[cnt], /:-+:/)) talign[cnt]="center"; + else if (match(talign[cnt], /-+:/)) talign[cnt]="right"; + else if (match(talign[cnt], /:-+/)) talign[cnt]="left"; + else talign[cnt]=""; + } - ttext = ttext "" + ttext = "\n" for (cnt = 1; cnt < cols; cnt++) - ttext = ttext "" inline(tarray[cnt]) "" - ttext = ttext "\n" - } - return "" ttext "
\n" _block(block); - - # Grid Tables (pandoc) - # (with, and without header) - } else if ( match( block, "^\\+(-+\\+)+\n" \ - "(\\|([^\n]+\\|)+\n)+" \ - "(\\+(:?=+:?\\+)+)\n" \ - "((\\|([^\n]+\\|)+\n)+" \ - "\\+(-+\\+)+(\n|$))+" \ - ) || \ - match( block, "^()()()" \ - "(\\+(:?-+:?\\+)+)\n" \ - "((\\|([^\n]+\\|)+\n)+" \ - "\\+(-+\\+)+(\n|$))+" \ - ) ) { - len = RLENGTH; st = RSTART; - #initialize empty arrays - split("", talign); split("", tarray); split("", tread); - cols = 0; cnt=0; ttext = ""; - - # Column Count - tmp = block; sub( "(\n.*)*$", "", tmp); - cols = split( tmp, tread, /\+/) - 2; - # debug(" Cols: " gensub( "^(\\+(:?-+:?\\+)+)(\n.*)*$", "\\1", 1, block )); - - # table alignment - match(block, "((:?=+:?\\+|(:-+|-+:|:-+:)\\+)+)"); - split( substr(block, RSTART, RLENGTH) , talign, /\+/ ); - # split( gensub( "^(.*\n)?\\+((:?=+:?\\+|(:-+|-+:|:-+:)\\+)+)(\n.*)$", "\\2", "g", block ), talign, /\+/ ); - # debug("Align: " gensub( "^(.*\n)?\\+((:?=+:?\\+|(:-+|-+:|:-+:)\\+)+)(\n.*)$", "\\2", "g", block )); - - for (cnt = 1; cnt <= cols; cnt++) { - if (match(talign[cnt], /:(-+|=+):/)) talign[cnt]="center"; - else if (match(talign[cnt], /(-+|=+):/)) talign[cnt]="right"; - else if (match(talign[cnt], /:(-+|=+)/ )) talign[cnt]="left"; - else talign[cnt]=""; - } + ttext = ttext "" inline(tarray[cnt]) "" + ttext = ttext "\n\n" - if ( match(block, "^\\+(-+\\+)+\n" \ - "(\\|([^\n]+\\|)+\n)+" \ - "\\+(:?=+:?\\+)+\n" \ - "((\\|([^\n]+\\|)+\n)+" \ - "\\+(-+\\+)+(\n|$))+" \ - ) ) { - # table header - block = substr(block, match(block, /(\n|$)/) + 1 ); - while ( match(block, "^\\|([^\n]+\\|)+\n") ) { + while ( match(block, "^((\\|)?([^\n]+\\|)+[^\n]+(\\|)?(\n|$))+" ) ){ tmp = substr(block, 1, match(block, /(\n|$)/)); - gsub( /\\\\/, "\\\", tmp); gsub(/\\\|/, "\\|", tmp); + gsub( /(^|[^\\])\\\|/, "\\1\\|", tmp ); gsub( /(^\||\|$)/, "", tmp ); - split(tmp, tread, /\|/); + split( tmp, tarray, /\|/); block = substr(block, match(block, /(\n|$)/) + 1 ); - for (cnt = 1; cnt <= cols; cnt++) - tarray[cnt] = tarray[cnt] "\n" tread[cnt]; - } - ttext = "\n" - for (cnt = 1; cnt <= cols; cnt++) - ttext = ttext "" _nblock(tarray[cnt]) "" - ttext = ttext "\n" - } + ttext = ttext "" + for (cnt = 1; cnt < cols; cnt++) + ttext = ttext "" inline(tarray[cnt]) "" + ttext = ttext "\n" + } + ret = ret "" ttext "
\n"; + continue; - # table body - block = substr(block, match(block, /(\n|$)/) + 1 ); - ttext = ttext "\n" + # Grid Tables (pandoc) + # (with, and without header) + } else if ( match( block, "^\\+(-+\\+)+\n" \ + "(\\|([^\n]+\\|)+\n)+" \ + "(\\+(:?=+:?\\+)+)\n" \ + "((\\|([^\n]+\\|)+\n)+" \ + "\\+(-+\\+)+(\n|$))+" \ + ) || \ + match( block, "^()()()" \ + "(\\+(:?-+:?\\+)+)\n" \ + "((\\|([^\n]+\\|)+\n)+" \ + "\\+(-+\\+)+(\n|$))+" \ + ) ) { + len = RLENGTH; st = RSTART; + #initialize empty arrays + split("", talign); split("", tarray); split("", tread); + cols = 0; cnt=0; ttext = ""; + + # Column Count + tmp = block; sub( "(\n.*)*$", "", tmp); + cols = split( tmp, tread, /\+/) - 2; + # debug(" Cols: " gensub( "^(\\+(:?-+:?\\+)+)(\n.*)*$", "\\1", 1, block )); + + # table alignment + match(block, "((:?=+:?\\+|(:-+|-+:|:-+:)\\+)+)"); + split( substr(block, RSTART, RLENGTH) , talign, /\+/ ); + # split( gensub( "^(.*\n)?\\+((:?=+:?\\+|(:-+|-+:|:-+:)\\+)+)(\n.*)$", "\\2", "g", block ), talign, /\+/ ); + # debug("Align: " gensub( "^(.*\n)?\\+((:?=+:?\\+|(:-+|-+:|:-+:)\\+)+)(\n.*)$", "\\2", "g", block )); + + for (cnt = 1; cnt <= cols; cnt++) { + if (match(talign[cnt], /:(-+|=+):/)) talign[cnt]="center"; + else if (match(talign[cnt], /(-+|=+):/)) talign[cnt]="right"; + else if (match(talign[cnt], /:(-+|=+)/ )) talign[cnt]="left"; + else talign[cnt]=""; + } - while ( match(block, /^((\|([^\n]+\|)+\n)+\+(-+\+)+(\n|$))+/ ) ){ - split("", tarray); - while ( match(block, /^\|([^\n]+\|)+\n/) ) { - tmp = substr(block, 1, match(block, /(\n|$)/)); - gsub( /\\\\/, "\\\", tmp); gsub(/\\\|/, "\\|", tmp); - gsub( /(^\||\|$)/, "", tmp); - split( tmp, tread, /\|/); + if ( match(block, "^\\+(-+\\+)+\n" \ + "(\\|([^\n]+\\|)+\n)+" \ + "\\+(:?=+:?\\+)+\n" \ + "((\\|([^\n]+\\|)+\n)+" \ + "\\+(-+\\+)+(\n|$))+" \ + ) ) { + # table header block = substr(block, match(block, /(\n|$)/) + 1 ); + while ( match(block, "^\\|([^\n]+\\|)+\n") ) { + tmp = substr(block, 1, match(block, /(\n|$)/)); + gsub( /\\\\/, "\\\", tmp); gsub(/\\\|/, "\\|", tmp); + gsub( /(^\||\|$)/, "", tmp ); + split(tmp, tread, /\|/); + block = substr(block, match(block, /(\n|$)/) + 1 ); + for (cnt = 1; cnt <= cols; cnt++) + tarray[cnt] = tarray[cnt] "\n" tread[cnt]; + } + + ttext = "\n" for (cnt = 1; cnt <= cols; cnt++) - tarray[cnt] = tarray[cnt] "\n" tread[cnt]; + ttext = ttext "" _nblock(tarray[cnt]) "" + ttext = ttext "\n" } + + # table body block = substr(block, match(block, /(\n|$)/) + 1 ); + ttext = ttext "\n" + + while ( match(block, /^((\|([^\n]+\|)+\n)+\+(-+\+)+(\n|$))+/ ) ){ + split("", tarray); + while ( match(block, /^\|([^\n]+\|)+\n/) ) { + tmp = substr(block, 1, match(block, /(\n|$)/)); + gsub( /\\\\/, "\\\", tmp); gsub(/\\\|/, "\\|", tmp); + gsub( /(^\||\|$)/, "", tmp); + split( tmp, tread, /\|/); + block = substr(block, match(block, /(\n|$)/) + 1 ); + for (cnt = 1; cnt <= cols; cnt++) + tarray[cnt] = tarray[cnt] "\n" tread[cnt]; + } + block = substr(block, match(block, /(\n|$)/) + 1 ); - ttext = ttext "" - for (cnt = 1; cnt <= cols; cnt++) - ttext = ttext "" _nblock(tarray[cnt]) "" - ttext = ttext "\n" - } - return "" ttext "
\n" _nblock(block); - - # Line Blocks (pandoc) - } else if ( match(block, /^\| [^\n]*(\n|$)(\| [^\n]*(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) { - len = RLENGTH; st = RSTART; - - text = substr(block, 1, len); gsub(/\n[[:space:]]+/, " ", text); - gsub(/\n\| /, "\n", text); gsub(/^\| |\n$/, "", text); - text = inline(text); gsub(/\n/, "
\n", text); - - return "
" text "
\n" _block( substr( block, len + 1) ); - - # Indented Code Block - } else if ( match(block, /^(( |\t)[^\n]*[^\n\t ][^\n]*(\n|$))(( |\t)[^\n]*(\n|$)|[\t ]*(\n|$))*/) ) { - len = RLENGTH; st = RSTART; - - code = substr(block, 1, len); - gsub(/(^|\n)( |\t)/, "\n", code); - gsub(/^\n|\n+$/, "", code); - return "
" HTML( code ) "
\n" \ - _block( substr( block, len + 1 ) ); - - # Fenced Divs (pandoc, custom) - } else if ( match( block, /^(:::+)/ ) ) { - guard = substr( block, 1, RLENGTH ); attrib = code = block; - sub(/^[^\n]+\n/, "", code); - sub(/^:::+[ \t]*\{?[ \t]*/, "", attrib); sub(/\}?[ \t]*\n.*$/, "", attrib); - # attrib = gensub(/^:::+[ \t]*\{?[ \t]*([^\}\n]*)\}?[ \t]*\n.*$/, "\\1", 1, attrib); - gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); - gsub(/(^ | $)/, "", attrib); - if ( match(code, "(^|\n)" guard "+(\n|$)" ) && attrib ) { - len = RLENGTH; st = RSTART; - return "
" _nblock( substr(code, 1, st - 1) ) "
\n" \ - _block( substr( code, st + len ) ); - } else if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) { - len = RLENGTH; st = RSTART; - return "
" _nblock( substr(code, 1, st - 1) ) "
\n" \ - _block( substr( code, st + len ) ); - } else { - match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ ); - len = RLENGTH; st = RSTART; - return "

" inline( substr(block, 1, st - 1) ) "

\n" \ - _block( substr(block, st + len) ); - } + ttext = ttext "" + for (cnt = 1; cnt <= cols; cnt++) + ttext = ttext "" _nblock(tarray[cnt]) "" + ttext = ttext "\n" + } + return ret "" ttext "
\n" _nblock(block); - # Fenced Code Block (pandoc) - } else if ( match( block, /^(~~~+|```+)/ ) ) { - guard = substr( block, 1, RLENGTH ); attrib = code = block; - sub(/^[^\n]+\n/, "", code); - sub(/^(~~~+|```+)[ \t]*\{?[ \t]*/, "", attrib); sub(/\}?[ \t]*\n.*$/, "", attrib); - # attrib = gensub(/^(~~~+|```+)[ \t]*\{?[ \t]*([^\}\n]*)\}?[ \t]*\n.*$/, "\\2", 1, attrib); - gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); - gsub(/(^ | $)/, "", attrib); - if ( match(code, "(^|\n)" guard "+(\n|$)" ) && attrib ) { + # Line Blocks (pandoc) + } else if ( match(block, /^\| [^\n]*(\n|$)(\| [^\n]*(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) { len = RLENGTH; st = RSTART; - return "
" HTML( substr(code, 1, st - 1) ) "
\n" \ - _block( substr( code, st + len ) ); - } else if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) { - len = RLENGTH; st = RSTART; - return "
" HTML( substr(code, 1, st - 1) ) "
\n" \ - _block( substr( code, st + len ) ); - } else { - match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ ); + + text = substr(block, 1, len); gsub(/\n[[:space:]]+/, " ", text); + gsub(/\n\| /, "\n", text); gsub(/^\| |\n$/, "", text); + text = inline(text); gsub(/\n/, "
\n", text); + + ret = ret "
" text "
\n"; block = substr( block, len + 1); + continue; + + # Indented Code Block + } else if ( match(block, /^(( |\t)[^\n]*[^\n\t ][^\n]*(\n|$))(( |\t)[^\n]*(\n|$)|[\t ]*(\n|$))*/) ) { len = RLENGTH; st = RSTART; - return "

" inline( substr(block, 1, st - 1) ) "

\n" \ - _block( substr(block, st + len) ); - } - # First Order Heading H1 + Attrib - } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n|$)/ ) ) { - len = RLENGTH; text = attrib = block; - sub(/([ \t]*\{([^\}\n]+)\})\n===+(\n.*)?$/, "", text); - sub(/\}\n===+(\n.*)?$/, "", attrib); sub(/^([^\n]+)[ \t]*\{/, "", attrib); - gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib); + code = substr(block, 1, len); + gsub(/(^|\n)( |\t)/, "\n", code); + gsub(/^\n|\n+$/, "", code); + ret = ret "
" HTML( code ) "
\n"; block = substr( block, len + 1 ); + continue; - return headline(1, text, attrib) _block( substr( block, len + 1 ) ); + # Fenced Divs (pandoc, custom) + } else if ( match( block, /^(:::+)/ ) ) { + guard = substr( block, 1, RLENGTH ); attrib = code = block; + sub(/^[^\n]+\n/, "", code); + sub(/^:::+[ \t]*\{?[ \t]*/, "", attrib); sub(/\}?[ \t]*\n.*$/, "", attrib); + # attrib = gensub(/^:::+[ \t]*\{?[ \t]*([^\}\n]*)\}?[ \t]*\n.*$/, "\\1", 1, attrib); + gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); + gsub(/(^ | $)/, "", attrib); + if ( match(code, "(^|\n)" guard "+(\n|$)" ) && attrib ) { + len = RLENGTH; st = RSTART; + ret = ret "
" _nblock( substr(code, 1, st - 1) ) "
\n"; + block = substr( code, st + len ); + continue; - # First Order Heading H1 - } else if ( match( block, /^([^\n]+)\n===+(\n|$)/ ) ) { - len = RLENGTH; text = substr(block, 1, len); - sub(/\n===+(\n.*)?$/, "", text); + } else if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) { + len = RLENGTH; st = RSTART; + ret = ret "
" _nblock( substr(code, 1, st - 1) ) "
\n"; block = substr( code, st + len ); + continue; + + } else { + match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ ); + len = RLENGTH; st = RSTART; + ret = ret "

" inline( substr(block, 1, st - 1) ) "

\n"; block = substr(block, st + len); + continue; + } - return headline(1, text, 0) _block( substr( block, len + 1 ) ); + # Fenced Code Block (pandoc) + } else if ( match( block, /^(~~~+|```+)/ ) ) { + guard = substr( block, 1, RLENGTH ); attrib = code = block; + sub(/^[^\n]+\n/, "", code); + sub(/^(~~~+|```+)[ \t]*\{?[ \t]*/, "", attrib); sub(/\}?[ \t]*\n.*$/, "", attrib); + # attrib = gensub(/^(~~~+|```+)[ \t]*\{?[ \t]*([^\}\n]*)\}?[ \t]*\n.*$/, "\\2", 1, attrib); + gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); + gsub(/(^ | $)/, "", attrib); + if ( match(code, "(^|\n)" guard "+(\n|$)" ) && attrib ) { + len = RLENGTH; st = RSTART; + ret = ret "
" HTML( substr(code, 1, st - 1) ) "
\n"; + block = substr( code, st + len ); + continue; - # Second Order Heading H2 + Attrib - } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n---+(\n|$)/ ) ) { - len = RLENGTH; text = attrib = block; - sub(/([ \t]*\{([^\}\n]+)\})\n---+(\n.*)?$/, "", text); - sub(/\}\n---+(\n.*)?$/, "", attrib); sub(/^([^\n]+)[ \t]*\{/, "", attrib); - gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib); + } else if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) { + len = RLENGTH; st = RSTART; + ret = ret "
" HTML( substr(code, 1, st - 1) ) "
\n"; + block = substr( code, st + len ); + continue; - return headline(2, text, attrib) _block( substr( block, len + 1) ); + } else { + match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ ); + len = RLENGTH; st = RSTART; + ret = ret "

" inline( substr(block, 1, st - 1) ) "

\n"; block = substr(block, st + len); + continue; + } - # Second Order Heading H2 - } else if ( match( block, /^([^\n]+)\n---+(\n|$)/ ) ) { - len = RLENGTH; text = substr(block, 1, len); - sub(/\n---+(\n.*)?$/, "", text); + # First Order Heading H1 + Attrib + } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n|$)/ ) ) { + len = RLENGTH; text = attrib = block; + sub(/([ \t]*\{([^\}\n]+)\})\n===+(\n.*)?$/, "", text); + sub(/\}\n===+(\n.*)?$/, "", attrib); sub(/^([^\n]+)[ \t]*\{/, "", attrib); + gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib); - return headline(2, text, 0) _block( substr( block, len + 1) ); + ret = ret headline(1, text, attrib) ; block = substr( block, len + 1 ); + continue; - # Nth Order Heading H1 H2 H3 H4 H5 H6 + Attrib - } else if ( match( block, /^(##?#?#?#?#?)[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n|$)/ ) ) { - len = RLENGTH; text = attrib = substr(block, 1, len); - match(block, /^##?#?#?#?#?[^#]/); n = RLENGTH - 1; + # First Order Heading H1 + } else if ( match( block, /^([^\n]+)\n===+(\n|$)/ ) ) { + len = RLENGTH; text = substr(block, 1, len); + sub(/\n===+(\n.*)?$/, "", text); - # sub(/^(##?#?#?#?#?)[ \t]*/, "", text); # not working in mawk - text = substr(text, n + 1); sub(/^[ \t]*/, "", text); - sub(/[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "", text); - sub(/^(##?#?#?#?#?)[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*[ \t]*\{/, "", attrib); - sub(/\}(\n.*)?$/, "", attrib); - gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib); + ret = ret headline(1, text, 0) ; block = substr( block, len + 1 ); + continue; - return headline( n, text, attrib ) _block( substr( block, len + 1) ); + # Second Order Heading H2 + Attrib + } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n---+(\n|$)/ ) ) { + len = RLENGTH; text = attrib = block; + sub(/([ \t]*\{([^\}\n]+)\})\n---+(\n.*)?$/, "", text); + sub(/\}\n---+(\n.*)?$/, "", attrib); sub(/^([^\n]+)[ \t]*\{/, "", attrib); + gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib); - # Nth Order Heading H1 H2 H3 H4 H5 H6 - } else if ( match( block, /^(##?#?#?#?#?)[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*(\n|$)/ ) ) { - len = RLENGTH; text = substr(block, 1, len); - match(block, /^##?#?#?#?#?[^#]/); n = RLENGTH - 1; - # sub(/^(##?#?#?#?#?)[ \t]+/, "", text); # not working in mawk - text = substr(text, n + 1); sub(/^[ \t]*/, "", text); - sub(/[ \t]*#*(\n.*)?$/, "", text); - - return headline( n, text, 0 ) _block( substr( block, len + 1) ); - - # block images (wrapped in
) - } else if ( match(block, "^!" lix "\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\)(\\{[a-zA-Z \t-]*\\})?(\n|$)") ) { - len = RLENGTH; text = href = title = attrib = substr( block, 1, len); - - sub("^!\\[", "", text); - sub("\\]\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\)(\\{[a-zA-Z \t-]*\\})?(\n.*)?$", "", text); - - sub("^!" lix "\\([\n\t ]*", "", href); - sub("([\n\t ]+" lit ")?[\n\t ]*\\)(\\{[a-zA-Z \t-]*\\})?(\n.*)?$", "", href); - - sub("^!" lix "\\([\n\t ]*" lid, "", title); - sub("[\n\t ]*\\)(\\{[a-zA-Z \t-]*\\})?(\n.*)?$", "", title); - sub("^[\n\t ]+", "", title); - - sub("^!" lix "\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\)", "", attrib); - sub("(\n.*)?$", "", attrib); - sub(/^\{[ \t]*/, "", attrib); sub(/[ \t]*\}$/, "", attrib); gsub(/[ \t]+/, " ", attrib); - - if ( match(href, /^<.*>$/) ) { sub(/^$/, "", href); } - if ( match(title, /^".*"$/) ) { sub(/^"/, "", title); sub(/"$/, "", title); } - else if ( match(title, /^'.*'$/) ) { sub(/^'/, "", title); sub(/'$/, "", title); } - else if ( match(title, /^\(.*\)$/) ) { sub(/^\(/, "", title); sub(/\)$/, "", title); } - - gsub(/^[\t ]+$/, "", text); gsub(/\\/, "", href); - - return "
" \ - "\""" \ - (title?"
" inline(title) "
":"") \ - "
\n\n" \ - _block( substr( block, len + 1) ); - - } else if ( match(block, /^!\[([^]]*)\] ?\[([^]]*)\](\n|$)/ ) ) { - len = RLENGTH; text = id = block; - sub(/(\n.*)?$/, "", text); sub( /^!\[/, "", text); sub(/\] ?\[([^\n]*)\]$/, "", text); - sub(/(\n.*)?$/, "", id); sub( /^!\[([^\n]*)\] ?\[/, "", id); sub(/\]$/, "", id); - # text = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\](\n.*)?$/, "\\1", 1, block); - # id = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\](\n.*)?$/, "\\2", 1, block); - if ( ! id ) id = text; - if ( rl_href[id] && rl_title[id] ) { - return "
" \ - "\""" \ - "
" inline(rl_title[id]) "
" \ - "
\n\n" \ - _block( substr( block, len + 1) ); - } else if ( rl_href[id] ) { - return "
" \ - "\""" \ - "
\n\n" \ - _block( substr( block, len + 1) ); + ret = ret headline(2, text, attrib) ; block = substr( block, len + 1); + continue; + + # Second Order Heading H2 + } else if ( match( block, /^([^\n]+)\n---+(\n|$)/ ) ) { + len = RLENGTH; text = substr(block, 1, len); + sub(/\n---+(\n.*)?$/, "", text); + + ret = ret headline(2, text, 0) ; block = substr( block, len + 1); + continue; + + # Nth Order Heading H1 H2 H3 H4 H5 H6 + Attrib + } else if ( match( block, /^(##?#?#?#?#?)[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n|$)/ ) ) { + len = RLENGTH; text = attrib = substr(block, 1, len); + match(block, /^##?#?#?#?#?[^#]/); n = RLENGTH - 1; + + # sub(/^(##?#?#?#?#?)[ \t]*/, "", text); # not working in mawk + text = substr(text, n + 1); sub(/^[ \t]*/, "", text); + sub(/[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "", text); + sub(/^(##?#?#?#?#?)[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*[ \t]*\{/, "", attrib); + sub(/\}(\n.*)?$/, "", attrib); + gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib); + + ret = ret headline( n, text, attrib ) ; block = substr( block, len + 1); + continue; + + # Nth Order Heading H1 H2 H3 H4 H5 H6 + } else if ( match( block, /^(##?#?#?#?#?)[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*(\n|$)/ ) ) { + len = RLENGTH; text = substr(block, 1, len); + match(block, /^##?#?#?#?#?[^#]/); n = RLENGTH - 1; + # sub(/^(##?#?#?#?#?)[ \t]+/, "", text); # not working in mawk + text = substr(text, n + 1); sub(/^[ \t]*/, "", text); + sub(/[ \t]*#*(\n.*)?$/, "", text); + + ret = ret headline( n, text, 0 ) ; block = substr( block, len + 1); + continue; + + # block images (wrapped in
) + } else if ( match(block, "^!" lix "\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\)(\\{[a-zA-Z \t-]*\\})?(\n|$)") ) { + len = RLENGTH; text = href = title = attrib = substr( block, 1, len); + + sub("^!\\[", "", text); + sub("\\]\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\)(\\{[a-zA-Z \t-]*\\})?(\n.*)?$", "", text); + + sub("^!" lix "\\([\n\t ]*", "", href); + sub("([\n\t ]+" lit ")?[\n\t ]*\\)(\\{[a-zA-Z \t-]*\\})?(\n.*)?$", "", href); + + sub("^!" lix "\\([\n\t ]*" lid, "", title); + sub("[\n\t ]*\\)(\\{[a-zA-Z \t-]*\\})?(\n.*)?$", "", title); + sub("^[\n\t ]+", "", title); + + sub("^!" lix "\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\)", "", attrib); + sub("(\n.*)?$", "", attrib); + sub(/^\{[ \t]*/, "", attrib); sub(/[ \t]*\}$/, "", attrib); gsub(/[ \t]+/, " ", attrib); + + if ( match(href, /^<.*>$/) ) { sub(/^$/, "", href); } + if ( match(title, /^".*"$/) ) { sub(/^"/, "", title); sub(/"$/, "", title); } + else if ( match(title, /^'.*'$/) ) { sub(/^'/, "", title); sub(/'$/, "", title); } + else if ( match(title, /^\(.*\)$/) ) { sub(/^\(/, "", title); sub(/\)$/, "", title); } + + gsub(/^[\t ]+$/, "", text); gsub(/\\/, "", href); + + ret = ret "
" \ + "\""" \ + (title?"
" inline(title) "
":"") \ + "
\n\n"; + block = substr( block, len + 1); + continue; + + } else if ( match(block, /^!\[([^]]*)\] ?\[([^]]*)\](\n|$)/ ) ) { + len = RLENGTH; text = id = block; + sub(/(\n.*)?$/, "", text); sub( /^!\[/, "", text); sub(/\] ?\[([^\n]*)\]$/, "", text); + sub(/(\n.*)?$/, "", id); sub( /^!\[([^\n]*)\] ?\[/, "", id); sub(/\]$/, "", id); + # text = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\](\n.*)?$/, "\\1", 1, block); + # id = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\](\n.*)?$/, "\\2", 1, block); + if ( ! id ) id = text; + if ( rl_href[id] && rl_title[id] ) { + ret = ret "
" \ + "\""" \ + "
" inline(rl_title[id]) "
" \ + "
\n\n"; + block = substr( block, len + 1); + continue; + + } else if ( rl_href[id] ) { + ret = ret "
" \ + "\""" \ + "
\n\n"; + block = substr( block, len + 1); + continue; + } else { + ret = ret "

" HTML(substr(block, 1, len)) "

\n" ; block = substr(block, len + 1); + continue; + } + + # Macros (standalone <> calls handled as block, so they are not wrapped in paragraph) + } else if ( match( block, /^<<(([^>]|>[^>])+)>>(\n|$)/ ) ) { + len = RLENGTH; text = block; + sub(/^<>(\n.*)?$/, "", text); + # text = gensub(/^<<(([^>]|>[^>])+)>>(\n.*)?$/, "\\1", 1, block); + ret = ret "" HTML(text) "" ; block = substr(block, len + 1); + continue; + + # Definition list + } else if (match( block, "^(([ \t]*\n)*[^:\n \t][^\n]+\n" \ + "([ \t]*\n)* ? ? ?:[ \t][^\n]+(\n|$)" \ + "(([ \t]*\n)* ? ? ?:[ \t][^\n]+(\n|$)" \ + "|[^:\n \t][^\n]+(\n|$)" \ + "|( ? ? ?\t| +)[^\n]+(\n|$)" \ + "|([ \t]*\n)+( ? ? ?\t| +)[^\n]+(\n|$))*)+" \ + )) { + list = substr( block, 1, RLENGTH); block = substr( block, RLENGTH + 1); + ret = ret "
\n" _dlist( list ) "
\n"; + continue; + + # Unordered list types + } else if ( text = _startlist( block, "ul", "-", "([+*•]|[0-9]+\\.|#\\.|[0-9]+\\)|#\\))") ) { + return ret text; + } else if ( text = _startlist( block, "ul", "\\+", "([-*•]|[0-9]+\\.|#\\.|[0-9]+\\)|#\\))") ) { + return ret text; + } else if ( text = _startlist( block, "ul", "\\*", "([-+•]|[0-9]+\\.|#\\.|[0-9]+\\)|#\\))") ) { + return ret text; + } else if ( text = _startlist( block, "ul", "•", "([-+*]|[0-9]+\\.|#\\.|[0-9]+\\)|#\\))") ) { + return ret text; + + # Ordered list types + } else if ( text = _startlist( block, "ol", "[0-9]+\\.", "([-+*•]|#\\.|[0-9]+\\)|#\\))") ) { + return ret text; + } else if ( text = _startlist( block, "ol", "[0-9]+\\)", "([-+*•]|[0-9]+\\.|#\\.|#\\))") ) { + return ret text; + } else if ( text = _startlist( block, "ol", "#\\.", "([-+*•]|[0-9]+\\.|[0-9]+\\)|#\\))") ) { + return ret text; + } else if ( text = _startlist( block, "ol", "#\\)", "([-+*•]|[0-9]+\\.|#\\.|[0-9]+\\))") ) { + return ret text; + + # Split paragraphs + } else if ( match( block, /(^|\n)[[:space:]]*(\n|$)/) ) { + len = RLENGTH; st = RSTART; + ret = ret _block( substr(block, 1, st - 1) ) "\n"; block = substr(block, st + len); + continue; + + # Horizontal rule + # } else if ( match( block, /(^|\n) ? ? ?((\* *){3,}|(- *){3,}|(_ *){3,})($|\n)/) ) { + } else if ( match( block, /(^|\n) ? ? ?((\* *)(\* *)(\* *)(\* *)*|(- *)(- *)(- *)(- *)*|(_ *)(_ *)(_ *)(_ *)*)($|\n)/) ) { + len = RLENGTH; st = RSTART; + ret = ret _block(substr(block, 1, st - 1)) "
\n"; block = substr(block, st + len); + continue; + + # Plain paragraph } else { - return "

" HTML(substr(block, 1, len)) "

\n" _block( substr(block, len + 1) ); + return ret "

" inline(block) "

\n"; } - - # Macros (standalone <> calls handled as block, so they are not wrapped in paragraph) - } else if ( match( block, /^<<(([^>]|>[^>])+)>>(\n|$)/ ) ) { - len = RLENGTH; text = block; - sub(/^<>(\n.*)?$/, "", text); - # text = gensub(/^<<(([^>]|>[^>])+)>>(\n.*)?$/, "\\1", 1, block); - return "" HTML(text) "" _block(substr(block, len + 1) ); - - # Definition list - } else if (match( block, "^(([ \t]*\n)*[^:\n \t][^\n]+\n" \ - "([ \t]*\n)* ? ? ?:[ \t][^\n]+(\n|$)" \ - "(([ \t]*\n)* ? ? ?:[ \t][^\n]+(\n|$)" \ - "|[^:\n \t][^\n]+(\n|$)" \ - "|( ? ? ?\t| +)[^\n]+(\n|$)" \ - "|([ \t]*\n)+( ? ? ?\t| +)[^\n]+(\n|$))*)+" \ - )) { - list = substr( block, 1, RLENGTH); block = substr( block, RLENGTH + 1); - return "
\n" _dlist( list ) "
\n" _block( block ); - - # Unordered list types - } else if ( text = _startlist( block, "ul", "-", "([+*•]|[0-9]+\\.|#\\.|[0-9]+\\)|#\\))") ) { - return text; - } else if ( text = _startlist( block, "ul", "\\+", "([-*•]|[0-9]+\\.|#\\.|[0-9]+\\)|#\\))") ) { - return text; - } else if ( text = _startlist( block, "ul", "\\*", "([-+•]|[0-9]+\\.|#\\.|[0-9]+\\)|#\\))") ) { - return text; - } else if ( text = _startlist( block, "ul", "•", "([-+*]|[0-9]+\\.|#\\.|[0-9]+\\)|#\\))") ) { - return text; - - # Ordered list types - } else if ( text = _startlist( block, "ol", "[0-9]+\\.", "([-+*•]|#\\.|[0-9]+\\)|#\\))") ) { - return text; - } else if ( text = _startlist( block, "ol", "[0-9]+\\)", "([-+*•]|[0-9]+\\.|#\\.|#\\))") ) { - return text; - } else if ( text = _startlist( block, "ol", "#\\.", "([-+*•]|[0-9]+\\.|[0-9]+\\)|#\\))") ) { - return text; - } else if ( text = _startlist( block, "ol", "#\\)", "([-+*•]|[0-9]+\\.|#\\.|[0-9]+\\))") ) { - return text; - - # Split paragraphs - } else if ( match( block, /(^|\n)[[:space:]]*(\n|$)/) ) { - len = RLENGTH; st = RSTART; - return _block( substr(block, 1, st - 1) ) "\n" \ - _block( substr(block, st + len) ); - - # Horizontal rule - # } else if ( match( block, /(^|\n) ? ? ?((\* *){3,}|(- *){3,}|(_ *){3,})($|\n)/) ) { - } else if ( match( block, /(^|\n) ? ? ?((\* *)(\* *)(\* *)(\* *)*|(- *)(- *)(- *)(- *)*|(_ *)(_ *)(_ *)(_ *)*)($|\n)/) ) { - len = RLENGTH; st = RSTART; - return _block(substr(block, 1, st - 1)) "
\n" _block(substr(block, st + len)); - - # Plain paragraph - } else { - return "

" inline(block) "

\n"; } + return ret; } function _startlist(block, type, mark, exclude, LOCAL, st, len, list, indent, it, text) { -- 2.39.2