Commit e6e2ea4c5a5b972779d9222c4f66b5a693444ce2

Martin Mitas 2018-06-11T11:43:47

md_analyze_line: Fix mixing list and table parsing. If table header underline is not nested the same way as the preceding line (i.e. the wannabe table header line), then it cannot form a table. Fixes #41.

diff --git a/md4c/md4c.c b/md4c/md4c.c
index 1646032..0efa34f 100644
--- a/md4c/md4c.c
+++ b/md4c/md4c.c
@@ -5549,12 +5549,6 @@ redo:
         }
     }
 
-    /* Check whether we are table continuation. */
-    if(pivot_line->type == MD_LINE_TABLE  &&  md_is_table_row(ctx, off, &off)) {
-        line->type = MD_LINE_TABLE;
-        goto done;
-    }
-
     /* Check for "brother" container. I.e. whether we are another list item
      * in already started list. */
     if(n_parents < ctx->n_containers  &&  n_brothers + n_children == 0) {
@@ -5646,6 +5640,14 @@ redo:
         }
     }
 
+    /* Check whether we are table continuation. */
+    if(pivot_line->type == MD_LINE_TABLE  &&  md_is_table_row(ctx, off, &off)  &&
+       n_parents == ctx->n_containers)
+    {
+        line->type = MD_LINE_TABLE;
+        goto done;
+    }
+
     /* Check for ATX header. */
     if(line->indent < ctx->code_indent_offset  &&  CH(off) == _T('#')) {
         unsigned level;
@@ -5689,7 +5691,8 @@ redo:
 
     /* Check for table underline. */
     if((ctx->r.flags & MD_FLAG_TABLES)  &&  pivot_line->type == MD_LINE_TEXT  &&
-       (CH(off) == _T('|') || CH(off) == _T('-') || CH(off) == _T(':')))
+       (CH(off) == _T('|') || CH(off) == _T('-') || CH(off) == _T(':'))  &&
+       n_parents == ctx->n_containers)
     {
         unsigned col_count;
 
diff --git a/test/tables.txt b/test/tables.txt
index 460fc2c..fc7a4da 100644
--- a/test/tables.txt
+++ b/test/tables.txt
@@ -270,3 +270,42 @@ baz      | qux
 quux     | quuz</p>
 
 ````````````````````````````````
+
+
+## GitHub Issues
+
+### [Issue 41](https://github.com/mity/md4c/issues/41)
+```````````````````````````````` example
+* x|x
+---|---
+.
+<ul>
+<li>x|x
+---|---</li>
+</ul>
+````````````````````````````````
+(Not a table, because the underline has wrong indentation and is not part of the
+list item.)
+
+```````````````````````````````` example
+* x|x
+  ---|---
+x|x
+.
+<ul>
+<li><table>
+<thead>
+<tr>
+<th>x</th>
+<th>x</th>
+</tr>
+</thead>
+<tbody>
+</tbody>
+</table>
+</li>
+</ul>
+<p>x|x</p>
+````````````````````````````````
+(Here the underline has the right indentation so the table is detected.
+But the last line is not part of it due its indentation.)