Commit a997cb21bf828479afb9377d7ebddb8633fc5d20

Martin Mitas 2020-10-18T09:34:10

Add MD_BLOCK_TABLE_DETAIL. This allows renderers to have the info about table dimension (table column and row count) in advance and e.g. simplify their memory allocation strategy.

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2dd35ee..4ab5de4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,27 +4,33 @@
 
 ## Next Version (Work in Progress)
 
+Changes:
+
+ * Add `MD_TABLE_DETAIL` structure into the API. The structure describes column
+   count and row count of the table, and pointer to it is passed into the
+   application-provided block callback with the `MD_BLOCK_TABLE` block type.
+
 Fixes:
 
-* [#131](https://github.com/mity/md4c/issues/131):
-  Fix handling of a reference image nested in a reference link.
+ * [#131](https://github.com/mity/md4c/issues/131):
+   Fix handling of a reference image nested in a reference link.
 
-* [#135](https://github.com/mity/md4c/issues/135):
-  Handle unmatched parenthesis pairs inside a permissive URL and WWW auto-links
-  in a way more compatible with the GFM.
+ * [#135](https://github.com/mity/md4c/issues/135):
+   Handle unmatched parenthesis pairs inside a permissive URL and WWW auto-links
+   in a way more compatible with the GFM.
 
 
 ## Version 0.4.6
 
 Fixes:
 
-* [#130](https://github.com/mity/md4c/issues/130):
-  Fix `ISANYOF` macro, which could provide unexpected results when encountering
-  zero byte in the input text; in some cases leading to broken internal state
-  of the parser.
+ * [#130](https://github.com/mity/md4c/issues/130):
+   Fix `ISANYOF` macro, which could provide unexpected results when encountering
+   zero byte in the input text; in some cases leading to broken internal state
+   of the parser.
 
-  The bug could result in denial of service and possibly also to other security
-  implications. Applications are advised to update to 0.4.6.
+   The bug could result in denial of service and possibly also to other security
+   implications. Applications are advised to update to 0.4.6.
 
 
 ## Version 0.4.5
diff --git a/src/md4c.c b/src/md4c.c
index 12ed351..0cc905d 100644
--- a/src/md4c.c
+++ b/src/md4c.c
@@ -4510,7 +4510,7 @@ md_process_table_block_contents(MD_CTX* ctx, int col_count, const MD_LINE* lines
     MD_ENTER_BLOCK(MD_BLOCK_TBODY, NULL);
     for(i = 2; i < n_lines; i++) {
         MD_CHECK(md_process_table_row(ctx, MD_BLOCK_TD,
-                        lines[i].beg, lines[i].end, align, col_count));
+                 lines[i].beg, lines[i].end, align, col_count));
     }
     MD_LEAVE_BLOCK(MD_BLOCK_TBODY, NULL);
 
@@ -4681,6 +4681,7 @@ md_process_leaf_block(MD_CTX* ctx, const MD_BLOCK* block)
     union {
         MD_BLOCK_H_DETAIL header;
         MD_BLOCK_CODE_DETAIL code;
+        MD_BLOCK_TABLE_DETAIL table;
     } det;
     MD_ATTRIBUTE_BUILD info_build;
     MD_ATTRIBUTE_BUILD lang_build;
@@ -4709,6 +4710,12 @@ md_process_leaf_block(MD_CTX* ctx, const MD_BLOCK* block)
             }
             break;
 
+        case MD_BLOCK_TABLE:
+            det.table.col_count = block->data;
+            det.table.head_row_count = 1;
+            det.table.body_row_count = block->n_lines - 2;
+            break;
+
         default:
             /* Noop. */
             break;
diff --git a/src/md4c.h b/src/md4c.h
index 8bba712..95f78f9 100644
--- a/src/md4c.h
+++ b/src/md4c.h
@@ -91,7 +91,8 @@ typedef enum MD_BLOCKTYPE {
     MD_BLOCK_P,
 
     /* <table>...</table> and its contents.
-     * Detail: Structure MD_BLOCK_TD_DETAIL (used with MD_BLOCK_TH and MD_BLOCK_TD)
+     * Detail: Structure MD_BLOCK_TABLE_DETAIL (for MD_BLOCK_TABLE),
+     *         structure MD_BLOCK_TD_DETAIL (for MD_BLOCK_TH and MD_BLOCK_TD)
      * Note all of these are used only if extension MD_FLAG_TABLES is enabled. */
     MD_BLOCK_TABLE,
     MD_BLOCK_THEAD,
@@ -267,6 +268,13 @@ typedef struct MD_BLOCK_CODE_DETAIL {
     MD_CHAR fence_char;     /* The character used for fenced code block; or zero for indented code block. */
 } MD_BLOCK_CODE_DETAIL;
 
+/* Detailed info for MD_BLOCK_TABLE. */
+typedef struct MD_BLOCK_TABLE_DETAIL {
+    unsigned col_count;         /* Count of columns in the table. */
+    unsigned head_row_count;    /* Count of rows in the table header (currently always 1) */
+    unsigned body_row_count;    /* Count of rows in the table body */
+} MD_BLOCK_TABLE_DETAIL;
+
 /* Detailed info for MD_BLOCK_TH and MD_BLOCK_TD. */
 typedef struct MD_BLOCK_TD_DETAIL {
     MD_ALIGN align;