commonmarker 0.23.10 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. data/Cargo.lock +1220 -0
  3. data/Cargo.toml +7 -0
  4. data/README.md +217 -170
  5. data/ext/commonmarker/Cargo.toml +20 -0
  6. data/ext/commonmarker/extconf.rb +3 -6
  7. data/ext/commonmarker/src/lib.rs +183 -0
  8. data/ext/commonmarker/src/node.rs +1095 -0
  9. data/ext/commonmarker/src/options.rs +165 -0
  10. data/ext/commonmarker/src/plugins/syntax_highlighting.rs +74 -0
  11. data/ext/commonmarker/src/plugins.rs +3 -0
  12. data/ext/commonmarker/src/utils.rs +8 -0
  13. data/lib/commonmarker/config.rb +90 -40
  14. data/lib/commonmarker/constants.rb +7 -0
  15. data/lib/commonmarker/extension.rb +14 -0
  16. data/lib/commonmarker/node/ast.rb +8 -0
  17. data/lib/commonmarker/node/inspect.rb +14 -4
  18. data/lib/commonmarker/node.rb +29 -47
  19. data/lib/commonmarker/renderer.rb +1 -127
  20. data/lib/commonmarker/utils.rb +22 -0
  21. data/lib/commonmarker/version.rb +2 -2
  22. data/lib/commonmarker.rb +27 -25
  23. metadata +38 -186
  24. data/Rakefile +0 -109
  25. data/bin/commonmarker +0 -118
  26. data/commonmarker.gemspec +0 -38
  27. data/ext/commonmarker/arena.c +0 -104
  28. data/ext/commonmarker/autolink.c +0 -508
  29. data/ext/commonmarker/autolink.h +0 -8
  30. data/ext/commonmarker/blocks.c +0 -1622
  31. data/ext/commonmarker/buffer.c +0 -278
  32. data/ext/commonmarker/buffer.h +0 -116
  33. data/ext/commonmarker/case_fold_switch.inc +0 -4327
  34. data/ext/commonmarker/chunk.h +0 -135
  35. data/ext/commonmarker/cmark-gfm-core-extensions.h +0 -54
  36. data/ext/commonmarker/cmark-gfm-extension_api.h +0 -737
  37. data/ext/commonmarker/cmark-gfm-extensions_export.h +0 -42
  38. data/ext/commonmarker/cmark-gfm.h +0 -833
  39. data/ext/commonmarker/cmark-gfm_export.h +0 -42
  40. data/ext/commonmarker/cmark-gfm_version.h +0 -7
  41. data/ext/commonmarker/cmark.c +0 -55
  42. data/ext/commonmarker/cmark_ctype.c +0 -44
  43. data/ext/commonmarker/cmark_ctype.h +0 -33
  44. data/ext/commonmarker/commonmark.c +0 -514
  45. data/ext/commonmarker/commonmarker.c +0 -1308
  46. data/ext/commonmarker/commonmarker.h +0 -16
  47. data/ext/commonmarker/config.h +0 -76
  48. data/ext/commonmarker/core-extensions.c +0 -27
  49. data/ext/commonmarker/entities.inc +0 -2138
  50. data/ext/commonmarker/ext_scanners.c +0 -879
  51. data/ext/commonmarker/ext_scanners.h +0 -24
  52. data/ext/commonmarker/footnotes.c +0 -63
  53. data/ext/commonmarker/footnotes.h +0 -27
  54. data/ext/commonmarker/houdini.h +0 -57
  55. data/ext/commonmarker/houdini_href_e.c +0 -100
  56. data/ext/commonmarker/houdini_html_e.c +0 -66
  57. data/ext/commonmarker/houdini_html_u.c +0 -149
  58. data/ext/commonmarker/html.c +0 -502
  59. data/ext/commonmarker/html.h +0 -27
  60. data/ext/commonmarker/inlines.c +0 -1788
  61. data/ext/commonmarker/inlines.h +0 -29
  62. data/ext/commonmarker/iterator.c +0 -159
  63. data/ext/commonmarker/iterator.h +0 -26
  64. data/ext/commonmarker/latex.c +0 -468
  65. data/ext/commonmarker/linked_list.c +0 -37
  66. data/ext/commonmarker/man.c +0 -274
  67. data/ext/commonmarker/map.c +0 -129
  68. data/ext/commonmarker/map.h +0 -44
  69. data/ext/commonmarker/node.c +0 -1045
  70. data/ext/commonmarker/node.h +0 -167
  71. data/ext/commonmarker/parser.h +0 -59
  72. data/ext/commonmarker/plaintext.c +0 -218
  73. data/ext/commonmarker/plugin.c +0 -36
  74. data/ext/commonmarker/plugin.h +0 -34
  75. data/ext/commonmarker/references.c +0 -43
  76. data/ext/commonmarker/references.h +0 -26
  77. data/ext/commonmarker/registry.c +0 -63
  78. data/ext/commonmarker/registry.h +0 -24
  79. data/ext/commonmarker/render.c +0 -213
  80. data/ext/commonmarker/render.h +0 -62
  81. data/ext/commonmarker/scanners.c +0 -14056
  82. data/ext/commonmarker/scanners.h +0 -70
  83. data/ext/commonmarker/scanners.re +0 -341
  84. data/ext/commonmarker/strikethrough.c +0 -167
  85. data/ext/commonmarker/strikethrough.h +0 -9
  86. data/ext/commonmarker/syntax_extension.c +0 -149
  87. data/ext/commonmarker/syntax_extension.h +0 -34
  88. data/ext/commonmarker/table.c +0 -917
  89. data/ext/commonmarker/table.h +0 -12
  90. data/ext/commonmarker/tagfilter.c +0 -60
  91. data/ext/commonmarker/tagfilter.h +0 -8
  92. data/ext/commonmarker/tasklist.c +0 -156
  93. data/ext/commonmarker/tasklist.h +0 -8
  94. data/ext/commonmarker/utf8.c +0 -317
  95. data/ext/commonmarker/utf8.h +0 -35
  96. data/ext/commonmarker/xml.c +0 -182
  97. data/lib/commonmarker/renderer/html_renderer.rb +0 -256
