return text;
}
-function URL ( text ) {
- gsub( /&/, "%26", text );
- gsub( /"/, "%22", text );
- gsub( /'/, "%27", text );
- gsub( /`/, "%60", text );
- gsub( /\?/, "%3F", text );
- gsub( /#/, "%23", text );
- gsub( /\[/, "%5B", text );
- gsub( /\]/, "%5D", text );
- gsub( / /, "%20", text );
- gsub( / /, "%09", text );
- gsub( /\\/, "%5C", text );
- return text;
-}
-
function inline( line, LOCAL, len, text, code, href, guard, ret ) {
ret = "";
while (line !~ /^$/) {
len = RLENGTH; href = text = substr(line, 1, len);
sub(/^\[\[/, "", href); sub(/(\|([^]]+))?\]\].*$/, "", href);
sub(/^\[\[([^]|]+)/, "", text); sub(/\]\].*$/, "", text); sub(/^\|/, "", text);
- # sub(/^\[\[([^]|]+)(\|([^]]+))?\]\]/, "\\1", href );
- # sub(/^\[\[([^]|]+)(\|([^]]+))?\]\]/, "\\3", text );
if ( ! text ) text = href;
ret = ret "<a href=\"" HTML(href) "\">" HTML(text) "</a>"; line = substr( line, len + 1);
continue;
continue;
# quick link email
- # } else if ( match( line, /^<[a-zA-Z0-9.!#$%&'\''*+\/=?^_`{|}~-]+@[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*>/ ) ) {
} else if ( match( line, /^<[a-zA-Z0-9.!#$%&'\''*+\/=?^_`{|}~-]+@([a-zA-Z0-9]\.[a-zA-Z0-9]|[a-zA-Z0-9-])+>/ ) ) {
len = RLENGTH;
href = HTML( substr( line, 2, len - 2) );
len = RLENGTH; text = id = substr(line, 1, len);
sub(/\n.*$/, "", text); sub(/^\[/, "", text); sub(/\] ?\[([^\n]*)\].*$/, "", text);
sub(/\n.*$/, "", id); sub(/^\[([^]]+)\] ?\[/, "", id); sub(/\].*$/, "", id);
- # text = gensub(/^\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\1", 1, text );
- # id = gensub(/^\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\2", 1, id );
if ( ! id ) id = text;
if ( rl_href[id] && rl_title[id] ) {
len = RLENGTH; text = id = substr(line, 1, len);
sub(/\n.*$/, "", text); sub(/^!\[/, "", text); sub(/\] ?\[([^\n]*)\].*$/, "", text);
sub(/\n.*$/, "", id); sub(/^!\[([^]]+)\] ?\[/, "", id); sub(/\].*$/, "", id);
- # text = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\].*/, "\\1", 1, substr(line, 1, len) );
- # id = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\].*/, "\\2", 1, substr(line, 1, len) );
if ( ! id ) id = text;
if ( rl_href[id] && rl_title[id] ) {
ret = ret "<img src=\"" HTML(rl_href[id]) "\" alt=\"" HTML(text) "\" title=\"" HTML(rl_title[id]) "\">";
continue;
# Literal HTML entities
- # } else if ( match( line, /^&([a-zA-Z]{2,32}|#[0-9]{1,7}|#[xX][0-9a-fA-F]{1,6});/) ) {
- # mawk does not support repitition ranges
+ # mawk does not support repitition ranges (i.e. "xyz{1,10}")
} else if ( match( line, /^&[a-zA-Z][a-zA-Z][a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?[a-zA-Z]?;/) ) {
len = RLENGTH;
ret = ret substr( line, 1, len ); line = substr(line, len + 1);
}
function headline( hlvl, htxt, attrib, LOCAL, sec, n, hid, hid2, HL) {
- # match(hstack, /([0-9]+( [0-9]+){5})$/); split( substr(hstack, RSTART), HL);
match(hstack, /([0-9]+( [0-9]+)( [0-9]+)( [0-9]+)( [0-9]+)( [0-9]+))$/); split( substr(hstack, RSTART), HL);
for ( n = hlvl; n <= 6; n++ ) { sec = sec (HL[n]?"</section>":""); }
hid = ""; for ( n = 2; n <= blvl; n++) { hid = hid BL[n] "/"; }
hid = hid HL[1]; for ( n = 2; n <= hlvl; n++) { hid = hid "." HL[n] ; }
- hid = hid ":" HTML(htxt); # anchor for TOC
- # hid2 = ":" HTML(htxt); # anchor for permalink
- # while ( headings[hid2] ) { n = n ? 2 : n + 1; hid2 = ":" HTML(htxt) "/" n; }
- # headings[hid2] = true;
+ hid = hid ":" HTML(htxt); # anchor for TOC and permalink
- # sub(/([0-9]+( [0-9]+){5})$/, "", hstack);
sub(/([0-9]+( [0-9]+)( [0-9]+)( [0-9]+)( [0-9]+)( [0-9]+))$/, "", hstack);
hstack = hstack HL[1] " " HL[2] " " HL[3] " " HL[4] " " HL[5] " " HL[6];
# 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";
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 ) {
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 ) {
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|$)/ ) ) {
} else if ( match( block, /^##?#?#?#?#?[^#\n]([^\n#]|#[^\t\n# ]|#[\t ]+[^\t\n ])+#*[\t ]*\{[\ta-zA-Z -]*\}(\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);
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|$)/ ) ) {
} else if ( match( block, /^##?#?#?#?#?[^#\n]([^\n#]|#[^\t\n# ]|#[\t ]+[^\t\n ])+#*(\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);
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 "<figure data-src=\"" HTML(rl_href[id]) "\">" \
} else if ( match( block, /^<<(([^>]|>[^>])+)>>(\n|$)/ ) ) {
len = RLENGTH; text = block;
sub(/^<</, "", text); sub(/>>(\n.*)?$/, "", text);
- # text = gensub(/^<<(([^>]|>[^>])+)>>(\n.*)?$/, "\\1", 1, block);
ret = ret "<code class=\"macro\">" HTML(text) "</code>" ; block = substr(block, len + 1);
continue;
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)) "<hr>\n"; block = substr(block, st + len);
st = RSTART; len = RLENGTH; list = substr( block, st, len);
sub("^\n", "", list); match(list, "^( | | )?"); indent = RLENGTH;
- # gsub( "(^|\n) {0," indent "}", "\n", list); sub("^\n", "", list);
# emulate greedy range matcher for mawk
it = "("; while ( indent > 0 ) { for (k = indent; k > 0; k--) { it = it " "; } it = it "|"; indent--; }
sub(/\|$/, ")?", it); sub(/^\($/, "", it);
st = (RLENGTH == -1) ? length(block) + 1 : RSTART;
text = substr(block, 1, st); block = substr(block, st + 1);
- # gsub("\n {0," indent "}", "\n", text);
# emulate greedy range matcher for mawk
it = "("; while ( indent > 0 ) { for (k = indent; k > 0; k--) { it = it " "; } it = it "|"; indent--; }
sub(/\|$/, ")?", it); sub(/^\($/, "", it);
sub( "^([ \t]*\n)*", "", text);
match(text, "^ ? ? ?:(\t| +)"); indent = RLENGTH;
sub( "^ ? ? ?:(\t| +)", "", text);
- # gsub( "(^|\n) {0," indent "}", "\n", text );
# emulate greedy range matcher for mawk
it = "("; while ( indent > 0 ) { for (k = indent; k > 0; k--) { it = it " "; } it = it "|"; indent--; }
sub(/\|$/, ")?", it); sub(/^\($/, "", it);
file = ""; rl_href[""] = ""; rl_title[""] = "";
if (ENVIRON["MD_HTML"] == "true") { AllowHTML = "true"; }
HL[1] = 0; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
- # hls = "0 0 0 0 0 0";
# Universal Patterns
nu = "([^_\\\\]|\\\\.|_[[:alnum:]])" # not underline (except when escaped, or inside a word)
# Fill array of reference links
f = file; rl_id;
re_reflink = "(^|\n) ? ? ?\\[([^]\n]+)\\]: ([^ \t\n]+)(\n?[ \t]+(\"([^\"]+)\"|'([^']+)'|\\(([^)]+)\\)))?(\n|$)";
- # /(^|\n) ? ? ?\[([^]\n]+)\]: ([^ \t\n]+)(\n?[ \t]+("([^"]+)"|'([^']+)'|\(([^)]+)\)))?(\n|$)/
while ( match(f, re_reflink ) ) {
tt = th = ti = substr(f, RSTART, RLENGTH); f = substr(f, RSTART + RLENGTH);
sub("(^|\n) ? ? ?\\[", "", ti); sub("\\]: ([^ \t\n]+)(\n?[ \t]+(\"([^\"]+)\"|'([^']+)'|\\(([^)]+)\\)))?(\n.*)?$", "", ti);
sub("(^|\n) ? ? ?\\[([^]\n]+)\\]: ([^ \t\n]+)", "", tt); sub("^\n?[ \t]+", "", tt); sub("(\n.*)?$", "", tt);
} else { tt = ""; }
rl_id = ti; rl_href[rl_id] = th; rl_title[rl_id] = tt;
- # rl_id = gensub( re_reflink, "\\2", 1, substr(f, RSTART, RLENGTH) );
- # rl_href[rl_id] = gensub( re_reflink, "\\3", 1, substr(f, RSTART, RLENGTH) );
- # rl_title[rl_id] = gensub( re_reflink, "\\5", 1, substr(f, RSTART, RLENGTH) );
- # f = substr(f, RSTART + RLENGTH);
rl_title[rl_id] = substr( rl_title[rl_id], 2, length(rl_title[rl_id]) - 2 );
if ( rl_href[rl_id] ~ /<.*>/ ) rl_href[rl_id] = substr( rl_href[rl_id], 2, length(rl_href[rl_id]) - 2 );
}
# Clear reflinks from File
while( gsub(re_reflink, "\n", file ) );
- # for (n in rl_href) { debug(n " | " rl_href[n] " | " rl_title[n] ); }
# Run Block Processing -> The Actual Markdown!
printf "%s", _nblock( file );