]> git.plutz.net Git - cgilite/blobdiff - markdown.awk
bugfix: do not accidentally strip white spaces from code spans
[cgilite] / markdown.awk
index 100242cb1ebb9b0e2a706a1025dbc552c51c190b..3fa248d25f2877fdc0e6f7eab9a5642635407d75 100755 (executable)
@@ -93,6 +93,7 @@ function URL ( text ) {
   gsub( /&/,  "%26",  text );
   gsub( /"/,  "%22", text );
   gsub( /'/,  "%27", text );
+  gsub( /`/,  "%60", text );
   gsub( /\?/,  "%3F", text );
   gsub( /#/,  "%23", text );
   gsub( /\[/,  "%5B", text );
@@ -115,8 +116,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,17 +131,17 @@ 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 ) )
     }
 
   # Wiki style links
-  } else if ( match( line, /^\[\[([^\]\|]+)(\|([^\]]+))?\]\]/) ) {
+  } else if ( match( line, /^\[\[([^]|]+)(\|[^]]+)?\]\]/) ) {
     len = RLENGTH;
-    href = gensub(/^\[\[([^\]\|]+)(\|([^\]]+))?\]\]/, "\\1", 1, substr(line, 1, len) );
-    text = gensub(/^\[\[([^\]\|]+)(\|([^\]]+))?\]\]/, "\\3", 1, substr(line, 1, len) );
+    href = gensub(/^\[\[([^]|]+)(\|([^]]+))?\]\]/, "\\1", 1, substr(line, 1, len) );
+    text = gensub(/^\[\[([^]|]+)(\|([^]]+))?\]\]/, "\\3", 1, substr(line, 1, len) );
     if ( ! text ) text = href;
     return "<a href=\"" URL(href) "\">" HTML(text) "</a>" inline( substr( line, len + 1) );
 
@@ -399,22 +400,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 +436,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++)