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,1044 +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
- return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.literal);
381
-
382
- case CMARK_NODE_CODE_BLOCK:
383
- return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.code.literal);
384
-
385
- default:
386
- break;
387
- }
388
-
389
- return NULL;
390
- }
391
-
392
- int cmark_node_set_literal(cmark_node *node, const char *content) {
393
- if (node == NULL) {
394
- return 0;
395
- }
396
-
397
- switch (node->type) {
398
- case CMARK_NODE_HTML_BLOCK:
399
- case CMARK_NODE_TEXT:
400
- case CMARK_NODE_HTML_INLINE:
401
- case CMARK_NODE_CODE:
402
- case CMARK_NODE_FOOTNOTE_REFERENCE:
403
- cmark_chunk_set_cstr(NODE_MEM(node), &node->as.literal, content);
404
- return 1;
405
-
406
- case CMARK_NODE_CODE_BLOCK:
407
- cmark_chunk_set_cstr(NODE_MEM(node), &node->as.code.literal, content);
408
- return 1;
409
-
410
- default:
411
- break;
412
- }
413
-
414
- return 0;
415
- }
416
-
417
- const char *cmark_node_get_string_content(cmark_node *node) {
418
- return (char *) node->content.ptr;
419
- }
420
-
421
- int cmark_node_set_string_content(cmark_node *node, const char *content) {
422
- cmark_strbuf_sets(&node->content, content);
423
- return true;
424
- }
425
-
426
- int cmark_node_get_heading_level(cmark_node *node) {
427
- if (node == NULL) {
428
- return 0;
429
- }
430
-
431
- switch (node->type) {
432
- case CMARK_NODE_HEADING:
433
- return node->as.heading.level;
434
-
435
- default:
436
- break;
437
- }
438
-
439
- return 0;
440
- }
441
-
442
- int cmark_node_set_heading_level(cmark_node *node, int level) {
443
- if (node == NULL || level < 1 || level > 6) {
444
- return 0;
445
- }
446
-
447
- switch (node->type) {
448
- case CMARK_NODE_HEADING:
449
- node->as.heading.level = level;
450
- return 1;
451
-
452
- default:
453
- break;
454
- }
455
-
456
- return 0;
457
- }
458
-
459
- cmark_list_type cmark_node_get_list_type(cmark_node *node) {
460
- if (node == NULL) {
461
- return CMARK_NO_LIST;
462
- }
463
-
464
- if (node->type == CMARK_NODE_LIST) {
465
- return node->as.list.list_type;
466
- } else {
467
- return CMARK_NO_LIST;
468
- }
469
- }
470
-
471
- int cmark_node_set_list_type(cmark_node *node, cmark_list_type type) {
472
- if (!(type == CMARK_BULLET_LIST || type == CMARK_ORDERED_LIST)) {
473
- return 0;
474
- }
475
-
476
- if (node == NULL) {
477
- return 0;
478
- }
479
-
480
- if (node->type == CMARK_NODE_LIST) {
481
- node->as.list.list_type = type;
482
- return 1;
483
- } else {
484
- return 0;
485
- }
486
- }
487
-
488
- cmark_delim_type cmark_node_get_list_delim(cmark_node *node) {
489
- if (node == NULL) {
490
- return CMARK_NO_DELIM;
491
- }
492
-
493
- if (node->type == CMARK_NODE_LIST) {
494
- return node->as.list.delimiter;
495
- } else {
496
- return CMARK_NO_DELIM;
497
- }
498
- }
499
-
500
- int cmark_node_set_list_delim(cmark_node *node, cmark_delim_type delim) {
501
- if (!(delim == CMARK_PERIOD_DELIM || delim == CMARK_PAREN_DELIM)) {
502
- return 0;
503
- }
504
-
505
- if (node == NULL) {
506
- return 0;
507
- }
508
-
509
- if (node->type == CMARK_NODE_LIST) {
510
- node->as.list.delimiter = delim;
511
- return 1;
512
- } else {
513
- return 0;
514
- }
515
- }
516
-
517
- int cmark_node_get_list_start(cmark_node *node) {
518
- if (node == NULL) {
519
- return 0;
520
- }
521
-
522
- if (node->type == CMARK_NODE_LIST) {
523
- return node->as.list.start;
524
- } else {
525
- return 0;
526
- }
527
- }
528
-
529
- int cmark_node_set_list_start(cmark_node *node, int start) {
530
- if (node == NULL || start < 0) {
531
- return 0;
532
- }
533
-
534
- if (node->type == CMARK_NODE_LIST) {
535
- node->as.list.start = start;
536
- return 1;
537
- } else {
538
- return 0;
539
- }
540
- }
541
-
542
- int cmark_node_get_list_tight(cmark_node *node) {
543
- if (node == NULL) {
544
- return 0;
545
- }
546
-
547
- if (node->type == CMARK_NODE_LIST) {
548
- return node->as.list.tight;
549
- } else {
550
- return 0;
551
- }
552
- }
553
-
554
- int cmark_node_set_list_tight(cmark_node *node, int tight) {
555
- if (node == NULL) {
556
- return 0;
557
- }
558
-
559
- if (node->type == CMARK_NODE_LIST) {
560
- node->as.list.tight = tight == 1;
561
- return 1;
562
- } else {
563
- return 0;
564
- }
565
- }
566
-
567
- int cmark_node_get_item_index(cmark_node *node) {
568
- if (node == NULL) {
569
- return 0;
570
- }
571
-
572
- if (node->type == CMARK_NODE_ITEM) {
573
- return node->as.list.start;
574
- } else {
575
- return 0;
576
- }
577
- }
578
-
579
- int cmark_node_set_item_index(cmark_node *node, int idx) {
580
- if (node == NULL || idx < 0) {
581
- return 0;
582
- }
583
-
584
- if (node->type == CMARK_NODE_ITEM) {
585
- node->as.list.start = idx;
586
- return 1;
587
- } else {
588
- return 0;
589
- }
590
- }
591
-
592
- const char *cmark_node_get_fence_info(cmark_node *node) {
593
- if (node == NULL) {
594
- return NULL;
595
- }
596
-
597
- if (node->type == CMARK_NODE_CODE_BLOCK) {
598
- return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.code.info);
599
- } else {
600
- return NULL;
601
- }
602
- }
603
-
604
- int cmark_node_set_fence_info(cmark_node *node, const char *info) {
605
- if (node == NULL) {
606
- return 0;
607
- }
608
-
609
- if (node->type == CMARK_NODE_CODE_BLOCK) {
610
- cmark_chunk_set_cstr(NODE_MEM(node), &node->as.code.info, info);
611
- return 1;
612
- } else {
613
- return 0;
614
- }
615
- }
616
-
617
- int cmark_node_get_fenced(cmark_node *node, int *length, int *offset, char *character) {
618
- if (node == NULL) {
619
- return 0;
620
- }
621
-
622
- if (node->type == CMARK_NODE_CODE_BLOCK) {
623
- *length = node->as.code.fence_length;
624
- *offset = node->as.code.fence_offset;
625
- *character = node->as.code.fence_char;
626
- return node->as.code.fenced;
627
- } else {
628
- return 0;
629
- }
630
- }
631
-
632
- int cmark_node_set_fenced(cmark_node * node, int fenced,
633
- int length, int offset, char character) {
634
- if (node == NULL) {
635
- return 0;
636
- }
637
-
638
- if (node->type == CMARK_NODE_CODE_BLOCK) {
639
- node->as.code.fenced = (int8_t)fenced;
640
- node->as.code.fence_length = (uint8_t)length;
641
- node->as.code.fence_offset = (uint8_t)offset;
642
- node->as.code.fence_char = character;
643
- return 1;
644
- } else {
645
- return 0;
646
- }
647
- }
648
-
649
- const char *cmark_node_get_url(cmark_node *node) {
650
- if (node == NULL) {
651
- return NULL;
652
- }
653
-
654
- switch (node->type) {
655
- case CMARK_NODE_LINK:
656
- case CMARK_NODE_IMAGE:
657
- return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.link.url);
658
- default:
659
- break;
660
- }
661
-
662
- return NULL;
663
- }
664
-
665
- int cmark_node_set_url(cmark_node *node, const char *url) {
666
- if (node == NULL) {
667
- return 0;
668
- }
669
-
670
- switch (node->type) {
671
- case CMARK_NODE_LINK:
672
- case CMARK_NODE_IMAGE:
673
- cmark_chunk_set_cstr(NODE_MEM(node), &node->as.link.url, url);
674
- return 1;
675
- default:
676
- break;
677
- }
678
-
679
- return 0;
680
- }
681
-
682
- const char *cmark_node_get_title(cmark_node *node) {
683
- if (node == NULL) {
684
- return NULL;
685
- }
686
-
687
- switch (node->type) {
688
- case CMARK_NODE_LINK:
689
- case CMARK_NODE_IMAGE:
690
- return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.link.title);
691
- default:
692
- break;
693
- }
694
-
695
- return NULL;
696
- }
697
-
698
- int cmark_node_set_title(cmark_node *node, const char *title) {
699
- if (node == NULL) {
700
- return 0;
701
- }
702
-
703
- switch (node->type) {
704
- case CMARK_NODE_LINK:
705
- case CMARK_NODE_IMAGE:
706
- cmark_chunk_set_cstr(NODE_MEM(node), &node->as.link.title, title);
707
- return 1;
708
- default:
709
- break;
710
- }
711
-
712
- return 0;
713
- }
714
-
715
- const char *cmark_node_get_on_enter(cmark_node *node) {
716
- if (node == NULL) {
717
- return NULL;
718
- }
719
-
720
- switch (node->type) {
721
- case CMARK_NODE_CUSTOM_INLINE:
722
- case CMARK_NODE_CUSTOM_BLOCK:
723
- return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.custom.on_enter);
724
- default:
725
- break;
726
- }
727
-
728
- return NULL;
729
- }
730
-
731
- int cmark_node_set_on_enter(cmark_node *node, const char *on_enter) {
732
- if (node == NULL) {
733
- return 0;
734
- }
735
-
736
- switch (node->type) {
737
- case CMARK_NODE_CUSTOM_INLINE:
738
- case CMARK_NODE_CUSTOM_BLOCK:
739
- cmark_chunk_set_cstr(NODE_MEM(node), &node->as.custom.on_enter, on_enter);
740
- return 1;
741
- default:
742
- break;
743
- }
744
-
745
- return 0;
746
- }
747
-
748
- const char *cmark_node_get_on_exit(cmark_node *node) {
749
- if (node == NULL) {
750
- return NULL;
751
- }
752
-
753
- switch (node->type) {
754
- case CMARK_NODE_CUSTOM_INLINE:
755
- case CMARK_NODE_CUSTOM_BLOCK:
756
- return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.custom.on_exit);
757
- default:
758
- break;
759
- }
760
-
761
- return NULL;
762
- }
763
-
764
- int cmark_node_set_on_exit(cmark_node *node, const char *on_exit) {
765
- if (node == NULL) {
766
- return 0;
767
- }
768
-
769
- switch (node->type) {
770
- case CMARK_NODE_CUSTOM_INLINE:
771
- case CMARK_NODE_CUSTOM_BLOCK:
772
- cmark_chunk_set_cstr(NODE_MEM(node), &node->as.custom.on_exit, on_exit);
773
- return 1;
774
- default:
775
- break;
776
- }
777
-
778
- return 0;
779
- }
780
-
781
- cmark_syntax_extension *cmark_node_get_syntax_extension(cmark_node *node) {
782
- if (node == NULL) {
783
- return NULL;
784
- }
785
-
786
- return node->extension;
787
- }
788
-
789
- int cmark_node_set_syntax_extension(cmark_node *node, cmark_syntax_extension *extension) {
790
- if (node == NULL) {
791
- return 0;
792
- }
793
-
794
- node->extension = extension;
795
- return 1;
796
- }
797
-
798
- int cmark_node_get_start_line(cmark_node *node) {
799
- if (node == NULL) {
800
- return 0;
801
- }
802
- return node->start_line;
803
- }
804
-
805
- int cmark_node_get_start_column(cmark_node *node) {
806
- if (node == NULL) {
807
- return 0;
808
- }
809
- return node->start_column;
810
- }
811
-
812
- int cmark_node_get_end_line(cmark_node *node) {
813
- if (node == NULL) {
814
- return 0;
815
- }
816
- return node->end_line;
817
- }
818
-
819
- int cmark_node_get_end_column(cmark_node *node) {
820
- if (node == NULL) {
821
- return 0;
822
- }
823
- return node->end_column;
824
- }
825
-
826
- // Unlink a node without adjusting its next, prev, and parent pointers.
827
- static void S_node_unlink(cmark_node *node) {
828
- if (node == NULL) {
829
- return;
830
- }
831
-
832
- if (node->prev) {
833
- node->prev->next = node->next;
834
- }
835
- if (node->next) {
836
- node->next->prev = node->prev;
837
- }
838
-
839
- // Adjust first_child and last_child of parent.
840
- cmark_node *parent = node->parent;
841
- if (parent) {
842
- if (parent->first_child == node) {
843
- parent->first_child = node->next;
844
- }
845
- if (parent->last_child == node) {
846
- parent->last_child = node->prev;
847
- }
848
- }
849
- }
850
-
851
- void cmark_node_unlink(cmark_node *node) {
852
- S_node_unlink(node);
853
-
854
- node->next = NULL;
855
- node->prev = NULL;
856
- node->parent = NULL;
857
- }
858
-
859
- int cmark_node_insert_before(cmark_node *node, cmark_node *sibling) {
860
- if (node == NULL || sibling == NULL) {
861
- return 0;
862
- }
863
-
864
- if (!node->parent || !S_can_contain(node->parent, sibling)) {
865
- return 0;
866
- }
867
-
868
- S_node_unlink(sibling);
869
-
870
- cmark_node *old_prev = node->prev;
871
-
872
- // Insert 'sibling' between 'old_prev' and 'node'.
873
- if (old_prev) {
874
- old_prev->next = sibling;
875
- }
876
- sibling->prev = old_prev;
877
- sibling->next = node;
878
- node->prev = sibling;
879
-
880
- // Set new parent.
881
- cmark_node *parent = node->parent;
882
- sibling->parent = parent;
883
-
884
- // Adjust first_child of parent if inserted as first child.
885
- if (parent && !old_prev) {
886
- parent->first_child = sibling;
887
- }
888
-
889
- return 1;
890
- }
891
-
892
- int cmark_node_insert_after(cmark_node *node, cmark_node *sibling) {
893
- if (node == NULL || sibling == NULL) {
894
- return 0;
895
- }
896
-
897
- if (!node->parent || !S_can_contain(node->parent, sibling)) {
898
- return 0;
899
- }
900
-
901
- S_node_unlink(sibling);
902
-
903
- cmark_node *old_next = node->next;
904
-
905
- // Insert 'sibling' between 'node' and 'old_next'.
906
- if (old_next) {
907
- old_next->prev = sibling;
908
- }
909
- sibling->next = old_next;
910
- sibling->prev = node;
911
- node->next = sibling;
912
-
913
- // Set new parent.
914
- cmark_node *parent = node->parent;
915
- sibling->parent = parent;
916
-
917
- // Adjust last_child of parent if inserted as last child.
918
- if (parent && !old_next) {
919
- parent->last_child = sibling;
920
- }
921
-
922
- return 1;
923
- }
924
-
925
- int cmark_node_replace(cmark_node *oldnode, cmark_node *newnode) {
926
- if (!cmark_node_insert_before(oldnode, newnode)) {
927
- return 0;
928
- }
929
- cmark_node_unlink(oldnode);
930
- return 1;
931
- }
932
-
933
- int cmark_node_prepend_child(cmark_node *node, cmark_node *child) {
934
- if (!S_can_contain(node, child)) {
935
- return 0;
936
- }
937
-
938
- S_node_unlink(child);
939
-
940
- cmark_node *old_first_child = node->first_child;
941
-
942
- child->next = old_first_child;
943
- child->prev = NULL;
944
- child->parent = node;
945
- node->first_child = child;
946
-
947
- if (old_first_child) {
948
- old_first_child->prev = child;
949
- } else {
950
- // Also set last_child if node previously had no children.
951
- node->last_child = child;
952
- }
953
-
954
- return 1;
955
- }
956
-
957
- int cmark_node_append_child(cmark_node *node, cmark_node *child) {
958
- if (!S_can_contain(node, child)) {
959
- return 0;
960
- }
961
-
962
- S_node_unlink(child);
963
-
964
- cmark_node *old_last_child = node->last_child;
965
-
966
- child->next = NULL;
967
- child->prev = old_last_child;
968
- child->parent = node;
969
- node->last_child = child;
970
-
971
- if (old_last_child) {
972
- old_last_child->next = child;
973
- } else {
974
- // Also set first_child if node previously had no children.
975
- node->first_child = child;
976
- }
977
-
978
- return 1;
979
- }
980
-
981
- static void S_print_error(FILE *out, cmark_node *node, const char *elem) {
982
- if (out == NULL) {
983
- return;
984
- }
985
- fprintf(out, "Invalid '%s' in node type %s at %d:%d\n", elem,
986
- cmark_node_get_type_string(node), node->start_line,
987
- node->start_column);
988
- }
989
-
990
- int cmark_node_check(cmark_node *node, FILE *out) {
991
- cmark_node *cur;
992
- int errors = 0;
993
-
994
- if (!node) {
995
- return 0;
996
- }
997
-
998
- cur = node;
999
- for (;;) {
1000
- if (cur->first_child) {
1001
- if (cur->first_child->prev != NULL) {
1002
- S_print_error(out, cur->first_child, "prev");
1003
- cur->first_child->prev = NULL;
1004
- ++errors;
1005
- }
1006
- if (cur->first_child->parent != cur) {
1007
- S_print_error(out, cur->first_child, "parent");
1008
- cur->first_child->parent = cur;
1009
- ++errors;
1010
- }
1011
- cur = cur->first_child;
1012
- continue;
1013
- }
1014
-
1015
- next_sibling:
1016
- if (cur == node) {
1017
- break;
1018
- }
1019
- if (cur->next) {
1020
- if (cur->next->prev != cur) {
1021
- S_print_error(out, cur->next, "prev");
1022
- cur->next->prev = cur;
1023
- ++errors;
1024
- }
1025
- if (cur->next->parent != cur->parent) {
1026
- S_print_error(out, cur->next, "parent");
1027
- cur->next->parent = cur->parent;
1028
- ++errors;
1029
- }
1030
- cur = cur->next;
1031
- continue;
1032
- }
1033
-
1034
- if (cur->parent->last_child != cur) {
1035
- S_print_error(out, cur->parent, "last_child");
1036
- cur->parent->last_child = cur;
1037
- ++errors;
1038
- }
1039
- cur = cur->parent;
1040
- goto next_sibling;
1041
- }
1042
-
1043
- return errors;
1044
- }