md_process_table_row: Enforce that all trable raws have the same count of cells. The count of columns is derived from the table header underline. If any other table line (i.e. the header line, or body line) has too few cells, additional empty cells are generated automatically. If the line has too many cells, the bogus ones are silently ignored. This allows consumers to be more simplistic; and it also seems to match behavior of Github more closely.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
diff --git a/md4c/md4c.c b/md4c/md4c.c
index 2435d6c..70cf252 100644
--- a/md4c/md4c.c
+++ b/md4c/md4c.c
@@ -4137,7 +4137,7 @@ abort:
static int
md_process_table_row(MD_CTX* ctx, MD_BLOCKTYPE cell_type, OFF beg, OFF end,
- const MD_ALIGN* align, int n_align)
+ const MD_ALIGN* align, int col_count)
{
MD_LINE line = { beg, end };
OFF* pipe_offs = NULL;
@@ -4165,21 +4165,17 @@ md_process_table_row(MD_CTX* ctx, MD_BLOCKTYPE cell_type, OFF beg, OFF end,
/* Process cells. */
MD_ENTER_BLOCK(MD_BLOCK_TR, NULL);
j = 0;
- if(beg < pipe_offs[0]) {
- MD_CHECK(md_process_table_cell(ctx, cell_type,
- (j < n_align ? align[j++] : MD_ALIGN_DEFAULT),
- beg, pipe_offs[0]));
- }
- for(i = 0; i < n-1; i++) {
- MD_CHECK(md_process_table_cell(ctx, cell_type,
- (j < n_align ? align[j++] : MD_ALIGN_DEFAULT),
- pipe_offs[i]+1, pipe_offs[i+1]));
- }
- if(pipe_offs[n-1] < end-1) {
- MD_CHECK(md_process_table_cell(ctx, cell_type,
- (j < n_align ? align[j++] : MD_ALIGN_DEFAULT),
- pipe_offs[n-1]+1, end));
- }
+ if(beg < pipe_offs[0] && j < col_count)
+ MD_CHECK(md_process_table_cell(ctx, cell_type, align[j++], beg, pipe_offs[0]));
+ for(i = 0; i < n-1 && j < col_count; i++)
+ MD_CHECK(md_process_table_cell(ctx, cell_type, align[j++], pipe_offs[i]+1, pipe_offs[i+1]));
+ if(pipe_offs[n-1] < end-1 && j < col_count)
+ MD_CHECK(md_process_table_cell(ctx, cell_type, align[j++], pipe_offs[n-1]+1, end));
+ /* Make sure we call enough table cells even if the current table contains
+ * too few of them. */
+ while(j < col_count)
+ MD_CHECK(md_process_table_cell(ctx, cell_type, align[j++], 0, 0));
+
MD_LEAVE_BLOCK(MD_BLOCK_TR, NULL);
abort:
@@ -4201,8 +4197,8 @@ md_process_table_block_contents(MD_CTX* ctx, int col_count, const MD_LINE* lines
int i;
int ret = 0;
- /* At least the line with column names and the table underline have to
- * be present. */
+ /* At least two lines have to be present: The column headers and the line
+ * with the underlines. */
MD_ASSERT(n_lines >= 2);
align = malloc(col_count * sizeof(MD_ALIGN));