commonmarker 0.23.6 → 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 +58 -37
  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 +19 -32
  14. metadata +33 -177
  15. data/Rakefile +0 -109
  16. data/bin/commonmarker +0 -118
  17. data/ext/commonmarker/arena.c +0 -103
  18. data/ext/commonmarker/autolink.c +0 -456
  19. data/ext/commonmarker/autolink.h +0 -8
  20. data/ext/commonmarker/blocks.c +0 -1596
  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 -736
  27. data/ext/commonmarker/cmark-gfm-extensions_export.h +0 -42
  28. data/ext/commonmarker/cmark-gfm.h +0 -817
  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 -529
  35. data/ext/commonmarker/commonmarker.c +0 -1307
  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 -486
  49. data/ext/commonmarker/html.h +0 -27
  50. data/ext/commonmarker/inlines.c +0 -1716
  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 -466
  55. data/ext/commonmarker/linked_list.c +0 -37
  56. data/ext/commonmarker/man.c +0 -278
  57. data/ext/commonmarker/map.c +0 -122
  58. data/ext/commonmarker/map.h +0 -41
  59. data/ext/commonmarker/node.c +0 -979
  60. data/ext/commonmarker/node.h +0 -125
  61. data/ext/commonmarker/parser.h +0 -58
  62. data/ext/commonmarker/plaintext.c +0 -235
  63. data/ext/commonmarker/plugin.c +0 -36
  64. data/ext/commonmarker/plugin.h +0 -34
  65. data/ext/commonmarker/references.c +0 -42
  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 -205
  70. data/ext/commonmarker/render.h +0 -62
  71. data/ext/commonmarker/scanners.c +0 -10508
  72. data/ext/commonmarker/scanners.h +0 -62
  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 -848
  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 -181
  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 -252
