]> git.plutz.net Git - cgilite/commitdiff
Allow headerless grid tables
authorPaul Hänsch <paul@plutz.net>
Sat, 20 May 2023 16:02:00 +0000 (18:02 +0200)
committerPaul Hänsch <paul@plutz.net>
Sat, 20 May 2023 16:02:00 +0000 (18:02 +0200)
markdown.awk

index 9955bf4ccd42f2b806538e20bd6b8af8b2e15202..100242cb1ebb9b0e2a706a1025dbc552c51c190b 100755 (executable)
@@ -48,7 +48,8 @@
 #   -  ?  Simple table (pandoc)
 #   -  ?  Multiline table (pandoc)
 #   - [x] Grid table (pandoc)
-#   - [x] Pipe table (php md pandoc)
+#     - [x] Headerless
+#   - [x] Pipe table (php md, pandoc)
 # - [x] Line blocks (pandoc)
 # - [x] Task lists (pandoc, custom)
 # - [ ] Definition lists (php md, pandoc)
@@ -64,7 +65,7 @@
 # - [x] ^Superscript^ ~Subscript~ (pandoc)
 # - [-] Bracketed spans (pandoc)
 #   - [-] Inline attributes (pandoc)
-# - [x] Image attributes (custom, pandoc inspired, inline only)
+# - [x] Image attributes (custom, pandoc inspired, not for reference style)
 # - [x] Wiki style links [[PageName]] / [[PageName|Link Text]]
 # - [-] TEX-Math (pandoc)
 # -  ?  Footnotes (php md)
@@ -393,46 +394,62 @@ function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent, attrib
     return "<table>" ttext "</tbody></table>\n" _block(block);
 
   # Grid Tables (pandoc)
-  } else if ( match(block, "^\\+(-+\\+)+\n" \
-                       "(\\|([^\n]+\\|)+\n)+" \
-                        "\\+(:?=+:?\\+)+\n" \
-                      "((\\|([^\n]+\\|)+\n)+" \
-                            "\\+(-+\\+)+(\n|$))+" \
-            ) ) {
+  # (with, and without header)
+  } else if ( match( block, "^\\+(-+\\+)+\n" \
+                            "(\\|([^\n]+\\|)+\n)+" \
+                            "(\\+(:?=+:?\\+)+)\n" \
+                           "((\\|([^\n]+\\|)+\n)+" \
+                             "\\+(-+\\+)+(\n|$))+", \
+                     tread \
+                   ) || \
+              match( block, "^()()()" \
+                            "(\\+(:?-+:?\\+)+)\n" \
+                           "((\\|([^\n]+\\|)+\n)+" \
+                             "\\+(-+\\+)+(\n|$))+", \
+                     tread \
+  ) ) {
     len = RLENGTH; st = RSTART;
     #initialize empty arrays
-    split("", talign); split("", tarray); split("", tread);
+    split("", talign); split("", tarray); split("", tread);
     cols = 0; cnt=0; ttext = "";
 
-    # table header and alignment
-    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|$)/)) \
-      )), tread, /\|/);
-      block = substr(block, match(block, /(\n|$)/) + 1 );
-      for (cnt = 1; cnt < cols; cnt++)
-        tarray[cnt] = tarray[cnt] "\n" tread[cnt];
+    # table alignment
+    cols = split( gensub( /(^\+|\+$)/, "", "g", tread[4] ), talign, /\+/ );
+
+    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]="";
     }
 
-    cols = split( \
-           gensub( /(^\+|\+$)/, "", "g", \
-           substr(block, 1, match(block, /(\n|$)/)) \
-    ), talign, /\+/);
-    block = substr(block, match(block, /(\n|$)/) + 1 );
+    if ( match(block, "^\\+(-+\\+)+\n" \
+                      "(\\|([^\n]+\\|)+\n)+" \
+                       "\\+(:?=+:?\\+)+\n" \
+                     "((\\|([^\n]+\\|)+\n)+" \
+                       "\\+(-+\\+)+(\n|$))+" \
+    ) ) {
+      # 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|$)/)) \
+        )), tread, /\|/);
+        block = substr(block, match(block, /(\n|$)/) + 1 );
+        for (cnt = 1; cnt <= cols; cnt++)
+          tarray[cnt] = tarray[cnt] "\n" tread[cnt];
+      }
 
-    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] "\">" _block(tarray[cnt]) "</th>"
+      ttext = ttext "</tr>\n</thead>"
     }
 
-    ttext = "<thead>\n<tr>"
-    for (cnt = 1; cnt < cols; cnt++)
-      ttext = ttext "<th align=\"" talign[cnt] "\">" _block(tarray[cnt]) "</th>"
-    ttext = ttext "</tr>\n</thead><tbody>\n"
+    # table body
+    block = substr(block, match(block, /(\n|$)/) + 1 );
+    ttext = ttext "<tbody>\n"
 
     while ( match(block, /^((\|([^\n]+\|)+\n)+\+(-+\+)+(\n|$))+/ ) ){
       split("", tarray);
@@ -442,13 +459,13 @@ function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent, attrib
                substr(block, 1, match(block, /(\n|$)/)) \
         )), tread, /\|/);
         block = substr(block, match(block, /(\n|$)/) + 1 );
-        for (cnt = 1; cnt < cols; cnt++)
+        for (cnt = 1; cnt <= cols; cnt++)
           tarray[cnt] = tarray[cnt] "\n" tread[cnt];
       }
       block = substr(block, match(block, /(\n|$)/) + 1 );
 
       ttext = ttext "<tr>"
-      for (cnt = 1; cnt < cols; cnt++)
+      for (cnt = 1; cnt <= cols; cnt++)
         ttext = ttext "<td align=\"" talign[cnt] "\">" _block(tarray[cnt]) "</td>"
       ttext = ttext "</tr>\n"
     }