@@ -1,1045 +0,0 @@
1
- #include <stdlib.h>
2
- #include <string.h>
3
-
4
- #include "config.h"
5
- #include "node.h"
6
- #include "syntax_extension.h"
7
-
8
- /**
9
- * Expensive safety checks are off by default, but can be enabled
10
- * by calling cmark_enable_safety_checks().
11
- */
12
- static bool enable_safety_checks = false;
13
-
14
- void cmark_enable_safety_checks(bool enable) {
15
- enable_safety_checks = enable;
16
- }
17
-
18
- static void S_node_unlink(cmark_node *node);
19
-
20
- #define NODE_MEM(node) cmark_node_mem(node)
21
-
22
- void cmark_register_node_flag(cmark_node_internal_flags *flags) {
23
- static cmark_node_internal_flags nextflag = CMARK_NODE__REGISTER_FIRST;
24
-
25
- // flags should be a pointer to a global variable and this function
26
- // should only be called once to initialize its value.
27
- if (*flags) {
28
- fprintf(stderr, "flag initialization error in cmark_register_node_flag\n");
29
- abort();
30
- }
31
-
32
- // Check that we haven't run out of bits.
33
- if (nextflag == 0) {
34
- fprintf(stderr, "too many flags in cmark_register_node_flag\n");
35
- abort();
36
- }
37
-
38
- *flags = nextflag;
39
- nextflag <<= 1;
40
- }
41
-
42
- void cmark_init_standard_node_flags(void) {}
43
-
44
- bool cmark_node_can_contain_type(cmark_node *node, cmark_node_type child_type) {
45
- if (child_type == CMARK_NODE_DOCUMENT) {
46
- return false;
47
- }
48
-
49
- if (node->extension && node->extension->can_contain_func) {
50
- return node->extension->can_contain_func(node->extension, node, child_type) != 0;
51
- }
52
-
53
- switch (node->type) {
54
- case CMARK_NODE_DOCUMENT:
55
- case CMARK_NODE_BLOCK_QUOTE:
56
- case CMARK_NODE_FOOTNOTE_DEFINITION:
57
- case CMARK_NODE_ITEM:
58
- return CMARK_NODE_TYPE_BLOCK_P(child_type) && child_type != CMARK_NODE_ITEM;
59
-
60
- case CMARK_NODE_LIST:
61
- return child_type == CMARK_NODE_ITEM;
62
-
63
- case CMARK_NODE_CUSTOM_BLOCK:
64
- return true;
65
-
66
- case CMARK_NODE_PARAGRAPH:
67
- case CMARK_NODE_HEADING:
68
- case CMARK_NODE_EMPH:
69
- case CMARK_NODE_STRONG:
70
- case CMARK_NODE_LINK:
71
- case CMARK_NODE_IMAGE:
72
- case CMARK_NODE_CUSTOM_INLINE:
73
- return CMARK_NODE_TYPE_INLINE_P(child_type);
74
-
75
- default:
76
- break;
77
- }
78
-
79
- return false;
80
- }
81
-
82
- static bool S_can_contain(cmark_node *node, cmark_node *child) {
83
- if (node == NULL || child == NULL) {
84
- return false;
85
- }
86
- if (NODE_MEM(node) != NODE_MEM(child)) {
87
- return 0;
88
- }
89
-
90
- if (enable_safety_checks) {
91
- // Verify that child is not an ancestor of node or equal to node.
92
- cmark_node *cur = node;
93
- do {
94
- if (cur == child) {
95
- return false;
96
- }
97
- cur = cur->parent;
98
- } while (cur != NULL);
99
- }
100
-
101
- return cmark_node_can_contain_type(node, (cmark_node_type) child->type);
102
- }
103
-
104
- cmark_node *cmark_node_new_with_mem_and_ext(cmark_node_type type, cmark_mem *mem, cmark_syntax_extension *extension) {
105
- cmark_node *node = (cmark_node *)mem->calloc(1, sizeof(*node));
106
- cmark_strbuf_init(mem, &node->content, 0);
107
- node->type = (uint16_t)type;
108
- node->extension = extension;
109
-
110
- switch (node->type) {
111
- case CMARK_NODE_HEADING:
112
- node->as.heading.level = 1;
113
- break;
114
-
115
- case CMARK_NODE_LIST: {
116
- cmark_list *list = &node->as.list;
117
- list->list_type = CMARK_BULLET_LIST;
118
- list->start = 0;
119
- list->tight = false;
120
- break;
121
- }
122
-
123
- default:
124
- break;
125
- }
126
-
127
- if (node->extension && node->extension->opaque_alloc_func) {
128
- node->extension->opaque_alloc_func(node->extension, mem, node);
129
- }
130
-
131
- return node;
132
- }
133
-
134
- cmark_node *cmark_node_new_with_ext(cmark_node_type type, cmark_syntax_extension *extension) {
135
- extern cmark_mem CMARK_DEFAULT_MEM_ALLOCATOR;
136
- return cmark_node_new_with_mem_and_ext(type, &CMARK_DEFAULT_MEM_ALLOCATOR, extension);
137
- }
138
-
139
- cmark_node *cmark_node_new_with_mem(cmark_node_type type, cmark_mem *mem)
140
- {
141
- return cmark_node_new_with_mem_and_ext(type, mem, NULL);
142
- }
143
-
144
- cmark_node *cmark_node_new(cmark_node_type type) {
145
- return cmark_node_new_with_ext(type, NULL);
146
- }
147
-
148
- static void free_node_as(cmark_node *node) {
149
- switch (node->type) {
150
- case CMARK_NODE_CODE_BLOCK:
151
- cmark_chunk_free(NODE_MEM(node), &node->as.code.info);
152
- cmark_chunk_free(NODE_MEM(node), &node->as.code.literal);
153
- break;
154
- case CMARK_NODE_TEXT:
155
- case CMARK_NODE_HTML_INLINE:
156
- case CMARK_NODE_CODE:
157
- case CMARK_NODE_HTML_BLOCK:
158
- case CMARK_NODE_FOOTNOTE_REFERENCE:
159
- case CMARK_NODE_FOOTNOTE_DEFINITION:
160
- cmark_chunk_free(NODE_MEM(node), &node->as.literal);
161
- break;
162
- case CMARK_NODE_LINK:
163
- case CMARK_NODE_IMAGE:
164
- cmark_chunk_free(NODE_MEM(node), &node->as.link.url);
165
- cmark_chunk_free(NODE_MEM(node), &node->as.link.title);
166
- break;
167
- case CMARK_NODE_CUSTOM_BLOCK:
168
- case CMARK_NODE_CUSTOM_INLINE:
169
- cmark_chunk_free(NODE_MEM(node), &node->as.custom.on_enter);
170
- cmark_chunk_free(NODE_MEM(node), &node->as.custom.on_exit);
171
- break;
172
- default:
173
- break;
174
- }
175
- }
176
-
177
- // Free a cmark_node list and any children.
178
- static void S_free_nodes(cmark_node *e) {
179
- cmark_node *next;
180
- while (e != NULL) {
181
- cmark_strbuf_free(&e->content);
182
-
183
- if (e->user_data && e->user_data_free_func)
184
- e->user_data_free_func(NODE_MEM(e), e->user_data);
185
-
186
- if (e->as.opaque && e->extension && e->extension->opaque_free_func)
187
- e->extension->opaque_free_func(e->extension, NODE_MEM(e), e);
188
-
189
- free_node_as(e);
190
-
191
- if (e->last_child) {
192
- // Splice children into list
193
- e->last_child->next = e->next;
194
- e->next = e->first_child;
195
- }
196
- next = e->next;
197
- NODE_MEM(e)->free(e);
198
- e = next;
199
- }
200
- }
201
-
202
- void cmark_node_free(cmark_node *node) {
203
- S_node_unlink(node);
204
- node->next = NULL;
205
- S_free_nodes(node);
206
- }
207
-
208
- cmark_node_type cmark_node_get_type(cmark_node *node) {
209
- if (node == NULL) {
210
- return CMARK_NODE_NONE;
211
- } else {
212
- return (cmark_node_type)node->type;
213
- }
214
- }
215
-
216
- int cmark_node_set_type(cmark_node * node, cmark_node_type type) {
217
- cmark_node_type initial_type;
218
-
219
- if (type == node->type)
220
- return 1;
221
-
222
- initial_type = (cmark_node_type) node->type;
223
- node->type = (uint16_t)type;
224
-
225
- if (!S_can_contain(node->parent, node)) {
226
- node->type = (uint16_t)initial_type;
227
- return 0;
228
- }
229
-
230
- /* We rollback the type to free the union members appropriately */
231
- node->type = (uint16_t)initial_type;
232
- free_node_as(node);
233
-
234
- node->type = (uint16_t)type;
235
-
236
- return 1;
237
- }
238
-
239
- const char *cmark_node_get_type_string(cmark_node *node) {
240
- if (node == NULL) {
241
- return "NONE";
242
- }
243
-
244
- if (node->extension && node->extension->get_type_string_func) {
245
- return node->extension->get_type_string_func(node->extension, node);
246
- }
247
-
248
- switch (node->type) {
249
- case CMARK_NODE_NONE:
250
- return "none";
251
- case CMARK_NODE_DOCUMENT:
252
- return "document";
253
- case CMARK_NODE_BLOCK_QUOTE:
254
- return "block_quote";
255
- case CMARK_NODE_LIST:
256
- return "list";
257
- case CMARK_NODE_ITEM:
258
- return "item";
259
- case CMARK_NODE_CODE_BLOCK:
260
- return "code_block";
261
- case CMARK_NODE_HTML_BLOCK:
262
- return "html_block";
263
- case CMARK_NODE_CUSTOM_BLOCK:
264
- return "custom_block";
265
- case CMARK_NODE_PARAGRAPH:
266
- return "paragraph";
267
- case CMARK_NODE_HEADING:
268
- return "heading";
269
- case CMARK_NODE_THEMATIC_BREAK:
270
- return "thematic_break";
271
- case CMARK_NODE_TEXT:
272
- return "text";
273
- case CMARK_NODE_SOFTBREAK:
274
- return "softbreak";
275
- case CMARK_NODE_LINEBREAK:
276
- return "linebreak";
277
- case CMARK_NODE_CODE:
278
- return "code";
279
- case CMARK_NODE_HTML_INLINE:
280
- return "html_inline";
281
- case CMARK_NODE_CUSTOM_INLINE:
282
- return "custom_inline";
283
- case CMARK_NODE_EMPH:
284
- return "emph";
285
- case CMARK_NODE_STRONG:
286
- return "strong";
287
- case CMARK_NODE_LINK:
288
- return "link";
289
- case CMARK_NODE_IMAGE:
290
- return "image";
291
- }
292
-
293
- return "<unknown>";
294
- }
295
-
296
- cmark_node *cmark_node_next(cmark_node *node) {
297
- if (node == NULL) {
298
- return NULL;
299
- } else {
300
- return node->next;
301
- }
302
- }
303
-
304
- cmark_node *cmark_node_previous(cmark_node *node) {
305
- if (node == NULL) {
306
- return NULL;
307
- } else {
308
- return node->prev;
309
- }
310
- }
311
-
312
- cmark_node *cmark_node_parent(cmark_node *node) {
313
- if (node == NULL) {
314
- return NULL;
315
- } else {
316
- return node->parent;
317
- }
318
- }
319
-
320
- cmark_node *cmark_node_first_child(cmark_node *node) {
321
- if (node == NULL) {
322
- return NULL;
323
- } else {
324
- return node->first_child;
325
- }
326
- }
327
-
328
- cmark_node *cmark_node_last_child(cmark_node *node) {
329
- if (node == NULL) {
330
- return NULL;
331
- } else {
332
- return node->last_child;
333
- }
334
- }
335
-
336
- cmark_node *cmark_node_parent_footnote_def(cmark_node *node) {
337
- if (node == NULL) {
338
- return NULL;
339
- } else {
340
- return node->parent_footnote_def;
341
- }
342
- }
343
-
344
- void *cmark_node_get_user_data(cmark_node *node) {
345
- if (node == NULL) {
346
- return NULL;
347
- } else {
348
- return node->user_data;
349
- }
350
- }
351
-
352
- int cmark_node_set_user_data(cmark_node *node, void *user_data) {
353
- if (node == NULL) {
354
- return 0;
355
- }
356
- node->user_data = user_data;
357
- return 1;
358
- }
359
-
360
- int cmark_node_set_user_data_free_func(cmark_node *node,
361
- cmark_free_func free_func) {
362
- if (node == NULL) {
363
- return 0;
364
- }
365
- node->user_data_free_func = free_func;
366
- return 1;
367
- }
368
-
369
- const char *cmark_node_get_literal(cmark_node *node) {
370
- if (node == NULL) {
371
- return NULL;
372
- }
373
-
374
- switch (node->type) {
375
- case CMARK_NODE_HTML_BLOCK:
376
- case CMARK_NODE_TEXT:
377
- case CMARK_NODE_HTML_INLINE:
378
- case CMARK_NODE_CODE:
379
- case CMARK_NODE_FOOTNOTE_REFERENCE:
380
- case CMARK_NODE_FOOTNOTE_DEFINITION:
381
- return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.literal);
382
-
383
- case CMARK_NODE_CODE_BLOCK:
384
- return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.code.literal);
385
-
386
- default:
387
- break;
388
- }
389
-
390
- return NULL;
391
- }
392
-
393
- int cmark_node_set_literal(cmark_node *node, const char *content) {
394
- if (node == NULL) {
395
- return 0;
396
- }
397
-
398
- switch (node->type) {
399
- case CMARK_NODE_HTML_BLOCK:
400
- case CMARK_NODE_TEXT:
401
- case CMARK_NODE_HTML_INLINE:
402
- case CMARK_NODE_CODE:
403
- case CMARK_NODE_FOOTNOTE_REFERENCE:
404
- cmark_chunk_set_cstr(NODE_MEM(node), &node->as.literal, content);
405
- return 1;
406
-
407
- case CMARK_NODE_CODE_BLOCK:
408
- cmark_chunk_set_cstr(NODE_MEM(node), &node->as.code.literal, content);
409
- return 1;
410
-
411
- default:
412
- break;
413
- }
414
-
415
- return 0;
416
- }
417
-
418
- const char *cmark_node_get_string_content(cmark_node *node) {
419
- return (char *) node->content.ptr;
420
- }
421
-
422
- int cmark_node_set_string_content(cmark_node *node, const char *content) {
423
- cmark_strbuf_sets(&node->content, content);
424
- return true;
425
- }
426
-
427
- int cmark_node_get_heading_level(cmark_node *node) {
428
- if (node == NULL) {
429
- return 0;
430
- }
431
-
432
- switch (node->type) {
433
- case CMARK_NODE_HEADING:
434
- return node->as.heading.level;
435
-
436
- default:
437
- break;
438
- }
439
-
440
- return 0;
441
- }
442
-
443
- int cmark_node_set_heading_level(cmark_node *node, int level) {
444
- if (node == NULL || level < 1 || level > 6) {
445
- return 0;
446
- }
447
-
448
- switch (node->type) {
449
- case CMARK_NODE_HEADING:
450
- node->as.heading.level = level;
451
- return 1;
452
-
453
- default:
454
- break;
455
- }
456
-
457
- return 0;
458
- }
459
-
460
- cmark_list_type cmark_node_get_list_type(cmark_node *node) {
461
- if (node == NULL) {
462
- return CMARK_NO_LIST;
463
- }
464
-
465
- if (node->type == CMARK_NODE_LIST) {
466
- return node->as.list.list_type;
467
- } else {
468
- return CMARK_NO_LIST;
469
- }
470
- }
471
-
472
- int cmark_node_set_list_type(cmark_node *node, cmark_list_type type) {
473
- if (!(type == CMARK_BULLET_LIST || type == CMARK_ORDERED_LIST)) {
474
- return 0;
475
- }
476
-
477
- if (node == NULL) {
478
- return 0;
479
- }
480
-
481
- if (node->type == CMARK_NODE_LIST) {
482
- node->as.list.list_type = type;
483
- return 1;
484
- } else {
485
- return 0;
486
- }
487
- }
488
-
489
- cmark_delim_type cmark_node_get_list_delim(cmark_node *node) {
490
- if (node == NULL) {
491
- return CMARK_NO_DELIM;
492
- }
493
-
494
- if (node->type == CMARK_NODE_LIST) {
495
- return node->as.list.delimiter;
496
- } else {
497
- return CMARK_NO_DELIM;
498
- }
499
- }
500
-
501
- int cmark_node_set_list_delim(cmark_node *node, cmark_delim_type delim) {
502
- if (!(delim == CMARK_PERIOD_DELIM || delim == CMARK_PAREN_DELIM)) {
503
- return 0;
504
- }
505
-
506
- if (node == NULL) {
507
- return 0;
508
- }
509
-
510
- if (node->type == CMARK_NODE_LIST) {
511
- node->as.list.delimiter = delim;
512
- return 1;
513
- } else {
514
- return 0;
515
- }
516
- }
517
-
518
- int cmark_node_get_list_start(cmark_node *node) {
519
- if (node == NULL) {
520
- return 0;
521
- }
522
-
523
- if (node->type == CMARK_NODE_LIST) {
524
- return node->as.list.start;
525
- } else {
526
- return 0;
527
- }
528
- }
529
-
530
- int cmark_node_set_list_start(cmark_node *node, int start) {
531
- if (node == NULL || start < 0) {
532
- return 0;
533
- }
534
-
535
- if (node->type == CMARK_NODE_LIST) {
536
- node->as.list.start = start;
537
- return 1;
538
- } else {
539
- return 0;
540
- }
541
- }
542
-
543
- int cmark_node_get_list_tight(cmark_node *node) {
544
- if (node == NULL) {
545
- return 0;
546
- }
547
-
548
- if (node->type == CMARK_NODE_LIST) {
549
- return node->as.list.tight;
550
- } else {
551
- return 0;
552
- }
553
- }
554
-
555
- int cmark_node_set_list_tight(cmark_node *node, int tight) {
556
- if (node == NULL) {
557
- return 0;
558
- }
559
-
560
- if (node->type == CMARK_NODE_LIST) {
561
- node->as.list.tight = tight == 1;
562
- return 1;
563
- } else {
564
- return 0;
565
- }
566
- }
567
-
568
- int cmark_node_get_item_index(cmark_node *node) {
569
- if (node == NULL) {
570
- return 0;
571
- }
572
-
573
- if (node->type == CMARK_NODE_ITEM) {
574
- return node->as.list.start;
575
- } else {
576
- return 0;
577
- }
578
- }
579
-
580
- int cmark_node_set_item_index(cmark_node *node, int idx) {
581
- if (node == NULL || idx < 0) {
582
- return 0;
583
- }
584
-
585
- if (node->type == CMARK_NODE_ITEM) {
586
- node->as.list.start = idx;
587
- return 1;
588
- } else {
589
- return 0;
590
- }
591
- }
592
-
593
- const char *cmark_node_get_fence_info(cmark_node *node) {
594
- if (node == NULL) {
595
- return NULL;
596
- }
597
-
598
- if (node->type == CMARK_NODE_CODE_BLOCK) {
599
- return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.code.info);
600
- } else {
601
- return NULL;
602
- }
603
- }
604
-
605
- int cmark_node_set_fence_info(cmark_node *node, const char *info) {
606
- if (node == NULL) {
607
- return 0;
608
- }
609
-
610
- if (node->type == CMARK_NODE_CODE_BLOCK) {
611
- cmark_chunk_set_cstr(NODE_MEM(node), &node->as.code.info, info);
612
- return 1;
613
- } else {
614
- return 0;
615
- }
616
- }
617
-
618
- int cmark_node_get_fenced(cmark_node *node, int *length, int *offset, char *character) {
619
- if (node == NULL) {
620
- return 0;
621
- }
622
-
623
- if (node->type == CMARK_NODE_CODE_BLOCK) {
624
- *length = node->as.code.fence_length;
625
- *offset = node->as.code.fence_offset;
626
- *character = node->as.code.fence_char;
627
- return node->as.code.fenced;
628
- } else {
629
- return 0;
630
- }
631
- }
632
-
633
- int cmark_node_set_fenced(cmark_node * node, int fenced,
634
- int length, int offset, char character) {
635
- if (node == NULL) {
636
- return 0;
637
- }
638
-
639
- if (node->type == CMARK_NODE_CODE_BLOCK) {
640
- node->as.code.fenced = (int8_t)fenced;
641
- node->as.code.fence_length = (uint8_t)length;
642
- node->as.code.fence_offset = (uint8_t)offset;
643
- node->as.code.fence_char = character;
644
- return 1;
645
- } else {
646
- return 0;
647
- }
648
- }
649
-
650
- const char *cmark_node_get_url(cmark_node *node) {
651
- if (node == NULL) {
652
- return NULL;
653
- }
654
-
655
- switch (node->type) {
656
- case CMARK_NODE_LINK:
657
- case CMARK_NODE_IMAGE:
658
- return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.link.url);
659
- default:
660
- break;
661
- }
662
-
663
- return NULL;
664
- }
665
-
666
- int cmark_node_set_url(cmark_node *node, const char *url) {
667
- if (node == NULL) {
668
- return 0;
669
- }
670
-
671
- switch (node->type) {
672
- case CMARK_NODE_LINK:
673
- case CMARK_NODE_IMAGE:
674
- cmark_chunk_set_cstr(NODE_MEM(node), &node->as.link.url, url);
675
- return 1;
676
- default:
677
- break;
678
- }
679
-
680
- return 0;
681
- }
682
-
683
- const char *cmark_node_get_title(cmark_node *node) {
684
- if (node == NULL) {
685
- return NULL;
686
- }
687
-
688
- switch (node->type) {
689
- case CMARK_NODE_LINK:
690
- case CMARK_NODE_IMAGE:
691
- return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.link.title);
692
- default:
693
- break;
694
- }
695
-
696
- return NULL;
697
- }
698
-
699
- int cmark_node_set_title(cmark_node *node, const char *title) {
700
- if (node == NULL) {
701
- return 0;
702
- }
703
-
704
- switch (node->type) {
705
- case CMARK_NODE_LINK:
706
- case CMARK_NODE_IMAGE:
707
- cmark_chunk_set_cstr(NODE_MEM(node), &node->as.link.title, title);
708
- return 1;
709
- default:
710
- break;
711
- }
712
-
713
- return 0;
714
- }
715
-
716
- const char *cmark_node_get_on_enter(cmark_node *node) {
717
- if (node == NULL) {
718
- return NULL;
719
- }
720
-
721
- switch (node->type) {
722
- case CMARK_NODE_CUSTOM_INLINE:
723
- case CMARK_NODE_CUSTOM_BLOCK:
724
- return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.custom.on_enter);
725
- default:
726
- break;
727
- }
728
-
729
- return NULL;
730
- }
731
-
732
- int cmark_node_set_on_enter(cmark_node *node, const char *on_enter) {
733
- if (node == NULL) {
734
- return 0;
735
- }
736
-
737
- switch (node->type) {
738
- case CMARK_NODE_CUSTOM_INLINE:
739
- case CMARK_NODE_CUSTOM_BLOCK:
740
- cmark_chunk_set_cstr(NODE_MEM(node), &node->as.custom.on_enter, on_enter);
741
- return 1;
742
- default:
743
- break;
744
- }
745
-
746
- return 0;
747
- }
748
-
749
- const char *cmark_node_get_on_exit(cmark_node *node) {
750
- if (node == NULL) {
751
- return NULL;
752
- }
753
-
754
- switch (node->type) {
755
- case CMARK_NODE_CUSTOM_INLINE:
756
- case CMARK_NODE_CUSTOM_BLOCK:
757
- return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.custom.on_exit);
758
- default:
759
- break;
760
- }
761
-
762
- return NULL;
763
- }
764
-
765
- int cmark_node_set_on_exit(cmark_node *node, const char *on_exit) {
766
- if (node == NULL) {
767
- return 0;
768
- }
769
-
770
- switch (node->type) {
771
- case CMARK_NODE_CUSTOM_INLINE:
772
- case CMARK_NODE_CUSTOM_BLOCK:
773
- cmark_chunk_set_cstr(NODE_MEM(node), &node->as.custom.on_exit, on_exit);
774
- return 1;
775
- default:
776
- break;
777
- }
778
-
779
- return 0;
780
- }
781
-
782
- cmark_syntax_extension *cmark_node_get_syntax_extension(cmark_node *node) {
783
- if (node == NULL) {
784
- return NULL;
785
- }
786
-
787
- return node->extension;
788
- }
789
-
790
- int cmark_node_set_syntax_extension(cmark_node *node, cmark_syntax_extension *extension) {
791
- if (node == NULL) {
792
- return 0;
793
- }
794
-
795
- node->extension = extension;
796
- return 1;
797
- }
798
-
799
- int cmark_node_get_start_line(cmark_node *node) {
800
- if (node == NULL) {
801
- return 0;
802
- }
803
- return node->start_line;
804
- }
805
-
806
- int cmark_node_get_start_column(cmark_node *node) {
807
- if (node == NULL) {
808
- return 0;
809
- }
810
- return node->start_column;
811
- }
812
-
813
- int cmark_node_get_end_line(cmark_node *node) {
814
- if (node == NULL) {
815
- return 0;
816
- }
817
- return node->end_line;
818
- }
819
-
820
- int cmark_node_get_end_column(cmark_node *node) {
821
- if (node == NULL) {
822
- return 0;
823
- }
824
- return node->end_column;
825
- }
826
-
827
- // Unlink a node without adjusting its next, prev, and parent pointers.
828
- static void S_node_unlink(cmark_node *node) {
829
- if (node == NULL) {
830
- return;
831
- }
832
-
833
- if (node->prev) {
834
- node->prev->next = node->next;
835
- }
836
- if (node->next) {
837
- node->next->prev = node->prev;
838
- }
839
-
840
- // Adjust first_child and last_child of parent.
841
- cmark_node *parent = node->parent;
842
- if (parent) {
843
- if (parent->first_child == node) {
844
- parent->first_child = node->next;
845
- }
846
- if (parent->last_child == node) {
847
- parent->last_child = node->prev;
848
- }
849
- }
850
- }
851
-
852
- void cmark_node_unlink(cmark_node *node) {
853
- S_node_unlink(node);
854
-
855
- node->next = NULL;
856
- node->prev = NULL;
857
- node->parent = NULL;
858
- }
859
-
860
- int cmark_node_insert_before(cmark_node *node, cmark_node *sibling) {
861
- if (node == NULL || sibling == NULL) {
862
- return 0;
863
- }
864
-
865
- if (!node->parent || !S_can_contain(node->parent, sibling)) {
866
- return 0;
867
- }
868
-
869
- S_node_unlink(sibling);
870
-
871
- cmark_node *old_prev = node->prev;
872
-
873
- // Insert 'sibling' between 'old_prev' and 'node'.
874
- if (old_prev) {
875
- old_prev->next = sibling;
876
- }
877
- sibling->prev = old_prev;
878
- sibling->next = node;
879
- node->prev = sibling;
880
-
881
- // Set new parent.
882
- cmark_node *parent = node->parent;
883
- sibling->parent = parent;
884
-
885
- // Adjust first_child of parent if inserted as first child.
886
- if (parent && !old_prev) {
887
- parent->first_child = sibling;
888
- }
889
-
890
- return 1;
891
- }
892
-
893
- int cmark_node_insert_after(cmark_node *node, cmark_node *sibling) {
894
- if (node == NULL || sibling == NULL) {
895
- return 0;
896
- }
897
-
898
- if (!node->parent || !S_can_contain(node->parent, sibling)) {
899
- return 0;
900
- }
901
-
902
- S_node_unlink(sibling);
903
-
904
- cmark_node *old_next = node->next;
905
-
906
- // Insert 'sibling' between 'node' and 'old_next'.
907
- if (old_next) {
908
- old_next->prev = sibling;
909
- }
910
- sibling->next = old_next;
911
- sibling->prev = node;
912
- node->next = sibling;
913
-
914
- // Set new parent.
915
- cmark_node *parent = node->parent;
916
- sibling->parent = parent;
917
-
918
- // Adjust last_child of parent if inserted as last child.
919
- if (parent && !old_next) {
920
- parent->last_child = sibling;
921
- }
922
-
923
- return 1;
924
- }
925
-
926
- int cmark_node_replace(cmark_node *oldnode, cmark_node *newnode) {
927
- if (!cmark_node_insert_before(oldnode, newnode)) {
928
- return 0;
929
- }
930
- cmark_node_unlink(oldnode);
931
- return 1;
932
- }
933
-
934
- int cmark_node_prepend_child(cmark_node *node, cmark_node *child) {
935
- if (!S_can_contain(node, child)) {
936
- return 0;
937
- }
938
-
939
- S_node_unlink(child);
940
-
941
- cmark_node *old_first_child = node->first_child;
942
-
943
- child->next = old_first_child;
944
- child->prev = NULL;
945
- child->parent = node;
946
- node->first_child = child;
947
-
948
- if (old_first_child) {
949
- old_first_child->prev = child;
950
- } else {
951
- // Also set last_child if node previously had no children.
952
- node->last_child = child;
953
- }
954
-
955
- return 1;
956
- }
957
-
958
- int cmark_node_append_child(cmark_node *node, cmark_node *child) {
959
- if (!S_can_contain(node, child)) {
960
- return 0;
961
- }
962
-
963
- S_node_unlink(child);
964
-
965
- cmark_node *old_last_child = node->last_child;
966
-
967
- child->next = NULL;
968
- child->prev = old_last_child;
969
- child->parent = node;
970
- node->last_child = child;
971
-
972
- if (old_last_child) {
973
- old_last_child->next = child;
974
- } else {
975
- // Also set first_child if node previously had no children.
976
- node->first_child = child;
977
- }
978
-
979
- return 1;
980
- }
981
-
982
- static void S_print_error(FILE *out, cmark_node *node, const char *elem) {
983
- if (out == NULL) {
984
- return;
985
- }
986
- fprintf(out, "Invalid '%s' in node type %s at %d:%d\n", elem,
987
- cmark_node_get_type_string(node), node->start_line,
988
- node->start_column);
989
- }
990
-
991
- int cmark_node_check(cmark_node *node, FILE *out) {
992
- cmark_node *cur;
993
- int errors = 0;
994
-
995
- if (!node) {
996
- return 0;
997
- }
998
-
999
- cur = node;
1000
- for (;;) {
1001
- if (cur->first_child) {
1002
- if (cur->first_child->prev != NULL) {
1003
- S_print_error(out, cur->first_child, "prev");
1004
- cur->first_child->prev = NULL;
1005
- ++errors;
1006
- }
1007
- if (cur->first_child->parent != cur) {
1008
- S_print_error(out, cur->first_child, "parent");
1009
- cur->first_child->parent = cur;
1010
- ++errors;
1011
- }
1012
- cur = cur->first_child;
1013
- continue;
1014
- }
1015
-
1016
- next_sibling:
1017
- if (cur == node) {
1018
- break;
1019
- }
1020
- if (cur->next) {
1021
- if (cur->next->prev != cur) {
1022
- S_print_error(out, cur->next, "prev");
1023
- cur->next->prev = cur;
1024
- ++errors;
1025
- }
1026
- if (cur->next->parent != cur->parent) {
1027
- S_print_error(out, cur->next, "parent");
1028
- cur->next->parent = cur->parent;
1029
- ++errors;
1030
- }
1031
- cur = cur->next;
1032
- continue;
1033
- }
1034
-
1035
- if (cur->parent->last_child != cur) {
1036
- S_print_error(out, cur->parent, "last_child");
1037
- cur->parent->last_child = cur;
1038
- ++errors;
1039
- }
1040
- cur = cur->parent;
1041
- goto next_sibling;
1042
- }
1043
-
1044
- return errors;
1045
- }