@@ -1,42 +0,0 @@
1
-
2
- #ifndef CMARK_GFM_EXPORT_H
3
- #define CMARK_GFM_EXPORT_H
4
-
5
- #ifdef CMARK_GFM_STATIC_DEFINE
6
- # define CMARK_GFM_EXPORT
7
- # define CMARK_GFM_NO_EXPORT
8
- #else
9
- # ifndef CMARK_GFM_EXPORT
10
- # ifdef libcmark_gfm_EXPORTS
11
- /* We are building this library */
12
- # define CMARK_GFM_EXPORT __attribute__((visibility("default")))
13
- # else
14
- /* We are using this library */
15
- # define CMARK_GFM_EXPORT __attribute__((visibility("default")))
16
- # endif
17
- # endif
18
-
19
- # ifndef CMARK_GFM_NO_EXPORT
20
- # define CMARK_GFM_NO_EXPORT __attribute__((visibility("hidden")))
21
- # endif
22
- #endif
23
-
24
- #ifndef CMARK_GFM_DEPRECATED
25
- # define CMARK_GFM_DEPRECATED __attribute__ ((__deprecated__))
26
- #endif
27
-
28
- #ifndef CMARK_GFM_DEPRECATED_EXPORT
29
- # define CMARK_GFM_DEPRECATED_EXPORT CMARK_GFM_EXPORT CMARK_GFM_DEPRECATED
30
- #endif
31
-
32
- #ifndef CMARK_GFM_DEPRECATED_NO_EXPORT
33
- # define CMARK_GFM_DEPRECATED_NO_EXPORT CMARK_GFM_NO_EXPORT CMARK_GFM_DEPRECATED
34
- #endif
35
-
36
- #if 0 /* DEFINE_NO_DEPRECATED */
37
- # ifndef CMARK_GFM_NO_DEPRECATED
38
- # define CMARK_GFM_NO_DEPRECATED
39
- # endif
40
- #endif
41
-
42
- #endif /* CMARK_GFM_EXPORT_H */
@@ -1,7 +0,0 @@
1
- #ifndef CMARK_GFM_VERSION_H
2
- #define CMARK_GFM_VERSION_H
3
-
4
- #define CMARK_GFM_VERSION ((0 << 24) | (29 << 16) | (0 << 8) | 6)
5
- #define CMARK_GFM_VERSION_STRING "0.29.0.gfm.6"
6
-
7
- #endif
@@ -1,55 +0,0 @@
1
- #include <stdlib.h>
2
- #include <assert.h>
3
- #include <stdio.h>
4
- #include "registry.h"
5
- #include "node.h"
6
- #include "houdini.h"
7
- #include "cmark-gfm.h"
8
- #include "buffer.h"
9
-
10
- cmark_node_type CMARK_NODE_LAST_BLOCK = CMARK_NODE_FOOTNOTE_DEFINITION;
11
- cmark_node_type CMARK_NODE_LAST_INLINE = CMARK_NODE_FOOTNOTE_REFERENCE;
12
-
13
- int cmark_version() { return CMARK_GFM_VERSION; }
14
-
15
- const char *cmark_version_string() { return CMARK_GFM_VERSION_STRING; }
16
-
17
- static void *xcalloc(size_t nmem, size_t size) {
18
- void *ptr = calloc(nmem, size);
19
- if (!ptr) {
20
- fprintf(stderr, "[cmark] calloc returned null pointer, aborting\n");
21
- abort();
22
- }
23
- return ptr;
24
- }
25
-
26
- static void *xrealloc(void *ptr, size_t size) {
27
- void *new_ptr = realloc(ptr, size);
28
- if (!new_ptr) {
29
- fprintf(stderr, "[cmark] realloc returned null pointer, aborting\n");
30
- abort();
31
- }
32
- return new_ptr;
33
- }
34
-
35
- static void xfree(void *ptr) {
36
- free(ptr);
37
- }
38
-
39
- cmark_mem CMARK_DEFAULT_MEM_ALLOCATOR = {xcalloc, xrealloc, xfree};
40
-
41
- cmark_mem *cmark_get_default_mem_allocator() {
42
- return &CMARK_DEFAULT_MEM_ALLOCATOR;
43
- }
44
-
45
- char *cmark_markdown_to_html(const char *text, size_t len, int options) {
46
- cmark_node *doc;
47
- char *result;
48
-
49
- doc = cmark_parse_document(text, len, options);
50
-
51
- result = cmark_render_html(doc, options, NULL);
52
- cmark_node_free(doc);
53
-
54
- return result;
55
- }
@@ -1,44 +0,0 @@
1
- #include <stdint.h>
2
-
3
- #include "cmark_ctype.h"
4
-
5
- /** 1 = space, 2 = punct, 3 = digit, 4 = alpha, 0 = other
6
- */
7
- static const uint8_t cmark_ctype_class[256] = {
8
- /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
9
- /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
10
- /* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11
- /* 2 */ 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
12
- /* 3 */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2,
13
- /* 4 */ 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
14
- /* 5 */ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 2, 2,
15
- /* 6 */ 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
16
- /* 7 */ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 2, 0,
17
- /* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18
- /* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19
- /* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
20
- /* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
21
- /* c */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
22
- /* d */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
23
- /* e */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
24
- /* f */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
25
-
26
- /**
27
- * Returns 1 if c is a "whitespace" character as defined by the spec.
28
- */
29
- int cmark_isspace(char c) { return cmark_ctype_class[(uint8_t)c] == 1; }
30
-
31
- /**
32
- * Returns 1 if c is an ascii punctuation character.
33
- */
34
- int cmark_ispunct(char c) { return cmark_ctype_class[(uint8_t)c] == 2; }
35
-
36
- int cmark_isalnum(char c) {
37
- uint8_t result;
38
- result = cmark_ctype_class[(uint8_t)c];
39
- return (result == 3 || result == 4);
40
- }
41
-
42
- int cmark_isdigit(char c) { return cmark_ctype_class[(uint8_t)c] == 3; }
43
-
44
- int cmark_isalpha(char c) { return cmark_ctype_class[(uint8_t)c] == 4; }
@@ -1,33 +0,0 @@
1
- #ifndef CMARK_CMARK_CTYPE_H
2
- #define CMARK_CMARK_CTYPE_H
3
-
4
- #ifdef __cplusplus
5
- extern "C" {
6
- #endif
7
-
8
- #include "cmark-gfm_export.h"
9
-
10
- /** Locale-independent versions of functions from ctype.h.
11
- * We want cmark to behave the same no matter what the system locale.
12
- */
13
-
14
- CMARK_GFM_EXPORT
15
- int cmark_isspace(char c);
16
-
17
- CMARK_GFM_EXPORT
18
- int cmark_ispunct(char c);
19
-
20
- CMARK_GFM_EXPORT
21
- int cmark_isalnum(char c);
22
-
23
- CMARK_GFM_EXPORT
24
- int cmark_isdigit(char c);
25
-
26
- CMARK_GFM_EXPORT
27
- int cmark_isalpha(char c);
28
-
29
- #ifdef __cplusplus
30
- }
31
- #endif
32
-
33
- #endif
@@ -1,529 +0,0 @@
1
- #include <stdlib.h>
2
- #include <stdio.h>
3
- #include <string.h>
4
- #include <stdint.h>
5
- #include <assert.h>
6
-
7
- #include "config.h"
8
- #include "cmark-gfm.h"
9
- #include "node.h"
10
- #include "buffer.h"
11
- #include "utf8.h"
12
- #include "scanners.h"
13
- #include "render.h"
14
- #include "syntax_extension.h"
15
-
16
- #define OUT(s, wrap, escaping) renderer->out(renderer, node, s, wrap, escaping)
17
- #define LIT(s) renderer->out(renderer, node, s, false, LITERAL)
18
- #define CR() renderer->cr(renderer)
19
- #define BLANKLINE() renderer->blankline(renderer)
20
- #define ENCODED_SIZE 20
21
- #define LISTMARKER_SIZE 20
22
-
23
- // Functions to convert cmark_nodes to commonmark strings.
24
-
25
- static CMARK_INLINE void outc(cmark_renderer *renderer, cmark_node *node,
26
- cmark_escaping escape,
27
- int32_t c, unsigned char nextc) {
28
- bool needs_escaping = false;
29
- bool follows_digit =
30
- renderer->buffer->size > 0 &&
31
- cmark_isdigit(renderer->buffer->ptr[renderer->buffer->size - 1]);
32
- char encoded[ENCODED_SIZE];
33
-
34
- needs_escaping =
35
- c < 0x80 && escape != LITERAL &&
36
- ((escape == NORMAL &&
37
- (c < 0x20 ||
38
- c == '*' || c == '_' || c == '[' || c == ']' || c == '#' || c == '<' ||
39
- c == '>' || c == '\\' || c == '`' || c == '~' || c == '!' ||
40
- (c == '&' && cmark_isalpha(nextc)) || (c == '!' && nextc == '[') ||
41
- (renderer->begin_content && (c == '-' || c == '+' || c == '=') &&
42
- // begin_content doesn't get set to false til we've passed digits
43
- // at the beginning of line, so...
44
- !follows_digit) ||
45
- (renderer->begin_content && (c == '.' || c == ')') && follows_digit &&
46
- (nextc == 0 || cmark_isspace(nextc))))) ||
47
- (escape == URL &&
48
- (c == '`' || c == '<' || c == '>' || cmark_isspace((char)c) || c == '\\' ||
49
- c == ')' || c == '(')) ||
50
- (escape == TITLE &&
51
- (c == '`' || c == '<' || c == '>' || c == '"' || c == '\\')));
52
-
53
- if (needs_escaping) {
54
- if (escape == URL && cmark_isspace((char)c)) {
55
- // use percent encoding for spaces
56
- snprintf(encoded, ENCODED_SIZE, "%%%2X", c);
57
- cmark_strbuf_puts(renderer->buffer, encoded);
58
- renderer->column += 3;
59
- } else if (cmark_ispunct((char)c)) {
60
- cmark_render_ascii(renderer, "\\");
61
- cmark_render_code_point(renderer, c);
62
- } else { // render as entity
63
- snprintf(encoded, ENCODED_SIZE, "&#%d;", c);
64
- cmark_strbuf_puts(renderer->buffer, encoded);
65
- renderer->column += (int)strlen(encoded);
66
- }
67
- } else {
68
- cmark_render_code_point(renderer, c);
69
- }
70
- }
71
-
72
- static int longest_backtick_sequence(const char *code) {
73
- int longest = 0;
74
- int current = 0;
75
- size_t i = 0;
76
- size_t code_len = strlen(code);
77
- while (i <= code_len) {
78
- if (code[i] == '`') {
79
- current++;
80
- } else {
81
- if (current > longest) {
82
- longest = current;
83
- }
84
- current = 0;
85
- }
86
- i++;
87
- }
88
- return longest;
89
- }
90
-
91
- static int shortest_unused_backtick_sequence(const char *code) {
92
- // note: if the shortest sequence is >= 32, this returns 32
93
- // so as not to overflow the bit array.
94
- uint32_t used = 1;
95
- int current = 0;
96
- size_t i = 0;
97
- size_t code_len = strlen(code);
98
- while (i <= code_len) {
99
- if (code[i] == '`') {
100
- current++;
101
- } else {
102
- if (current > 0 && current < 32) {
103
- used |= (1U << current);
104
- }
105
- current = 0;
106
- }
107
- i++;
108
- }
109
- // return number of first bit that is 0:
110
- i = 0;
111
- while (i < 32 && used & 1) {
112
- used = used >> 1;
113
- i++;
114
- }
115
- return (int)i;
116
- }
117
-
118
- static bool is_autolink(cmark_node *node) {
119
- cmark_chunk *title;
120
- cmark_chunk *url;
121
- cmark_node *link_text;
122
- char *realurl;
123
- int realurllen;
124
-
125
- if (node->type != CMARK_NODE_LINK) {
126
- return false;
127
- }
128
-
129
- url = &node->as.link.url;
130
- if (url->len == 0 || scan_scheme(url, 0) == 0) {
131
- return false;
132
- }
133
-
134
- title = &node->as.link.title;
135
- // if it has a title, we can't treat it as an autolink:
136
- if (title->len > 0) {
137
- return false;
138
- }
139
-
140
- link_text = node->first_child;
141
- if (link_text == NULL) {
142
- return false;
143
- }
144
- cmark_consolidate_text_nodes(link_text);
145
- realurl = (char *)url->data;
146
- realurllen = url->len;
147
- if (strncmp(realurl, "mailto:", 7) == 0) {
148
- realurl += 7;
149
- realurllen -= 7;
150
- }
151
- return (realurllen == link_text->as.literal.len &&
152
- strncmp(realurl, (char *)link_text->as.literal.data,
153
- link_text->as.literal.len) == 0);
154
- }
155
-
156
- // if node is a block node, returns node.
157
- // otherwise returns first block-level node that is an ancestor of node.
158
- // if there is no block-level ancestor, returns NULL.
159
- static cmark_node *get_containing_block(cmark_node *node) {
160
- while (node) {
161
- if (CMARK_NODE_BLOCK_P(node)) {
162
- return node;
163
- } else {
164
- node = node->parent;
165
- }
166
- }
167
- return NULL;
168
- }
169
-
170
- static int S_render_node(cmark_renderer *renderer, cmark_node *node,
171
- cmark_event_type ev_type, int options) {
172
- cmark_node *tmp;
173
- int list_number;
174
- cmark_delim_type list_delim;
175
- int numticks;
176
- bool extra_spaces;
177
- int i;
178
- bool entering = (ev_type == CMARK_EVENT_ENTER);
179
- const char *info, *code, *title;
180
- char fencechar[2] = {'\0', '\0'};
181
- size_t info_len, code_len;
182
- char listmarker[LISTMARKER_SIZE];
183
- char *emph_delim;
184
- bool first_in_list_item;
185
- bufsize_t marker_width;
186
- bool allow_wrap = renderer->width > 0 && !(CMARK_OPT_NOBREAKS & options) &&
187
- !(CMARK_OPT_HARDBREAKS & options);
188
-
189
- // Don't adjust tight list status til we've started the list.
190
- // Otherwise we loose the blank line between a paragraph and
191
- // a following list.
192
- if (!(node->type == CMARK_NODE_ITEM && node->prev == NULL && entering)) {
193
- tmp = get_containing_block(node);
194
- renderer->in_tight_list_item =
195
- tmp && // tmp might be NULL if there is no containing block
196
- ((tmp->type == CMARK_NODE_ITEM &&
197
- cmark_node_get_list_tight(tmp->parent)) ||
198
- (tmp && tmp->parent && tmp->parent->type == CMARK_NODE_ITEM &&
199
- cmark_node_get_list_tight(tmp->parent->parent)));
200
- }
201
-
202
- if (node->extension && node->extension->commonmark_render_func) {
203
- node->extension->commonmark_render_func(node->extension, renderer, node, ev_type, options);
204
- return 1;
205
- }
206
-
207
- switch (node->type) {
208
- case CMARK_NODE_DOCUMENT:
209
- break;
210
-
211
- case CMARK_NODE_BLOCK_QUOTE:
212
- if (entering) {
213
- LIT("> ");
214
- renderer->begin_content = true;
215
- cmark_strbuf_puts(renderer->prefix, "> ");
216
- } else {
217
- cmark_strbuf_truncate(renderer->prefix, renderer->prefix->size - 2);
218
- BLANKLINE();
219
- }
220
- break;
221
-
222
- case CMARK_NODE_LIST:
223
- if (!entering && node->next && (node->next->type == CMARK_NODE_CODE_BLOCK ||
224
- node->next->type == CMARK_NODE_LIST)) {
225
- // this ensures that a following indented code block or list will be
226
- // inteprereted correctly.
227
- CR();
228
- LIT("<!-- end list -->");
229
- BLANKLINE();
230
- }
231
- break;
232
-
233
- case CMARK_NODE_ITEM:
234
- if (cmark_node_get_list_type(node->parent) == CMARK_BULLET_LIST) {
235
- marker_width = 4;
236
- } else {
237
- list_number = cmark_node_get_list_start(node->parent);
238
- list_delim = cmark_node_get_list_delim(node->parent);
239
- tmp = node;
240
- while (tmp->prev) {
241
- tmp = tmp->prev;
242
- list_number += 1;
243
- }
244
- // we ensure a width of at least 4 so
245
- // we get nice transition from single digits
246
- // to double
247
- snprintf(listmarker, LISTMARKER_SIZE, "%d%s%s", list_number,
248
- list_delim == CMARK_PAREN_DELIM ? ")" : ".",
249
- list_number < 10 ? " " : " ");
250
- marker_width = (bufsize_t)strlen(listmarker);
251
- }
252
- if (entering) {
253
- if (cmark_node_get_list_type(node->parent) == CMARK_BULLET_LIST) {
254
- LIT(" - ");
255
- renderer->begin_content = true;
256
- } else {
257
- LIT(listmarker);
258
- renderer->begin_content = true;
259
- }
260
- for (i = marker_width; i--;) {
261
- cmark_strbuf_putc(renderer->prefix, ' ');
262
- }
263
- } else {
264
- cmark_strbuf_truncate(renderer->prefix,
265
- renderer->prefix->size - marker_width);
266
- CR();
267
- }
268
- break;
269
-
270
- case CMARK_NODE_HEADING:
271
- if (entering) {
272
- for (i = cmark_node_get_heading_level(node); i > 0; i--) {
273
- LIT("#");
274
- }
275
- LIT(" ");
276
- renderer->begin_content = true;
277
- renderer->no_linebreaks = true;
278
- } else {
279
- renderer->no_linebreaks = false;
280
- BLANKLINE();
281
- }
282
- break;
283
-
284
- case CMARK_NODE_CODE_BLOCK:
285
- first_in_list_item = node->prev == NULL && node->parent &&
286
- node->parent->type == CMARK_NODE_ITEM;
287
-
288
- if (!first_in_list_item) {
289
- BLANKLINE();
290
- }
291
- info = cmark_node_get_fence_info(node);
292
- info_len = strlen(info);
293
- fencechar[0] = strchr(info, '`') == NULL ? '`' : '~';
294
- code = cmark_node_get_literal(node);
295
- code_len = strlen(code);
296
- // use indented form if no info, and code doesn't
297
- // begin or end with a blank line, and code isn't
298
- // first thing in a list item
299
- if (info_len == 0 && (code_len > 2 && !cmark_isspace(code[0]) &&
300
- !(cmark_isspace(code[code_len - 1]) &&
301
- cmark_isspace(code[code_len - 2]))) &&
302
- !first_in_list_item) {
303
- LIT(" ");
304
- cmark_strbuf_puts(renderer->prefix, " ");
305
- OUT(cmark_node_get_literal(node), false, LITERAL);
306
- cmark_strbuf_truncate(renderer->prefix, renderer->prefix->size - 4);
307
- } else {
308
- numticks = longest_backtick_sequence(code) + 1;
309
- if (numticks < 3) {
310
- numticks = 3;
311
- }
312
- for (i = 0; i < numticks; i++) {
313
- LIT(fencechar);
314
- }
315
- LIT(" ");
316
- OUT(info, false, LITERAL);
317
- CR();
318
- OUT(cmark_node_get_literal(node), false, LITERAL);
319
- CR();
320
- for (i = 0; i < numticks; i++) {
321
- LIT(fencechar);
322
- }
323
- }
324
- BLANKLINE();
325
- break;
326
-
327
- case CMARK_NODE_HTML_BLOCK:
328
- BLANKLINE();
329
- OUT(cmark_node_get_literal(node), false, LITERAL);
330
- BLANKLINE();
331
- break;
332
-
333
- case CMARK_NODE_CUSTOM_BLOCK:
334
- BLANKLINE();
335
- OUT(entering ? cmark_node_get_on_enter(node) : cmark_node_get_on_exit(node),
336
- false, LITERAL);
337
- BLANKLINE();
338
- break;
339
-
340
- case CMARK_NODE_THEMATIC_BREAK:
341
- BLANKLINE();
342
- LIT("-----");
343
- BLANKLINE();
344
- break;
345
-
346
- case CMARK_NODE_PARAGRAPH:
347
- if (!entering) {
348
- BLANKLINE();
349
- }
350
- break;
351
-
352
- case CMARK_NODE_TEXT:
353
- OUT(cmark_node_get_literal(node), allow_wrap, NORMAL);
354
- break;
355
-
356
- case CMARK_NODE_LINEBREAK:
357
- if (!(CMARK_OPT_HARDBREAKS & options)) {
358
- LIT(" ");
359
- }
360
- CR();
361
- break;
362
-
363
- case CMARK_NODE_SOFTBREAK:
364
- if (CMARK_OPT_HARDBREAKS & options) {
365
- LIT(" ");
366
- CR();
367
- } else if (!renderer->no_linebreaks && renderer->width == 0 &&
368
- !(CMARK_OPT_HARDBREAKS & options) &&
369
- !(CMARK_OPT_NOBREAKS & options)) {
370
- CR();
371
- } else {
372
- OUT(" ", allow_wrap, LITERAL);
373
- }
374
- break;
375
-
376
- case CMARK_NODE_CODE:
377
- code = cmark_node_get_literal(node);
378
- code_len = strlen(code);
379
- numticks = shortest_unused_backtick_sequence(code);
380
- extra_spaces = code_len == 0 ||
381
- code[0] == '`' || code[code_len - 1] == '`' ||
382
- code[0] == ' ' || code[code_len - 1] == ' ';
383
- for (i = 0; i < numticks; i++) {
384
- LIT("`");
385
- }
386
- if (extra_spaces) {
387
- LIT(" ");
388
- }
389
- OUT(cmark_node_get_literal(node), allow_wrap, LITERAL);
390
- if (extra_spaces) {
391
- LIT(" ");
392
- }
393
- for (i = 0; i < numticks; i++) {
394
- LIT("`");
395
- }
396
- break;
397
-
398
- case CMARK_NODE_HTML_INLINE:
399
- OUT(cmark_node_get_literal(node), false, LITERAL);
400
- break;
401
-
402
- case CMARK_NODE_CUSTOM_INLINE:
403
- OUT(entering ? cmark_node_get_on_enter(node) : cmark_node_get_on_exit(node),
404
- false, LITERAL);
405
- break;
406
-
407
- case CMARK_NODE_STRONG:
408
- if (entering) {
409
- LIT("**");
410
- } else {
411
- LIT("**");
412
- }
413
- break;
414
-
415
- case CMARK_NODE_EMPH:
416
- // If we have EMPH(EMPH(x)), we need to use *_x_*
417
- // because **x** is STRONG(x):
418
- if (node->parent && node->parent->type == CMARK_NODE_EMPH &&
419
- node->next == NULL && node->prev == NULL) {
420
- emph_delim = "_";
421
- } else {
422
- emph_delim = "*";
423
- }
424
- if (entering) {
425
- LIT(emph_delim);
426
- } else {
427
- LIT(emph_delim);
428
- }
429
- break;
430
-
431
- case CMARK_NODE_LINK:
432
- if (is_autolink(node)) {
433
- if (entering) {
434
- LIT("<");
435
- if (strncmp(cmark_node_get_url(node), "mailto:", 7) == 0) {
436
- LIT((const char *)cmark_node_get_url(node) + 7);
437
- } else {
438
- LIT((const char *)cmark_node_get_url(node));
439
- }
440
- LIT(">");
441
- // return signal to skip contents of node...
442
- return 0;
443
- }
444
- } else {
445
- if (entering) {
446
- LIT("[");
447
- } else {
448
- LIT("](");
449
- OUT(cmark_node_get_url(node), false, URL);
450
- title = cmark_node_get_title(node);
451
- if (strlen(title) > 0) {
452
- LIT(" \"");
453
- OUT(title, false, TITLE);
454
- LIT("\"");
455
- }
456
- LIT(")");
457
- }
458
- }
459
- break;
460
-
461
- case CMARK_NODE_IMAGE:
462
- if (entering) {
463
- LIT("![");
464
- } else {
465
- LIT("](");
466
- OUT(cmark_node_get_url(node), false, URL);
467
- title = cmark_node_get_title(node);
468
- if (strlen(title) > 0) {
469
- OUT(" \"", allow_wrap, LITERAL);
470
- OUT(title, false, TITLE);
471
- LIT("\"");
472
- }
473
- LIT(")");
474
- }
475
- break;
476
-
477
- case CMARK_NODE_FOOTNOTE_REFERENCE:
478
- if (entering) {
479
- LIT("[^");
480
-
481
- char *footnote_label = renderer->mem->calloc(node->parent_footnote_def->as.literal.len + 1, sizeof(char));
482
- memmove(footnote_label, node->parent_footnote_def->as.literal.data, node->parent_footnote_def->as.literal.len);
483
-
484
- OUT(footnote_label, false, LITERAL);
485
- renderer->mem->free(footnote_label);
486
-
487
- LIT("]");
488
- }
489
- break;
490
-
491
- case CMARK_NODE_FOOTNOTE_DEFINITION:
492
- if (entering) {
493
- renderer->footnote_ix += 1;
494
- LIT("[^");
495
-
496
- char *footnote_label = renderer->mem->calloc(node->as.literal.len + 1, sizeof(char));
497
- memmove(footnote_label, node->as.literal.data, node->as.literal.len);
498
-
499
- OUT(footnote_label, false, LITERAL);
500
- renderer->mem->free(footnote_label);
501
-
502
- LIT("]:\n");
503
-
504
- cmark_strbuf_puts(renderer->prefix, " ");
505
- } else {
506
- cmark_strbuf_truncate(renderer->prefix, renderer->prefix->size - 4);
507
- }
508
- break;
509
-
510
- default:
511
- assert(false);
512
- break;
513
- }
514
-
515
- return 1;
516
- }
517
-
518
- char *cmark_render_commonmark(cmark_node *root, int options, int width) {
519
- return cmark_render_commonmark_with_mem(root, options, width, cmark_node_mem(root));
520
- }
521
-
522
- char *cmark_render_commonmark_with_mem(cmark_node *root, int options, int width, cmark_mem *mem) {
523
- if (options & CMARK_OPT_HARDBREAKS) {
524
- // disable breaking on width, since it has
525
- // a different meaning with OPT_HARDBREAKS
526
- width = 0;
527
- }
528
- return cmark_render(mem, root, options, width, outc, S_render_node);
529
- }