markly 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/bin/markly +94 -0
- data/ext/markly/arena.c +103 -0
- data/ext/markly/autolink.c +425 -0
- data/ext/markly/autolink.h +8 -0
- data/ext/markly/blocks.c +1585 -0
- data/ext/markly/buffer.c +278 -0
- data/ext/markly/buffer.h +116 -0
- data/ext/markly/case_fold_switch.inc +4327 -0
- data/ext/markly/chunk.h +135 -0
- data/ext/markly/cmark-gfm-core-extensions.h +54 -0
- data/ext/markly/cmark-gfm-extension_api.h +736 -0
- data/ext/markly/cmark-gfm-extensions_export.h +42 -0
- data/ext/markly/cmark-gfm.h +817 -0
- data/ext/markly/cmark-gfm_export.h +42 -0
- data/ext/markly/cmark-gfm_version.h +7 -0
- data/ext/markly/cmark.c +55 -0
- data/ext/markly/cmark_ctype.c +44 -0
- data/ext/markly/cmark_ctype.h +33 -0
- data/ext/markly/commonmark.c +519 -0
- data/ext/markly/config.h +76 -0
- data/ext/markly/core-extensions.c +27 -0
- data/ext/markly/entities.inc +2138 -0
- data/ext/markly/ext_scanners.c +1159 -0
- data/ext/markly/ext_scanners.h +24 -0
- data/ext/markly/extconf.rb +7 -0
- data/ext/markly/footnotes.c +40 -0
- data/ext/markly/footnotes.h +25 -0
- data/ext/markly/houdini.h +57 -0
- data/ext/markly/houdini_href_e.c +100 -0
- data/ext/markly/houdini_html_e.c +66 -0
- data/ext/markly/houdini_html_u.c +149 -0
- data/ext/markly/html.c +465 -0
- data/ext/markly/html.h +27 -0
- data/ext/markly/inlines.c +1633 -0
- data/ext/markly/inlines.h +29 -0
- data/ext/markly/iterator.c +159 -0
- data/ext/markly/iterator.h +26 -0
- data/ext/markly/latex.c +466 -0
- data/ext/markly/linked_list.c +37 -0
- data/ext/markly/man.c +278 -0
- data/ext/markly/map.c +122 -0
- data/ext/markly/map.h +41 -0
- data/ext/markly/markly.c +1226 -0
- data/ext/markly/markly.h +16 -0
- data/ext/markly/node.c +979 -0
- data/ext/markly/node.h +118 -0
- data/ext/markly/parser.h +58 -0
- data/ext/markly/plaintext.c +235 -0
- data/ext/markly/plugin.c +36 -0
- data/ext/markly/plugin.h +34 -0
- data/ext/markly/references.c +42 -0
- data/ext/markly/references.h +26 -0
- data/ext/markly/registry.c +63 -0
- data/ext/markly/registry.h +24 -0
- data/ext/markly/render.c +205 -0
- data/ext/markly/render.h +62 -0
- data/ext/markly/scanners.c +20382 -0
- data/ext/markly/scanners.h +62 -0
- data/ext/markly/scanners.re +326 -0
- data/ext/markly/strikethrough.c +167 -0
- data/ext/markly/strikethrough.h +9 -0
- data/ext/markly/syntax_extension.c +149 -0
- data/ext/markly/syntax_extension.h +34 -0
- data/ext/markly/table.c +803 -0
- data/ext/markly/table.h +12 -0
- data/ext/markly/tagfilter.c +60 -0
- data/ext/markly/tagfilter.h +8 -0
- data/ext/markly/tasklist.c +156 -0
- data/ext/markly/tasklist.h +8 -0
- data/ext/markly/utf8.c +317 -0
- data/ext/markly/utf8.h +35 -0
- data/ext/markly/xml.c +181 -0
- data/lib/markly.rb +43 -0
- data/lib/markly/flags.rb +37 -0
- data/lib/markly/markly.so +0 -0
- data/lib/markly/node.rb +70 -0
- data/lib/markly/node/inspect.rb +59 -0
- data/lib/markly/renderer.rb +133 -0
- data/lib/markly/renderer/html_renderer.rb +252 -0
- data/lib/markly/version.rb +5 -0
- metadata +211 -0
data/ext/markly/blocks.c
ADDED
@@ -0,0 +1,1585 @@
|
|
1
|
+
/**
|
2
|
+
* Block parsing implementation.
|
3
|
+
*
|
4
|
+
* For a high-level overview of the block parsing process,
|
5
|
+
* see http://spec.commonmark.org/0.24/#phase-1-block-structure
|
6
|
+
*/
|
7
|
+
|
8
|
+
#include <stdlib.h>
|
9
|
+
#include <assert.h>
|
10
|
+
#include <stdio.h>
|
11
|
+
|
12
|
+
#include "cmark_ctype.h"
|
13
|
+
#include "syntax_extension.h"
|
14
|
+
#include "config.h"
|
15
|
+
#include "parser.h"
|
16
|
+
#include "cmark-gfm.h"
|
17
|
+
#include "node.h"
|
18
|
+
#include "references.h"
|
19
|
+
#include "utf8.h"
|
20
|
+
#include "scanners.h"
|
21
|
+
#include "inlines.h"
|
22
|
+
#include "houdini.h"
|
23
|
+
#include "buffer.h"
|
24
|
+
#include "footnotes.h"
|
25
|
+
|
26
|
+
#define CODE_INDENT 4
|
27
|
+
#define TAB_STOP 4
|
28
|
+
|
29
|
+
#ifndef MIN
|
30
|
+
#define MIN(x, y) ((x < y) ? x : y)
|
31
|
+
#endif
|
32
|
+
|
33
|
+
#define peek_at(i, n) (i)->data[n]
|
34
|
+
|
35
|
+
static bool S_last_line_blank(const cmark_node *node) {
|
36
|
+
return (node->flags & CMARK_NODE__LAST_LINE_BLANK) != 0;
|
37
|
+
}
|
38
|
+
|
39
|
+
static bool S_last_line_checked(const cmark_node *node) {
|
40
|
+
return (node->flags & CMARK_NODE__LAST_LINE_CHECKED) != 0;
|
41
|
+
}
|
42
|
+
|
43
|
+
static CMARK_INLINE cmark_node_type S_type(const cmark_node *node) {
|
44
|
+
return (cmark_node_type)node->type;
|
45
|
+
}
|
46
|
+
|
47
|
+
static void S_set_last_line_blank(cmark_node *node, bool is_blank) {
|
48
|
+
if (is_blank)
|
49
|
+
node->flags |= CMARK_NODE__LAST_LINE_BLANK;
|
50
|
+
else
|
51
|
+
node->flags &= ~CMARK_NODE__LAST_LINE_BLANK;
|
52
|
+
}
|
53
|
+
|
54
|
+
static void S_set_last_line_checked(cmark_node *node) {
|
55
|
+
node->flags |= CMARK_NODE__LAST_LINE_CHECKED;
|
56
|
+
}
|
57
|
+
|
58
|
+
static CMARK_INLINE bool S_is_line_end_char(char c) {
|
59
|
+
return (c == '\n' || c == '\r');
|
60
|
+
}
|
61
|
+
|
62
|
+
static CMARK_INLINE bool S_is_space_or_tab(char c) {
|
63
|
+
return (c == ' ' || c == '\t');
|
64
|
+
}
|
65
|
+
|
66
|
+
static void S_parser_feed(cmark_parser *parser, const unsigned char *buffer,
|
67
|
+
size_t len, bool eof);
|
68
|
+
|
69
|
+
static void S_process_line(cmark_parser *parser, const unsigned char *buffer,
|
70
|
+
bufsize_t bytes);
|
71
|
+
|
72
|
+
static cmark_node *make_block(cmark_mem *mem, cmark_node_type tag,
|
73
|
+
int start_line, int start_column) {
|
74
|
+
cmark_node *e;
|
75
|
+
|
76
|
+
e = (cmark_node *)mem->calloc(1, sizeof(*e));
|
77
|
+
cmark_strbuf_init(mem, &e->content, 32);
|
78
|
+
e->type = (uint16_t)tag;
|
79
|
+
e->flags = CMARK_NODE__OPEN;
|
80
|
+
e->start_line = start_line;
|
81
|
+
e->start_column = start_column;
|
82
|
+
e->end_line = start_line;
|
83
|
+
|
84
|
+
return e;
|
85
|
+
}
|
86
|
+
|
87
|
+
// Create a root document node.
|
88
|
+
static cmark_node *make_document(cmark_mem *mem) {
|
89
|
+
cmark_node *e = make_block(mem, CMARK_NODE_DOCUMENT, 1, 1);
|
90
|
+
return e;
|
91
|
+
}
|
92
|
+
|
93
|
+
int cmark_parser_attach_syntax_extension(cmark_parser *parser,
|
94
|
+
cmark_syntax_extension *extension) {
|
95
|
+
parser->syntax_extensions = cmark_llist_append(parser->mem, parser->syntax_extensions, extension);
|
96
|
+
if (extension->match_inline || extension->insert_inline_from_delim) {
|
97
|
+
parser->inline_syntax_extensions = cmark_llist_append(
|
98
|
+
parser->mem, parser->inline_syntax_extensions, extension);
|
99
|
+
}
|
100
|
+
|
101
|
+
return 1;
|
102
|
+
}
|
103
|
+
|
104
|
+
static void cmark_parser_dispose(cmark_parser *parser) {
|
105
|
+
if (parser->root)
|
106
|
+
cmark_node_free(parser->root);
|
107
|
+
|
108
|
+
if (parser->refmap)
|
109
|
+
cmark_map_free(parser->refmap);
|
110
|
+
}
|
111
|
+
|
112
|
+
static void cmark_parser_reset(cmark_parser *parser) {
|
113
|
+
cmark_llist *saved_exts = parser->syntax_extensions;
|
114
|
+
cmark_llist *saved_inline_exts = parser->inline_syntax_extensions;
|
115
|
+
int saved_options = parser->options;
|
116
|
+
cmark_mem *saved_mem = parser->mem;
|
117
|
+
|
118
|
+
cmark_parser_dispose(parser);
|
119
|
+
|
120
|
+
memset(parser, 0, sizeof(cmark_parser));
|
121
|
+
parser->mem = saved_mem;
|
122
|
+
|
123
|
+
cmark_strbuf_init(parser->mem, &parser->curline, 256);
|
124
|
+
cmark_strbuf_init(parser->mem, &parser->linebuf, 0);
|
125
|
+
|
126
|
+
cmark_node *document = make_document(parser->mem);
|
127
|
+
|
128
|
+
parser->refmap = cmark_reference_map_new(parser->mem);
|
129
|
+
parser->root = document;
|
130
|
+
parser->current = document;
|
131
|
+
|
132
|
+
parser->syntax_extensions = saved_exts;
|
133
|
+
parser->inline_syntax_extensions = saved_inline_exts;
|
134
|
+
parser->options = saved_options;
|
135
|
+
}
|
136
|
+
|
137
|
+
cmark_parser *cmark_parser_new_with_mem(int options, cmark_mem *mem) {
|
138
|
+
cmark_parser *parser = (cmark_parser *)mem->calloc(1, sizeof(cmark_parser));
|
139
|
+
parser->mem = mem;
|
140
|
+
parser->options = options;
|
141
|
+
cmark_parser_reset(parser);
|
142
|
+
return parser;
|
143
|
+
}
|
144
|
+
|
145
|
+
cmark_parser *cmark_parser_new(int options) {
|
146
|
+
extern cmark_mem CMARK_DEFAULT_MEM_ALLOCATOR;
|
147
|
+
return cmark_parser_new_with_mem(options, &CMARK_DEFAULT_MEM_ALLOCATOR);
|
148
|
+
}
|
149
|
+
|
150
|
+
void cmark_parser_free(cmark_parser *parser) {
|
151
|
+
cmark_mem *mem = parser->mem;
|
152
|
+
cmark_parser_dispose(parser);
|
153
|
+
cmark_strbuf_free(&parser->curline);
|
154
|
+
cmark_strbuf_free(&parser->linebuf);
|
155
|
+
cmark_llist_free(parser->mem, parser->syntax_extensions);
|
156
|
+
cmark_llist_free(parser->mem, parser->inline_syntax_extensions);
|
157
|
+
mem->free(parser);
|
158
|
+
}
|
159
|
+
|
160
|
+
static cmark_node *finalize(cmark_parser *parser, cmark_node *b);
|
161
|
+
|
162
|
+
// Returns true if line has only space characters, else false.
|
163
|
+
static bool is_blank(cmark_strbuf *s, bufsize_t offset) {
|
164
|
+
while (offset < s->size) {
|
165
|
+
switch (s->ptr[offset]) {
|
166
|
+
case '\r':
|
167
|
+
case '\n':
|
168
|
+
return true;
|
169
|
+
case ' ':
|
170
|
+
offset++;
|
171
|
+
break;
|
172
|
+
case '\t':
|
173
|
+
offset++;
|
174
|
+
break;
|
175
|
+
default:
|
176
|
+
return false;
|
177
|
+
}
|
178
|
+
}
|
179
|
+
|
180
|
+
return true;
|
181
|
+
}
|
182
|
+
|
183
|
+
static CMARK_INLINE bool accepts_lines(cmark_node_type block_type) {
|
184
|
+
return (block_type == CMARK_NODE_PARAGRAPH ||
|
185
|
+
block_type == CMARK_NODE_HEADING ||
|
186
|
+
block_type == CMARK_NODE_CODE_BLOCK);
|
187
|
+
}
|
188
|
+
|
189
|
+
static CMARK_INLINE bool contains_inlines(cmark_node *node) {
|
190
|
+
if (node->extension && node->extension->contains_inlines_func) {
|
191
|
+
return node->extension->contains_inlines_func(node->extension, node) != 0;
|
192
|
+
}
|
193
|
+
|
194
|
+
return (node->type == CMARK_NODE_PARAGRAPH ||
|
195
|
+
node->type == CMARK_NODE_HEADING);
|
196
|
+
}
|
197
|
+
|
198
|
+
static void add_line(cmark_node *node, cmark_chunk *ch, cmark_parser *parser) {
|
199
|
+
int chars_to_tab;
|
200
|
+
int i;
|
201
|
+
assert(node->flags & CMARK_NODE__OPEN);
|
202
|
+
if (parser->partially_consumed_tab) {
|
203
|
+
parser->offset += 1; // skip over tab
|
204
|
+
// add space characters:
|
205
|
+
chars_to_tab = TAB_STOP - (parser->column % TAB_STOP);
|
206
|
+
for (i = 0; i < chars_to_tab; i++) {
|
207
|
+
cmark_strbuf_putc(&node->content, ' ');
|
208
|
+
}
|
209
|
+
}
|
210
|
+
cmark_strbuf_put(&node->content, ch->data + parser->offset,
|
211
|
+
ch->len - parser->offset);
|
212
|
+
}
|
213
|
+
|
214
|
+
static void remove_trailing_blank_lines(cmark_strbuf *ln) {
|
215
|
+
bufsize_t i;
|
216
|
+
unsigned char c;
|
217
|
+
|
218
|
+
for (i = ln->size - 1; i >= 0; --i) {
|
219
|
+
c = ln->ptr[i];
|
220
|
+
|
221
|
+
if (c != ' ' && c != '\t' && !S_is_line_end_char(c))
|
222
|
+
break;
|
223
|
+
}
|
224
|
+
|
225
|
+
if (i < 0) {
|
226
|
+
cmark_strbuf_clear(ln);
|
227
|
+
return;
|
228
|
+
}
|
229
|
+
|
230
|
+
for (; i < ln->size; ++i) {
|
231
|
+
c = ln->ptr[i];
|
232
|
+
|
233
|
+
if (!S_is_line_end_char(c))
|
234
|
+
continue;
|
235
|
+
|
236
|
+
cmark_strbuf_truncate(ln, i);
|
237
|
+
break;
|
238
|
+
}
|
239
|
+
}
|
240
|
+
|
241
|
+
// Check to see if a node ends with a blank line, descending
|
242
|
+
// if needed into lists and sublists.
|
243
|
+
static bool S_ends_with_blank_line(cmark_node *node) {
|
244
|
+
if (S_last_line_checked(node)) {
|
245
|
+
return(S_last_line_blank(node));
|
246
|
+
} else if ((S_type(node) == CMARK_NODE_LIST ||
|
247
|
+
S_type(node) == CMARK_NODE_ITEM) && node->last_child) {
|
248
|
+
S_set_last_line_checked(node);
|
249
|
+
return(S_ends_with_blank_line(node->last_child));
|
250
|
+
} else {
|
251
|
+
S_set_last_line_checked(node);
|
252
|
+
return (S_last_line_blank(node));
|
253
|
+
}
|
254
|
+
}
|
255
|
+
|
256
|
+
// returns true if content remains after link defs are resolved.
|
257
|
+
static bool resolve_reference_link_definitions(
|
258
|
+
cmark_parser *parser,
|
259
|
+
cmark_node *b) {
|
260
|
+
bufsize_t pos;
|
261
|
+
cmark_strbuf *node_content = &b->content;
|
262
|
+
cmark_chunk chunk = {node_content->ptr, node_content->size, 0};
|
263
|
+
while (chunk.len && chunk.data[0] == '[' &&
|
264
|
+
(pos = cmark_parse_reference_inline(parser->mem, &chunk,
|
265
|
+
parser->refmap))) {
|
266
|
+
|
267
|
+
chunk.data += pos;
|
268
|
+
chunk.len -= pos;
|
269
|
+
}
|
270
|
+
cmark_strbuf_drop(node_content, (node_content->size - chunk.len));
|
271
|
+
return !is_blank(&b->content, 0);
|
272
|
+
}
|
273
|
+
|
274
|
+
static cmark_node *finalize(cmark_parser *parser, cmark_node *b) {
|
275
|
+
bufsize_t pos;
|
276
|
+
cmark_node *item;
|
277
|
+
cmark_node *subitem;
|
278
|
+
cmark_node *parent;
|
279
|
+
bool has_content;
|
280
|
+
|
281
|
+
parent = b->parent;
|
282
|
+
assert(b->flags &
|
283
|
+
CMARK_NODE__OPEN); // shouldn't call finalize on closed blocks
|
284
|
+
b->flags &= ~CMARK_NODE__OPEN;
|
285
|
+
|
286
|
+
if (parser->curline.size == 0) {
|
287
|
+
// end of input - line number has not been incremented
|
288
|
+
b->end_line = parser->line_number;
|
289
|
+
b->end_column = parser->last_line_length;
|
290
|
+
} else if (S_type(b) == CMARK_NODE_DOCUMENT ||
|
291
|
+
(S_type(b) == CMARK_NODE_CODE_BLOCK && b->as.code.fenced) ||
|
292
|
+
(S_type(b) == CMARK_NODE_HEADING && b->as.heading.setext)) {
|
293
|
+
b->end_line = parser->line_number;
|
294
|
+
b->end_column = parser->curline.size;
|
295
|
+
if (b->end_column && parser->curline.ptr[b->end_column - 1] == '\n')
|
296
|
+
b->end_column -= 1;
|
297
|
+
if (b->end_column && parser->curline.ptr[b->end_column - 1] == '\r')
|
298
|
+
b->end_column -= 1;
|
299
|
+
} else {
|
300
|
+
b->end_line = parser->line_number - 1;
|
301
|
+
b->end_column = parser->last_line_length;
|
302
|
+
}
|
303
|
+
|
304
|
+
cmark_strbuf *node_content = &b->content;
|
305
|
+
|
306
|
+
switch (S_type(b)) {
|
307
|
+
case CMARK_NODE_PARAGRAPH:
|
308
|
+
{
|
309
|
+
has_content = resolve_reference_link_definitions(parser, b);
|
310
|
+
if (!has_content) {
|
311
|
+
// remove blank node (former reference def)
|
312
|
+
cmark_node_free(b);
|
313
|
+
}
|
314
|
+
break;
|
315
|
+
}
|
316
|
+
|
317
|
+
case CMARK_NODE_CODE_BLOCK:
|
318
|
+
if (!b->as.code.fenced) { // indented code
|
319
|
+
remove_trailing_blank_lines(node_content);
|
320
|
+
cmark_strbuf_putc(node_content, '\n');
|
321
|
+
} else {
|
322
|
+
// first line of contents becomes info
|
323
|
+
for (pos = 0; pos < node_content->size; ++pos) {
|
324
|
+
if (S_is_line_end_char(node_content->ptr[pos]))
|
325
|
+
break;
|
326
|
+
}
|
327
|
+
assert(pos < node_content->size);
|
328
|
+
|
329
|
+
cmark_strbuf tmp = CMARK_BUF_INIT(parser->mem);
|
330
|
+
houdini_unescape_html_f(&tmp, node_content->ptr, pos);
|
331
|
+
cmark_strbuf_trim(&tmp);
|
332
|
+
cmark_strbuf_unescape(&tmp);
|
333
|
+
b->as.code.info = cmark_chunk_buf_detach(&tmp);
|
334
|
+
|
335
|
+
if (node_content->ptr[pos] == '\r')
|
336
|
+
pos += 1;
|
337
|
+
if (node_content->ptr[pos] == '\n')
|
338
|
+
pos += 1;
|
339
|
+
cmark_strbuf_drop(node_content, pos);
|
340
|
+
}
|
341
|
+
b->as.code.literal = cmark_chunk_buf_detach(node_content);
|
342
|
+
break;
|
343
|
+
|
344
|
+
case CMARK_NODE_HTML_BLOCK:
|
345
|
+
b->as.literal = cmark_chunk_buf_detach(node_content);
|
346
|
+
break;
|
347
|
+
|
348
|
+
case CMARK_NODE_LIST: // determine tight/loose status
|
349
|
+
b->as.list.tight = true; // tight by default
|
350
|
+
item = b->first_child;
|
351
|
+
|
352
|
+
while (item) {
|
353
|
+
// check for non-final non-empty list item ending with blank line:
|
354
|
+
if (S_last_line_blank(item) && item->next) {
|
355
|
+
b->as.list.tight = false;
|
356
|
+
break;
|
357
|
+
}
|
358
|
+
// recurse into children of list item, to see if there are
|
359
|
+
// spaces between them:
|
360
|
+
subitem = item->first_child;
|
361
|
+
while (subitem) {
|
362
|
+
if ((item->next || subitem->next) &&
|
363
|
+
S_ends_with_blank_line(subitem)) {
|
364
|
+
b->as.list.tight = false;
|
365
|
+
break;
|
366
|
+
}
|
367
|
+
subitem = subitem->next;
|
368
|
+
}
|
369
|
+
if (!(b->as.list.tight)) {
|
370
|
+
break;
|
371
|
+
}
|
372
|
+
item = item->next;
|
373
|
+
}
|
374
|
+
|
375
|
+
break;
|
376
|
+
|
377
|
+
default:
|
378
|
+
break;
|
379
|
+
}
|
380
|
+
|
381
|
+
return parent;
|
382
|
+
}
|
383
|
+
|
384
|
+
// Add a node as child of another. Return pointer to child.
|
385
|
+
static cmark_node *add_child(cmark_parser *parser, cmark_node *parent,
|
386
|
+
cmark_node_type block_type, int start_column) {
|
387
|
+
assert(parent);
|
388
|
+
|
389
|
+
// if 'parent' isn't the kind of node that can accept this child,
|
390
|
+
// then back up til we hit a node that can.
|
391
|
+
while (!cmark_node_can_contain_type(parent, block_type)) {
|
392
|
+
parent = finalize(parser, parent);
|
393
|
+
}
|
394
|
+
|
395
|
+
cmark_node *child =
|
396
|
+
make_block(parser->mem, block_type, parser->line_number, start_column);
|
397
|
+
child->parent = parent;
|
398
|
+
|
399
|
+
if (parent->last_child) {
|
400
|
+
parent->last_child->next = child;
|
401
|
+
child->prev = parent->last_child;
|
402
|
+
} else {
|
403
|
+
parent->first_child = child;
|
404
|
+
child->prev = NULL;
|
405
|
+
}
|
406
|
+
parent->last_child = child;
|
407
|
+
return child;
|
408
|
+
}
|
409
|
+
|
410
|
+
void cmark_manage_extensions_special_characters(cmark_parser *parser, int add) {
|
411
|
+
cmark_llist *tmp_ext;
|
412
|
+
|
413
|
+
for (tmp_ext = parser->inline_syntax_extensions; tmp_ext; tmp_ext=tmp_ext->next) {
|
414
|
+
cmark_syntax_extension *ext = (cmark_syntax_extension *) tmp_ext->data;
|
415
|
+
cmark_llist *tmp_char;
|
416
|
+
for (tmp_char = ext->special_inline_chars; tmp_char; tmp_char=tmp_char->next) {
|
417
|
+
unsigned char c = (unsigned char)(size_t)tmp_char->data;
|
418
|
+
if (add)
|
419
|
+
cmark_inlines_add_special_character(c, ext->emphasis);
|
420
|
+
else
|
421
|
+
cmark_inlines_remove_special_character(c, ext->emphasis);
|
422
|
+
}
|
423
|
+
}
|
424
|
+
}
|
425
|
+
|
426
|
+
// Walk through node and all children, recursively, parsing
|
427
|
+
// string content into inline content where appropriate.
|
428
|
+
static void process_inlines(cmark_parser *parser,
|
429
|
+
cmark_map *refmap, int options) {
|
430
|
+
cmark_iter *iter = cmark_iter_new(parser->root);
|
431
|
+
cmark_node *cur;
|
432
|
+
cmark_event_type ev_type;
|
433
|
+
|
434
|
+
cmark_manage_extensions_special_characters(parser, true);
|
435
|
+
|
436
|
+
while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
|
437
|
+
cur = cmark_iter_get_node(iter);
|
438
|
+
if (ev_type == CMARK_EVENT_ENTER) {
|
439
|
+
if (contains_inlines(cur)) {
|
440
|
+
cmark_parse_inlines(parser, cur, refmap, options);
|
441
|
+
}
|
442
|
+
}
|
443
|
+
}
|
444
|
+
|
445
|
+
cmark_manage_extensions_special_characters(parser, false);
|
446
|
+
|
447
|
+
cmark_iter_free(iter);
|
448
|
+
}
|
449
|
+
|
450
|
+
static int sort_footnote_by_ix(const void *_a, const void *_b) {
|
451
|
+
cmark_footnote *a = *(cmark_footnote **)_a;
|
452
|
+
cmark_footnote *b = *(cmark_footnote **)_b;
|
453
|
+
return (int)a->ix - (int)b->ix;
|
454
|
+
}
|
455
|
+
|
456
|
+
static void process_footnotes(cmark_parser *parser) {
|
457
|
+
// * Collect definitions in a map.
|
458
|
+
// * Iterate the references in the document in order, assigning indices to
|
459
|
+
// definitions in the order they're seen.
|
460
|
+
// * Write out the footnotes at the bottom of the document in index order.
|
461
|
+
|
462
|
+
cmark_map *map = cmark_footnote_map_new(parser->mem);
|
463
|
+
|
464
|
+
cmark_iter *iter = cmark_iter_new(parser->root);
|
465
|
+
cmark_node *cur;
|
466
|
+
cmark_event_type ev_type;
|
467
|
+
|
468
|
+
while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
|
469
|
+
cur = cmark_iter_get_node(iter);
|
470
|
+
if (ev_type == CMARK_EVENT_EXIT && cur->type == CMARK_NODE_FOOTNOTE_DEFINITION) {
|
471
|
+
cmark_node_unlink(cur);
|
472
|
+
cmark_footnote_create(map, cur);
|
473
|
+
}
|
474
|
+
}
|
475
|
+
|
476
|
+
cmark_iter_free(iter);
|
477
|
+
iter = cmark_iter_new(parser->root);
|
478
|
+
unsigned int ix = 0;
|
479
|
+
|
480
|
+
while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
|
481
|
+
cur = cmark_iter_get_node(iter);
|
482
|
+
if (ev_type == CMARK_EVENT_EXIT && cur->type == CMARK_NODE_FOOTNOTE_REFERENCE) {
|
483
|
+
cmark_footnote *footnote = (cmark_footnote *)cmark_map_lookup(map, &cur->as.literal);
|
484
|
+
if (footnote) {
|
485
|
+
if (!footnote->ix)
|
486
|
+
footnote->ix = ++ix;
|
487
|
+
|
488
|
+
char n[32];
|
489
|
+
snprintf(n, sizeof(n), "%d", footnote->ix);
|
490
|
+
cmark_chunk_free(parser->mem, &cur->as.literal);
|
491
|
+
cmark_strbuf buf = CMARK_BUF_INIT(parser->mem);
|
492
|
+
cmark_strbuf_puts(&buf, n);
|
493
|
+
|
494
|
+
cur->as.literal = cmark_chunk_buf_detach(&buf);
|
495
|
+
} else {
|
496
|
+
cmark_node *text = (cmark_node *)parser->mem->calloc(1, sizeof(*text));
|
497
|
+
cmark_strbuf_init(parser->mem, &text->content, 0);
|
498
|
+
text->type = (uint16_t) CMARK_NODE_TEXT;
|
499
|
+
|
500
|
+
cmark_strbuf buf = CMARK_BUF_INIT(parser->mem);
|
501
|
+
cmark_strbuf_puts(&buf, "[^");
|
502
|
+
cmark_strbuf_put(&buf, cur->as.literal.data, cur->as.literal.len);
|
503
|
+
cmark_strbuf_putc(&buf, ']');
|
504
|
+
|
505
|
+
text->as.literal = cmark_chunk_buf_detach(&buf);
|
506
|
+
cmark_node_insert_after(cur, text);
|
507
|
+
cmark_node_free(cur);
|
508
|
+
}
|
509
|
+
}
|
510
|
+
}
|
511
|
+
|
512
|
+
cmark_iter_free(iter);
|
513
|
+
|
514
|
+
if (map->sorted) {
|
515
|
+
qsort(map->sorted, map->size, sizeof(cmark_map_entry *), sort_footnote_by_ix);
|
516
|
+
for (unsigned int i = 0; i < map->size; ++i) {
|
517
|
+
cmark_footnote *footnote = (cmark_footnote *)map->sorted[i];
|
518
|
+
if (!footnote->ix)
|
519
|
+
continue;
|
520
|
+
cmark_node_append_child(parser->root, footnote->node);
|
521
|
+
footnote->node = NULL;
|
522
|
+
}
|
523
|
+
}
|
524
|
+
|
525
|
+
cmark_map_free(map);
|
526
|
+
}
|
527
|
+
|
528
|
+
// Attempts to parse a list item marker (bullet or enumerated).
|
529
|
+
// On success, returns length of the marker, and populates
|
530
|
+
// data with the details. On failure, returns 0.
|
531
|
+
static bufsize_t parse_list_marker(cmark_mem *mem, cmark_chunk *input,
|
532
|
+
bufsize_t pos, bool interrupts_paragraph,
|
533
|
+
cmark_list **dataptr) {
|
534
|
+
unsigned char c;
|
535
|
+
bufsize_t startpos;
|
536
|
+
cmark_list *data;
|
537
|
+
bufsize_t i;
|
538
|
+
|
539
|
+
startpos = pos;
|
540
|
+
c = peek_at(input, pos);
|
541
|
+
|
542
|
+
if (c == '*' || c == '-' || c == '+') {
|
543
|
+
pos++;
|
544
|
+
if (!cmark_isspace(peek_at(input, pos))) {
|
545
|
+
return 0;
|
546
|
+
}
|
547
|
+
|
548
|
+
if (interrupts_paragraph) {
|
549
|
+
i = pos;
|
550
|
+
// require non-blank content after list marker:
|
551
|
+
while (S_is_space_or_tab(peek_at(input, i))) {
|
552
|
+
i++;
|
553
|
+
}
|
554
|
+
if (peek_at(input, i) == '\n') {
|
555
|
+
return 0;
|
556
|
+
}
|
557
|
+
}
|
558
|
+
|
559
|
+
data = (cmark_list *)mem->calloc(1, sizeof(*data));
|
560
|
+
data->marker_offset = 0; // will be adjusted later
|
561
|
+
data->list_type = CMARK_BULLET_LIST;
|
562
|
+
data->bullet_char = c;
|
563
|
+
data->start = 0;
|
564
|
+
data->delimiter = CMARK_NO_DELIM;
|
565
|
+
data->tight = false;
|
566
|
+
} else if (cmark_isdigit(c)) {
|
567
|
+
int start = 0;
|
568
|
+
int digits = 0;
|
569
|
+
|
570
|
+
do {
|
571
|
+
start = (10 * start) + (peek_at(input, pos) - '0');
|
572
|
+
pos++;
|
573
|
+
digits++;
|
574
|
+
// We limit to 9 digits to avoid overflow,
|
575
|
+
// assuming max int is 2^31 - 1
|
576
|
+
// This also seems to be the limit for 'start' in some browsers.
|
577
|
+
} while (digits < 9 && cmark_isdigit(peek_at(input, pos)));
|
578
|
+
|
579
|
+
if (interrupts_paragraph && start != 1) {
|
580
|
+
return 0;
|
581
|
+
}
|
582
|
+
c = peek_at(input, pos);
|
583
|
+
if (c == '.' || c == ')') {
|
584
|
+
pos++;
|
585
|
+
if (!cmark_isspace(peek_at(input, pos))) {
|
586
|
+
return 0;
|
587
|
+
}
|
588
|
+
if (interrupts_paragraph) {
|
589
|
+
// require non-blank content after list marker:
|
590
|
+
i = pos;
|
591
|
+
while (S_is_space_or_tab(peek_at(input, i))) {
|
592
|
+
i++;
|
593
|
+
}
|
594
|
+
if (S_is_line_end_char(peek_at(input, i))) {
|
595
|
+
return 0;
|
596
|
+
}
|
597
|
+
}
|
598
|
+
|
599
|
+
data = (cmark_list *)mem->calloc(1, sizeof(*data));
|
600
|
+
data->marker_offset = 0; // will be adjusted later
|
601
|
+
data->list_type = CMARK_ORDERED_LIST;
|
602
|
+
data->bullet_char = 0;
|
603
|
+
data->start = start;
|
604
|
+
data->delimiter = (c == '.' ? CMARK_PERIOD_DELIM : CMARK_PAREN_DELIM);
|
605
|
+
data->tight = false;
|
606
|
+
} else {
|
607
|
+
return 0;
|
608
|
+
}
|
609
|
+
} else {
|
610
|
+
return 0;
|
611
|
+
}
|
612
|
+
|
613
|
+
*dataptr = data;
|
614
|
+
return (pos - startpos);
|
615
|
+
}
|
616
|
+
|
617
|
+
// Return 1 if list item belongs in list, else 0.
|
618
|
+
static int lists_match(cmark_list *list_data, cmark_list *item_data) {
|
619
|
+
return (list_data->list_type == item_data->list_type &&
|
620
|
+
list_data->delimiter == item_data->delimiter &&
|
621
|
+
// list_data->marker_offset == item_data.marker_offset &&
|
622
|
+
list_data->bullet_char == item_data->bullet_char);
|
623
|
+
}
|
624
|
+
|
625
|
+
static cmark_node *finalize_document(cmark_parser *parser) {
|
626
|
+
while (parser->current != parser->root) {
|
627
|
+
parser->current = finalize(parser, parser->current);
|
628
|
+
}
|
629
|
+
|
630
|
+
finalize(parser, parser->root);
|
631
|
+
process_inlines(parser, parser->refmap, parser->options);
|
632
|
+
if (parser->options & CMARK_OPT_FOOTNOTES)
|
633
|
+
process_footnotes(parser);
|
634
|
+
|
635
|
+
return parser->root;
|
636
|
+
}
|
637
|
+
|
638
|
+
cmark_node *cmark_parse_file(FILE *f, int options) {
|
639
|
+
unsigned char buffer[4096];
|
640
|
+
cmark_parser *parser = cmark_parser_new(options);
|
641
|
+
size_t bytes;
|
642
|
+
cmark_node *document;
|
643
|
+
|
644
|
+
while ((bytes = fread(buffer, 1, sizeof(buffer), f)) > 0) {
|
645
|
+
bool eof = bytes < sizeof(buffer);
|
646
|
+
S_parser_feed(parser, buffer, bytes, eof);
|
647
|
+
if (eof) {
|
648
|
+
break;
|
649
|
+
}
|
650
|
+
}
|
651
|
+
|
652
|
+
document = cmark_parser_finish(parser);
|
653
|
+
cmark_parser_free(parser);
|
654
|
+
return document;
|
655
|
+
}
|
656
|
+
|
657
|
+
cmark_node *cmark_parse_document(const char *buffer, size_t len, int options) {
|
658
|
+
cmark_parser *parser = cmark_parser_new(options);
|
659
|
+
cmark_node *document;
|
660
|
+
|
661
|
+
S_parser_feed(parser, (const unsigned char *)buffer, len, true);
|
662
|
+
|
663
|
+
document = cmark_parser_finish(parser);
|
664
|
+
cmark_parser_free(parser);
|
665
|
+
return document;
|
666
|
+
}
|
667
|
+
|
668
|
+
void cmark_parser_feed(cmark_parser *parser, const char *buffer, size_t len) {
|
669
|
+
S_parser_feed(parser, (const unsigned char *)buffer, len, false);
|
670
|
+
}
|
671
|
+
|
672
|
+
void cmark_parser_feed_reentrant(cmark_parser *parser, const char *buffer, size_t len) {
|
673
|
+
cmark_strbuf saved_linebuf;
|
674
|
+
|
675
|
+
cmark_strbuf_init(parser->mem, &saved_linebuf, 0);
|
676
|
+
cmark_strbuf_puts(&saved_linebuf, cmark_strbuf_cstr(&parser->linebuf));
|
677
|
+
cmark_strbuf_clear(&parser->linebuf);
|
678
|
+
|
679
|
+
S_parser_feed(parser, (const unsigned char *)buffer, len, true);
|
680
|
+
|
681
|
+
cmark_strbuf_sets(&parser->linebuf, cmark_strbuf_cstr(&saved_linebuf));
|
682
|
+
cmark_strbuf_free(&saved_linebuf);
|
683
|
+
}
|
684
|
+
|
685
|
+
static void S_parser_feed(cmark_parser *parser, const unsigned char *buffer,
|
686
|
+
size_t len, bool eof) {
|
687
|
+
const unsigned char *end = buffer + len;
|
688
|
+
static const uint8_t repl[] = {239, 191, 189};
|
689
|
+
|
690
|
+
if (parser->last_buffer_ended_with_cr && *buffer == '\n') {
|
691
|
+
// skip NL if last buffer ended with CR ; see #117
|
692
|
+
buffer++;
|
693
|
+
}
|
694
|
+
parser->last_buffer_ended_with_cr = false;
|
695
|
+
while (buffer < end) {
|
696
|
+
const unsigned char *eol;
|
697
|
+
bufsize_t chunk_len;
|
698
|
+
bool process = false;
|
699
|
+
for (eol = buffer; eol < end; ++eol) {
|
700
|
+
if (S_is_line_end_char(*eol)) {
|
701
|
+
process = true;
|
702
|
+
break;
|
703
|
+
}
|
704
|
+
if (*eol == '\0' && eol < end) {
|
705
|
+
break;
|
706
|
+
}
|
707
|
+
}
|
708
|
+
if (eol >= end && eof) {
|
709
|
+
process = true;
|
710
|
+
}
|
711
|
+
|
712
|
+
chunk_len = (bufsize_t)(eol - buffer);
|
713
|
+
if (process) {
|
714
|
+
if (parser->linebuf.size > 0) {
|
715
|
+
cmark_strbuf_put(&parser->linebuf, buffer, chunk_len);
|
716
|
+
S_process_line(parser, parser->linebuf.ptr, parser->linebuf.size);
|
717
|
+
cmark_strbuf_clear(&parser->linebuf);
|
718
|
+
} else {
|
719
|
+
S_process_line(parser, buffer, chunk_len);
|
720
|
+
}
|
721
|
+
} else {
|
722
|
+
if (eol < end && *eol == '\0') {
|
723
|
+
// omit NULL byte
|
724
|
+
cmark_strbuf_put(&parser->linebuf, buffer, chunk_len);
|
725
|
+
// add replacement character
|
726
|
+
cmark_strbuf_put(&parser->linebuf, repl, 3);
|
727
|
+
} else {
|
728
|
+
cmark_strbuf_put(&parser->linebuf, buffer, chunk_len);
|
729
|
+
}
|
730
|
+
}
|
731
|
+
|
732
|
+
buffer += chunk_len;
|
733
|
+
if (buffer < end) {
|
734
|
+
if (*buffer == '\0') {
|
735
|
+
// skip over NULL
|
736
|
+
buffer++;
|
737
|
+
} else {
|
738
|
+
// skip over line ending characters
|
739
|
+
if (*buffer == '\r') {
|
740
|
+
buffer++;
|
741
|
+
if (buffer == end)
|
742
|
+
parser->last_buffer_ended_with_cr = true;
|
743
|
+
}
|
744
|
+
if (buffer < end && *buffer == '\n')
|
745
|
+
buffer++;
|
746
|
+
}
|
747
|
+
}
|
748
|
+
}
|
749
|
+
}
|
750
|
+
|
751
|
+
static void chop_trailing_hashtags(cmark_chunk *ch) {
|
752
|
+
bufsize_t n, orig_n;
|
753
|
+
|
754
|
+
cmark_chunk_rtrim(ch);
|
755
|
+
orig_n = n = ch->len - 1;
|
756
|
+
|
757
|
+
// if string ends in space followed by #s, remove these:
|
758
|
+
while (n >= 0 && peek_at(ch, n) == '#')
|
759
|
+
n--;
|
760
|
+
|
761
|
+
// Check for a space before the final #s:
|
762
|
+
if (n != orig_n && n >= 0 && S_is_space_or_tab(peek_at(ch, n))) {
|
763
|
+
ch->len = n;
|
764
|
+
cmark_chunk_rtrim(ch);
|
765
|
+
}
|
766
|
+
}
|
767
|
+
|
768
|
+
// Check for thematic break. On failure, return 0 and update
|
769
|
+
// thematic_break_kill_pos with the index at which the
|
770
|
+
// parse fails. On success, return length of match.
|
771
|
+
// "...three or more hyphens, asterisks,
|
772
|
+
// or underscores on a line by themselves. If you wish, you may use
|
773
|
+
// spaces between the hyphens or asterisks."
|
774
|
+
static int S_scan_thematic_break(cmark_parser *parser, cmark_chunk *input,
|
775
|
+
bufsize_t offset) {
|
776
|
+
bufsize_t i;
|
777
|
+
char c;
|
778
|
+
char nextc = '\0';
|
779
|
+
int count;
|
780
|
+
i = offset;
|
781
|
+
c = peek_at(input, i);
|
782
|
+
if (!(c == '*' || c == '_' || c == '-')) {
|
783
|
+
parser->thematic_break_kill_pos = i;
|
784
|
+
return 0;
|
785
|
+
}
|
786
|
+
count = 1;
|
787
|
+
while ((nextc = peek_at(input, ++i))) {
|
788
|
+
if (nextc == c) {
|
789
|
+
count++;
|
790
|
+
} else if (nextc != ' ' && nextc != '\t') {
|
791
|
+
break;
|
792
|
+
}
|
793
|
+
}
|
794
|
+
if (count >= 3 && (nextc == '\r' || nextc == '\n')) {
|
795
|
+
return (i - offset) + 1;
|
796
|
+
} else {
|
797
|
+
parser->thematic_break_kill_pos = i;
|
798
|
+
return 0;
|
799
|
+
}
|
800
|
+
}
|
801
|
+
|
802
|
+
// Find first nonspace character from current offset, setting
|
803
|
+
// parser->first_nonspace, parser->first_nonspace_column,
|
804
|
+
// parser->indent, and parser->blank. Does not advance parser->offset.
|
805
|
+
static void S_find_first_nonspace(cmark_parser *parser, cmark_chunk *input) {
|
806
|
+
char c;
|
807
|
+
int chars_to_tab = TAB_STOP - (parser->column % TAB_STOP);
|
808
|
+
|
809
|
+
if (parser->first_nonspace <= parser->offset) {
|
810
|
+
parser->first_nonspace = parser->offset;
|
811
|
+
parser->first_nonspace_column = parser->column;
|
812
|
+
while ((c = peek_at(input, parser->first_nonspace))) {
|
813
|
+
if (c == ' ') {
|
814
|
+
parser->first_nonspace += 1;
|
815
|
+
parser->first_nonspace_column += 1;
|
816
|
+
chars_to_tab = chars_to_tab - 1;
|
817
|
+
if (chars_to_tab == 0) {
|
818
|
+
chars_to_tab = TAB_STOP;
|
819
|
+
}
|
820
|
+
} else if (c == '\t') {
|
821
|
+
parser->first_nonspace += 1;
|
822
|
+
parser->first_nonspace_column += chars_to_tab;
|
823
|
+
chars_to_tab = TAB_STOP;
|
824
|
+
} else {
|
825
|
+
break;
|
826
|
+
}
|
827
|
+
}
|
828
|
+
}
|
829
|
+
|
830
|
+
parser->indent = parser->first_nonspace_column - parser->column;
|
831
|
+
parser->blank = S_is_line_end_char(peek_at(input, parser->first_nonspace));
|
832
|
+
}
|
833
|
+
|
834
|
+
// Advance parser->offset and parser->column. parser->offset is the
|
835
|
+
// byte position in input; parser->column is a virtual column number
|
836
|
+
// that takes into account tabs. (Multibyte characters are not taken
|
837
|
+
// into account, because the Markdown line prefixes we are interested in
|
838
|
+
// analyzing are entirely ASCII.) The count parameter indicates
|
839
|
+
// how far to advance the offset. If columns is true, then count
|
840
|
+
// indicates a number of columns; otherwise, a number of bytes.
|
841
|
+
// If advancing a certain number of columns partially consumes
|
842
|
+
// a tab character, parser->partially_consumed_tab is set to true.
|
843
|
+
static void S_advance_offset(cmark_parser *parser, cmark_chunk *input,
|
844
|
+
bufsize_t count, bool columns) {
|
845
|
+
char c;
|
846
|
+
int chars_to_tab;
|
847
|
+
int chars_to_advance;
|
848
|
+
while (count > 0 && (c = peek_at(input, parser->offset))) {
|
849
|
+
if (c == '\t') {
|
850
|
+
chars_to_tab = TAB_STOP - (parser->column % TAB_STOP);
|
851
|
+
if (columns) {
|
852
|
+
parser->partially_consumed_tab = chars_to_tab > count;
|
853
|
+
chars_to_advance = MIN(count, chars_to_tab);
|
854
|
+
parser->column += chars_to_advance;
|
855
|
+
parser->offset += (parser->partially_consumed_tab ? 0 : 1);
|
856
|
+
count -= chars_to_advance;
|
857
|
+
} else {
|
858
|
+
parser->partially_consumed_tab = false;
|
859
|
+
parser->column += chars_to_tab;
|
860
|
+
parser->offset += 1;
|
861
|
+
count -= 1;
|
862
|
+
}
|
863
|
+
} else {
|
864
|
+
parser->partially_consumed_tab = false;
|
865
|
+
parser->offset += 1;
|
866
|
+
parser->column += 1; // assume ascii; block starts are ascii
|
867
|
+
count -= 1;
|
868
|
+
}
|
869
|
+
}
|
870
|
+
}
|
871
|
+
|
872
|
+
static bool S_last_child_is_open(cmark_node *container) {
|
873
|
+
return container->last_child &&
|
874
|
+
(container->last_child->flags & CMARK_NODE__OPEN);
|
875
|
+
}
|
876
|
+
|
877
|
+
static bool parse_block_quote_prefix(cmark_parser *parser, cmark_chunk *input) {
|
878
|
+
bool res = false;
|
879
|
+
bufsize_t matched = 0;
|
880
|
+
|
881
|
+
matched =
|
882
|
+
parser->indent <= 3 && peek_at(input, parser->first_nonspace) == '>';
|
883
|
+
if (matched) {
|
884
|
+
|
885
|
+
S_advance_offset(parser, input, parser->indent + 1, true);
|
886
|
+
|
887
|
+
if (S_is_space_or_tab(peek_at(input, parser->offset))) {
|
888
|
+
S_advance_offset(parser, input, 1, true);
|
889
|
+
}
|
890
|
+
|
891
|
+
res = true;
|
892
|
+
}
|
893
|
+
return res;
|
894
|
+
}
|
895
|
+
|
896
|
+
static bool parse_footnote_definition_block_prefix(cmark_parser *parser, cmark_chunk *input,
|
897
|
+
cmark_node *container) {
|
898
|
+
if (parser->indent >= 4) {
|
899
|
+
S_advance_offset(parser, input, 4, true);
|
900
|
+
return true;
|
901
|
+
} else if (input->len > 0 && (input->data[0] == '\n' || (input->data[0] == '\r' && input->data[1] == '\n'))) {
|
902
|
+
return true;
|
903
|
+
}
|
904
|
+
|
905
|
+
return false;
|
906
|
+
}
|
907
|
+
|
908
|
+
static bool parse_node_item_prefix(cmark_parser *parser, cmark_chunk *input,
|
909
|
+
cmark_node *container) {
|
910
|
+
bool res = false;
|
911
|
+
|
912
|
+
if (parser->indent >=
|
913
|
+
container->as.list.marker_offset + container->as.list.padding) {
|
914
|
+
S_advance_offset(parser, input, container->as.list.marker_offset +
|
915
|
+
container->as.list.padding,
|
916
|
+
true);
|
917
|
+
res = true;
|
918
|
+
} else if (parser->blank && container->first_child != NULL) {
|
919
|
+
// if container->first_child is NULL, then the opening line
|
920
|
+
// of the list item was blank after the list marker; in this
|
921
|
+
// case, we are done with the list item.
|
922
|
+
S_advance_offset(parser, input, parser->first_nonspace - parser->offset,
|
923
|
+
false);
|
924
|
+
res = true;
|
925
|
+
}
|
926
|
+
return res;
|
927
|
+
}
|
928
|
+
|
929
|
+
static bool parse_code_block_prefix(cmark_parser *parser, cmark_chunk *input,
|
930
|
+
cmark_node *container,
|
931
|
+
bool *should_continue) {
|
932
|
+
bool res = false;
|
933
|
+
|
934
|
+
if (!container->as.code.fenced) { // indented
|
935
|
+
if (parser->indent >= CODE_INDENT) {
|
936
|
+
S_advance_offset(parser, input, CODE_INDENT, true);
|
937
|
+
res = true;
|
938
|
+
} else if (parser->blank) {
|
939
|
+
S_advance_offset(parser, input, parser->first_nonspace - parser->offset,
|
940
|
+
false);
|
941
|
+
res = true;
|
942
|
+
}
|
943
|
+
} else { // fenced
|
944
|
+
bufsize_t matched = 0;
|
945
|
+
|
946
|
+
if (parser->indent <= 3 && (peek_at(input, parser->first_nonspace) ==
|
947
|
+
container->as.code.fence_char)) {
|
948
|
+
matched = scan_close_code_fence(input, parser->first_nonspace);
|
949
|
+
}
|
950
|
+
|
951
|
+
if (matched >= container->as.code.fence_length) {
|
952
|
+
// closing fence - and since we're at
|
953
|
+
// the end of a line, we can stop processing it:
|
954
|
+
*should_continue = false;
|
955
|
+
S_advance_offset(parser, input, matched, false);
|
956
|
+
parser->current = finalize(parser, container);
|
957
|
+
} else {
|
958
|
+
// skip opt. spaces of fence parser->offset
|
959
|
+
int i = container->as.code.fence_offset;
|
960
|
+
|
961
|
+
while (i > 0 && S_is_space_or_tab(peek_at(input, parser->offset))) {
|
962
|
+
S_advance_offset(parser, input, 1, true);
|
963
|
+
i--;
|
964
|
+
}
|
965
|
+
res = true;
|
966
|
+
}
|
967
|
+
}
|
968
|
+
|
969
|
+
return res;
|
970
|
+
}
|
971
|
+
|
972
|
+
static bool parse_html_block_prefix(cmark_parser *parser,
|
973
|
+
cmark_node *container) {
|
974
|
+
bool res = false;
|
975
|
+
int html_block_type = container->as.html_block_type;
|
976
|
+
|
977
|
+
assert(html_block_type >= 1 && html_block_type <= 7);
|
978
|
+
switch (html_block_type) {
|
979
|
+
case 1:
|
980
|
+
case 2:
|
981
|
+
case 3:
|
982
|
+
case 4:
|
983
|
+
case 5:
|
984
|
+
// these types of blocks can accept blanks
|
985
|
+
res = true;
|
986
|
+
break;
|
987
|
+
case 6:
|
988
|
+
case 7:
|
989
|
+
res = !parser->blank;
|
990
|
+
break;
|
991
|
+
}
|
992
|
+
|
993
|
+
return res;
|
994
|
+
}
|
995
|
+
|
996
|
+
static bool parse_extension_block(cmark_parser *parser,
|
997
|
+
cmark_node *container,
|
998
|
+
cmark_chunk *input)
|
999
|
+
{
|
1000
|
+
bool res = false;
|
1001
|
+
|
1002
|
+
if (container->extension->last_block_matches) {
|
1003
|
+
if (container->extension->last_block_matches(
|
1004
|
+
container->extension, parser, input->data, input->len, container))
|
1005
|
+
res = true;
|
1006
|
+
}
|
1007
|
+
|
1008
|
+
return res;
|
1009
|
+
}
|
1010
|
+
|
1011
|
+
/**
|
1012
|
+
* For each containing node, try to parse the associated line start.
|
1013
|
+
*
|
1014
|
+
* Will not close unmatched blocks, as we may have a lazy continuation
|
1015
|
+
* line -> http://spec.commonmark.org/0.24/#lazy-continuation-line
|
1016
|
+
*
|
1017
|
+
* Returns: The last matching node, or NULL
|
1018
|
+
*/
|
1019
|
+
static cmark_node *check_open_blocks(cmark_parser *parser, cmark_chunk *input,
|
1020
|
+
bool *all_matched) {
|
1021
|
+
bool should_continue = true;
|
1022
|
+
*all_matched = false;
|
1023
|
+
cmark_node *container = parser->root;
|
1024
|
+
cmark_node_type cont_type;
|
1025
|
+
|
1026
|
+
while (S_last_child_is_open(container)) {
|
1027
|
+
container = container->last_child;
|
1028
|
+
cont_type = S_type(container);
|
1029
|
+
|
1030
|
+
S_find_first_nonspace(parser, input);
|
1031
|
+
|
1032
|
+
if (container->extension) {
|
1033
|
+
if (!parse_extension_block(parser, container, input))
|
1034
|
+
goto done;
|
1035
|
+
continue;
|
1036
|
+
}
|
1037
|
+
|
1038
|
+
switch (cont_type) {
|
1039
|
+
case CMARK_NODE_BLOCK_QUOTE:
|
1040
|
+
if (!parse_block_quote_prefix(parser, input))
|
1041
|
+
goto done;
|
1042
|
+
break;
|
1043
|
+
case CMARK_NODE_ITEM:
|
1044
|
+
if (!parse_node_item_prefix(parser, input, container))
|
1045
|
+
goto done;
|
1046
|
+
break;
|
1047
|
+
case CMARK_NODE_CODE_BLOCK:
|
1048
|
+
if (!parse_code_block_prefix(parser, input, container, &should_continue))
|
1049
|
+
goto done;
|
1050
|
+
break;
|
1051
|
+
case CMARK_NODE_HEADING:
|
1052
|
+
// a heading can never contain more than one line
|
1053
|
+
goto done;
|
1054
|
+
case CMARK_NODE_HTML_BLOCK:
|
1055
|
+
if (!parse_html_block_prefix(parser, container))
|
1056
|
+
goto done;
|
1057
|
+
break;
|
1058
|
+
case CMARK_NODE_PARAGRAPH:
|
1059
|
+
if (parser->blank)
|
1060
|
+
goto done;
|
1061
|
+
break;
|
1062
|
+
case CMARK_NODE_FOOTNOTE_DEFINITION:
|
1063
|
+
if (!parse_footnote_definition_block_prefix(parser, input, container))
|
1064
|
+
goto done;
|
1065
|
+
break;
|
1066
|
+
default:
|
1067
|
+
break;
|
1068
|
+
}
|
1069
|
+
}
|
1070
|
+
|
1071
|
+
*all_matched = true;
|
1072
|
+
|
1073
|
+
done:
|
1074
|
+
if (!*all_matched) {
|
1075
|
+
container = container->parent; // back up to last matching node
|
1076
|
+
}
|
1077
|
+
|
1078
|
+
if (!should_continue) {
|
1079
|
+
container = NULL;
|
1080
|
+
}
|
1081
|
+
|
1082
|
+
return container;
|
1083
|
+
}
|
1084
|
+
|
1085
|
+
static void open_new_blocks(cmark_parser *parser, cmark_node **container,
|
1086
|
+
cmark_chunk *input, bool all_matched) {
|
1087
|
+
bool indented;
|
1088
|
+
cmark_list *data = NULL;
|
1089
|
+
bool maybe_lazy = S_type(parser->current) == CMARK_NODE_PARAGRAPH;
|
1090
|
+
cmark_node_type cont_type = S_type(*container);
|
1091
|
+
bufsize_t matched = 0;
|
1092
|
+
int lev = 0;
|
1093
|
+
bool save_partially_consumed_tab;
|
1094
|
+
bool has_content;
|
1095
|
+
int save_offset;
|
1096
|
+
int save_column;
|
1097
|
+
|
1098
|
+
while (cont_type != CMARK_NODE_CODE_BLOCK &&
|
1099
|
+
cont_type != CMARK_NODE_HTML_BLOCK) {
|
1100
|
+
|
1101
|
+
S_find_first_nonspace(parser, input);
|
1102
|
+
indented = parser->indent >= CODE_INDENT;
|
1103
|
+
|
1104
|
+
if (!indented && peek_at(input, parser->first_nonspace) == '>') {
|
1105
|
+
|
1106
|
+
bufsize_t blockquote_startpos = parser->first_nonspace;
|
1107
|
+
|
1108
|
+
S_advance_offset(parser, input,
|
1109
|
+
parser->first_nonspace + 1 - parser->offset, false);
|
1110
|
+
// optional following character
|
1111
|
+
if (S_is_space_or_tab(peek_at(input, parser->offset))) {
|
1112
|
+
S_advance_offset(parser, input, 1, true);
|
1113
|
+
}
|
1114
|
+
*container = add_child(parser, *container, CMARK_NODE_BLOCK_QUOTE,
|
1115
|
+
blockquote_startpos + 1);
|
1116
|
+
|
1117
|
+
} else if (!indented && (matched = scan_atx_heading_start(
|
1118
|
+
input, parser->first_nonspace))) {
|
1119
|
+
bufsize_t hashpos;
|
1120
|
+
int level = 0;
|
1121
|
+
bufsize_t heading_startpos = parser->first_nonspace;
|
1122
|
+
|
1123
|
+
S_advance_offset(parser, input,
|
1124
|
+
parser->first_nonspace + matched - parser->offset,
|
1125
|
+
false);
|
1126
|
+
*container = add_child(parser, *container, CMARK_NODE_HEADING,
|
1127
|
+
heading_startpos + 1);
|
1128
|
+
|
1129
|
+
hashpos = cmark_chunk_strchr(input, '#', parser->first_nonspace);
|
1130
|
+
|
1131
|
+
while (peek_at(input, hashpos) == '#') {
|
1132
|
+
level++;
|
1133
|
+
hashpos++;
|
1134
|
+
}
|
1135
|
+
|
1136
|
+
(*container)->as.heading.level = level;
|
1137
|
+
(*container)->as.heading.setext = false;
|
1138
|
+
(*container)->internal_offset = matched;
|
1139
|
+
|
1140
|
+
} else if (!indented && (matched = scan_open_code_fence(
|
1141
|
+
input, parser->first_nonspace))) {
|
1142
|
+
*container = add_child(parser, *container, CMARK_NODE_CODE_BLOCK,
|
1143
|
+
parser->first_nonspace + 1);
|
1144
|
+
(*container)->as.code.fenced = true;
|
1145
|
+
(*container)->as.code.fence_char = peek_at(input, parser->first_nonspace);
|
1146
|
+
(*container)->as.code.fence_length = (matched > 255) ? 255 : (uint8_t)matched;
|
1147
|
+
(*container)->as.code.fence_offset =
|
1148
|
+
(int8_t)(parser->first_nonspace - parser->offset);
|
1149
|
+
(*container)->as.code.info = cmark_chunk_literal("");
|
1150
|
+
S_advance_offset(parser, input,
|
1151
|
+
parser->first_nonspace + matched - parser->offset,
|
1152
|
+
false);
|
1153
|
+
|
1154
|
+
} else if (!indented && ((matched = scan_html_block_start(
|
1155
|
+
input, parser->first_nonspace)) ||
|
1156
|
+
(cont_type != CMARK_NODE_PARAGRAPH &&
|
1157
|
+
(matched = scan_html_block_start_7(
|
1158
|
+
input, parser->first_nonspace))))) {
|
1159
|
+
*container = add_child(parser, *container, CMARK_NODE_HTML_BLOCK,
|
1160
|
+
parser->first_nonspace + 1);
|
1161
|
+
(*container)->as.html_block_type = matched;
|
1162
|
+
// note, we don't adjust parser->offset because the tag is part of the
|
1163
|
+
// text
|
1164
|
+
} else if (!indented && cont_type == CMARK_NODE_PARAGRAPH &&
|
1165
|
+
(lev =
|
1166
|
+
scan_setext_heading_line(input, parser->first_nonspace))) {
|
1167
|
+
// finalize paragraph, resolving reference links
|
1168
|
+
has_content = resolve_reference_link_definitions(parser, *container);
|
1169
|
+
|
1170
|
+
if (has_content) {
|
1171
|
+
|
1172
|
+
(*container)->type = (uint16_t)CMARK_NODE_HEADING;
|
1173
|
+
(*container)->as.heading.level = lev;
|
1174
|
+
(*container)->as.heading.setext = true;
|
1175
|
+
S_advance_offset(parser, input, input->len - 1 - parser->offset, false);
|
1176
|
+
}
|
1177
|
+
} else if (!indented &&
|
1178
|
+
!(cont_type == CMARK_NODE_PARAGRAPH && !all_matched) &&
|
1179
|
+
(parser->thematic_break_kill_pos <= parser->first_nonspace) &&
|
1180
|
+
(matched = S_scan_thematic_break(parser, input, parser->first_nonspace))) {
|
1181
|
+
// it's only now that we know the line is not part of a setext heading:
|
1182
|
+
*container = add_child(parser, *container, CMARK_NODE_THEMATIC_BREAK,
|
1183
|
+
parser->first_nonspace + 1);
|
1184
|
+
S_advance_offset(parser, input, input->len - 1 - parser->offset, false);
|
1185
|
+
} else if (!indented &&
|
1186
|
+
parser->options & CMARK_OPT_FOOTNOTES &&
|
1187
|
+
(matched = scan_footnote_definition(input, parser->first_nonspace))) {
|
1188
|
+
cmark_chunk c = cmark_chunk_dup(input, parser->first_nonspace + 2, matched - 2);
|
1189
|
+
cmark_chunk_to_cstr(parser->mem, &c);
|
1190
|
+
|
1191
|
+
while (c.data[c.len - 1] != ']')
|
1192
|
+
--c.len;
|
1193
|
+
--c.len;
|
1194
|
+
|
1195
|
+
S_advance_offset(parser, input, parser->first_nonspace + matched - parser->offset, false);
|
1196
|
+
*container = add_child(parser, *container, CMARK_NODE_FOOTNOTE_DEFINITION, parser->first_nonspace + matched + 1);
|
1197
|
+
(*container)->as.literal = c;
|
1198
|
+
|
1199
|
+
(*container)->internal_offset = matched;
|
1200
|
+
} else if ((!indented || cont_type == CMARK_NODE_LIST) &&
|
1201
|
+
parser->indent < 4 &&
|
1202
|
+
(matched = parse_list_marker(
|
1203
|
+
parser->mem, input, parser->first_nonspace,
|
1204
|
+
(*container)->type == CMARK_NODE_PARAGRAPH, &data))) {
|
1205
|
+
|
1206
|
+
// Note that we can have new list items starting with >= 4
|
1207
|
+
// spaces indent, as long as the list container is still open.
|
1208
|
+
int i = 0;
|
1209
|
+
|
1210
|
+
// compute padding:
|
1211
|
+
S_advance_offset(parser, input,
|
1212
|
+
parser->first_nonspace + matched - parser->offset,
|
1213
|
+
false);
|
1214
|
+
|
1215
|
+
save_partially_consumed_tab = parser->partially_consumed_tab;
|
1216
|
+
save_offset = parser->offset;
|
1217
|
+
save_column = parser->column;
|
1218
|
+
|
1219
|
+
while (parser->column - save_column <= 5 &&
|
1220
|
+
S_is_space_or_tab(peek_at(input, parser->offset))) {
|
1221
|
+
S_advance_offset(parser, input, 1, true);
|
1222
|
+
}
|
1223
|
+
|
1224
|
+
i = parser->column - save_column;
|
1225
|
+
if (i >= 5 || i < 1 ||
|
1226
|
+
// only spaces after list marker:
|
1227
|
+
S_is_line_end_char(peek_at(input, parser->offset))) {
|
1228
|
+
data->padding = matched + 1;
|
1229
|
+
parser->offset = save_offset;
|
1230
|
+
parser->column = save_column;
|
1231
|
+
parser->partially_consumed_tab = save_partially_consumed_tab;
|
1232
|
+
if (i > 0) {
|
1233
|
+
S_advance_offset(parser, input, 1, true);
|
1234
|
+
}
|
1235
|
+
} else {
|
1236
|
+
data->padding = matched + i;
|
1237
|
+
}
|
1238
|
+
|
1239
|
+
// check container; if it's a list, see if this list item
|
1240
|
+
// can continue the list; otherwise, create a list container.
|
1241
|
+
|
1242
|
+
data->marker_offset = parser->indent;
|
1243
|
+
|
1244
|
+
if (cont_type != CMARK_NODE_LIST ||
|
1245
|
+
!lists_match(&((*container)->as.list), data)) {
|
1246
|
+
*container = add_child(parser, *container, CMARK_NODE_LIST,
|
1247
|
+
parser->first_nonspace + 1);
|
1248
|
+
|
1249
|
+
memcpy(&((*container)->as.list), data, sizeof(*data));
|
1250
|
+
}
|
1251
|
+
|
1252
|
+
// add the list item
|
1253
|
+
*container = add_child(parser, *container, CMARK_NODE_ITEM,
|
1254
|
+
parser->first_nonspace + 1);
|
1255
|
+
/* TODO: static */
|
1256
|
+
memcpy(&((*container)->as.list), data, sizeof(*data));
|
1257
|
+
parser->mem->free(data);
|
1258
|
+
} else if (indented && !maybe_lazy && !parser->blank) {
|
1259
|
+
S_advance_offset(parser, input, CODE_INDENT, true);
|
1260
|
+
*container = add_child(parser, *container, CMARK_NODE_CODE_BLOCK,
|
1261
|
+
parser->offset + 1);
|
1262
|
+
(*container)->as.code.fenced = false;
|
1263
|
+
(*container)->as.code.fence_char = 0;
|
1264
|
+
(*container)->as.code.fence_length = 0;
|
1265
|
+
(*container)->as.code.fence_offset = 0;
|
1266
|
+
(*container)->as.code.info = cmark_chunk_literal("");
|
1267
|
+
} else {
|
1268
|
+
cmark_llist *tmp;
|
1269
|
+
cmark_node *new_container = NULL;
|
1270
|
+
|
1271
|
+
for (tmp = parser->syntax_extensions; tmp; tmp=tmp->next) {
|
1272
|
+
cmark_syntax_extension *ext = (cmark_syntax_extension *) tmp->data;
|
1273
|
+
|
1274
|
+
if (ext->try_opening_block) {
|
1275
|
+
new_container = ext->try_opening_block(
|
1276
|
+
ext, indented, parser, *container, input->data, input->len);
|
1277
|
+
|
1278
|
+
if (new_container) {
|
1279
|
+
*container = new_container;
|
1280
|
+
break;
|
1281
|
+
}
|
1282
|
+
}
|
1283
|
+
}
|
1284
|
+
|
1285
|
+
if (!new_container) {
|
1286
|
+
break;
|
1287
|
+
}
|
1288
|
+
}
|
1289
|
+
|
1290
|
+
if (accepts_lines(S_type(*container))) {
|
1291
|
+
// if it's a line container, it can't contain other containers
|
1292
|
+
break;
|
1293
|
+
}
|
1294
|
+
|
1295
|
+
cont_type = S_type(*container);
|
1296
|
+
maybe_lazy = false;
|
1297
|
+
}
|
1298
|
+
}
|
1299
|
+
|
1300
|
+
static void add_text_to_container(cmark_parser *parser, cmark_node *container,
|
1301
|
+
cmark_node *last_matched_container,
|
1302
|
+
cmark_chunk *input) {
|
1303
|
+
cmark_node *tmp;
|
1304
|
+
// what remains at parser->offset is a text line. add the text to the
|
1305
|
+
// appropriate container.
|
1306
|
+
|
1307
|
+
S_find_first_nonspace(parser, input);
|
1308
|
+
|
1309
|
+
if (parser->blank && container->last_child)
|
1310
|
+
S_set_last_line_blank(container->last_child, true);
|
1311
|
+
|
1312
|
+
// block quote lines are never blank as they start with >
|
1313
|
+
// and we don't count blanks in fenced code for purposes of tight/loose
|
1314
|
+
// lists or breaking out of lists. we also don't set last_line_blank
|
1315
|
+
// on an empty list item.
|
1316
|
+
const cmark_node_type ctype = S_type(container);
|
1317
|
+
const bool last_line_blank =
|
1318
|
+
(parser->blank && ctype != CMARK_NODE_BLOCK_QUOTE &&
|
1319
|
+
ctype != CMARK_NODE_HEADING && ctype != CMARK_NODE_THEMATIC_BREAK &&
|
1320
|
+
!(ctype == CMARK_NODE_CODE_BLOCK && container->as.code.fenced) &&
|
1321
|
+
!(ctype == CMARK_NODE_ITEM && container->first_child == NULL &&
|
1322
|
+
container->start_line == parser->line_number));
|
1323
|
+
|
1324
|
+
S_set_last_line_blank(container, last_line_blank);
|
1325
|
+
|
1326
|
+
tmp = container;
|
1327
|
+
while (tmp->parent) {
|
1328
|
+
S_set_last_line_blank(tmp->parent, false);
|
1329
|
+
tmp = tmp->parent;
|
1330
|
+
}
|
1331
|
+
|
1332
|
+
// If the last line processed belonged to a paragraph node,
|
1333
|
+
// and we didn't match all of the line prefixes for the open containers,
|
1334
|
+
// and we didn't start any new containers,
|
1335
|
+
// and the line isn't blank,
|
1336
|
+
// then treat this as a "lazy continuation line" and add it to
|
1337
|
+
// the open paragraph.
|
1338
|
+
if (parser->current != last_matched_container &&
|
1339
|
+
container == last_matched_container && !parser->blank &&
|
1340
|
+
S_type(parser->current) == CMARK_NODE_PARAGRAPH) {
|
1341
|
+
add_line(parser->current, input, parser);
|
1342
|
+
} else { // not a lazy continuation
|
1343
|
+
// Finalize any blocks that were not matched and set cur to container:
|
1344
|
+
while (parser->current != last_matched_container) {
|
1345
|
+
parser->current = finalize(parser, parser->current);
|
1346
|
+
assert(parser->current != NULL);
|
1347
|
+
}
|
1348
|
+
|
1349
|
+
if (S_type(container) == CMARK_NODE_CODE_BLOCK) {
|
1350
|
+
add_line(container, input, parser);
|
1351
|
+
} else if (S_type(container) == CMARK_NODE_HTML_BLOCK) {
|
1352
|
+
add_line(container, input, parser);
|
1353
|
+
|
1354
|
+
int matches_end_condition;
|
1355
|
+
switch (container->as.html_block_type) {
|
1356
|
+
case 1:
|
1357
|
+
// </script>, </style>, </pre>
|
1358
|
+
matches_end_condition =
|
1359
|
+
scan_html_block_end_1(input, parser->first_nonspace);
|
1360
|
+
break;
|
1361
|
+
case 2:
|
1362
|
+
// -->
|
1363
|
+
matches_end_condition =
|
1364
|
+
scan_html_block_end_2(input, parser->first_nonspace);
|
1365
|
+
break;
|
1366
|
+
case 3:
|
1367
|
+
// ?>
|
1368
|
+
matches_end_condition =
|
1369
|
+
scan_html_block_end_3(input, parser->first_nonspace);
|
1370
|
+
break;
|
1371
|
+
case 4:
|
1372
|
+
// >
|
1373
|
+
matches_end_condition =
|
1374
|
+
scan_html_block_end_4(input, parser->first_nonspace);
|
1375
|
+
break;
|
1376
|
+
case 5:
|
1377
|
+
// ]]>
|
1378
|
+
matches_end_condition =
|
1379
|
+
scan_html_block_end_5(input, parser->first_nonspace);
|
1380
|
+
break;
|
1381
|
+
default:
|
1382
|
+
matches_end_condition = 0;
|
1383
|
+
break;
|
1384
|
+
}
|
1385
|
+
|
1386
|
+
if (matches_end_condition) {
|
1387
|
+
container = finalize(parser, container);
|
1388
|
+
assert(parser->current != NULL);
|
1389
|
+
}
|
1390
|
+
} else if (parser->blank) {
|
1391
|
+
// ??? do nothing
|
1392
|
+
} else if (accepts_lines(S_type(container))) {
|
1393
|
+
if (S_type(container) == CMARK_NODE_HEADING &&
|
1394
|
+
container->as.heading.setext == false) {
|
1395
|
+
chop_trailing_hashtags(input);
|
1396
|
+
}
|
1397
|
+
S_advance_offset(parser, input, parser->first_nonspace - parser->offset,
|
1398
|
+
false);
|
1399
|
+
add_line(container, input, parser);
|
1400
|
+
} else {
|
1401
|
+
// create paragraph container for line
|
1402
|
+
container = add_child(parser, container, CMARK_NODE_PARAGRAPH,
|
1403
|
+
parser->first_nonspace + 1);
|
1404
|
+
S_advance_offset(parser, input, parser->first_nonspace - parser->offset,
|
1405
|
+
false);
|
1406
|
+
add_line(container, input, parser);
|
1407
|
+
}
|
1408
|
+
|
1409
|
+
parser->current = container;
|
1410
|
+
}
|
1411
|
+
}
|
1412
|
+
|
1413
|
+
/* See http://spec.commonmark.org/0.24/#phase-1-block-structure */
|
1414
|
+
static void S_process_line(cmark_parser *parser, const unsigned char *buffer,
|
1415
|
+
bufsize_t bytes) {
|
1416
|
+
cmark_node *last_matched_container;
|
1417
|
+
bool all_matched = true;
|
1418
|
+
cmark_node *container;
|
1419
|
+
cmark_chunk input;
|
1420
|
+
cmark_node *current;
|
1421
|
+
|
1422
|
+
cmark_strbuf_clear(&parser->curline);
|
1423
|
+
|
1424
|
+
if (parser->options & CMARK_OPT_VALIDATE_UTF8)
|
1425
|
+
cmark_utf8proc_check(&parser->curline, buffer, bytes);
|
1426
|
+
else
|
1427
|
+
cmark_strbuf_put(&parser->curline, buffer, bytes);
|
1428
|
+
|
1429
|
+
bytes = parser->curline.size;
|
1430
|
+
|
1431
|
+
// ensure line ends with a newline:
|
1432
|
+
if (bytes == 0 || !S_is_line_end_char(parser->curline.ptr[bytes - 1]))
|
1433
|
+
cmark_strbuf_putc(&parser->curline, '\n');
|
1434
|
+
|
1435
|
+
parser->offset = 0;
|
1436
|
+
parser->column = 0;
|
1437
|
+
parser->first_nonspace = 0;
|
1438
|
+
parser->first_nonspace_column = 0;
|
1439
|
+
parser->thematic_break_kill_pos = 0;
|
1440
|
+
parser->indent = 0;
|
1441
|
+
parser->blank = false;
|
1442
|
+
parser->partially_consumed_tab = false;
|
1443
|
+
|
1444
|
+
input.data = parser->curline.ptr;
|
1445
|
+
input.len = parser->curline.size;
|
1446
|
+
input.alloc = 0;
|
1447
|
+
|
1448
|
+
// Skip UTF-8 BOM.
|
1449
|
+
if (parser->line_number == 0 &&
|
1450
|
+
input.len >= 3 &&
|
1451
|
+
memcmp(input.data, "\xef\xbb\xbf", 3) == 0)
|
1452
|
+
parser->offset += 3;
|
1453
|
+
|
1454
|
+
parser->line_number++;
|
1455
|
+
|
1456
|
+
last_matched_container = check_open_blocks(parser, &input, &all_matched);
|
1457
|
+
|
1458
|
+
if (!last_matched_container)
|
1459
|
+
goto finished;
|
1460
|
+
|
1461
|
+
container = last_matched_container;
|
1462
|
+
|
1463
|
+
current = parser->current;
|
1464
|
+
|
1465
|
+
open_new_blocks(parser, &container, &input, all_matched);
|
1466
|
+
|
1467
|
+
/* parser->current might have changed if feed_reentrant was called */
|
1468
|
+
if (current == parser->current)
|
1469
|
+
add_text_to_container(parser, container, last_matched_container, &input);
|
1470
|
+
|
1471
|
+
finished:
|
1472
|
+
parser->last_line_length = input.len;
|
1473
|
+
if (parser->last_line_length &&
|
1474
|
+
input.data[parser->last_line_length - 1] == '\n')
|
1475
|
+
parser->last_line_length -= 1;
|
1476
|
+
if (parser->last_line_length &&
|
1477
|
+
input.data[parser->last_line_length - 1] == '\r')
|
1478
|
+
parser->last_line_length -= 1;
|
1479
|
+
|
1480
|
+
cmark_strbuf_clear(&parser->curline);
|
1481
|
+
}
|
1482
|
+
|
1483
|
+
cmark_node *cmark_parser_finish(cmark_parser *parser) {
|
1484
|
+
cmark_node *res;
|
1485
|
+
cmark_llist *extensions;
|
1486
|
+
|
1487
|
+
/* Parser was already finished once */
|
1488
|
+
if (parser->root == NULL)
|
1489
|
+
return NULL;
|
1490
|
+
|
1491
|
+
if (parser->linebuf.size) {
|
1492
|
+
S_process_line(parser, parser->linebuf.ptr, parser->linebuf.size);
|
1493
|
+
cmark_strbuf_clear(&parser->linebuf);
|
1494
|
+
}
|
1495
|
+
|
1496
|
+
finalize_document(parser);
|
1497
|
+
|
1498
|
+
cmark_consolidate_text_nodes(parser->root);
|
1499
|
+
|
1500
|
+
cmark_strbuf_free(&parser->curline);
|
1501
|
+
cmark_strbuf_free(&parser->linebuf);
|
1502
|
+
|
1503
|
+
#if CMARK_DEBUG_NODES
|
1504
|
+
if (cmark_node_check(parser->root, stderr)) {
|
1505
|
+
abort();
|
1506
|
+
}
|
1507
|
+
#endif
|
1508
|
+
|
1509
|
+
for (extensions = parser->syntax_extensions; extensions; extensions = extensions->next) {
|
1510
|
+
cmark_syntax_extension *ext = (cmark_syntax_extension *) extensions->data;
|
1511
|
+
if (ext->postprocess_func) {
|
1512
|
+
cmark_node *processed = ext->postprocess_func(ext, parser, parser->root);
|
1513
|
+
if (processed)
|
1514
|
+
parser->root = processed;
|
1515
|
+
}
|
1516
|
+
}
|
1517
|
+
|
1518
|
+
res = parser->root;
|
1519
|
+
parser->root = NULL;
|
1520
|
+
|
1521
|
+
cmark_parser_reset(parser);
|
1522
|
+
|
1523
|
+
return res;
|
1524
|
+
}
|
1525
|
+
|
1526
|
+
int cmark_parser_get_line_number(cmark_parser *parser) {
|
1527
|
+
return parser->line_number;
|
1528
|
+
}
|
1529
|
+
|
1530
|
+
bufsize_t cmark_parser_get_offset(cmark_parser *parser) {
|
1531
|
+
return parser->offset;
|
1532
|
+
}
|
1533
|
+
|
1534
|
+
bufsize_t cmark_parser_get_column(cmark_parser *parser) {
|
1535
|
+
return parser->column;
|
1536
|
+
}
|
1537
|
+
|
1538
|
+
int cmark_parser_get_first_nonspace(cmark_parser *parser) {
|
1539
|
+
return parser->first_nonspace;
|
1540
|
+
}
|
1541
|
+
|
1542
|
+
int cmark_parser_get_first_nonspace_column(cmark_parser *parser) {
|
1543
|
+
return parser->first_nonspace_column;
|
1544
|
+
}
|
1545
|
+
|
1546
|
+
int cmark_parser_get_indent(cmark_parser *parser) {
|
1547
|
+
return parser->indent;
|
1548
|
+
}
|
1549
|
+
|
1550
|
+
int cmark_parser_is_blank(cmark_parser *parser) {
|
1551
|
+
return parser->blank;
|
1552
|
+
}
|
1553
|
+
|
1554
|
+
int cmark_parser_has_partially_consumed_tab(cmark_parser *parser) {
|
1555
|
+
return parser->partially_consumed_tab;
|
1556
|
+
}
|
1557
|
+
|
1558
|
+
int cmark_parser_get_last_line_length(cmark_parser *parser) {
|
1559
|
+
return parser->last_line_length;
|
1560
|
+
}
|
1561
|
+
|
1562
|
+
cmark_node *cmark_parser_add_child(cmark_parser *parser,
|
1563
|
+
cmark_node *parent,
|
1564
|
+
cmark_node_type block_type,
|
1565
|
+
int start_column) {
|
1566
|
+
return add_child(parser, parent, block_type, start_column);
|
1567
|
+
}
|
1568
|
+
|
1569
|
+
void cmark_parser_advance_offset(cmark_parser *parser,
|
1570
|
+
const char *input,
|
1571
|
+
int count,
|
1572
|
+
int columns) {
|
1573
|
+
cmark_chunk input_chunk = cmark_chunk_literal(input);
|
1574
|
+
|
1575
|
+
S_advance_offset(parser, &input_chunk, count, columns != 0);
|
1576
|
+
}
|
1577
|
+
|
1578
|
+
void cmark_parser_set_backslash_ispunct_func(cmark_parser *parser,
|
1579
|
+
cmark_ispunct_func func) {
|
1580
|
+
parser->backslash_ispunct = func;
|
1581
|
+
}
|
1582
|
+
|
1583
|
+
cmark_llist *cmark_parser_get_syntax_extensions(cmark_parser *parser) {
|
1584
|
+
return parser->syntax_extensions;
|
1585
|
+
}
|