#
# Extensions - Block elements:
# ----------------------------
+# - [x] Automatic <section>-wrapping (custom)
# - ? Heading identifiers (php md, pandoc)
# - [x] Automatic heading identifiers (custom)
# - [x] Fenced code blocks (php md, pandoc)
}
# Nested Block, resets heading counters
-function _nblock( block, LOCAL, st, len, hlvl, htxt, hlsav, sec, guard, code, indent, attrib ) {
+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;
+ # Block Level
+ blvl++;
+
block = _block( block );
sec = ""; for ( n = 1; n <= 6; n++ ) { sec = sec (HL[n]?"</section>":""); }
- split(hlsav, HL);
+ split(hlsav, HL); blvl--;
return block sec;
}
# First Order Heading H1
} else if ( match( block, /^[^\n]+\n===+(\n|$)/ ) ) {
len = RLENGTH; sec = "";
+
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;
- return sec "<section id=\"" HL[1] ":" URL(gensub( /\n.*$/, "", "g", block )) "\"><h1>" \
- inline( gensub( /\n.*$/, "", "g", block ) ) \
- "<a class=\"anchor\" href=\"#" HL[1] ":" \
- URL(gensub( /\n.*$/, "", "g", block )) "\"></a></h1>\n\n" \
+ hid = HL[1] ":" URL(gensub( /\n.*$/, "", "g", 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" \
_block( substr( block, len + 1 ) );
# Second Order Heading H2
} else if ( match( block, /^[^\n]+\n---+(\n|$)/ ) ) {
len = RLENGTH; sec = "";
+
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;
- return sec "<section id=\"" HL[1] "." HL[2] ":" URL(gensub( /\n.*$/, "", "g", block )) "\">" \
+ hid= HL[1] "." HL[2] ":" URL(gensub( /\n.*$/, "", "g", block ));
+
+ return sec "<section class=\"h2\"" ((blvl > 1)?"":" id=\"" hid "\"") ">" \
"<h2>" inline( gensub( /\n.*$/, "", "g", block ) ) \
- "<a class=\"anchor\" href=\"#" HL[1] "." HL[2] ":" \
- URL(gensub( /\n.*$/, "", "g", block )) "\"></a></h2>\n\n" \
+ ((blvl > 1)?"":"<a class=\"anchor\" href=\"#" hid "\"></a>") \
+ "</h2>\n\n" \
_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 = "";
+
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);
+
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] ; }
- return sec "<section id=\"" hid ":" URL(htxt) "\"><h" hlvl ">" inline( htxt ) \
- "<a class=\"anchor\" href=\"#" hid "\"></a></h" hlvl ">\n\n" \
+ hid = hid ":" URL(htxt);
+
+ 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" \
_block( substr( block, len + 1) );
# block images (wrapped in <figure>)