]> git.plutz.net Git - cgilite/blobdiff - markdown.awk
bugfix do not escape # character in link references
[cgilite] / markdown.awk
index 33c12bdc9a5be4bcccd3e5bf1b504182291b0c36..0e6d949c4abb66633b68ba5ac770a5605c22d72f 100755 (executable)
@@ -38,6 +38,7 @@
 #
 # 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)
@@ -89,12 +90,13 @@ function HTML ( text ) {
   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 );
@@ -115,8 +117,8 @@ function inline( line, LOCAL, len, code, href, guard ) {
     return "";
 
   # omit processing of escaped characters
-  } else if ( line ~ /^\\[]\\`\*_\{\}\(\)#\+-\.![]/) {
-    return substr(line, 2, 1) inline( substr(line, 3) );
+  } else if ( line ~ /^\\./) {
+    return HTML(substr(line, 2, 1)) inline( substr(line, 3) );
 
   # hard brakes
   } else if ( match(line, /^  \n/) ) {
@@ -130,7 +132,7 @@ function inline( line, LOCAL, len, code, href, guard ) {
       code = substr( line, len + 1, match( substr(line, len + 1), guard ) - 1)
       len = 2 * length(guard) + length(code)
       #  strip single surrounding white spaces
-      code = gensub( / (.*) /, "\\1", "1" , code)
+      code = gensub( /^ | $/, "", "g" , code)
       #  escape HTML within code span
       gsub( /&/, "\\&amp;", code ); gsub( /</, "\\&lt;", code ); gsub( />/, "\\&gt;", code );
       return "<code>" code "</code>" inline( substr( line, len + 1 ) )
@@ -194,16 +196,16 @@ function inline( line, LOCAL, len, code, href, guard ) {
     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) );
     }
 
@@ -214,10 +216,10 @@ function inline( line, LOCAL, len, code, href, guard ) {
       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) );
@@ -305,7 +307,23 @@ function inline( line, LOCAL, len, code, href, guard ) {
   }
 }
 
-function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent, attrib ) {
+# 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;
+
+  # Block Level
+  blvl++;
+
+  block = _block( block );
+  sec = ""; for ( n = 1; n <= 6; n++ ) { sec = sec (HL[n]?"</section>":""); }
+  split(hlsav, HL); blvl--;
+  return block sec;
+}
+
+function _block( block, LOCAL, st, len, hlvl, htxt, sec, guard, code, indent, attrib ) {
   gsub( /^\n+|\n+$/, "", block );
 
   if ( block == "" ) {
@@ -343,7 +361,7 @@ function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent, attrib
   } else if ( match( block, /^> /) ) {
     match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match(block, /$/);
     len = RLENGTH; st = RSTART;
-    return "<blockquote>\n" _block( gensub( /(^|\n)> /, "\n", "g", substr(block, 1, st - 1) ) ) "</blockquote>\n\n" \
+    return "<blockquote>\n" _nblock( gensub( /(^|\n)> /, "\n", "g", substr(block, 1, st - 1) ) ) "</blockquote>\n\n" \
            _block( substr(block, st + len) );
 
   # Pipe Tables (pandoc / php md / gfm )
@@ -399,22 +417,25 @@ function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent, attrib
                             "(\\|([^\n]+\\|)+\n)+" \
                             "(\\+(:?=+:?\\+)+)\n" \
                            "((\\|([^\n]+\\|)+\n)+" \
-                             "\\+(-+\\+)+(\n|$))+", \
-                     tread \
+                             "\\+(-+\\+)+(\n|$))+" \
                    ) || \
               match( block, "^()()()" \
                             "(\\+(:?-+:?\\+)+)\n" \
                            "((\\|([^\n]+\\|)+\n)+" \
-                             "\\+(-+\\+)+(\n|$))+", \
-                     tread \
+                             "\\+(-+\\+)+(\n|$))+" \
   ) ) {
     len = RLENGTH; st = RSTART;
     #initialize empty arrays
-    split("", talign); split("", tarray); split("", tread);
+    split("", talign); split("", tarray); split("", tread);
     cols = 0; cnt=0; ttext = "";
 
+    # Column Count
+    cols = split(   gensub( "^(\\+(:?-+:?\\+)+)(\n.*)*$", "\\1", 1, block), tread, /\+/) - 2;
+    # debug(" Cols: " gensub( "^(\\+(:?-+:?\\+)+)(\n.*)*$", "\\1", 1, block ));
+
     # table alignment
-    cols = split( gensub( /(^\+|\+$)/, "", "g", tread[4] ), 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";
@@ -432,9 +453,9 @@ function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent, attrib
       # table header
       block = substr(block, match(block, /(\n|$)/) + 1 );
       while ( match(block, "^\\|([^\n]+\\|)+\n") ) {
-        cols = split( gensub( /(^\||\|$)/, "", "g", \
-               gensub( /(^|[^\\])\\\|/, "\\1\\&#x7C;", "g", \
-               substr(block, 1, match(block, /(\n|$)/)) \
+        split( gensub( /(^\||\|$)/, "", "g", \
+                 gensub( /(^|[^\\])\\\|/, "\\1\\&#x7C;", "g", \
+                   substr(block, 1, match(block, /(\n|$)/)) \
         )), tread, /\|/);
         block = substr(block, match(block, /(\n|$)/) + 1 );
         for (cnt = 1; cnt <= cols; cnt++)
@@ -443,7 +464,7 @@ function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent, attrib
 
       ttext = "<thead>\n<tr>"
       for (cnt = 1; cnt <= cols; cnt++)
-        ttext = ttext "<th align=\"" talign[cnt] "\">" _block(tarray[cnt]) "</th>"
+        ttext = ttext "<th align=\"" talign[cnt] "\">" _nblock(tarray[cnt]) "</th>"
       ttext = ttext "</tr>\n</thead>"
     }
 
@@ -466,10 +487,10 @@ function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent, attrib
 
       ttext = ttext "<tr>"
       for (cnt = 1; cnt <= cols; cnt++)
-        ttext = ttext "<td align=\"" talign[cnt] "\">" _block(tarray[cnt]) "</td>"
+        ttext = ttext "<td align=\"" talign[cnt] "\">" _nblock(tarray[cnt]) "</td>"
       ttext = ttext "</tr>\n"
     }
-    return "<table>" ttext "</tbody></table>\n" _block(block);
+    return "<table>" ttext "</tbody></table>\n" _nblock(block);
 
   # Line Blocks (pandoc)
   } else if ( match(block, /^\| [^\n]*(\n|$)(\| [^\n]*(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
@@ -499,7 +520,7 @@ function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent, attrib
     gsub(/(^ | $)/, "", attrib);
     if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) {
       len = RLENGTH; st = RSTART;
-      return "<div class=\"" attrib "\">" _block( substr(code, 1, st - 1) ) "</div>\n" \
+      return "<div class=\"" attrib "\">" _nblock( substr(code, 1, st - 1) ) "</div>\n" \
              _block( substr( code, st + len ) );
     } else {
       match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
@@ -550,35 +571,50 @@ function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent, attrib
   gsub("(^|\n) {0," indent "}", "\n", list);
   return "\n<ol>\n" _list( substr(list, 2) ) "</ol>\n" _block( block );
 
-  # First Order Heading
+  # First Order Heading H1
   } else if ( match( block, /^[^\n]+\n===+(\n|$)/ ) ) {
-    len = RLENGTH;
+    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 "<h1 id=\"" HL[1] ":" URL(gensub( /\n.*$/, "", "g", block )) "\">" \
-           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 ), 1);
+
+    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
+  # Second Order Heading H2
   } else if ( match( block, /^[^\n]+\n---+(\n|$)/ ) ) {
-    len = RLENGTH;
+    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 "<h2 id=\"" HL[1] "." HL[2] ":" URL(gensub( /\n.*$/, "", "g", block )) "\">" \
-           inline( gensub( /\n.*$/, "", "g", block ) ) \
-           "<a class=\"anchor\" href=\"#" HL[1] "." HL[2] ":" \
-           URL(gensub( /\n.*$/, "", "g", block )) "\"></a></h2>\n\n" \
+    hid= HL[1] "." HL[2] ":" URL(gensub( /\n.*$/, "", "g", block ), 1);
+
+    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" \
            _block( substr( block, len + 1) );
 
-  # Nth Order Heading
+  # Nth Order Heading H1 H2 H3 H4 H5 H6
   } else if ( match( block, /^#{1,6}[ \t]*[^\n]+([ \t]*#*)(\n|$)/ ) ) {
-    len = RLENGTH;
+    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);
-    HL[hlvl]++; for ( n = hlvl + 1; n < 7; n++) { HL[n] = 0;}
+
+    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 "<h" hlvl " id=\"" hid ":" URL(htxt) "\">" inline( htxt ) \
-           "<a class=\"anchor\" href=\"#" hid "\"></a></h" hlvl ">\n\n" \
+    hid = hid ":" URL(htxt, 1);
+
+    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>)
@@ -589,25 +625,25 @@ function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent, attrib
     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) );
     }
@@ -619,14 +655,14 @@ function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent, attrib
       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 {
@@ -637,7 +673,7 @@ function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent, attrib
   } else if ( AllowMacros && match( block, /^<<(([^>]|>[^>])+)>>(\n|$)/) ) {
     len = RLENGTH;
     text = gensub(/^<<(([^>]|>[^>])+)>>(\n.*)?$/, "\\1", 1, block);
-    return macro(text) _block(substr(block, len + 1));
+    return macro(text) _block(substr(block, len + 1) );
 
   # Split paragraphs
   } else if ( match( block, /(^|\n)[[:space:]]*(\n|$)/) ) {
@@ -672,9 +708,9 @@ function _list( block, last, LOCAL, p) {
   # if this should be a paragraph item
   # either previous item (last) or current item (p) contains blank lines
   if (match(last, /\n[[:space:]]*\n/) || match(p, /\n[[:space:]]*\n/) ) {
-    last = p; p = _block(p);
+    last = p; p = _nblock(p);
   } else {
-    last = p; p = _block(p);
+    last = p; p = _nblock(p);
     sub( /^<p>/, "", p );
     sub( /<\/p>\n/, "", p );
   }
@@ -709,6 +745,7 @@ BEGIN {
   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";
 
   # Buffering of full file ist necessary, e.g. to find reference links
   while (getline) { file = file $0 "\n"; }
@@ -732,5 +769,5 @@ BEGIN {
   # for (n in rl_href) { debug(n " | " rl_href[n] " | " rl_title[n] ); }
 
   # Run Block Processing -> The Actual Markdown!
-  printf "%s", _block( file );
+  printf "%s", _nblock( file );
 }