Skip to content

Commit

Permalink
Support for table column spans
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugenij-W committed Jan 20, 2021
1 parent 2b6ebdf commit 3104cff
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Fixes:
Fix reference link definition label matching in a case when the label ends
with a Unicode character with non-trivial case folding mapping.

* [#29](https://github.com/mity/md4c/issues/29):
Implement Wiky-style ('|2> text |') table cell-span extension.

## Version 0.4.6

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ extensions:

* With the flag `MD_FLAG_TABLES`, GitHub-style tables are supported.

* With the flag `MD_FLAG_TABLE_CELLSPAN`, wiky-style ('|2> text |') table
cell-spans are supported.

* With the flag `MD_FLAG_TASKLISTS`, GitHub-style task lists are supported.

* With the flag `MD_FLAG_STRIKETHROUGH`, strike-through spans are enabled
Expand Down
4 changes: 4 additions & 0 deletions md2html/md2html.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ static const CMDLINE_OPTION cmdline_options[] = {
{ 0, "fpermissive-www-autolinks", '.', 0 },
{ 0, "fstrikethrough", 'S', 0 },
{ 0, "ftables", 'T', 0 },
{ 0, "ftable-cellspan", 'J', 0 },
{ 0, "ftasklists", 'X', 0 },
{ 0, "funderline", '_', 0 },
{ 0, "fverbatim-entities", 'E', 0 },
Expand Down Expand Up @@ -258,6 +259,8 @@ usage(void)
" --fpermissive-email-autolinks\n"
" --fstrikethrough Enable strike-through spans\n"
" --ftables Enable tables\n"
" --ftable-cellspan\n"
" Enable table cell-span extension '|2> text |'\n"
" --ftasklists Enable task lists\n"
" --funderline Enable underline spans\n"
" --fwiki-links Enable wiki links\n"
Expand Down Expand Up @@ -322,6 +325,7 @@ cmdline_callback(int opt, char const* value, void* data)
case '@': parser_flags |= MD_FLAG_PERMISSIVEEMAILAUTOLINKS; break;
case 'V': parser_flags |= MD_FLAG_PERMISSIVEAUTOLINKS; break;
case 'T': parser_flags |= MD_FLAG_TABLES; break;
case 'J': parser_flags |= MD_FLAG_TABLE_CELLSPAN; break;
case 'S': parser_flags |= MD_FLAG_STRIKETHROUGH; break;
case 'L': parser_flags |= MD_FLAG_LATEXMATHSPANS; break;
case 'K': parser_flags |= MD_FLAG_WIKILINKS; break;
Expand Down
17 changes: 13 additions & 4 deletions src/md4c-html.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,24 @@ render_open_code_block(MD_HTML* r, const MD_BLOCK_CODE_DETAIL* det)
static void
render_open_td_block(MD_HTML* r, const MD_CHAR* cell_type, const MD_BLOCK_TD_DETAIL* det)
{
MD_CHAR tail[2];

RENDER_VERBATIM(r, "<");
RENDER_VERBATIM(r, cell_type);

switch(det->align) {
case MD_ALIGN_LEFT: RENDER_VERBATIM(r, " align=\"left\">"); break;
case MD_ALIGN_CENTER: RENDER_VERBATIM(r, " align=\"center\">"); break;
case MD_ALIGN_RIGHT: RENDER_VERBATIM(r, " align=\"right\">"); break;
default: RENDER_VERBATIM(r, ">"); break;
case MD_ALIGN_LEFT: RENDER_VERBATIM(r, " align=\"left\""); break;
case MD_ALIGN_CENTER: RENDER_VERBATIM(r, " align=\"center\""); break;
case MD_ALIGN_RIGHT: RENDER_VERBATIM(r, " align=\"right\""); break;
case MD_ALIGN_DEFAULT: break;
}
if (det->colspan > 1) {
RENDER_VERBATIM(r, " colspan=\"");
tail[0] = '0' + det->colspan;
tail[1] = '\"';
render_verbatim(r, tail, 2);
}
RENDER_VERBATIM(r, ">");
}

static void
Expand Down
41 changes: 30 additions & 11 deletions src/md4c.c
Original file line number Diff line number Diff line change
Expand Up @@ -4410,24 +4410,22 @@ md_analyze_table_alignment(MD_CTX* ctx, OFF beg, OFF end, MD_ALIGN* align, int n
static int md_process_normal_block_contents(MD_CTX* ctx, const MD_LINE* lines, int n_lines);

static int
md_process_table_cell(MD_CTX* ctx, MD_BLOCKTYPE cell_type, MD_ALIGN align, OFF beg, OFF end)
md_process_table_cell(MD_CTX* ctx, MD_BLOCKTYPE cell_type, MD_BLOCK_TD_DETAIL *det, OFF beg, OFF end)
{
MD_LINE line;
MD_BLOCK_TD_DETAIL det;
int ret = 0;

while(beg < end && ISWHITESPACE(beg))
beg++;
while(end > beg && ISWHITESPACE(end-1))
end--;

det.align = align;
line.beg = beg;
line.end = end;

MD_ENTER_BLOCK(cell_type, &det);
MD_ENTER_BLOCK(cell_type, det);
MD_CHECK(md_process_normal_block_contents(ctx, &line, 1));
MD_LEAVE_BLOCK(cell_type, &det);
MD_LEAVE_BLOCK(cell_type, det);

abort:
return ret;
Expand All @@ -4438,6 +4436,10 @@ md_process_table_row(MD_CTX* ctx, MD_BLOCKTYPE cell_type, OFF beg, OFF end,
const MD_ALIGN* align, int col_count)
{
MD_LINE line;
MD_LINE cell;
MD_BLOCK_TD_DETAIL det;

unsigned is_spans;
OFF* pipe_offs = NULL;
int i, j, k, n;
int ret = 0;
Expand All @@ -4464,19 +4466,36 @@ md_process_table_row(MD_CTX* ctx, MD_BLOCKTYPE cell_type, OFF beg, OFF end,
MD_MARK* mark = &ctx->marks[i];
pipe_offs[j++] = mark->end;
}
pipe_offs[j++] = end+1;
pipe_offs[j] = end+1;

is_spans = ctx->parser.flags & MD_FLAG_TABLE_CELLSPAN;
/* Process cells. */
MD_ENTER_BLOCK(MD_BLOCK_TR, NULL);
k = 0;
for(i = 0; i < j-1 && k < col_count; i++) {
if(pipe_offs[i] < pipe_offs[i+1]-1)
MD_CHECK(md_process_table_cell(ctx, cell_type, align[k++], pipe_offs[i], pipe_offs[i+1]-1));
for(i = 0; i < j && k < col_count; i++) {
cell.beg = pipe_offs[i];
cell.end = pipe_offs[i+1]-1;

if(cell.beg < cell.end) {
det.colspan = 0;
if (is_spans && cell.end - cell.beg >= 2 &&
ISDIGIT(cell.beg) && CH(cell.beg+1) == '>') {
det.colspan = CH(cell.beg) - '0';
cell.beg += 2;
}
det.align = align[k++];
MD_CHECK(md_process_table_cell(ctx, cell_type, &det, cell.beg, cell.end));
if (is_spans && det.colspan > 1)
k += det.colspan - 1;
}
}
/* Make sure we call enough table cells even if the current table contains
* too few of them. */
while(k < col_count)
MD_CHECK(md_process_table_cell(ctx, cell_type, align[k++], 0, 0));
det.colspan = 0;
while(k < col_count) {
det.align = align[k++];
MD_CHECK(md_process_table_cell(ctx, cell_type, &det, 0, 0));
}
MD_LEAVE_BLOCK(MD_BLOCK_TR, NULL);

abort:
Expand Down
2 changes: 2 additions & 0 deletions src/md4c.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ typedef struct MD_BLOCK_TABLE_DETAIL {
/* Detailed info for MD_BLOCK_TH and MD_BLOCK_TD. */
typedef struct MD_BLOCK_TD_DETAIL {
MD_ALIGN align;
int colspan;
} MD_BLOCK_TD_DETAIL;

/* Detailed info for MD_SPAN_A. */
Expand Down Expand Up @@ -316,6 +317,7 @@ typedef struct MD_SPAN_WIKILINK {
#define MD_FLAG_LATEXMATHSPANS 0x1000 /* Enable $ and $$ containing LaTeX equations. */
#define MD_FLAG_WIKILINKS 0x2000 /* Enable wiki links extension. */
#define MD_FLAG_UNDERLINE 0x4000 /* Enable underline extension (and disables '_' for normal emphasis). */
#define MD_FLAG_TABLE_CELLSPAN 0x8000 /* Enable table colspan extension '|2> 2-cell-span |'. */

#define MD_FLAG_PERMISSIVEAUTOLINKS (MD_FLAG_PERMISSIVEEMAILAUTOLINKS | MD_FLAG_PERMISSIVEURLAUTOLINKS | MD_FLAG_PERMISSIVEWWWAUTOLINKS)
#define MD_FLAG_NOHTML (MD_FLAG_NOHTMLBLOCKS | MD_FLAG_NOHTMLSPANS)
Expand Down

0 comments on commit 3104cff

Please sign in to comment.