commonmarker 0.23.9 → 1.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +70 -212
  3. data/commonmarker.gemspec +34 -31
  4. data/ext/commonmarker/Cargo.toml +12 -0
  5. data/ext/commonmarker/_util.rb +102 -0
  6. data/ext/commonmarker/extconf.rb +4 -5
  7. data/ext/commonmarker/src/comrak_options.rs +107 -0
  8. data/ext/commonmarker/src/lib.rs +27 -0
  9. data/lib/commonmarker/config.rb +57 -38
  10. data/lib/commonmarker/extension.rb +14 -0
  11. data/lib/commonmarker/renderer.rb +1 -127
  12. data/lib/commonmarker/version.rb +2 -2
  13. data/lib/commonmarker.rb +14 -29
  14. metadata +37 -181
  15. data/Rakefile +0 -109
  16. data/bin/commonmarker +0 -118
  17. data/ext/commonmarker/arena.c +0 -104
  18. data/ext/commonmarker/autolink.c +0 -508
  19. data/ext/commonmarker/autolink.h +0 -8
  20. data/ext/commonmarker/blocks.c +0 -1620
  21. data/ext/commonmarker/buffer.c +0 -278
  22. data/ext/commonmarker/buffer.h +0 -116
  23. data/ext/commonmarker/case_fold_switch.inc +0 -4327
  24. data/ext/commonmarker/chunk.h +0 -135
  25. data/ext/commonmarker/cmark-gfm-core-extensions.h +0 -54
  26. data/ext/commonmarker/cmark-gfm-extension_api.h +0 -737
  27. data/ext/commonmarker/cmark-gfm-extensions_export.h +0 -42
  28. data/ext/commonmarker/cmark-gfm.h +0 -833
  29. data/ext/commonmarker/cmark-gfm_export.h +0 -42
  30. data/ext/commonmarker/cmark-gfm_version.h +0 -7
  31. data/ext/commonmarker/cmark.c +0 -55
  32. data/ext/commonmarker/cmark_ctype.c +0 -44
  33. data/ext/commonmarker/cmark_ctype.h +0 -33
  34. data/ext/commonmarker/commonmark.c +0 -514
  35. data/ext/commonmarker/commonmarker.c +0 -1308
  36. data/ext/commonmarker/commonmarker.h +0 -16
  37. data/ext/commonmarker/config.h +0 -76
  38. data/ext/commonmarker/core-extensions.c +0 -27
  39. data/ext/commonmarker/entities.inc +0 -2138
  40. data/ext/commonmarker/ext_scanners.c +0 -879
  41. data/ext/commonmarker/ext_scanners.h +0 -24
  42. data/ext/commonmarker/footnotes.c +0 -63
  43. data/ext/commonmarker/footnotes.h +0 -27
  44. data/ext/commonmarker/houdini.h +0 -57
  45. data/ext/commonmarker/houdini_href_e.c +0 -100
  46. data/ext/commonmarker/houdini_html_e.c +0 -66
  47. data/ext/commonmarker/houdini_html_u.c +0 -149
  48. data/ext/commonmarker/html.c +0 -502
  49. data/ext/commonmarker/html.h +0 -27
  50. data/ext/commonmarker/inlines.c +0 -1788
  51. data/ext/commonmarker/inlines.h +0 -29
  52. data/ext/commonmarker/iterator.c +0 -159
  53. data/ext/commonmarker/iterator.h +0 -26
  54. data/ext/commonmarker/latex.c +0 -468
  55. data/ext/commonmarker/linked_list.c +0 -37
  56. data/ext/commonmarker/man.c +0 -274
  57. data/ext/commonmarker/map.c +0 -129
  58. data/ext/commonmarker/map.h +0 -44
  59. data/ext/commonmarker/node.c +0 -1044
  60. data/ext/commonmarker/node.h +0 -166
  61. data/ext/commonmarker/parser.h +0 -59
  62. data/ext/commonmarker/plaintext.c +0 -218
  63. data/ext/commonmarker/plugin.c +0 -36
  64. data/ext/commonmarker/plugin.h +0 -34
  65. data/ext/commonmarker/references.c +0 -43
  66. data/ext/commonmarker/references.h +0 -26
  67. data/ext/commonmarker/registry.c +0 -63
  68. data/ext/commonmarker/registry.h +0 -24
  69. data/ext/commonmarker/render.c +0 -213
  70. data/ext/commonmarker/render.h +0 -62
  71. data/ext/commonmarker/scanners.c +0 -14056
  72. data/ext/commonmarker/scanners.h +0 -70
  73. data/ext/commonmarker/scanners.re +0 -341
  74. data/ext/commonmarker/strikethrough.c +0 -167
  75. data/ext/commonmarker/strikethrough.h +0 -9
  76. data/ext/commonmarker/syntax_extension.c +0 -149
  77. data/ext/commonmarker/syntax_extension.h +0 -34
  78. data/ext/commonmarker/table.c +0 -872
  79. data/ext/commonmarker/table.h +0 -12
  80. data/ext/commonmarker/tagfilter.c +0 -60
  81. data/ext/commonmarker/tagfilter.h +0 -8
  82. data/ext/commonmarker/tasklist.c +0 -156
  83. data/ext/commonmarker/tasklist.h +0 -8
  84. data/ext/commonmarker/utf8.c +0 -317
  85. data/ext/commonmarker/utf8.h +0 -35
  86. data/ext/commonmarker/xml.c +0 -182
  87. data/lib/commonmarker/node/inspect.rb +0 -47
  88. data/lib/commonmarker/node.rb +0 -83
  89. data/lib/commonmarker/renderer/html_renderer.rb +0 -256
