Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ bstrlib.txt
build
cmark.dSYM/*
cmark
.vscode

# Testing and benchmark
alltests.md
Expand Down
66 changes: 52 additions & 14 deletions extensions/table.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ cmark_node_type CMARK_NODE_TABLE, CMARK_NODE_TABLE_ROW,

typedef struct {
uint16_t n_columns;
int paragraph_offset;
cmark_llist *cells;
} table_row;

Expand Down Expand Up @@ -115,6 +116,7 @@ static table_row *row_from_string(cmark_syntax_extension *self,
int len) {
table_row *row = NULL;
bufsize_t cell_matched = 1, pipe_matched = 1, offset;
int cell_end_offset;

row = (table_row *)parser->mem->calloc(1, sizeof(table_row));
row->n_columns = 0;
Expand All @@ -129,20 +131,32 @@ static table_row *row_from_string(cmark_syntax_extension *self,
pipe_matched = scan_table_cell_end(string, len, offset + cell_matched);

if (cell_matched || pipe_matched) {
cmark_strbuf *cell_buf = unescape_pipes(parser->mem, string + offset,
cell_matched);
cmark_strbuf_trim(cell_buf);

node_cell *cell = (node_cell *)parser->mem->calloc(1, sizeof(*cell));
cell->buf = cell_buf;
cell->start_offset = offset;
cell->end_offset = offset + cell_matched - 1;
while (cell->start_offset > 0 && string[cell->start_offset - 1] != '|') {
--cell->start_offset;
++cell->internal_offset;
cell_end_offset = offset + cell_matched - 1;

if (string[cell_end_offset] == '\n' || string[cell_end_offset] == '\r') {
row->paragraph_offset = cell_end_offset;

cmark_llist_free_full(parser->mem, row->cells, (cmark_free_func)free_table_cell);
row->cells = NULL;
row->n_columns = 0;
} else {
cmark_strbuf *cell_buf = unescape_pipes(parser->mem, string + offset,
cell_matched);
cmark_strbuf_trim(cell_buf);

node_cell *cell = (node_cell *)parser->mem->calloc(1, sizeof(*cell));
cell->buf = cell_buf;
cell->start_offset = offset;
cell->end_offset = cell_end_offset;

while (cell->start_offset > 0 && string[cell->start_offset - 1] != '|') {
--cell->start_offset;
++cell->internal_offset;
}

row->n_columns += 1;
row->cells = cmark_llist_append(parser->mem, row->cells, cell);
}
row->n_columns += 1;
row->cells = cmark_llist_append(parser->mem, row->cells, cell);
}

offset += cell_matched + pipe_matched;
Expand All @@ -161,6 +175,25 @@ static table_row *row_from_string(cmark_syntax_extension *self,
return row;
}

static void try_inserting_table_header_paragraph(cmark_parser *parser,
cmark_node *parent_container,
unsigned char *parent_string,
int paragraph_offset) {
cmark_node *paragraph;
cmark_strbuf *paragraph_content;

paragraph = cmark_node_new_with_mem(CMARK_NODE_PARAGRAPH, parser->mem);

paragraph_content = unescape_pipes(parser->mem, parent_string, paragraph_offset);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed redundant type conversions 7f69c4e

cmark_strbuf_trim(paragraph_content);
cmark_node_set_string_content(paragraph, (char *) paragraph_content->ptr);

if (!cmark_node_insert_before(parent_container, paragraph)) {
parser->mem->free(paragraph_content);
parser->mem->free(paragraph);
}
}

static cmark_node *try_opening_table_header(cmark_syntax_extension *self,
cmark_parser *parser,
cmark_node *parent_container,
Expand Down Expand Up @@ -217,6 +250,11 @@ static cmark_node *try_opening_table_header(cmark_syntax_extension *self,
return parent_container;
}

if (header_row->paragraph_offset) {
try_inserting_table_header_paragraph(parser, parent_container, (unsigned char *)parent_string,
header_row->paragraph_offset);
}

cmark_node_set_syntax_extension(parent_container, self);

parent_container->as.opaque = parser->mem->calloc(1, sizeof(node_table));
Expand Down Expand Up @@ -761,4 +799,4 @@ int cmark_gfm_extensions_set_table_row_is_header(cmark_node *node, int is_header

((node_table_row *)node->as.opaque)->is_header = (is_header != 0);
return 1;
}
}
28 changes: 27 additions & 1 deletion test/extensions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Hello!

| _abc_ | セン |
| ----- | ---- |
| this row has a space at the end | |
| this row has a space at the end | |
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| this row has a space at the end | |
| this row has a space at the end | |

This was probably removed by accident.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is "whitespace trimming" on vscode ❤️

| But _**inline elements do**_. | x |

Hi!
Expand Down Expand Up @@ -472,6 +472,32 @@ Here's a link to [Freedom Planet 2][].
</table>
````````````````````````````````

### a table can be recognised when separated from a paragraph of text without an empty line

```````````````````````````````` example
123
456
| a | b |
| ---| --- |
d | e
.
<p>123
456</p>
<table>
<thead>
<tr>
<th>a</th>
<th>b</th>
</tr>
</thead>
<tbody>
<tr>
<td>d</td>
<td>e</td>
</tr>
</tbody>
</table>
````````````````````````````````

## Strikethroughs

Expand Down