]> git.plutz.net Git - cgilite/commitdiff
Implemented Pipe Tables
authorPaul Hänsch <paul@plutz.net>
Sun, 26 Jun 2022 12:23:08 +0000 (14:23 +0200)
committerPaul Hänsch <paul@plutz.net>
Sun, 26 Jun 2022 12:23:08 +0000 (14:23 +0200)
markdown.awk

index 540292c5cfbedd31875114e7d9cca073f3d629ff..6271996440dab3b37132f8eccdecbf6094a4db99 100755 (executable)
@@ -46,7 +46,7 @@
 #   -  ?  Simple table (pandoc)
 #   -  ?  Multiline table (pandoc)
 #   -  ?  Grid table (pandoc)
 #   -  ?  Simple table (pandoc)
 #   -  ?  Multiline table (pandoc)
 #   -  ?  Grid table (pandoc)
-#   -  ?  Pipe table (php md pandoc)
+#   - [x] Pipe table (php md pandoc)
 # - [x] Line blocks (pandoc)
 # - [x] Task lists (pandoc, custom)
 # - [ ] Definition lists (php md, pandoc)
 # - [x] Line blocks (pandoc)
 # - [x] Task lists (pandoc, custom)
 # - [ ] Definition lists (php md, pandoc)
@@ -296,6 +296,53 @@ function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent, attrib
     return "<blockquote>\n" _block( gensub( /(^|\n)> /, "\n", "g", substr(block, 1, st - 1) ) ) "</blockquote>\n\n" \
            _block( substr(block, st + len) );
 
     return "<blockquote>\n" _block( gensub( /(^|\n)> /, "\n", "g", substr(block, 1, st - 1) ) ) "</blockquote>\n\n" \
            _block( substr(block, st + len) );
 
+  # Pipe Tables (pandoc / php md / gfm )
+  } else if ( match(block, "^((\\|)?([^\n]+\\|)+[^\n]+(\\|)?)\n" \
+                           "((\\|)?:?(-+:?[\\|+])+:?-+:?(\\|)?)\n" \
+                           "((\\|)?([^\n]+\\|)+[^\n]+(\\|)?(\n|$))+" ) ) {
+    len = RLENGTH; st = RSTART;
+    #initialize empty arrays
+    split("", talign); split("", tarray);
+    cols = 0; ttext = ""; cnt=0;
+
+    # table header and alignment
+    split( gensub( /(^\||\|$)/, "", "g", \
+           gensub( /(^|[^\\])\\\|/, "\\1\\&#x7C;", "g", \
+           substr(block, 1, match(block, /(\n|$)/)) \
+    )), tarray, /\|/);
+    block = substr(block, match(block, /(\n|$)/) + 1 );
+    cols = split( \
+           gensub( /(^\||\|$)/, "", "g", \
+           substr(block, 1, match(block, /(\n|$)/)) \
+    ), talign, /[+\|]/);
+    block = substr(block, match(block, /(\n|$)/) + 1 );
+
+    for( cnt = 1; cnt < cols; cnt++ ) {
+           if (match(talign[cnt], /:-+:/)) talign[cnt]="center";
+      else if (match(talign[cnt],  /-+:/)) talign[cnt]="right";
+      else if (match(talign[cnt],  /:-+/)) talign[cnt]="left";
+      else talign[cnt]="";
+    }
+
+    ttext = "<thead>\n<tr>"
+    for (cnt = 1; cnt < cols; cnt++)
+      ttext = ttext "<th align=\"" talign[cnt] "\">" inline(tarray[cnt]) "</th>"
+    ttext = ttext "</tr>\n</thead><tbody>\n"
+
+    while ( match(block, "^((\\|)?([^\n]+\\|)+[^\n]+(\\|)?(\n|$))+" ) ){
+      split( gensub( /(^\||\|$)/, "", "g", \
+             gensub( /(^|[^\\])\\\|/, "\\1\\&#x7C;", "g", \
+             substr(block, 1, match(block, /(\n|$)/)) \
+      )), tarray, /\|/);
+      block = substr(block, match(block, /(\n|$)/) + 1 );
+
+      ttext = ttext "<tr>"
+      for (cnt = 1; cnt < cols; cnt++)
+        ttext = ttext "<td align=\"" talign[cnt] "\">" inline(tarray[cnt]) "</td>"
+      ttext = ttext "</tr>\n"
+    }
+    return "<table>" ttext "</tbody></table>\n" _block( substr(block, len + 1) );
+
   # Line Blocks (pandoc)
   } else if ( match(block, /^\| [^\n]*(\n|$)(\| [^\n]*(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
     len = RLENGTH; st = RSTART;
   # Line Blocks (pandoc)
   } else if ( match(block, /^\| [^\n]*(\n|$)(\| [^\n]*(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
     len = RLENGTH; st = RSTART;