@@ -1,872 +0,0 @@
1
- #include <cmark-gfm-extension_api.h>
2
- #include <html.h>
3
- #include <inlines.h>
4
- #include <parser.h>
5
- #include <references.h>
6
- #include <string.h>
7
- #include <render.h>
8
-
9
- #include "ext_scanners.h"
10
- #include "strikethrough.h"
11
- #include "table.h"
12
- #include "cmark-gfm-core-extensions.h"
13
-
14
- // Custom node flag, initialized in `create_table_extension`.
15
- static cmark_node_internal_flags CMARK_NODE__TABLE_VISITED;
16
-
17
- cmark_node_type CMARK_NODE_TABLE, CMARK_NODE_TABLE_ROW,
18
- CMARK_NODE_TABLE_CELL;
19
-
20
- typedef struct {
21
- cmark_strbuf *buf;
22
- int start_offset, end_offset, internal_offset;
23
- } node_cell;
24
-
25
- typedef struct {
26
- uint16_t n_columns;
27
- int paragraph_offset;
28
- node_cell *cells;
29
- } table_row;
30
-
31
- typedef struct {
32
- uint16_t n_columns;
33
- uint8_t *alignments;
34
- } node_table;
35
-
36
- typedef struct {
37
- bool is_header;
38
- } node_table_row;
39
-
40
- static void free_table_cell(cmark_mem *mem, node_cell *cell) {
41
- cmark_strbuf_free((cmark_strbuf *)cell->buf);
42
- mem->free(cell->buf);
43
- }
44
-
45
- static void free_row_cells(cmark_mem *mem, table_row *row) {
46
- while (row->n_columns > 0) {
47
- free_table_cell(mem, &row->cells[--row->n_columns]);
48
- }
49
- mem->free(row->cells);
50
- row->cells = NULL;
51
- }
52
-
53
- static void free_table_row(cmark_mem *mem, table_row *row) {
54
- if (!row)
55
- return;
56
-
57
- free_row_cells(mem, row);
58
- mem->free(row);
59
- }
60
-
61
- static void free_node_table(cmark_mem *mem, void *ptr) {
62
- node_table *t = (node_table *)ptr;
63
- mem->free(t->alignments);
64
- mem->free(t);
65
- }
66
-
67
- static void free_node_table_row(cmark_mem *mem, void *ptr) {
68
- mem->free(ptr);
69
- }
70
-
71
- static int get_n_table_columns(cmark_node *node) {
72
- if (!node || node->type != CMARK_NODE_TABLE)
73
- return -1;
74
-
75
- return (int)((node_table *)node->as.opaque)->n_columns;
76
- }
77
-
78
- static int set_n_table_columns(cmark_node *node, uint16_t n_columns) {
79
- if (!node || node->type != CMARK_NODE_TABLE)
80
- return 0;
81
-
82
- ((node_table *)node->as.opaque)->n_columns = n_columns;
83
- return 1;
84
- }
85
-
86
- static uint8_t *get_table_alignments(cmark_node *node) {
87
- if (!node || node->type != CMARK_NODE_TABLE)
88
- return 0;
89
-
90
- return ((node_table *)node->as.opaque)->alignments;
91
- }
92
-
93
- static int set_table_alignments(cmark_node *node, uint8_t *alignments) {
94
- if (!node || node->type != CMARK_NODE_TABLE)
95
- return 0;
96
-
97
- ((node_table *)node->as.opaque)->alignments = alignments;
98
- return 1;
99
- }
100
-
101
- static cmark_strbuf *unescape_pipes(cmark_mem *mem, unsigned char *string, bufsize_t len)
102
- {
103
- cmark_strbuf *res = (cmark_strbuf *)mem->calloc(1, sizeof(cmark_strbuf));
104
- bufsize_t r, w;
105
-
106
- cmark_strbuf_init(mem, res, len + 1);
107
- cmark_strbuf_put(res, string, len);
108
- cmark_strbuf_putc(res, '\0');
109
-
110
- for (r = 0, w = 0; r < len; ++r) {
111
- if (res->ptr[r] == '\\' && res->ptr[r + 1] == '|')
112
- r++;
113
-
114
- res->ptr[w++] = res->ptr[r];
115
- }
116
-
117
- cmark_strbuf_truncate(res, w);
118
-
119
- return res;
120
- }
121
-
122
- // Adds a new cell to the end of the row. A pointer to the new cell is returned
123
- // for the caller to initialize.
124
- static node_cell* append_row_cell(cmark_mem *mem, table_row *row) {
125
- const uint32_t n_columns = row->n_columns + 1;
126
- // realloc when n_columns is a power of 2
127
- if ((n_columns & (n_columns-1)) == 0) {
128
- // make sure we never wrap row->n_columns
129
- // offset will != len and our exit will clean up as intended
130
- if (n_columns > UINT16_MAX) {
131
- return NULL;
132
- }
133
- // Use realloc to double the size of the buffer.
134
- row->cells = (node_cell *)mem->realloc(row->cells, (2 * n_columns - 1) * sizeof(node_cell));
135
- }
136
- row->n_columns = (uint16_t)n_columns;
137
- return &row->cells[n_columns-1];
138
- }
139
-
140
- static table_row *row_from_string(cmark_syntax_extension *self,
141
- cmark_parser *parser, unsigned char *string,
142
- int len) {
143
- // Parses a single table row. It has the following form:
144
- // `delim? table_cell (delim table_cell)* delim? newline`
145
- // Note that cells are allowed to be empty.
146
- //
147
- // From the GitHub-flavored Markdown specification:
148
- //
149
- // > Each row consists of cells containing arbitrary text, in which inlines
150
- // > are parsed, separated by pipes (|). A leading and trailing pipe is also
151
- // > recommended for clarity of reading, and if there’s otherwise parsing
152
- // > ambiguity.
153
-
154
- table_row *row = NULL;
155
- bufsize_t cell_matched = 1, pipe_matched = 1, offset;
156
- int expect_more_cells = 1;
157
- int row_end_offset = 0;
158
- int int_overflow_abort = 0;
159
-
160
- row = (table_row *)parser->mem->calloc(1, sizeof(table_row));
161
- row->n_columns = 0;
162
- row->cells = NULL;
163
-
164
- // Scan past the (optional) leading pipe.
165
- offset = scan_table_cell_end(string, len, 0);
166
-
167
- // Parse the cells of the row. Stop if we reach the end of the input, or if we
168
- // cannot detect any more cells.
169
- while (offset < len && expect_more_cells) {
170
- cell_matched = scan_table_cell(string, len, offset);
171
- pipe_matched = scan_table_cell_end(string, len, offset + cell_matched);
172
-
173
- if (cell_matched || pipe_matched) {
174
- // We are guaranteed to have a cell, since (1) either we found some
175
- // content and cell_matched, or (2) we found an empty cell followed by a
176
- // pipe.
177
- cmark_strbuf *cell_buf = unescape_pipes(parser->mem, string + offset,
178
- cell_matched);
179
- cmark_strbuf_trim(cell_buf);
180
-
181
- node_cell *cell = append_row_cell(parser->mem, row);
182
- if (!cell) {
183
- int_overflow_abort = 1;
184
- cmark_strbuf_free(cell_buf);
185
- parser->mem->free(cell_buf);
186
- break;
187
- }
188
- cell->buf = cell_buf;
189
- cell->start_offset = offset;
190
- cell->end_offset = offset + cell_matched - 1;
191
- cell->internal_offset = 0;
192
-
193
- while (cell->start_offset > row->paragraph_offset && string[cell->start_offset - 1] != '|') {
194
- --cell->start_offset;
195
- ++cell->internal_offset;
196
- }
197
- }
198
-
199
- offset += cell_matched + pipe_matched;
200
-
201
- if (pipe_matched) {
202
- expect_more_cells = 1;
203
- } else {
204
- // We've scanned the last cell. Check if we have reached the end of the row
205
- row_end_offset = scan_table_row_end(string, len, offset);
206
- offset += row_end_offset;
207
-
208
- // If the end of the row is not the end of the input,
209
- // the row is not a real row but potentially part of the paragraph
210
- // preceding the table.
211
- if (row_end_offset && offset != len) {
212
- row->paragraph_offset = offset;
213
-
214
- free_row_cells(parser->mem, row);
215
-
216
- // Scan past the (optional) leading pipe.
217
- offset += scan_table_cell_end(string, len, offset);
218
-
219
- expect_more_cells = 1;
220
- } else {
221
- expect_more_cells = 0;
222
- }
223
- }
224
- }
225
-
226
- if (offset != len || row->n_columns == 0 || int_overflow_abort) {
227
- free_table_row(parser->mem, row);
228
- row = NULL;
229
- }
230
-
231
- return row;
232
- }
233
-
234
- static void try_inserting_table_header_paragraph(cmark_parser *parser,
235
- cmark_node *parent_container,
236
- unsigned char *parent_string,
237
- int paragraph_offset) {
238
- cmark_node *paragraph;
239
- cmark_strbuf *paragraph_content;
240
-
241
- paragraph = cmark_node_new_with_mem(CMARK_NODE_PARAGRAPH, parser->mem);
242
-
243
- paragraph_content = unescape_pipes(parser->mem, parent_string, paragraph_offset);
244
- cmark_strbuf_trim(paragraph_content);
245
- cmark_node_set_string_content(paragraph, (char *) paragraph_content->ptr);
246
- cmark_strbuf_free(paragraph_content);
247
- parser->mem->free(paragraph_content);
248
-
249
- if (!cmark_node_insert_before(parent_container, paragraph)) {
250
- parser->mem->free(paragraph);
251
- }
252
- }
253
-
254
- static cmark_node *try_opening_table_header(cmark_syntax_extension *self,
255
- cmark_parser *parser,
256
- cmark_node *parent_container,
257
- unsigned char *input, int len) {
258
- cmark_node *table_header;
259
- table_row *header_row = NULL;
260
- table_row *marker_row = NULL;
261
- node_table_row *ntr;
262
- const char *parent_string;
263
- uint16_t i;
264
-
265
- if (parent_container->flags & CMARK_NODE__TABLE_VISITED) {
266
- return parent_container;
267
- }
268
-
269
- if (!scan_table_start(input, len, cmark_parser_get_first_nonspace(parser))) {
270
- return parent_container;
271
- }
272
-
273
- // Since scan_table_start was successful, we must have a marker row.
274
- marker_row = row_from_string(self, parser,
275
- input + cmark_parser_get_first_nonspace(parser),
276
- len - cmark_parser_get_first_nonspace(parser));
277
- // assert may be optimized out, don't rely on it for security boundaries
278
- if (!marker_row) {
279
- return parent_container;
280
- }
281
-
282
- assert(marker_row);
283
-
284
- cmark_arena_push();
285
-
286
- // Check for a matching header row. We call `row_from_string` with the entire
287
- // (potentially long) parent container as input, but this should be safe since
288
- // `row_from_string` bails out early if it does not find a row.
289
- parent_string = cmark_node_get_string_content(parent_container);
290
- header_row = row_from_string(self, parser, (unsigned char *)parent_string,
291
- (int)strlen(parent_string));
292
- if (!header_row || header_row->n_columns != marker_row->n_columns) {
293
- free_table_row(parser->mem, marker_row);
294
- free_table_row(parser->mem, header_row);
295
- cmark_arena_pop();
296
- parent_container->flags |= CMARK_NODE__TABLE_VISITED;
297
- return parent_container;
298
- }
299
-
300
- if (cmark_arena_pop()) {
301
- marker_row = row_from_string(
302
- self, parser, input + cmark_parser_get_first_nonspace(parser),
303
- len - cmark_parser_get_first_nonspace(parser));
304
- header_row = row_from_string(self, parser, (unsigned char *)parent_string,
305
- (int)strlen(parent_string));
306
- // row_from_string can return NULL, add additional check to ensure n_columns match
307
- if (!marker_row || !header_row || header_row->n_columns != marker_row->n_columns) {
308
- free_table_row(parser->mem, marker_row);
309
- free_table_row(parser->mem, header_row);
310
- return parent_container;
311
- }
312
- }
313
-
314
- if (!cmark_node_set_type(parent_container, CMARK_NODE_TABLE)) {
315
- free_table_row(parser->mem, header_row);
316
- free_table_row(parser->mem, marker_row);
317
- return parent_container;
318
- }
319
-
320
- if (header_row->paragraph_offset) {
321
- try_inserting_table_header_paragraph(parser, parent_container, (unsigned char *)parent_string,
322
- header_row->paragraph_offset);
323
- }
324
-
325
- cmark_node_set_syntax_extension(parent_container, self);
326
- parent_container->as.opaque = parser->mem->calloc(1, sizeof(node_table));
327
- set_n_table_columns(parent_container, header_row->n_columns);
328
-
329
- // allocate alignments based on marker_row->n_columns
330
- // since we populate the alignments array based on marker_row->cells
331
- uint8_t *alignments =
332
- (uint8_t *)parser->mem->calloc(marker_row->n_columns, sizeof(uint8_t));
333
- for (i = 0; i < marker_row->n_columns; ++i) {
334
- node_cell *node = &marker_row->cells[i];
335
- bool left = node->buf->ptr[0] == ':', right = node->buf->ptr[node->buf->size - 1] == ':';
336
-
337
- if (left && right)
338
- alignments[i] = 'c';
339
- else if (left)
340
- alignments[i] = 'l';
341
- else if (right)
342
- alignments[i] = 'r';
343
- }
344
- set_table_alignments(parent_container, alignments);
345
-
346
- table_header =
347
- cmark_parser_add_child(parser, parent_container, CMARK_NODE_TABLE_ROW,
348
- parent_container->start_column);
349
- cmark_node_set_syntax_extension(table_header, self);
350
- table_header->end_column = parent_container->start_column + (int)strlen(parent_string) - 2;
351
- table_header->start_line = table_header->end_line = parent_container->start_line;
352
-
353
- table_header->as.opaque = ntr = (node_table_row *)parser->mem->calloc(1, sizeof(node_table_row));
354
- ntr->is_header = true;
355
-
356
- {
357
- for (i = 0; i < header_row->n_columns; ++i) {
358
- node_cell *cell = &header_row->cells[i];
359
- cmark_node *header_cell = cmark_parser_add_child(parser, table_header,
360
- CMARK_NODE_TABLE_CELL, parent_container->start_column + cell->start_offset);
361
- header_cell->start_line = header_cell->end_line = parent_container->start_line;
362
- header_cell->internal_offset = cell->internal_offset;
363
- header_cell->end_column = parent_container->start_column + cell->end_offset;
364
- cmark_node_set_string_content(header_cell, (char *) cell->buf->ptr);
365
- cmark_node_set_syntax_extension(header_cell, self);
366
- }
367
- }
368
-
369
- cmark_parser_advance_offset(
370
- parser, (char *)input,
371
- (int)strlen((char *)input) - 1 - cmark_parser_get_offset(parser), false);
372
-
373
- free_table_row(parser->mem, header_row);
374
- free_table_row(parser->mem, marker_row);
375
- return parent_container;
376
- }
377
-
378
- static cmark_node *try_opening_table_row(cmark_syntax_extension *self,
379
- cmark_parser *parser,
380
- cmark_node *parent_container,
381
- unsigned char *input, int len) {
382
- cmark_node *table_row_block;
383
- table_row *row;
384
-
385
- if (cmark_parser_is_blank(parser))
386
- return NULL;
387
-
388
- table_row_block =
389
- cmark_parser_add_child(parser, parent_container, CMARK_NODE_TABLE_ROW,
390
- parent_container->start_column);
391
- cmark_node_set_syntax_extension(table_row_block, self);
392
- table_row_block->end_column = parent_container->end_column;
393
- table_row_block->as.opaque = parser->mem->calloc(1, sizeof(node_table_row));
394
-
395
- row = row_from_string(self, parser, input + cmark_parser_get_first_nonspace(parser),
396
- len - cmark_parser_get_first_nonspace(parser));
397
-
398
- if (!row) {
399
- // clean up the dangling node
400
- cmark_node_free(table_row_block);
401
- return NULL;
402
- }
403
-
404
- {
405
- int i, table_columns = get_n_table_columns(parent_container);
406
-
407
- for (i = 0; i < row->n_columns && i < table_columns; ++i) {
408
- node_cell *cell = &row->cells[i];
409
- cmark_node *node = cmark_parser_add_child(parser, table_row_block,
410
- CMARK_NODE_TABLE_CELL, parent_container->start_column + cell->start_offset);
411
- node->internal_offset = cell->internal_offset;
412
- node->end_column = parent_container->start_column + cell->end_offset;
413
- cmark_node_set_string_content(node, (char *) cell->buf->ptr);
414
- cmark_node_set_syntax_extension(node, self);
415
- }
416
-
417
- for (; i < table_columns; ++i) {
418
- cmark_node *node = cmark_parser_add_child(
419
- parser, table_row_block, CMARK_NODE_TABLE_CELL, 0);
420
- cmark_node_set_syntax_extension(node, self);
421
- }
422
- }
423
-
424
- free_table_row(parser->mem, row);
425
-
426
- cmark_parser_advance_offset(parser, (char *)input,
427
- len - 1 - cmark_parser_get_offset(parser), false);
428
-
429
- return table_row_block;
430
- }
431
-
432
- static cmark_node *try_opening_table_block(cmark_syntax_extension *self,
433
- int indented, cmark_parser *parser,
434
- cmark_node *parent_container,
435
- unsigned char *input, int len) {
436
- cmark_node_type parent_type = cmark_node_get_type(parent_container);
437
-
438
- if (!indented && parent_type == CMARK_NODE_PARAGRAPH) {
439
- return try_opening_table_header(self, parser, parent_container, input, len);
440
- } else if (!indented && parent_type == CMARK_NODE_TABLE) {
441
- return try_opening_table_row(self, parser, parent_container, input, len);
442
- }
443
-
444
- return NULL;
445
- }
446
-
447
- static int matches(cmark_syntax_extension *self, cmark_parser *parser,
448
- unsigned char *input, int len,
449
- cmark_node *parent_container) {
450
- int res = 0;
451
-
452
- if (cmark_node_get_type(parent_container) == CMARK_NODE_TABLE) {
453
- cmark_arena_push();
454
- table_row *new_row = row_from_string(
455
- self, parser, input + cmark_parser_get_first_nonspace(parser),
456
- len - cmark_parser_get_first_nonspace(parser));
457
- if (new_row && new_row->n_columns)
458
- res = 1;
459
- free_table_row(parser->mem, new_row);
460
- cmark_arena_pop();
461
- }
462
-
463
- return res;
464
- }
465
-
466
- static const char *get_type_string(cmark_syntax_extension *self,
467
- cmark_node *node) {
468
- if (node->type == CMARK_NODE_TABLE) {
469
- return "table";
470
- } else if (node->type == CMARK_NODE_TABLE_ROW) {
471
- if (((node_table_row *)node->as.opaque)->is_header)
472
- return "table_header";
473
- else
474
- return "table_row";
475
- } else if (node->type == CMARK_NODE_TABLE_CELL) {
476
- return "table_cell";
477
- }
478
-
479
- return "<unknown>";
480
- }
481
-
482
- static int can_contain(cmark_syntax_extension *extension, cmark_node *node,
483
- cmark_node_type child_type) {
484
- if (node->type == CMARK_NODE_TABLE) {
485
- return child_type == CMARK_NODE_TABLE_ROW;
486
- } else if (node->type == CMARK_NODE_TABLE_ROW) {
487
- return child_type == CMARK_NODE_TABLE_CELL;
488
- } else if (node->type == CMARK_NODE_TABLE_CELL) {
489
- return child_type == CMARK_NODE_TEXT || child_type == CMARK_NODE_CODE ||
490
- child_type == CMARK_NODE_EMPH || child_type == CMARK_NODE_STRONG ||
491
- child_type == CMARK_NODE_LINK || child_type == CMARK_NODE_IMAGE ||
492
- child_type == CMARK_NODE_STRIKETHROUGH ||
493
- child_type == CMARK_NODE_HTML_INLINE ||
494
- child_type == CMARK_NODE_FOOTNOTE_REFERENCE;
495
- }
496
- return false;
497
- }
498
-
499
- static int contains_inlines(cmark_syntax_extension *extension,
500
- cmark_node *node) {
501
- return node->type == CMARK_NODE_TABLE_CELL;
502
- }
503
-
504
- static void commonmark_render(cmark_syntax_extension *extension,
505
- cmark_renderer *renderer, cmark_node *node,
506
- cmark_event_type ev_type, int options) {
507
- bool entering = (ev_type == CMARK_EVENT_ENTER);
508
-
509
- if (node->type == CMARK_NODE_TABLE) {
510
- renderer->blankline(renderer);
511
- } else if (node->type == CMARK_NODE_TABLE_ROW) {
512
- if (entering) {
513
- renderer->cr(renderer);
514
- renderer->out(renderer, node, "|", false, LITERAL);
515
- }
516
- } else if (node->type == CMARK_NODE_TABLE_CELL) {
517
- if (entering) {
518
- renderer->out(renderer, node, " ", false, LITERAL);
519
- } else {
520
- renderer->out(renderer, node, " |", false, LITERAL);
521
- if (((node_table_row *)node->parent->as.opaque)->is_header &&
522
- !node->next) {
523
- int i;
524
- uint8_t *alignments = get_table_alignments(node->parent->parent);
525
- uint16_t n_cols =
526
- ((node_table *)node->parent->parent->as.opaque)->n_columns;
527
- renderer->cr(renderer);
528
- renderer->out(renderer, node, "|", false, LITERAL);
529
- for (i = 0; i < n_cols; i++) {
530
- switch (alignments[i]) {
531
- case 0: renderer->out(renderer, node, " --- |", false, LITERAL); break;
532
- case 'l': renderer->out(renderer, node, " :-- |", false, LITERAL); break;
533
- case 'c': renderer->out(renderer, node, " :-: |", false, LITERAL); break;
534
- case 'r': renderer->out(renderer, node, " --: |", false, LITERAL); break;
535
- }
536
- }
537
- renderer->cr(renderer);
538
- }
539
- }
540
- } else {
541
- assert(false);
542
- }
543
- }
544
-
545
- static void latex_render(cmark_syntax_extension *extension,
546
- cmark_renderer *renderer, cmark_node *node,
547
- cmark_event_type ev_type, int options) {
548
- bool entering = (ev_type == CMARK_EVENT_ENTER);
549
-
550
- if (node->type == CMARK_NODE_TABLE) {
551
- if (entering) {
552
- int i;
553
- uint16_t n_cols;
554
- uint8_t *alignments = get_table_alignments(node);
555
-
556
- renderer->cr(renderer);
557
- renderer->out(renderer, node, "\\begin{table}", false, LITERAL);
558
- renderer->cr(renderer);
559
- renderer->out(renderer, node, "\\begin{tabular}{", false, LITERAL);
560
-
561
- n_cols = ((node_table *)node->as.opaque)->n_columns;
562
- for (i = 0; i < n_cols; i++) {
563
- switch(alignments[i]) {
564
- case 0:
565
- case 'l':
566
- renderer->out(renderer, node, "l", false, LITERAL);
567
- break;
568
- case 'c':
569
- renderer->out(renderer, node, "c", false, LITERAL);
570
- break;
571
- case 'r':
572
- renderer->out(renderer, node, "r", false, LITERAL);
573
- break;
574
- }
575
- }
576
- renderer->out(renderer, node, "}", false, LITERAL);
577
- renderer->cr(renderer);
578
- } else {
579
- renderer->out(renderer, node, "\\end{tabular}", false, LITERAL);
580
- renderer->cr(renderer);
581
- renderer->out(renderer, node, "\\end{table}", false, LITERAL);
582
- renderer->cr(renderer);
583
- }
584
- } else if (node->type == CMARK_NODE_TABLE_ROW) {
585
- if (!entering) {
586
- renderer->cr(renderer);
587
- }
588
- } else if (node->type == CMARK_NODE_TABLE_CELL) {
589
- if (!entering) {
590
- if (node->next) {
591
- renderer->out(renderer, node, " & ", false, LITERAL);
592
- } else {
593
- renderer->out(renderer, node, " \\\\", false, LITERAL);
594
- }
595
- }
596
- } else {
597
- assert(false);
598
- }
599
- }
600
-
601
- static const char *xml_attr(cmark_syntax_extension *extension,
602
- cmark_node *node) {
603
- if (node->type == CMARK_NODE_TABLE_CELL) {
604
- if (cmark_gfm_extensions_get_table_row_is_header(node->parent)) {
605
- uint8_t *alignments = get_table_alignments(node->parent->parent);
606
- int i = 0;
607
- cmark_node *n;
608
- for (n = node->parent->first_child; n; n = n->next, ++i)
609
- if (n == node)
610
- break;
611
- switch (alignments[i]) {
612
- case 'l': return " align=\"left\"";
613
- case 'c': return " align=\"center\"";
614
- case 'r': return " align=\"right\"";
615
- }
616
- }
617
- }
618
-
619
- return NULL;
620
- }
621
-
622
- static void man_render(cmark_syntax_extension *extension,
623
- cmark_renderer *renderer, cmark_node *node,
624
- cmark_event_type ev_type, int options) {
625
- bool entering = (ev_type == CMARK_EVENT_ENTER);
626
-
627
- if (node->type == CMARK_NODE_TABLE) {
628
- if (entering) {
629
- int i;
630
- uint16_t n_cols;
631
- uint8_t *alignments = get_table_alignments(node);
632
-
633
- renderer->cr(renderer);
634
- renderer->out(renderer, node, ".TS", false, LITERAL);
635
- renderer->cr(renderer);
636
- renderer->out(renderer, node, "tab(@);", false, LITERAL);
637
- renderer->cr(renderer);
638
-
639
- n_cols = ((node_table *)node->as.opaque)->n_columns;
640
-
641
- for (i = 0; i < n_cols; i++) {
642
- switch (alignments[i]) {
643
- case 'l':
644
- renderer->out(renderer, node, "l", false, LITERAL);
645
- break;
646
- case 0:
647
- case 'c':
648
- renderer->out(renderer, node, "c", false, LITERAL);
649
- break;
650
- case 'r':
651
- renderer->out(renderer, node, "r", false, LITERAL);
652
- break;
653
- }
654
- }
655
-
656
- if (n_cols) {
657
- renderer->out(renderer, node, ".", false, LITERAL);
658
- renderer->cr(renderer);
659
- }
660
- } else {
661
- renderer->out(renderer, node, ".TE", false, LITERAL);
662
- renderer->cr(renderer);
663
- }
664
- } else if (node->type == CMARK_NODE_TABLE_ROW) {
665
- if (!entering) {
666
- renderer->cr(renderer);
667
- }
668
- } else if (node->type == CMARK_NODE_TABLE_CELL) {
669
- if (!entering && node->next) {
670
- renderer->out(renderer, node, "@", false, LITERAL);
671
- }
672
- } else {
673
- assert(false);
674
- }
675
- }
676
-
677
- static void html_table_add_align(cmark_strbuf* html, const char* align, int options) {
678
- if (options & CMARK_OPT_TABLE_PREFER_STYLE_ATTRIBUTES) {
679
- cmark_strbuf_puts(html, " style=\"text-align: ");
680
- cmark_strbuf_puts(html, align);
681
- cmark_strbuf_puts(html, "\"");
682
- } else {
683
- cmark_strbuf_puts(html, " align=\"");
684
- cmark_strbuf_puts(html, align);
685
- cmark_strbuf_puts(html, "\"");
686
- }
687
- }
688
-
689
- struct html_table_state {
690
- unsigned need_closing_table_body : 1;
691
- unsigned in_table_header : 1;
692
- };
693
-
694
- static void html_render(cmark_syntax_extension *extension,
695
- cmark_html_renderer *renderer, cmark_node *node,
696
- cmark_event_type ev_type, int options) {
697
- bool entering = (ev_type == CMARK_EVENT_ENTER);
698
- cmark_strbuf *html = renderer->html;
699
- cmark_node *n;
700
-
701
- // XXX: we just monopolise renderer->opaque.
702
- struct html_table_state *table_state =
703
- (struct html_table_state *)&renderer->opaque;
704
-
705
- if (node->type == CMARK_NODE_TABLE) {
706
- if (entering) {
707
- cmark_html_render_cr(html);
708
- cmark_strbuf_puts(html, "<table");
709
- cmark_html_render_sourcepos(node, html, options);
710
- cmark_strbuf_putc(html, '>');
711
- table_state->need_closing_table_body = false;
712
- } else {
713
- if (table_state->need_closing_table_body) {
714
- cmark_html_render_cr(html);
715
- cmark_strbuf_puts(html, "</tbody>");
716
- cmark_html_render_cr(html);
717
- }
718
- table_state->need_closing_table_body = false;
719
- cmark_html_render_cr(html);
720
- cmark_strbuf_puts(html, "</table>");
721
- cmark_html_render_cr(html);
722
- }
723
- } else if (node->type == CMARK_NODE_TABLE_ROW) {
724
- if (entering) {
725
- cmark_html_render_cr(html);
726
- if (((node_table_row *)node->as.opaque)->is_header) {
727
- table_state->in_table_header = 1;
728
- cmark_strbuf_puts(html, "<thead>");
729
- cmark_html_render_cr(html);
730
- } else if (!table_state->need_closing_table_body) {
731
- cmark_strbuf_puts(html, "<tbody>");
732
- cmark_html_render_cr(html);
733
- table_state->need_closing_table_body = 1;
734
- }
735
- cmark_strbuf_puts(html, "<tr");
736
- cmark_html_render_sourcepos(node, html, options);
737
- cmark_strbuf_putc(html, '>');
738
- } else {
739
- cmark_html_render_cr(html);
740
- cmark_strbuf_puts(html, "</tr>");
741
- if (((node_table_row *)node->as.opaque)->is_header) {
742
- cmark_html_render_cr(html);
743
- cmark_strbuf_puts(html, "</thead>");
744
- table_state->in_table_header = false;
745
- }
746
- }
747
- } else if (node->type == CMARK_NODE_TABLE_CELL) {
748
- uint8_t *alignments = get_table_alignments(node->parent->parent);
749
- if (entering) {
750
- cmark_html_render_cr(html);
751
- if (table_state->in_table_header) {
752
- cmark_strbuf_puts(html, "<th");
753
- } else {
754
- cmark_strbuf_puts(html, "<td");
755
- }
756
-
757
- int i = 0;
758
- for (n = node->parent->first_child; n; n = n->next, ++i)
759
- if (n == node)
760
- break;
761
-
762
- switch (alignments[i]) {
763
- case 'l': html_table_add_align(html, "left", options); break;
764
- case 'c': html_table_add_align(html, "center", options); break;
765
- case 'r': html_table_add_align(html, "right", options); break;
766
- }
767
-
768
- cmark_html_render_sourcepos(node, html, options);
769
- cmark_strbuf_putc(html, '>');
770
- } else {
771
- if (table_state->in_table_header) {
772
- cmark_strbuf_puts(html, "</th>");
773
- } else {
774
- cmark_strbuf_puts(html, "</td>");
775
- }
776
- }
777
- } else {
778
- assert(false);
779
- }
780
- }
781
-
782
- static void opaque_alloc(cmark_syntax_extension *self, cmark_mem *mem, cmark_node *node) {
783
- if (node->type == CMARK_NODE_TABLE) {
784
- node->as.opaque = mem->calloc(1, sizeof(node_table));
785
- } else if (node->type == CMARK_NODE_TABLE_ROW) {
786
- node->as.opaque = mem->calloc(1, sizeof(node_table_row));
787
- } else if (node->type == CMARK_NODE_TABLE_CELL) {
788
- node->as.opaque = mem->calloc(1, sizeof(node_cell));
789
- }
790
- }
791
-
792
- static void opaque_free(cmark_syntax_extension *self, cmark_mem *mem, cmark_node *node) {
793
- if (node->type == CMARK_NODE_TABLE) {
794
- free_node_table(mem, node->as.opaque);
795
- } else if (node->type == CMARK_NODE_TABLE_ROW) {
796
- free_node_table_row(mem, node->as.opaque);
797
- }
798
- }
799
-
800
- static int escape(cmark_syntax_extension *self, cmark_node *node, int c) {
801
- return
802
- node->type != CMARK_NODE_TABLE &&
803
- node->type != CMARK_NODE_TABLE_ROW &&
804
- node->type != CMARK_NODE_TABLE_CELL &&
805
- c == '|';
806
- }
807
-
808
- cmark_syntax_extension *create_table_extension(void) {
809
- cmark_syntax_extension *self = cmark_syntax_extension_new("table");
810
-
811
- cmark_register_node_flag(&CMARK_NODE__TABLE_VISITED);
812
- cmark_syntax_extension_set_match_block_func(self, matches);
813
- cmark_syntax_extension_set_open_block_func(self, try_opening_table_block);
814
- cmark_syntax_extension_set_get_type_string_func(self, get_type_string);
815
- cmark_syntax_extension_set_can_contain_func(self, can_contain);
816
- cmark_syntax_extension_set_contains_inlines_func(self, contains_inlines);
817
- cmark_syntax_extension_set_commonmark_render_func(self, commonmark_render);
818
- cmark_syntax_extension_set_plaintext_render_func(self, commonmark_render);
819
- cmark_syntax_extension_set_latex_render_func(self, latex_render);
820
- cmark_syntax_extension_set_xml_attr_func(self, xml_attr);
821
- cmark_syntax_extension_set_man_render_func(self, man_render);
822
- cmark_syntax_extension_set_html_render_func(self, html_render);
823
- cmark_syntax_extension_set_opaque_alloc_func(self, opaque_alloc);
824
- cmark_syntax_extension_set_opaque_free_func(self, opaque_free);
825
- cmark_syntax_extension_set_commonmark_escape_func(self, escape);
826
- CMARK_NODE_TABLE = cmark_syntax_extension_add_node(0);
827
- CMARK_NODE_TABLE_ROW = cmark_syntax_extension_add_node(0);
828
- CMARK_NODE_TABLE_CELL = cmark_syntax_extension_add_node(0);
829
-
830
- return self;
831
- }
832
-
833
- uint16_t cmark_gfm_extensions_get_table_columns(cmark_node *node) {
834
- if (node->type != CMARK_NODE_TABLE)
835
- return 0;
836
-
837
- return ((node_table *)node->as.opaque)->n_columns;
838
- }
839
-
840
- uint8_t *cmark_gfm_extensions_get_table_alignments(cmark_node *node) {
841
- if (node->type != CMARK_NODE_TABLE)
842
- return 0;
843
-
844
- return ((node_table *)node->as.opaque)->alignments;
845
- }
846
-
847
- int cmark_gfm_extensions_set_table_columns(cmark_node *node, uint16_t n_columns) {
848
- return set_n_table_columns(node, n_columns);
849
- }
850
-
851
- int cmark_gfm_extensions_set_table_alignments(cmark_node *node, uint16_t ncols, uint8_t *alignments) {
852
- uint8_t *a = (uint8_t *)cmark_node_mem(node)->calloc(1, ncols);
853
- memcpy(a, alignments, ncols);
854
- return set_table_alignments(node, a);
855
- }
856
-
857
- int cmark_gfm_extensions_get_table_row_is_header(cmark_node *node)
858
- {
859
- if (!node || node->type != CMARK_NODE_TABLE_ROW)
860
- return 0;
861
-
862
- return ((node_table_row *)node->as.opaque)->is_header;
863
- }
864
-
865
- int cmark_gfm_extensions_set_table_row_is_header(cmark_node *node, int is_header)
866
- {
867
- if (!node || node->type != CMARK_NODE_TABLE_ROW)
868
- return 0;
869
-
870
- ((node_table_row *)node->as.opaque)->is_header = (is_header != 0);
871
- return 1;
872
- }