]> git.plutz.net Git - cgilite/commitdiff
simpler block nesting, fix: h2 after paragraph
authorPaul Hänsch <paul@plutz.net>
Wed, 30 Aug 2023 10:31:15 +0000 (12:31 +0200)
committerPaul Hänsch <paul@plutz.net>
Wed, 30 Aug 2023 10:31:15 +0000 (12:31 +0200)
markdown.awk

index 5915f011ece716dc8ab33825017226bf368e3a19..86657adf28c4c347b49557bc77e8c2a9105efb42 100755 (executable)
@@ -41,6 +41,7 @@
 # - [x] Automatic <section>-wrapping (custom)
 # -  ?  Heading identifiers (php md, pandoc)
 #   - [x] Heading attributes (custom)
+#   - [ ] <hr> ends section
 # - [x] Automatic heading identifiers (custom)
 # - [x] Fenced code blocks (php md, pandoc)
 #   - [x] Fenced code attributes
@@ -313,6 +314,7 @@ function headline( hlvl, htxt, attrib, LOCAL, sec, n, 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);
@@ -327,8 +329,7 @@ function headline( hlvl, htxt, attrib, LOCAL, sec, n, HL) {
 }
 
 # Nested Block, resets heading counters
-function _nblock( block, LOCAL, hlsav, sec, n ) {
-  hlsav = hstack;
+function _nblock( block, LOCAL, sec, n ) {
   hstack = hstack " 0 0 0 0 0 0";
 
   # Block Level
@@ -338,7 +339,8 @@ function _nblock( block, LOCAL, hlsav, sec, 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>":""); }
-  hstack = hlsav; blvl--;
+
+  sub("( +[0-9]+){6} *$", "", hstack); blvl--;
   return block sec;
 }
 
@@ -573,16 +575,14 @@ function _block( block, LOCAL, st, len, text, title, attrib, href, guard, code,
     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 ) );
+    return headline(1, text, attrib) _block( substr( block, len + 1 ) );
 
   # First Order Heading H1
   } else if ( match( block, /^([^\n]+)\n===+(\n|$)/ ) ) {
     len = RLENGTH;
     text   = gensub(/^([^\n]+)\n===+(\n.*)?$/, "\\1", 1, block)
 
-    return headline(1, text, 0) \
-           _block( substr( block, len + 1 ) );
+    return headline(1, text, 0) _block( substr( block, len + 1 ) );
 
   # Second Order Heading H2 + Attrib
   } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n---+(\n|$)/ ) ) {
@@ -591,16 +591,14 @@ function _block( block, LOCAL, st, len, text, title, attrib, href, guard, code,
     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) );
+    return headline(2, text, attrib) _block( substr( block, len + 1) );
 
   # Second Order Heading H2
   } else if ( match( block, /^([^\n]+)\n---+(\n|$)/ ) ) {
     len = RLENGTH;
     text   = gensub(/^([^\n]+)\n---+(\n.*)?$/, "\\1", 1, block)
 
-    return headline(2, text, 0) \
-           _block( substr( block, len + 1) );
+    return headline(2, text, 0) _block( substr( block, len + 1) );
 
   # 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|$)/ ) ) {
@@ -610,8 +608,7 @@ function _block( block, LOCAL, st, len, text, title, attrib, href, guard, code,
     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);
 
-    return headline( length(n), text, attrib ) \
-           _block( substr( block, len + 1) );
+    return headline( length(n), text, attrib ) _block( substr( block, len + 1) );
 
   # 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|$)/ ) ) {
@@ -619,8 +616,7 @@ function _block( block, LOCAL, st, len, text, title, attrib, href, guard, code,
     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 headline( length(n), text, 0 ) \
-           _block( substr( block, len + 1) );
+    return headline( length(n), text, 0 ) _block( substr( block, len + 1) );
 
   # block images (wrapped in <figure>)
   } else if ( match(block, /^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n|$)/) ) {
@@ -680,11 +676,6 @@ function _block( block, LOCAL, st, len, text, title, attrib, href, guard, code,
     text = gensub(/^<<(([^>]|>[^>])+)>>(\n.*)?$/, "\\1", 1, block);
     return macro(text) _block(substr(block, len + 1) );
 
-  # Horizontal rule
-  } else if ( match( block, /(^|\n) ? ? ?((\* *){3,}|(- *){3,}|(_ *){3,})($|\n)/) ) {
-    len = RLENGTH; st = RSTART;
-    return _block(substr(block, 1, st - 1)) "<hr>\n" _block(substr(block, st + len));
-
   # Definition list
   } else if (match( block, "^(([ \t]*\n)*[^:\n \t][^\n]+\n" \
                            "([ \t]*\n)* ? ? ?:[ \t][^\n]+(\n|$)" \
@@ -736,6 +727,11 @@ function _block( block, LOCAL, st, len, text, title, attrib, href, guard, code,
     return _block( substr(block, 1, st - 1) ) "\n" \
            _block( substr(block, st + len) );
 
+  # Horizontal rule
+  } else if ( match( block, /(^|\n) ? ? ?((\* *){3,}|(- *){3,}|(_ *){3,})($|\n)/) ) {
+    len = RLENGTH; st = RSTART;
+    return _block(substr(block, 1, st - 1)) "<hr>\n" _block(substr(block, st + len));
+
   # Plain paragraph
   } else {
     return "<p>" inline(block) "</p>\n";