# ----------------------------
# - [x] Automatic <section>-wrapping (custom)
# - ? Heading identifiers (php md, pandoc)
+# - [x] Heading attributes (custom)
# - [x] Automatic heading identifiers (custom)
# - [x] Fenced code blocks (php md, pandoc)
# - [x] Fenced code attributes
return text;
}
-function URL ( text ) {
+function URL ( text, sharp ) {
gsub( /&/, "%26", text );
gsub( /"/, "%22", text );
gsub( /'/, "%27", text );
gsub( /`/, "%60", text );
gsub( /\?/, "%3F", text );
- gsub( /#/, "%23", text );
+ if (sharp) gsub( /#/, "%23", text );
gsub( /\[/, "%5B", text );
gsub( /\]/, "%5D", text );
gsub( / /, "%20", text );
title = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/, "\\4", "g", substr(line, 1, len) );
attrib = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/, "\\6", "g", substr(line, 1, len) );
if ( title && attrib ) {
- return "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\" title=\"" HTML(title) "\" class=\"" HTML(attrib) "\">" \
+ return "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" title=\"" HTML(title) "\" class=\"" HTML(attrib) "\">" \
inline( substr( line, len + 1) );
} else if ( title ) {
- return "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\" title=\"" HTML(title) "\">" \
+ return "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" title=\"" HTML(title) "\">" \
inline( substr( line, len + 1) );
} else if ( attrib ) {
- return "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
+ return "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
inline( substr( line, len + 1) );
} else {
- return "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\">" \
+ return "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\">" \
inline( substr( line, len + 1) );
}
id = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\].*/, "\\2", 1, substr(line, 1, len) );
if ( ! id ) id = text;
if ( rl_href[id] && rl_title[id] ) {
- return "<img src=\"" URL(rl_href[id]) "\" alt=\"" HTML(text) "\" title=\"" HTML(rl_title[id]) "\">" \
+ return "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\" title=\"" HTML(rl_title[id]) "\">" \
inline( substr( line, len + 1) );
} else if ( rl_href[id] ) {
- return "<img src=\"" URL(rl_href[id]) "\" alt=\"" HTML(text) "\">" \
+ return "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\">" \
inline( substr( line, len + 1) );
} else {
return "" HTML(substr(line, 1, len)) inline( substr(line, len + 1) );
}
}
+function headline( hlvl, htxt, attrib, LOCAL, sec, n, HL) {
+ split( gensub( /^(.* )?([0-9]+( [0-9]+){5})$/, "\\2" ,"1", hstack), HL);
+
+ for ( n = hlvl; n <= 6; n++ ) { sec = sec (HL[n]?"</section>":""); }
+ HL[hlvl]++; for ( n = hlvl + 1; n <= 6; n++) { HL[n] = 0;}
+ 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 ":" URL(htxt, 1);
+
+ hstack = gensub( /^(.* )?([0-9]+( [0-9]+){5})$/, "\\1" ,"1", hstack) \
+ HL[1] " " HL[2] " " HL[3] " " HL[4] " " HL[5] " " HL[6];
+
+ return sec "<section class=\"" (attrib ? "h" hlvl " " attrib : "h" hlvl) "\" id=\"" hid "\">" \
+ "<h" hlvl (attrib ? " class=\"" attrib "\"" : "") ">" inline( htxt ) \
+ "<a class=\"anchor\" href=\"#" hid "\"></a>" \
+ "</h" hlvl ">\n\n";
+}
+
# Nested Block, resets heading counters
-function _nblock( block, LOCAL, hlsav, sec ) {
- # Keeping arrays in a local scope is awkward, so we serialize the HL array
- # into the scalar hlsav
- hlsav = HL[1] " " HL[2] " " HL[3] " " HL[4] " " HL[5] " " HL[6];
- HL[1] = 0; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
+function _nblock( block, LOCAL, hlsav, sec, n ) {
+ hlsav = hstack;
+ hstack = hstack " 0 0 0 0 0 0";
# Block Level
- blvl++;
+ blvl++; BL[blvl]++;
+ for ( n = blvl + 1; n in BL; n++) { delete BL[n]; }
block = _block( block );
+ split( gensub( /^(.* )?([0-9]+( [0-9]+){5})$/, "\\2" ,"1", hstack), HL);
sec = ""; for ( n = 1; n <= 6; n++ ) { sec = sec (HL[n]?"</section>":""); }
- split(hlsav, HL); blvl--;
+ hstack = hlsav; blvl--;
return block sec;
}
} else if ( match( block, /^> /) ) {
match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match(block, /$/);
len = RLENGTH; st = RSTART;
- return "<blockquote>\n" _nblock( gensub( /(^|\n)> /, "\n", "g", substr(block, 1, st - 1) ) ) "</blockquote>\n\n" \
+ return "<blockquote>" gensub( /^\n|\n$/, "", "g", _nblock( gensub( /(^|\n)> /, "\n", "g", substr(block, 1, st - 1) ) ) ) "</blockquote>\n\n" \
_block( substr(block, st + len) );
# Pipe Tables (pandoc / php md / gfm )
gsub("(^|\n) {0," indent "}", "\n", list);
return "\n<ol>\n" _list( substr(list, 2) ) "</ol>\n" _block( block );
- # First Order Heading H1
- } else if ( match( block, /^[^\n]+\n===+(\n|$)/ ) ) {
- len = RLENGTH; sec = "";
+ # First Order Heading H1 + Attrib
+ } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n|$)/ ) ) {
+ len = RLENGTH;
+ text = gensub(/^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n.*)?$/, "\\1", 1, block)
+ attrib = gensub(/^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n.*)?$/, "\\3", 1, block)
+ gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
+
+ return headline(1, text, attrib) \
+ _block( substr( block, len + 1 ) );
- for ( n = 1; n <= 6; n++ ) { sec = sec (HL[n]?"</section>":""); }
- HL[1]++; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
- hid = HL[1] ":" URL(gensub( /\n.*$/, "", "g", block ));
+ # First Order Heading H1
+ } else if ( match( block, /^([^\n]+)\n===+(\n|$)/ ) ) {
+ len = RLENGTH;
+ text = gensub(/^([^\n]+)\n===+(\n.*)?$/, "\\1", 1, block)
- return sec "<section class=\"h1\"" ((blvl > 1)?"":" id=\"" hid "\"") ">" \
- "<h1>" inline( gensub( /\n.*$/, "", "g", block ) ) \
- ((blvl > 1)?"":"<a class=\"anchor\" href=\"#" hid "\"></a>") \
- "</h1>\n\n" \
+ return headline(1, text, 0) \
_block( substr( block, len + 1 ) );
- # Second Order Heading H2
- } else if ( match( block, /^[^\n]+\n---+(\n|$)/ ) ) {
- len = RLENGTH; sec = "";
+ # Second Order Heading H2 + Attrib
+ } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n---+(\n|$)/ ) ) {
+ len = RLENGTH;
+ text = gensub(/^([^\n]+)([ \t]*\{([^\}\n]+)\})\n---+(\n.*)?$/, "\\1", 1, block)
+ attrib = gensub(/^([^\n]+)([ \t]*\{([^\}\n]+)\})\n---+(\n.*)?$/, "\\3", 1, block)
+ gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
+
+ return headline(2, text, attrib) \
+ _block( substr( block, len + 1) );
- for ( n = 2; n <= 6; n++ ) { sec = sec (HL[n]?"</section>":""); }
- HL[2]++; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
- hid= HL[1] "." HL[2] ":" URL(gensub( /\n.*$/, "", "g", block ));
+ # Second Order Heading H2
+ } else if ( match( block, /^([^\n]+)\n---+(\n|$)/ ) ) {
+ len = RLENGTH;
+ text = gensub(/^([^\n]+)\n---+(\n.*)?$/, "\\1", 1, block)
- return sec "<section class=\"h2\"" ((blvl > 1)?"":" id=\"" hid "\"") ">" \
- "<h2>" inline( gensub( /\n.*$/, "", "g", block ) ) \
- ((blvl > 1)?"":"<a class=\"anchor\" href=\"#" hid "\"></a>") \
- "</h2>\n\n" \
+ return headline(2, text, 0) \
_block( substr( block, len + 1) );
- # Nth Order Heading H1 H2 H3 H4 H5 H6
- } else if ( match( block, /^#{1,6}[ \t]*[^\n]+([ \t]*#*)(\n|$)/ ) ) {
- len = RLENGTH; sec = "";
+ # Nth Order Heading H1 H2 H3 H4 H5 H6 + Attrib
+ } else if ( match( block, /^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n|$)/ ) ) {
+ len = RLENGTH;
+ n = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "\\1", 1, block);
+ text = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "\\2", 1, block);
+ attrib = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "\\5", 1, block);
+ gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
- hlvl = length( gensub( /^(#{1,6}).*$/, "\\1", "g", block ) );
- htxt = gensub(/^#{1,6}[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[^\n#])+)([ \t]*#*)(\n.*)?$/, "\\1", 1, block);
+ return headline( length(n), text, attrib ) \
+ _block( substr( block, len + 1) );
- for ( n = hlvl; n <= 6; n++ ) { sec = sec (HL[n]?"</section>":""); }
- HL[hlvl]++; for ( n = hlvl + 1; n <= 6; n++) { HL[n] = 0;}
- hid = HL[1]; for ( n = 2; n <= hlvl; n++) { hid = hid "." HL[n] ; }
- hid = hid ":" URL(htxt);
+ # Nth Order Heading H1 H2 H3 H4 H5 H6
+ } else if ( match( block, /^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*(\n|$)/ ) ) {
+ len = RLENGTH;
+ n = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*(\n.*)?$/, "\\1", 1, block);
+ text = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*(\n.*)?$/, "\\2", 1, block);
- return sec "<section class=\"h" hlvl "\"" ((blvl > 1)?"":" id=\"" hid "\"") ">" \
- "<h" hlvl ">" inline( htxt ) \
- ((blvl > 1)?"":"<a class=\"anchor\" href=\"#" hid "\"></a>") \
- "</h" hlvl ">\n\n" \
+ return headline( length(n), text, 0 ) \
_block( substr( block, len + 1) );
# block images (wrapped in <figure>)
title = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\4", "g", block);
attrib = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\6", "g", block);
if ( title && attrib ) {
- return "<figure data-src=\"" URL(href) "\" class=\"" HTML(attrib) "\">" \
- "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
+ return "<figure data-src=\"" URL(href, 1) "\" class=\"" HTML(attrib) "\">" \
+ "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
"<figcaption>" inline(title) "</figcaption>" \
"</figure>\n\n" \
_block( substr( block, len + 1) );
} else if ( title ) {
- return "<figure data-src=\"" URL(href) "\">" \
- "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\">" \
+ return "<figure data-src=\"" URL(href, 1) "\">" \
+ "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\">" \
"<figcaption>" inline(title) "</figcaption>" \
"</figure>\n\n" \
_block( substr( block, len + 1) );
} else if ( attrib ) {
- return "<figure data-src=\"" URL(href) "\" class=\"" HTML(attrib) "\">" \
- "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
+ return "<figure data-src=\"" URL(href, 1) "\" class=\"" HTML(attrib) "\">" \
+ "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
"</figure>\n\n" \
_block( substr( block, len + 1) );
} else {
- return "<figure data-src=\"" URL(href) "\">" \
- "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\">" \
+ return "<figure data-src=\"" URL(href, 1) "\">" \
+ "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\">" \
"</figure>\n\n" \
_block( substr( block, len + 1) );
}
id = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\](\n.*)?$/, "\\2", 1, block);
if ( ! id ) id = text;
if ( rl_href[id] && rl_title[id] ) {
- return "<figure data-src=\"" URL(rl_href[id]) "\">" \
- "<img src=\"" URL(rl_href[id]) "\" alt=\"" HTML(text) "\">" \
+ return "<figure data-src=\"" URL(rl_href[id], 1) "\">" \
+ "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\">" \
"<figcaption>" inline(rl_title[id]) "</figcaption>" \
"</figure>\n\n" \
_block( substr( block, len + 1) );
} else if ( rl_href[id] ) {
- return "<figure data-src=\"" URL(rl_href[id]) "\">" \
- "<img src=\"" URL(rl_href[id]) "\" alt=\"" HTML(text) "\">" \
+ return "<figure data-src=\"" URL(rl_href[id], 1) "\">" \
+ "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\">" \
"</figure>\n\n" \
_block( substr( block, len + 1) );
} else {