commonmarker 0.23.10 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Cargo.lock +1220 -0
- data/Cargo.toml +7 -0
- data/README.md +217 -170
- data/ext/commonmarker/Cargo.toml +20 -0
- data/ext/commonmarker/extconf.rb +3 -6
- data/ext/commonmarker/src/lib.rs +183 -0
- data/ext/commonmarker/src/node.rs +1115 -0
- data/ext/commonmarker/src/options.rs +165 -0
- data/ext/commonmarker/src/plugins/syntax_highlighting.rs +74 -0
- data/ext/commonmarker/src/plugins.rs +3 -0
- data/ext/commonmarker/src/utils.rs +8 -0
- data/lib/commonmarker/config.rb +90 -40
- data/lib/commonmarker/constants.rb +7 -0
- data/lib/commonmarker/extension.rb +14 -0
- data/lib/commonmarker/node/ast.rb +8 -0
- data/lib/commonmarker/node/inspect.rb +14 -4
- data/lib/commonmarker/node.rb +29 -47
- data/lib/commonmarker/renderer.rb +1 -127
- data/lib/commonmarker/utils.rb +22 -0
- data/lib/commonmarker/version.rb +2 -2
- data/lib/commonmarker.rb +27 -25
- metadata +38 -186
- data/Rakefile +0 -109
- data/bin/commonmarker +0 -118
- data/commonmarker.gemspec +0 -38
- data/ext/commonmarker/arena.c +0 -104
- data/ext/commonmarker/autolink.c +0 -508
- data/ext/commonmarker/autolink.h +0 -8
- data/ext/commonmarker/blocks.c +0 -1622
- data/ext/commonmarker/buffer.c +0 -278
- data/ext/commonmarker/buffer.h +0 -116
- data/ext/commonmarker/case_fold_switch.inc +0 -4327
- data/ext/commonmarker/chunk.h +0 -135
- data/ext/commonmarker/cmark-gfm-core-extensions.h +0 -54
- data/ext/commonmarker/cmark-gfm-extension_api.h +0 -737
- data/ext/commonmarker/cmark-gfm-extensions_export.h +0 -42
- data/ext/commonmarker/cmark-gfm.h +0 -833
- data/ext/commonmarker/cmark-gfm_export.h +0 -42
- data/ext/commonmarker/cmark-gfm_version.h +0 -7
- data/ext/commonmarker/cmark.c +0 -55
- data/ext/commonmarker/cmark_ctype.c +0 -44
- data/ext/commonmarker/cmark_ctype.h +0 -33
- data/ext/commonmarker/commonmark.c +0 -514
- data/ext/commonmarker/commonmarker.c +0 -1308
- data/ext/commonmarker/commonmarker.h +0 -16
- data/ext/commonmarker/config.h +0 -76
- data/ext/commonmarker/core-extensions.c +0 -27
- data/ext/commonmarker/entities.inc +0 -2138
- data/ext/commonmarker/ext_scanners.c +0 -879
- data/ext/commonmarker/ext_scanners.h +0 -24
- data/ext/commonmarker/footnotes.c +0 -63
- data/ext/commonmarker/footnotes.h +0 -27
- data/ext/commonmarker/houdini.h +0 -57
- data/ext/commonmarker/houdini_href_e.c +0 -100
- data/ext/commonmarker/houdini_html_e.c +0 -66
- data/ext/commonmarker/houdini_html_u.c +0 -149
- data/ext/commonmarker/html.c +0 -502
- data/ext/commonmarker/html.h +0 -27
- data/ext/commonmarker/inlines.c +0 -1788
- data/ext/commonmarker/inlines.h +0 -29
- data/ext/commonmarker/iterator.c +0 -159
- data/ext/commonmarker/iterator.h +0 -26
- data/ext/commonmarker/latex.c +0 -468
- data/ext/commonmarker/linked_list.c +0 -37
- data/ext/commonmarker/man.c +0 -274
- data/ext/commonmarker/map.c +0 -129
- data/ext/commonmarker/map.h +0 -44
- data/ext/commonmarker/node.c +0 -1045
- data/ext/commonmarker/node.h +0 -167
- data/ext/commonmarker/parser.h +0 -59
- data/ext/commonmarker/plaintext.c +0 -218
- data/ext/commonmarker/plugin.c +0 -36
- data/ext/commonmarker/plugin.h +0 -34
- data/ext/commonmarker/references.c +0 -43
- data/ext/commonmarker/references.h +0 -26
- data/ext/commonmarker/registry.c +0 -63
- data/ext/commonmarker/registry.h +0 -24
- data/ext/commonmarker/render.c +0 -213
- data/ext/commonmarker/render.h +0 -62
- data/ext/commonmarker/scanners.c +0 -14056
- data/ext/commonmarker/scanners.h +0 -70
- data/ext/commonmarker/scanners.re +0 -341
- data/ext/commonmarker/strikethrough.c +0 -167
- data/ext/commonmarker/strikethrough.h +0 -9
- data/ext/commonmarker/syntax_extension.c +0 -149
- data/ext/commonmarker/syntax_extension.h +0 -34
- data/ext/commonmarker/table.c +0 -917
- data/ext/commonmarker/table.h +0 -12
- data/ext/commonmarker/tagfilter.c +0 -60
- data/ext/commonmarker/tagfilter.h +0 -8
- data/ext/commonmarker/tasklist.c +0 -156
- data/ext/commonmarker/tasklist.h +0 -8
- data/ext/commonmarker/utf8.c +0 -317
- data/ext/commonmarker/utf8.h +0 -35
- data/ext/commonmarker/xml.c +0 -182
- data/lib/commonmarker/renderer/html_renderer.rb +0 -256
@@ -1,1308 +0,0 @@
|
|
1
|
-
#include "commonmarker.h"
|
2
|
-
#include "cmark-gfm.h"
|
3
|
-
#include "houdini.h"
|
4
|
-
#include "node.h"
|
5
|
-
#include "registry.h"
|
6
|
-
#include "parser.h"
|
7
|
-
#include "syntax_extension.h"
|
8
|
-
#include "cmark-gfm-core-extensions.h"
|
9
|
-
|
10
|
-
static VALUE rb_eNodeError;
|
11
|
-
static VALUE rb_cNode;
|
12
|
-
|
13
|
-
static VALUE sym_document;
|
14
|
-
static VALUE sym_blockquote;
|
15
|
-
static VALUE sym_list;
|
16
|
-
static VALUE sym_list_item;
|
17
|
-
static VALUE sym_code_block;
|
18
|
-
static VALUE sym_html;
|
19
|
-
static VALUE sym_paragraph;
|
20
|
-
static VALUE sym_header;
|
21
|
-
static VALUE sym_hrule;
|
22
|
-
static VALUE sym_text;
|
23
|
-
static VALUE sym_softbreak;
|
24
|
-
static VALUE sym_linebreak;
|
25
|
-
static VALUE sym_code;
|
26
|
-
static VALUE sym_inline_html;
|
27
|
-
static VALUE sym_emph;
|
28
|
-
static VALUE sym_strong;
|
29
|
-
static VALUE sym_link;
|
30
|
-
static VALUE sym_image;
|
31
|
-
static VALUE sym_footnote_reference;
|
32
|
-
static VALUE sym_footnote_definition;
|
33
|
-
|
34
|
-
static VALUE sym_bullet_list;
|
35
|
-
static VALUE sym_ordered_list;
|
36
|
-
|
37
|
-
static VALUE sym_left;
|
38
|
-
static VALUE sym_right;
|
39
|
-
static VALUE sym_center;
|
40
|
-
|
41
|
-
static VALUE encode_utf8_string(const char *c_string) {
|
42
|
-
VALUE string = rb_str_new2(c_string);
|
43
|
-
int enc = rb_enc_find_index("UTF-8");
|
44
|
-
rb_enc_associate_index(string, enc);
|
45
|
-
return string;
|
46
|
-
}
|
47
|
-
|
48
|
-
/* Encode a C string using the encoding from Ruby string +source+. */
|
49
|
-
static VALUE encode_source_string(const char *c_string, VALUE source) {
|
50
|
-
VALUE string = rb_str_new2(c_string);
|
51
|
-
rb_enc_copy(string, source);
|
52
|
-
return string;
|
53
|
-
}
|
54
|
-
|
55
|
-
static void rb_mark_c_struct(void *data) {
|
56
|
-
cmark_node *node = data;
|
57
|
-
cmark_node *child;
|
58
|
-
|
59
|
-
/* Mark the parent to make sure that the tree won't be freed as
|
60
|
-
long as a child node is referenced. */
|
61
|
-
cmark_node *parent = cmark_node_parent(node);
|
62
|
-
if (parent) {
|
63
|
-
void *user_data = cmark_node_get_user_data(parent);
|
64
|
-
if (!user_data) {
|
65
|
-
/* This should never happen. Child can nodes can only
|
66
|
-
be returned from parents that already are
|
67
|
-
associated with a Ruby object. */
|
68
|
-
fprintf(stderr, "parent without user_data\n");
|
69
|
-
abort();
|
70
|
-
}
|
71
|
-
rb_gc_mark((VALUE)user_data);
|
72
|
-
}
|
73
|
-
|
74
|
-
/* Mark all children to make sure their cached Ruby objects won't
|
75
|
-
be freed. */
|
76
|
-
for (child = cmark_node_first_child(node); child != NULL;
|
77
|
-
child = cmark_node_next(child)) {
|
78
|
-
void *user_data = cmark_node_get_user_data(child);
|
79
|
-
if (user_data)
|
80
|
-
rb_gc_mark((VALUE)user_data);
|
81
|
-
}
|
82
|
-
}
|
83
|
-
|
84
|
-
static void rb_free_c_struct(void *data) {
|
85
|
-
/* It's important that the `free` function does not inspect the
|
86
|
-
node data, as it may be part of a tree that was already freed. */
|
87
|
-
cmark_node_free(data);
|
88
|
-
}
|
89
|
-
|
90
|
-
static VALUE rb_node_to_value(cmark_node *node) {
|
91
|
-
void *user_data;
|
92
|
-
RUBY_DATA_FUNC free_func;
|
93
|
-
VALUE val;
|
94
|
-
|
95
|
-
if (node == NULL)
|
96
|
-
return Qnil;
|
97
|
-
|
98
|
-
user_data = cmark_node_get_user_data(node);
|
99
|
-
if (user_data)
|
100
|
-
return (VALUE)user_data;
|
101
|
-
|
102
|
-
/* Only free tree roots. */
|
103
|
-
free_func = cmark_node_parent(node) ? NULL : rb_free_c_struct;
|
104
|
-
val = Data_Wrap_Struct(rb_cNode, rb_mark_c_struct, free_func, node);
|
105
|
-
cmark_node_set_user_data(node, (void *)val);
|
106
|
-
|
107
|
-
return val;
|
108
|
-
}
|
109
|
-
|
110
|
-
/* If the node structure is changed, the finalizers must be updated. */
|
111
|
-
|
112
|
-
static void rb_parent_added(VALUE val) { RDATA(val)->dfree = NULL; }
|
113
|
-
|
114
|
-
static void rb_parent_removed(VALUE val) {
|
115
|
-
RDATA(val)->dfree = rb_free_c_struct;
|
116
|
-
}
|
117
|
-
|
118
|
-
static cmark_parser *prepare_parser(VALUE rb_options, VALUE rb_extensions) {
|
119
|
-
int options;
|
120
|
-
VALUE rb_ext_name;
|
121
|
-
int i;
|
122
|
-
|
123
|
-
FIXNUM_P(rb_options);
|
124
|
-
options = FIX2INT(rb_options);
|
125
|
-
|
126
|
-
Check_Type(rb_extensions, T_ARRAY);
|
127
|
-
|
128
|
-
cmark_parser *parser = cmark_parser_new(options);
|
129
|
-
|
130
|
-
for (i = 0; i < RARRAY_LEN(rb_extensions); ++i) {
|
131
|
-
rb_ext_name = rb_ary_entry(rb_extensions, i);
|
132
|
-
|
133
|
-
if (!SYMBOL_P(rb_ext_name)) {
|
134
|
-
cmark_parser_free(parser);
|
135
|
-
rb_raise(rb_eTypeError, "extension names should be Symbols; got a %"PRIsVALUE"", rb_obj_class(rb_ext_name));
|
136
|
-
}
|
137
|
-
|
138
|
-
cmark_syntax_extension *syntax_extension =
|
139
|
-
cmark_find_syntax_extension(rb_id2name(SYM2ID(rb_ext_name)));
|
140
|
-
|
141
|
-
if (!syntax_extension) {
|
142
|
-
cmark_parser_free(parser);
|
143
|
-
rb_raise(rb_eArgError, "extension %s not found", rb_id2name(SYM2ID(rb_ext_name)));
|
144
|
-
}
|
145
|
-
|
146
|
-
cmark_parser_attach_syntax_extension(parser, syntax_extension);
|
147
|
-
}
|
148
|
-
|
149
|
-
return parser;
|
150
|
-
}
|
151
|
-
|
152
|
-
/*
|
153
|
-
* Internal: Parses a Markdown string into an HTML string.
|
154
|
-
*
|
155
|
-
*/
|
156
|
-
static VALUE rb_markdown_to_html(VALUE self, VALUE rb_text, VALUE rb_options, VALUE rb_extensions) {
|
157
|
-
char *html;
|
158
|
-
cmark_parser *parser;
|
159
|
-
cmark_node *doc;
|
160
|
-
|
161
|
-
Check_Type(rb_text, T_STRING);
|
162
|
-
|
163
|
-
parser = prepare_parser(rb_options, rb_extensions);
|
164
|
-
|
165
|
-
cmark_parser_feed(parser, StringValuePtr(rb_text), RSTRING_LEN(rb_text));
|
166
|
-
doc = cmark_parser_finish(parser);
|
167
|
-
|
168
|
-
if (doc == NULL) {
|
169
|
-
cmark_parser_free(parser);
|
170
|
-
rb_raise(rb_eNodeError, "error parsing document");
|
171
|
-
}
|
172
|
-
|
173
|
-
html = cmark_render_html(doc, parser->options, parser->syntax_extensions);
|
174
|
-
|
175
|
-
cmark_parser_free(parser);
|
176
|
-
cmark_node_free(doc);
|
177
|
-
|
178
|
-
return rb_utf8_str_new_cstr(html);
|
179
|
-
}
|
180
|
-
|
181
|
-
/*
|
182
|
-
* Internal: Parses a Markdown string into an HTML string.
|
183
|
-
*
|
184
|
-
*/
|
185
|
-
static VALUE rb_markdown_to_xml(VALUE self, VALUE rb_text, VALUE rb_options, VALUE rb_extensions) {
|
186
|
-
char *xml;
|
187
|
-
cmark_parser *parser;
|
188
|
-
cmark_node *doc;
|
189
|
-
|
190
|
-
Check_Type(rb_text, T_STRING);
|
191
|
-
|
192
|
-
parser = prepare_parser(rb_options, rb_extensions);
|
193
|
-
|
194
|
-
cmark_parser_feed(parser, StringValuePtr(rb_text), RSTRING_LEN(rb_text));
|
195
|
-
doc = cmark_parser_finish(parser);
|
196
|
-
|
197
|
-
if (doc == NULL) {
|
198
|
-
cmark_parser_free(parser);
|
199
|
-
rb_raise(rb_eNodeError, "error parsing document");
|
200
|
-
}
|
201
|
-
|
202
|
-
xml = cmark_render_xml(doc, parser->options);
|
203
|
-
|
204
|
-
cmark_parser_free(parser);
|
205
|
-
cmark_node_free(doc);
|
206
|
-
|
207
|
-
return rb_utf8_str_new_cstr(xml);
|
208
|
-
}
|
209
|
-
|
210
|
-
/*
|
211
|
-
* Internal: Creates a node based on a node type.
|
212
|
-
*
|
213
|
-
* type - A {Symbol} representing the node to be created. Must be one of the
|
214
|
-
* following:
|
215
|
-
* - `:document`
|
216
|
-
* - `:blockquote`
|
217
|
-
* - `:list`
|
218
|
-
* - `:list_item`
|
219
|
-
* - `:code_block`
|
220
|
-
* - `:html`
|
221
|
-
* - `:paragraph`
|
222
|
-
* - `:header`
|
223
|
-
* - `:hrule`
|
224
|
-
* - `:text`
|
225
|
-
* - `:softbreak`
|
226
|
-
* - `:linebreak`
|
227
|
-
* - `:code`
|
228
|
-
* - `:inline_html`
|
229
|
-
* - `:emph`
|
230
|
-
* - `:strong`
|
231
|
-
* - `:link`
|
232
|
-
* - `:image`
|
233
|
-
*/
|
234
|
-
static VALUE rb_node_new(VALUE self, VALUE type) {
|
235
|
-
cmark_node_type node_type = 0;
|
236
|
-
cmark_node *node;
|
237
|
-
|
238
|
-
Check_Type(type, T_SYMBOL);
|
239
|
-
|
240
|
-
if (type == sym_document)
|
241
|
-
node_type = CMARK_NODE_DOCUMENT;
|
242
|
-
else if (type == sym_blockquote)
|
243
|
-
node_type = CMARK_NODE_BLOCK_QUOTE;
|
244
|
-
else if (type == sym_list)
|
245
|
-
node_type = CMARK_NODE_LIST;
|
246
|
-
else if (type == sym_list_item)
|
247
|
-
node_type = CMARK_NODE_ITEM;
|
248
|
-
else if (type == sym_code_block)
|
249
|
-
node_type = CMARK_NODE_CODE_BLOCK;
|
250
|
-
else if (type == sym_html)
|
251
|
-
node_type = CMARK_NODE_HTML;
|
252
|
-
else if (type == sym_paragraph)
|
253
|
-
node_type = CMARK_NODE_PARAGRAPH;
|
254
|
-
else if (type == sym_header)
|
255
|
-
node_type = CMARK_NODE_HEADER;
|
256
|
-
else if (type == sym_hrule)
|
257
|
-
node_type = CMARK_NODE_HRULE;
|
258
|
-
else if (type == sym_text)
|
259
|
-
node_type = CMARK_NODE_TEXT;
|
260
|
-
else if (type == sym_softbreak)
|
261
|
-
node_type = CMARK_NODE_SOFTBREAK;
|
262
|
-
else if (type == sym_linebreak)
|
263
|
-
node_type = CMARK_NODE_LINEBREAK;
|
264
|
-
else if (type == sym_code)
|
265
|
-
node_type = CMARK_NODE_CODE;
|
266
|
-
else if (type == sym_inline_html)
|
267
|
-
node_type = CMARK_NODE_INLINE_HTML;
|
268
|
-
else if (type == sym_emph)
|
269
|
-
node_type = CMARK_NODE_EMPH;
|
270
|
-
else if (type == sym_strong)
|
271
|
-
node_type = CMARK_NODE_STRONG;
|
272
|
-
else if (type == sym_link)
|
273
|
-
node_type = CMARK_NODE_LINK;
|
274
|
-
else if (type == sym_image)
|
275
|
-
node_type = CMARK_NODE_IMAGE;
|
276
|
-
else if (type == sym_footnote_reference)
|
277
|
-
node_type = CMARK_NODE_FOOTNOTE_REFERENCE;
|
278
|
-
else if (type == sym_footnote_definition)
|
279
|
-
node_type = CMARK_NODE_FOOTNOTE_DEFINITION;
|
280
|
-
else
|
281
|
-
rb_raise(rb_eNodeError, "invalid node of type %d", node_type);
|
282
|
-
|
283
|
-
node = cmark_node_new(node_type);
|
284
|
-
if (node == NULL) {
|
285
|
-
rb_raise(rb_eNodeError, "could not create node of type %d", node_type);
|
286
|
-
}
|
287
|
-
|
288
|
-
return rb_node_to_value(node);
|
289
|
-
}
|
290
|
-
|
291
|
-
/*
|
292
|
-
* Internal: Parses a Markdown string into a document.
|
293
|
-
*
|
294
|
-
*/
|
295
|
-
static VALUE rb_parse_document(VALUE self, VALUE rb_text, VALUE rb_len,
|
296
|
-
VALUE rb_options, VALUE rb_extensions) {
|
297
|
-
char *text;
|
298
|
-
int len;
|
299
|
-
cmark_parser *parser;
|
300
|
-
cmark_node *doc;
|
301
|
-
Check_Type(rb_text, T_STRING);
|
302
|
-
Check_Type(rb_len, T_FIXNUM);
|
303
|
-
Check_Type(rb_options, T_FIXNUM);
|
304
|
-
|
305
|
-
parser = prepare_parser(rb_options, rb_extensions);
|
306
|
-
|
307
|
-
text = (char *)RSTRING_PTR(rb_text);
|
308
|
-
len = FIX2INT(rb_len);
|
309
|
-
|
310
|
-
cmark_parser_feed(parser, text, len);
|
311
|
-
doc = cmark_parser_finish(parser);
|
312
|
-
if (doc == NULL) {
|
313
|
-
rb_raise(rb_eNodeError, "error parsing document");
|
314
|
-
}
|
315
|
-
cmark_parser_free(parser);
|
316
|
-
|
317
|
-
return rb_node_to_value(doc);
|
318
|
-
}
|
319
|
-
|
320
|
-
/*
|
321
|
-
* Public: Fetch the string contents of the node.
|
322
|
-
*
|
323
|
-
* Returns a {String}.
|
324
|
-
*/
|
325
|
-
static VALUE rb_node_get_string_content(VALUE self) {
|
326
|
-
const char *text;
|
327
|
-
cmark_node *node;
|
328
|
-
Data_Get_Struct(self, cmark_node, node);
|
329
|
-
|
330
|
-
text = cmark_node_get_literal(node);
|
331
|
-
if (text == NULL) {
|
332
|
-
rb_raise(rb_eNodeError, "could not get string content");
|
333
|
-
}
|
334
|
-
|
335
|
-
return encode_utf8_string(text);
|
336
|
-
}
|
337
|
-
|
338
|
-
/*
|
339
|
-
* Public: Sets the string content of the node.
|
340
|
-
*
|
341
|
-
* string - A {String} containing new content.
|
342
|
-
*
|
343
|
-
* Raises NodeError if the string content can't be set.
|
344
|
-
*/
|
345
|
-
static VALUE rb_node_set_string_content(VALUE self, VALUE s) {
|
346
|
-
char *text;
|
347
|
-
cmark_node *node;
|
348
|
-
Check_Type(s, T_STRING);
|
349
|
-
|
350
|
-
Data_Get_Struct(self, cmark_node, node);
|
351
|
-
text = StringValueCStr(s);
|
352
|
-
|
353
|
-
if (!cmark_node_set_literal(node, text)) {
|
354
|
-
rb_raise(rb_eNodeError, "could not set string content");
|
355
|
-
}
|
356
|
-
|
357
|
-
return Qnil;
|
358
|
-
}
|
359
|
-
|
360
|
-
/*
|
361
|
-
* Public: Fetches the list type of the node.
|
362
|
-
*
|
363
|
-
* Returns a {Symbol} representing the node's type.
|
364
|
-
*/
|
365
|
-
static VALUE rb_node_get_type(VALUE self) {
|
366
|
-
int node_type;
|
367
|
-
cmark_node *node;
|
368
|
-
VALUE symbol;
|
369
|
-
const char *s;
|
370
|
-
|
371
|
-
Data_Get_Struct(self, cmark_node, node);
|
372
|
-
|
373
|
-
node_type = cmark_node_get_type(node);
|
374
|
-
symbol = Qnil;
|
375
|
-
|
376
|
-
switch (node_type) {
|
377
|
-
case CMARK_NODE_DOCUMENT:
|
378
|
-
symbol = sym_document;
|
379
|
-
break;
|
380
|
-
case CMARK_NODE_BLOCK_QUOTE:
|
381
|
-
symbol = sym_blockquote;
|
382
|
-
break;
|
383
|
-
case CMARK_NODE_LIST:
|
384
|
-
symbol = sym_list;
|
385
|
-
break;
|
386
|
-
case CMARK_NODE_ITEM:
|
387
|
-
symbol = sym_list_item;
|
388
|
-
break;
|
389
|
-
case CMARK_NODE_CODE_BLOCK:
|
390
|
-
symbol = sym_code_block;
|
391
|
-
break;
|
392
|
-
case CMARK_NODE_HTML:
|
393
|
-
symbol = sym_html;
|
394
|
-
break;
|
395
|
-
case CMARK_NODE_PARAGRAPH:
|
396
|
-
symbol = sym_paragraph;
|
397
|
-
break;
|
398
|
-
case CMARK_NODE_HEADER:
|
399
|
-
symbol = sym_header;
|
400
|
-
break;
|
401
|
-
case CMARK_NODE_HRULE:
|
402
|
-
symbol = sym_hrule;
|
403
|
-
break;
|
404
|
-
case CMARK_NODE_TEXT:
|
405
|
-
symbol = sym_text;
|
406
|
-
break;
|
407
|
-
case CMARK_NODE_SOFTBREAK:
|
408
|
-
symbol = sym_softbreak;
|
409
|
-
break;
|
410
|
-
case CMARK_NODE_LINEBREAK:
|
411
|
-
symbol = sym_linebreak;
|
412
|
-
break;
|
413
|
-
case CMARK_NODE_CODE:
|
414
|
-
symbol = sym_code;
|
415
|
-
break;
|
416
|
-
case CMARK_NODE_INLINE_HTML:
|
417
|
-
symbol = sym_inline_html;
|
418
|
-
break;
|
419
|
-
case CMARK_NODE_EMPH:
|
420
|
-
symbol = sym_emph;
|
421
|
-
break;
|
422
|
-
case CMARK_NODE_STRONG:
|
423
|
-
symbol = sym_strong;
|
424
|
-
break;
|
425
|
-
case CMARK_NODE_LINK:
|
426
|
-
symbol = sym_link;
|
427
|
-
break;
|
428
|
-
case CMARK_NODE_IMAGE:
|
429
|
-
symbol = sym_image;
|
430
|
-
break;
|
431
|
-
case CMARK_NODE_FOOTNOTE_REFERENCE:
|
432
|
-
symbol = sym_footnote_reference;
|
433
|
-
break;
|
434
|
-
case CMARK_NODE_FOOTNOTE_DEFINITION:
|
435
|
-
symbol = sym_footnote_definition;
|
436
|
-
break;
|
437
|
-
default:
|
438
|
-
if (node->extension) {
|
439
|
-
s = node->extension->get_type_string_func(node->extension, node);
|
440
|
-
return ID2SYM(rb_intern(s));
|
441
|
-
}
|
442
|
-
rb_raise(rb_eNodeError, "invalid node type %d", node_type);
|
443
|
-
}
|
444
|
-
|
445
|
-
return symbol;
|
446
|
-
}
|
447
|
-
|
448
|
-
/*
|
449
|
-
* Public: Fetches the sourcepos of the node.
|
450
|
-
*
|
451
|
-
* Returns a {Hash} containing {Symbol} keys of the positions.
|
452
|
-
*/
|
453
|
-
static VALUE rb_node_get_sourcepos(VALUE self) {
|
454
|
-
int start_line, start_column, end_line, end_column;
|
455
|
-
VALUE result;
|
456
|
-
|
457
|
-
cmark_node *node;
|
458
|
-
Data_Get_Struct(self, cmark_node, node);
|
459
|
-
|
460
|
-
start_line = cmark_node_get_start_line(node);
|
461
|
-
start_column = cmark_node_get_start_column(node);
|
462
|
-
end_line = cmark_node_get_end_line(node);
|
463
|
-
end_column = cmark_node_get_end_column(node);
|
464
|
-
|
465
|
-
result = rb_hash_new();
|
466
|
-
rb_hash_aset(result, CSTR2SYM("start_line"), INT2NUM(start_line));
|
467
|
-
rb_hash_aset(result, CSTR2SYM("start_column"), INT2NUM(start_column));
|
468
|
-
rb_hash_aset(result, CSTR2SYM("end_line"), INT2NUM(end_line));
|
469
|
-
rb_hash_aset(result, CSTR2SYM("end_column"), INT2NUM(end_column));
|
470
|
-
|
471
|
-
return result;
|
472
|
-
}
|
473
|
-
|
474
|
-
/*
|
475
|
-
* Public: Returns the type of the current pointer as a string.
|
476
|
-
*
|
477
|
-
* Returns a {String}.
|
478
|
-
*/
|
479
|
-
static VALUE rb_node_get_type_string(VALUE self) {
|
480
|
-
cmark_node *node;
|
481
|
-
Data_Get_Struct(self, cmark_node, node);
|
482
|
-
|
483
|
-
return rb_str_new2(cmark_node_get_type_string(node));
|
484
|
-
}
|
485
|
-
|
486
|
-
/*
|
487
|
-
* Internal: Unlinks the node from the tree (fixing pointers in
|
488
|
-
* parents and siblings appropriately).
|
489
|
-
*/
|
490
|
-
static VALUE rb_node_unlink(VALUE self) {
|
491
|
-
cmark_node *node;
|
492
|
-
Data_Get_Struct(self, cmark_node, node);
|
493
|
-
|
494
|
-
cmark_node_unlink(node);
|
495
|
-
|
496
|
-
rb_parent_removed(self);
|
497
|
-
|
498
|
-
return Qnil;
|
499
|
-
}
|
500
|
-
|
501
|
-
/* Public: Fetches the first child of the node.
|
502
|
-
*
|
503
|
-
* Returns a {Node} if a child exists, `nil` otherise.
|
504
|
-
*/
|
505
|
-
static VALUE rb_node_first_child(VALUE self) {
|
506
|
-
cmark_node *node, *child;
|
507
|
-
Data_Get_Struct(self, cmark_node, node);
|
508
|
-
|
509
|
-
child = cmark_node_first_child(node);
|
510
|
-
|
511
|
-
return rb_node_to_value(child);
|
512
|
-
}
|
513
|
-
|
514
|
-
/* Public: Fetches the next sibling of the node.
|
515
|
-
*
|
516
|
-
* Returns a {Node} if a sibling exists, `nil` otherwise.
|
517
|
-
*/
|
518
|
-
static VALUE rb_node_next(VALUE self) {
|
519
|
-
cmark_node *node, *next;
|
520
|
-
Data_Get_Struct(self, cmark_node, node);
|
521
|
-
|
522
|
-
next = cmark_node_next(node);
|
523
|
-
|
524
|
-
return rb_node_to_value(next);
|
525
|
-
}
|
526
|
-
|
527
|
-
/*
|
528
|
-
* Public: Inserts a node as a sibling before the current node.
|
529
|
-
*
|
530
|
-
* sibling - A sibling {Node} to insert.
|
531
|
-
*
|
532
|
-
* Returns `true` if successful.
|
533
|
-
* Raises NodeError if the node can't be inserted.
|
534
|
-
*/
|
535
|
-
static VALUE rb_node_insert_before(VALUE self, VALUE sibling) {
|
536
|
-
cmark_node *node1, *node2;
|
537
|
-
Data_Get_Struct(self, cmark_node, node1);
|
538
|
-
|
539
|
-
Data_Get_Struct(sibling, cmark_node, node2);
|
540
|
-
|
541
|
-
if (!cmark_node_insert_before(node1, node2)) {
|
542
|
-
rb_raise(rb_eNodeError, "could not insert before");
|
543
|
-
}
|
544
|
-
|
545
|
-
rb_parent_added(sibling);
|
546
|
-
|
547
|
-
return Qtrue;
|
548
|
-
}
|
549
|
-
|
550
|
-
/* Internal: Convert the node to an HTML string.
|
551
|
-
*
|
552
|
-
* Returns a {String}.
|
553
|
-
*/
|
554
|
-
static VALUE rb_render_html(VALUE self, VALUE rb_options, VALUE rb_extensions) {
|
555
|
-
int options, extensions_len;
|
556
|
-
VALUE rb_ext_name;
|
557
|
-
int i;
|
558
|
-
cmark_node *node;
|
559
|
-
cmark_llist *extensions = NULL;
|
560
|
-
cmark_mem *mem = cmark_get_default_mem_allocator();
|
561
|
-
Check_Type(rb_options, T_FIXNUM);
|
562
|
-
Check_Type(rb_extensions, T_ARRAY);
|
563
|
-
|
564
|
-
options = FIX2INT(rb_options);
|
565
|
-
extensions_len = RARRAY_LEN(rb_extensions);
|
566
|
-
|
567
|
-
Data_Get_Struct(self, cmark_node, node);
|
568
|
-
|
569
|
-
for (i = 0; i < extensions_len; ++i) {
|
570
|
-
rb_ext_name = RARRAY_PTR(rb_extensions)[i];
|
571
|
-
|
572
|
-
if (!SYMBOL_P(rb_ext_name)) {
|
573
|
-
cmark_llist_free(mem, extensions);
|
574
|
-
rb_raise(rb_eTypeError, "extension names should be Symbols; got a %"PRIsVALUE"", rb_obj_class(rb_ext_name));
|
575
|
-
}
|
576
|
-
|
577
|
-
cmark_syntax_extension *syntax_extension =
|
578
|
-
cmark_find_syntax_extension(rb_id2name(SYM2ID(rb_ext_name)));
|
579
|
-
|
580
|
-
if (!syntax_extension) {
|
581
|
-
cmark_llist_free(mem, extensions);
|
582
|
-
rb_raise(rb_eArgError, "extension %s not found\n", rb_id2name(SYM2ID(rb_ext_name)));
|
583
|
-
}
|
584
|
-
|
585
|
-
extensions = cmark_llist_append(mem, extensions, syntax_extension);
|
586
|
-
}
|
587
|
-
|
588
|
-
char *html = cmark_render_html(node, options, extensions);
|
589
|
-
VALUE ruby_html = rb_str_new2(html);
|
590
|
-
|
591
|
-
cmark_llist_free(mem, extensions);
|
592
|
-
free(html);
|
593
|
-
|
594
|
-
return ruby_html;
|
595
|
-
}
|
596
|
-
|
597
|
-
/* Internal: Convert the node to an XML string.
|
598
|
-
*
|
599
|
-
* Returns a {String}.
|
600
|
-
*/
|
601
|
-
static VALUE rb_render_xml(VALUE self, VALUE rb_options) {
|
602
|
-
int options;
|
603
|
-
cmark_node *node;
|
604
|
-
Check_Type(rb_options, T_FIXNUM);
|
605
|
-
|
606
|
-
options = FIX2INT(rb_options);
|
607
|
-
|
608
|
-
Data_Get_Struct(self, cmark_node, node);
|
609
|
-
|
610
|
-
char *xml = cmark_render_xml(node, options);
|
611
|
-
VALUE ruby_xml = rb_str_new2(xml);
|
612
|
-
|
613
|
-
free(xml);
|
614
|
-
|
615
|
-
return ruby_xml;
|
616
|
-
}
|
617
|
-
|
618
|
-
/* Internal: Convert the node to a CommonMark string.
|
619
|
-
*
|
620
|
-
* Returns a {String}.
|
621
|
-
*/
|
622
|
-
static VALUE rb_render_commonmark(int argc, VALUE *argv, VALUE self) {
|
623
|
-
VALUE rb_options, rb_width;
|
624
|
-
rb_scan_args(argc, argv, "11", &rb_options, &rb_width);
|
625
|
-
|
626
|
-
int width = 120;
|
627
|
-
if (!NIL_P(rb_width)) {
|
628
|
-
Check_Type(rb_width, T_FIXNUM);
|
629
|
-
width = FIX2INT(rb_width);
|
630
|
-
}
|
631
|
-
|
632
|
-
int options;
|
633
|
-
cmark_node *node;
|
634
|
-
Check_Type(rb_options, T_FIXNUM);
|
635
|
-
|
636
|
-
options = FIX2INT(rb_options);
|
637
|
-
Data_Get_Struct(self, cmark_node, node);
|
638
|
-
|
639
|
-
char *cmark = cmark_render_commonmark(node, options, width);
|
640
|
-
VALUE ruby_cmark = rb_str_new2(cmark);
|
641
|
-
free(cmark);
|
642
|
-
|
643
|
-
return ruby_cmark;
|
644
|
-
}
|
645
|
-
|
646
|
-
/* Internal: Convert the node to a plain textstring.
|
647
|
-
*
|
648
|
-
* Returns a {String}.
|
649
|
-
*/
|
650
|
-
static VALUE rb_render_plaintext(int argc, VALUE *argv, VALUE self) {
|
651
|
-
VALUE rb_options, rb_width;
|
652
|
-
rb_scan_args(argc, argv, "11", &rb_options, &rb_width);
|
653
|
-
|
654
|
-
int width = 120;
|
655
|
-
if (!NIL_P(rb_width)) {
|
656
|
-
Check_Type(rb_width, T_FIXNUM);
|
657
|
-
width = FIX2INT(rb_width);
|
658
|
-
}
|
659
|
-
|
660
|
-
int options;
|
661
|
-
cmark_node *node;
|
662
|
-
Check_Type(rb_options, T_FIXNUM);
|
663
|
-
|
664
|
-
options = FIX2INT(rb_options);
|
665
|
-
Data_Get_Struct(self, cmark_node, node);
|
666
|
-
|
667
|
-
char *text = cmark_render_plaintext(node, options, width);
|
668
|
-
VALUE ruby_text = rb_str_new2(text);
|
669
|
-
free(text);
|
670
|
-
|
671
|
-
return ruby_text;
|
672
|
-
}
|
673
|
-
|
674
|
-
/*
|
675
|
-
* Public: Inserts a node as a sibling after the current node.
|
676
|
-
*
|
677
|
-
* sibling - A sibling {Node} to insert.
|
678
|
-
*
|
679
|
-
* Returns `true` if successful.
|
680
|
-
* Raises NodeError if the node can't be inserted.
|
681
|
-
*/
|
682
|
-
static VALUE rb_node_insert_after(VALUE self, VALUE sibling) {
|
683
|
-
cmark_node *node1, *node2;
|
684
|
-
Data_Get_Struct(self, cmark_node, node1);
|
685
|
-
|
686
|
-
Data_Get_Struct(sibling, cmark_node, node2);
|
687
|
-
|
688
|
-
if (!cmark_node_insert_after(node1, node2)) {
|
689
|
-
rb_raise(rb_eNodeError, "could not insert after");
|
690
|
-
}
|
691
|
-
|
692
|
-
rb_parent_added(sibling);
|
693
|
-
|
694
|
-
return Qtrue;
|
695
|
-
}
|
696
|
-
|
697
|
-
/*
|
698
|
-
* Public: Inserts a node as the first child of the current node.
|
699
|
-
*
|
700
|
-
* child - A child {Node} to insert.
|
701
|
-
*
|
702
|
-
* Returns `true` if successful.
|
703
|
-
* Raises NodeError if the node can't be inserted.
|
704
|
-
*/
|
705
|
-
static VALUE rb_node_prepend_child(VALUE self, VALUE child) {
|
706
|
-
cmark_node *node1, *node2;
|
707
|
-
Data_Get_Struct(self, cmark_node, node1);
|
708
|
-
|
709
|
-
Data_Get_Struct(child, cmark_node, node2);
|
710
|
-
|
711
|
-
if (!cmark_node_prepend_child(node1, node2)) {
|
712
|
-
rb_raise(rb_eNodeError, "could not prepend child");
|
713
|
-
}
|
714
|
-
|
715
|
-
rb_parent_added(child);
|
716
|
-
|
717
|
-
return Qtrue;
|
718
|
-
}
|
719
|
-
|
720
|
-
/*
|
721
|
-
* Public: Inserts a node as the last child of the current node.
|
722
|
-
*
|
723
|
-
* child - A child {Node} to insert.
|
724
|
-
*
|
725
|
-
* Returns `true` if successful.
|
726
|
-
* Raises NodeError if the node can't be inserted.
|
727
|
-
*/
|
728
|
-
static VALUE rb_node_append_child(VALUE self, VALUE child) {
|
729
|
-
cmark_node *node1, *node2;
|
730
|
-
Data_Get_Struct(self, cmark_node, node1);
|
731
|
-
|
732
|
-
Data_Get_Struct(child, cmark_node, node2);
|
733
|
-
|
734
|
-
if (!cmark_node_append_child(node1, node2)) {
|
735
|
-
rb_raise(rb_eNodeError, "could not append child");
|
736
|
-
}
|
737
|
-
|
738
|
-
rb_parent_added(child);
|
739
|
-
|
740
|
-
return Qtrue;
|
741
|
-
}
|
742
|
-
|
743
|
-
/* Public: Fetches the first child of the current node.
|
744
|
-
*
|
745
|
-
* Returns a {Node} if a child exists, `nil` otherise.
|
746
|
-
*/
|
747
|
-
static VALUE rb_node_last_child(VALUE self) {
|
748
|
-
cmark_node *node, *child;
|
749
|
-
Data_Get_Struct(self, cmark_node, node);
|
750
|
-
|
751
|
-
child = cmark_node_last_child(node);
|
752
|
-
|
753
|
-
return rb_node_to_value(child);
|
754
|
-
}
|
755
|
-
|
756
|
-
/* Public: Fetches the parent of the current node.
|
757
|
-
*
|
758
|
-
* Returns a {Node} if a parent exists, `nil` otherise.
|
759
|
-
*/
|
760
|
-
static VALUE rb_node_parent(VALUE self) {
|
761
|
-
cmark_node *node, *parent;
|
762
|
-
Data_Get_Struct(self, cmark_node, node);
|
763
|
-
|
764
|
-
parent = cmark_node_parent(node);
|
765
|
-
|
766
|
-
return rb_node_to_value(parent);
|
767
|
-
}
|
768
|
-
|
769
|
-
/* Public: Fetches the previous sibling of the current node.
|
770
|
-
*
|
771
|
-
* Returns a {Node} if a parent exists, `nil` otherise.
|
772
|
-
*/
|
773
|
-
static VALUE rb_node_previous(VALUE self) {
|
774
|
-
cmark_node *node, *previous;
|
775
|
-
Data_Get_Struct(self, cmark_node, node);
|
776
|
-
|
777
|
-
previous = cmark_node_previous(node);
|
778
|
-
|
779
|
-
return rb_node_to_value(previous);
|
780
|
-
}
|
781
|
-
|
782
|
-
/*
|
783
|
-
* Public: Gets the URL of the current node (must be a `:link` or `:image`).
|
784
|
-
*
|
785
|
-
* Returns a {String}.
|
786
|
-
* Raises a NodeError if the URL can't be retrieved.
|
787
|
-
*/
|
788
|
-
static VALUE rb_node_get_url(VALUE self) {
|
789
|
-
const char *text;
|
790
|
-
cmark_node *node;
|
791
|
-
Data_Get_Struct(self, cmark_node, node);
|
792
|
-
|
793
|
-
text = cmark_node_get_url(node);
|
794
|
-
if (text == NULL) {
|
795
|
-
rb_raise(rb_eNodeError, "could not get url");
|
796
|
-
}
|
797
|
-
|
798
|
-
return rb_str_new2(text);
|
799
|
-
}
|
800
|
-
|
801
|
-
/*
|
802
|
-
* Public: Sets the URL of the current node (must be a `:link` or `:image`).
|
803
|
-
*
|
804
|
-
* url - A {String} representing the new URL
|
805
|
-
*
|
806
|
-
* Raises a NodeError if the URL can't be set.
|
807
|
-
*/
|
808
|
-
static VALUE rb_node_set_url(VALUE self, VALUE url) {
|
809
|
-
cmark_node *node;
|
810
|
-
char *text;
|
811
|
-
Check_Type(url, T_STRING);
|
812
|
-
|
813
|
-
Data_Get_Struct(self, cmark_node, node);
|
814
|
-
text = StringValueCStr(url);
|
815
|
-
|
816
|
-
if (!cmark_node_set_url(node, text)) {
|
817
|
-
rb_raise(rb_eNodeError, "could not set url");
|
818
|
-
}
|
819
|
-
|
820
|
-
return Qnil;
|
821
|
-
}
|
822
|
-
|
823
|
-
/*
|
824
|
-
* Public: Gets the title of the current node (must be a `:link` or `:image`).
|
825
|
-
*
|
826
|
-
* Returns a {String}.
|
827
|
-
* Raises a NodeError if the title can't be retrieved.
|
828
|
-
*/
|
829
|
-
static VALUE rb_node_get_title(VALUE self) {
|
830
|
-
const char *text;
|
831
|
-
cmark_node *node;
|
832
|
-
Data_Get_Struct(self, cmark_node, node);
|
833
|
-
|
834
|
-
text = cmark_node_get_title(node);
|
835
|
-
if (text == NULL) {
|
836
|
-
rb_raise(rb_eNodeError, "could not get title");
|
837
|
-
}
|
838
|
-
|
839
|
-
return rb_str_new2(text);
|
840
|
-
}
|
841
|
-
|
842
|
-
/*
|
843
|
-
* Public: Sets the title of the current node (must be a `:link` or `:image`).
|
844
|
-
*
|
845
|
-
* title - A {String} representing the new title
|
846
|
-
*
|
847
|
-
* Raises a NodeError if the title can't be set.
|
848
|
-
*/
|
849
|
-
static VALUE rb_node_set_title(VALUE self, VALUE title) {
|
850
|
-
char *text;
|
851
|
-
cmark_node *node;
|
852
|
-
Check_Type(title, T_STRING);
|
853
|
-
|
854
|
-
Data_Get_Struct(self, cmark_node, node);
|
855
|
-
text = StringValueCStr(title);
|
856
|
-
|
857
|
-
if (!cmark_node_set_title(node, text)) {
|
858
|
-
rb_raise(rb_eNodeError, "could not set title");
|
859
|
-
}
|
860
|
-
|
861
|
-
return Qnil;
|
862
|
-
}
|
863
|
-
|
864
|
-
/*
|
865
|
-
* Public: Gets the header level of the current node (must be a `:header`).
|
866
|
-
*
|
867
|
-
* Returns a {Number} representing the header level.
|
868
|
-
* Raises a NodeError if the header level can't be retrieved.
|
869
|
-
*/
|
870
|
-
static VALUE rb_node_get_header_level(VALUE self) {
|
871
|
-
int header_level;
|
872
|
-
cmark_node *node;
|
873
|
-
Data_Get_Struct(self, cmark_node, node);
|
874
|
-
|
875
|
-
header_level = cmark_node_get_header_level(node);
|
876
|
-
|
877
|
-
if (header_level == 0) {
|
878
|
-
rb_raise(rb_eNodeError, "could not get header_level");
|
879
|
-
}
|
880
|
-
|
881
|
-
return INT2NUM(header_level);
|
882
|
-
}
|
883
|
-
|
884
|
-
/*
|
885
|
-
* Public: Sets the header level of the current node (must be a `:header`).
|
886
|
-
*
|
887
|
-
* level - A {Number} representing the new header level
|
888
|
-
*
|
889
|
-
* Raises a NodeError if the header level can't be set.
|
890
|
-
*/
|
891
|
-
static VALUE rb_node_set_header_level(VALUE self, VALUE level) {
|
892
|
-
int l;
|
893
|
-
cmark_node *node;
|
894
|
-
Check_Type(level, T_FIXNUM);
|
895
|
-
|
896
|
-
Data_Get_Struct(self, cmark_node, node);
|
897
|
-
l = FIX2INT(level);
|
898
|
-
|
899
|
-
if (!cmark_node_set_header_level(node, l)) {
|
900
|
-
rb_raise(rb_eNodeError, "could not set header_level");
|
901
|
-
}
|
902
|
-
|
903
|
-
return Qnil;
|
904
|
-
}
|
905
|
-
|
906
|
-
/*
|
907
|
-
* Public: Gets the list type of the current node (must be a `:list`).
|
908
|
-
*
|
909
|
-
* Returns a {Symbol}.
|
910
|
-
* Raises a NodeError if the title can't be retrieved.
|
911
|
-
*/
|
912
|
-
static VALUE rb_node_get_list_type(VALUE self) {
|
913
|
-
int list_type;
|
914
|
-
cmark_node *node;
|
915
|
-
VALUE symbol;
|
916
|
-
Data_Get_Struct(self, cmark_node, node);
|
917
|
-
|
918
|
-
list_type = cmark_node_get_list_type(node);
|
919
|
-
|
920
|
-
if (list_type == CMARK_BULLET_LIST) {
|
921
|
-
symbol = sym_bullet_list;
|
922
|
-
} else if (list_type == CMARK_ORDERED_LIST) {
|
923
|
-
symbol = sym_ordered_list;
|
924
|
-
} else {
|
925
|
-
rb_raise(rb_eNodeError, "could not get list_type");
|
926
|
-
}
|
927
|
-
|
928
|
-
return symbol;
|
929
|
-
}
|
930
|
-
|
931
|
-
/*
|
932
|
-
* Public: Sets the list type of the current node (must be a `:list`).
|
933
|
-
*
|
934
|
-
* level - A {Symbol} representing the new list type
|
935
|
-
*
|
936
|
-
* Raises a NodeError if the list type can't be set.
|
937
|
-
*/
|
938
|
-
static VALUE rb_node_set_list_type(VALUE self, VALUE list_type) {
|
939
|
-
int type = 0;
|
940
|
-
cmark_node *node;
|
941
|
-
Check_Type(list_type, T_SYMBOL);
|
942
|
-
|
943
|
-
Data_Get_Struct(self, cmark_node, node);
|
944
|
-
|
945
|
-
if (list_type == sym_bullet_list) {
|
946
|
-
type = CMARK_BULLET_LIST;
|
947
|
-
} else if (list_type == sym_ordered_list) {
|
948
|
-
type = CMARK_ORDERED_LIST;
|
949
|
-
} else {
|
950
|
-
rb_raise(rb_eNodeError, "invalid list_type");
|
951
|
-
}
|
952
|
-
|
953
|
-
if (!cmark_node_set_list_type(node, type)) {
|
954
|
-
rb_raise(rb_eNodeError, "could not set list_type");
|
955
|
-
}
|
956
|
-
|
957
|
-
return Qnil;
|
958
|
-
}
|
959
|
-
|
960
|
-
/*
|
961
|
-
* Public: Gets the starting number the current node (must be an
|
962
|
-
* `:ordered_list`).
|
963
|
-
*
|
964
|
-
* Returns a {Number} representing the starting number.
|
965
|
-
* Raises a NodeError if the starting number can't be retrieved.
|
966
|
-
*/
|
967
|
-
static VALUE rb_node_get_list_start(VALUE self) {
|
968
|
-
cmark_node *node;
|
969
|
-
Data_Get_Struct(self, cmark_node, node);
|
970
|
-
|
971
|
-
if (cmark_node_get_type(node) != CMARK_NODE_LIST ||
|
972
|
-
cmark_node_get_list_type(node) != CMARK_ORDERED_LIST) {
|
973
|
-
rb_raise(rb_eNodeError, "can't get list_start for non-ordered list %d",
|
974
|
-
cmark_node_get_list_type(node));
|
975
|
-
}
|
976
|
-
|
977
|
-
return INT2NUM(cmark_node_get_list_start(node));
|
978
|
-
}
|
979
|
-
|
980
|
-
/*
|
981
|
-
* Public: Sets the starting number of the current node (must be an
|
982
|
-
* `:ordered_list`).
|
983
|
-
*
|
984
|
-
* level - A {Number} representing the new starting number
|
985
|
-
*
|
986
|
-
* Raises a NodeError if the starting number can't be set.
|
987
|
-
*/
|
988
|
-
static VALUE rb_node_set_list_start(VALUE self, VALUE start) {
|
989
|
-
int s;
|
990
|
-
cmark_node *node;
|
991
|
-
Check_Type(start, T_FIXNUM);
|
992
|
-
|
993
|
-
Data_Get_Struct(self, cmark_node, node);
|
994
|
-
s = FIX2INT(start);
|
995
|
-
|
996
|
-
if (!cmark_node_set_list_start(node, s)) {
|
997
|
-
rb_raise(rb_eNodeError, "could not set list_start");
|
998
|
-
}
|
999
|
-
|
1000
|
-
return Qnil;
|
1001
|
-
}
|
1002
|
-
|
1003
|
-
/*
|
1004
|
-
* Public: Gets the tight status the current node (must be a `:list`).
|
1005
|
-
*
|
1006
|
-
* Returns a `true` if the list is tight, `false` otherwise.
|
1007
|
-
* Raises a NodeError if the starting number can't be retrieved.
|
1008
|
-
*/
|
1009
|
-
static VALUE rb_node_get_list_tight(VALUE self) {
|
1010
|
-
int flag;
|
1011
|
-
cmark_node *node;
|
1012
|
-
Data_Get_Struct(self, cmark_node, node);
|
1013
|
-
|
1014
|
-
if (cmark_node_get_type(node) != CMARK_NODE_LIST) {
|
1015
|
-
rb_raise(rb_eNodeError, "can't get list_tight for non-list");
|
1016
|
-
}
|
1017
|
-
|
1018
|
-
flag = cmark_node_get_list_tight(node);
|
1019
|
-
|
1020
|
-
return flag ? Qtrue : Qfalse;
|
1021
|
-
}
|
1022
|
-
|
1023
|
-
/*
|
1024
|
-
* Public: Sets the tight status of the current node (must be a `:list`).
|
1025
|
-
*
|
1026
|
-
* tight - A {Boolean} representing the new tightness
|
1027
|
-
*
|
1028
|
-
* Raises a NodeError if the tightness can't be set.
|
1029
|
-
*/
|
1030
|
-
static VALUE rb_node_set_list_tight(VALUE self, VALUE tight) {
|
1031
|
-
int t;
|
1032
|
-
cmark_node *node;
|
1033
|
-
Data_Get_Struct(self, cmark_node, node);
|
1034
|
-
t = RTEST(tight);
|
1035
|
-
|
1036
|
-
if (!cmark_node_set_list_tight(node, t)) {
|
1037
|
-
rb_raise(rb_eNodeError, "could not set list_tight");
|
1038
|
-
}
|
1039
|
-
|
1040
|
-
return Qnil;
|
1041
|
-
}
|
1042
|
-
|
1043
|
-
/*
|
1044
|
-
* Public: Gets the fence info of the current node (must be a `:code_block`).
|
1045
|
-
*
|
1046
|
-
* Returns a {String} representing the fence info.
|
1047
|
-
* Raises a NodeError if the fence info can't be retrieved.
|
1048
|
-
*/
|
1049
|
-
static VALUE rb_node_get_fence_info(VALUE self) {
|
1050
|
-
const char *fence_info;
|
1051
|
-
cmark_node *node;
|
1052
|
-
Data_Get_Struct(self, cmark_node, node);
|
1053
|
-
|
1054
|
-
fence_info = cmark_node_get_fence_info(node);
|
1055
|
-
|
1056
|
-
if (fence_info == NULL) {
|
1057
|
-
rb_raise(rb_eNodeError, "could not get fence_info");
|
1058
|
-
}
|
1059
|
-
|
1060
|
-
return rb_str_new2(fence_info);
|
1061
|
-
}
|
1062
|
-
|
1063
|
-
/*
|
1064
|
-
* Public: Sets the fence info of the current node (must be a `:code_block`).
|
1065
|
-
*
|
1066
|
-
* info - A {String} representing the new fence info
|
1067
|
-
*
|
1068
|
-
* Raises a NodeError if the fence info can't be set.
|
1069
|
-
*/
|
1070
|
-
static VALUE rb_node_set_fence_info(VALUE self, VALUE info) {
|
1071
|
-
char *text;
|
1072
|
-
cmark_node *node;
|
1073
|
-
Check_Type(info, T_STRING);
|
1074
|
-
|
1075
|
-
Data_Get_Struct(self, cmark_node, node);
|
1076
|
-
text = StringValueCStr(info);
|
1077
|
-
|
1078
|
-
if (!cmark_node_set_fence_info(node, text)) {
|
1079
|
-
rb_raise(rb_eNodeError, "could not set fence_info");
|
1080
|
-
}
|
1081
|
-
|
1082
|
-
return Qnil;
|
1083
|
-
}
|
1084
|
-
|
1085
|
-
static VALUE rb_node_get_tasklist_item_checked(VALUE self) {
|
1086
|
-
int tasklist_state;
|
1087
|
-
cmark_node *node;
|
1088
|
-
Data_Get_Struct(self, cmark_node, node);
|
1089
|
-
|
1090
|
-
tasklist_state = cmark_gfm_extensions_get_tasklist_item_checked(node);
|
1091
|
-
|
1092
|
-
if (tasklist_state == 1) {
|
1093
|
-
return Qtrue;
|
1094
|
-
} else {
|
1095
|
-
return Qfalse;
|
1096
|
-
}
|
1097
|
-
}
|
1098
|
-
|
1099
|
-
/*
|
1100
|
-
* Public: Sets the checkbox state of the current node (must be a `:tasklist`).
|
1101
|
-
*
|
1102
|
-
* item_checked - A {Boolean} representing the new checkbox state
|
1103
|
-
*
|
1104
|
-
* Returns a {Boolean} representing the new checkbox state.
|
1105
|
-
* Raises a NodeError if the checkbox state can't be set.
|
1106
|
-
*/
|
1107
|
-
static VALUE rb_node_set_tasklist_item_checked(VALUE self, VALUE item_checked) {
|
1108
|
-
int tasklist_state;
|
1109
|
-
cmark_node *node;
|
1110
|
-
Data_Get_Struct(self, cmark_node, node);
|
1111
|
-
tasklist_state = RTEST(item_checked);
|
1112
|
-
|
1113
|
-
if (!cmark_gfm_extensions_set_tasklist_item_checked(node, tasklist_state)) {
|
1114
|
-
rb_raise(rb_eNodeError, "could not set tasklist_item_checked");
|
1115
|
-
};
|
1116
|
-
|
1117
|
-
if (tasklist_state) {
|
1118
|
-
return Qtrue;
|
1119
|
-
} else {
|
1120
|
-
return Qfalse;
|
1121
|
-
}
|
1122
|
-
}
|
1123
|
-
|
1124
|
-
// TODO: remove this, superseded by the above method
|
1125
|
-
static VALUE rb_node_get_tasklist_state(VALUE self) {
|
1126
|
-
int tasklist_state;
|
1127
|
-
cmark_node *node;
|
1128
|
-
Data_Get_Struct(self, cmark_node, node);
|
1129
|
-
|
1130
|
-
tasklist_state = cmark_gfm_extensions_get_tasklist_item_checked(node);
|
1131
|
-
|
1132
|
-
if (tasklist_state == 1) {
|
1133
|
-
return rb_str_new2("checked");
|
1134
|
-
} else {
|
1135
|
-
return rb_str_new2("unchecked");
|
1136
|
-
}
|
1137
|
-
}
|
1138
|
-
|
1139
|
-
static VALUE rb_node_get_table_alignments(VALUE self) {
|
1140
|
-
uint16_t column_count, i;
|
1141
|
-
uint8_t *alignments;
|
1142
|
-
cmark_node *node;
|
1143
|
-
VALUE ary;
|
1144
|
-
Data_Get_Struct(self, cmark_node, node);
|
1145
|
-
|
1146
|
-
column_count = cmark_gfm_extensions_get_table_columns(node);
|
1147
|
-
alignments = cmark_gfm_extensions_get_table_alignments(node);
|
1148
|
-
|
1149
|
-
if (!column_count || !alignments) {
|
1150
|
-
rb_raise(rb_eNodeError, "could not get column_count or alignments");
|
1151
|
-
}
|
1152
|
-
|
1153
|
-
ary = rb_ary_new();
|
1154
|
-
for (i = 0; i < column_count; ++i) {
|
1155
|
-
if (alignments[i] == 'l')
|
1156
|
-
rb_ary_push(ary, sym_left);
|
1157
|
-
else if (alignments[i] == 'c')
|
1158
|
-
rb_ary_push(ary, sym_center);
|
1159
|
-
else if (alignments[i] == 'r')
|
1160
|
-
rb_ary_push(ary, sym_right);
|
1161
|
-
else
|
1162
|
-
rb_ary_push(ary, Qnil);
|
1163
|
-
}
|
1164
|
-
return ary;
|
1165
|
-
}
|
1166
|
-
|
1167
|
-
/* Internal: Escapes href URLs safely. */
|
1168
|
-
static VALUE rb_html_escape_href(VALUE self, VALUE rb_text) {
|
1169
|
-
char *result;
|
1170
|
-
cmark_node *node;
|
1171
|
-
Check_Type(rb_text, T_STRING);
|
1172
|
-
|
1173
|
-
Data_Get_Struct(self, cmark_node, node);
|
1174
|
-
|
1175
|
-
cmark_mem *mem = cmark_node_mem(node);
|
1176
|
-
cmark_strbuf buf = CMARK_BUF_INIT(mem);
|
1177
|
-
|
1178
|
-
if (houdini_escape_href(&buf, (const uint8_t *)RSTRING_PTR(rb_text),
|
1179
|
-
RSTRING_LEN(rb_text))) {
|
1180
|
-
result = (char *)cmark_strbuf_detach(&buf);
|
1181
|
-
return encode_source_string(result, rb_text);
|
1182
|
-
|
1183
|
-
}
|
1184
|
-
|
1185
|
-
return rb_text;
|
1186
|
-
}
|
1187
|
-
|
1188
|
-
/* Internal: Escapes HTML content safely. */
|
1189
|
-
static VALUE rb_html_escape_html(VALUE self, VALUE rb_text) {
|
1190
|
-
char *result;
|
1191
|
-
cmark_node *node;
|
1192
|
-
Check_Type(rb_text, T_STRING);
|
1193
|
-
|
1194
|
-
Data_Get_Struct(self, cmark_node, node);
|
1195
|
-
|
1196
|
-
cmark_mem *mem = cmark_node_mem(node);
|
1197
|
-
cmark_strbuf buf = CMARK_BUF_INIT(mem);
|
1198
|
-
|
1199
|
-
if (houdini_escape_html0(&buf, (const uint8_t *)RSTRING_PTR(rb_text),
|
1200
|
-
RSTRING_LEN(rb_text), 0)) {
|
1201
|
-
result = (char *)cmark_strbuf_detach(&buf);
|
1202
|
-
return encode_source_string(result, rb_text);
|
1203
|
-
}
|
1204
|
-
|
1205
|
-
return rb_text;
|
1206
|
-
}
|
1207
|
-
|
1208
|
-
VALUE rb_extensions(VALUE self) {
|
1209
|
-
cmark_llist *exts, *it;
|
1210
|
-
cmark_syntax_extension *ext;
|
1211
|
-
VALUE ary = rb_ary_new();
|
1212
|
-
|
1213
|
-
cmark_mem *mem = cmark_get_default_mem_allocator();
|
1214
|
-
exts = cmark_list_syntax_extensions(mem);
|
1215
|
-
for (it = exts; it; it = it->next) {
|
1216
|
-
ext = it->data;
|
1217
|
-
rb_ary_push(ary, rb_str_new2(ext->name));
|
1218
|
-
}
|
1219
|
-
cmark_llist_free(mem, exts);
|
1220
|
-
|
1221
|
-
return ary;
|
1222
|
-
}
|
1223
|
-
|
1224
|
-
__attribute__((visibility("default"))) void Init_commonmarker() {
|
1225
|
-
VALUE module;
|
1226
|
-
sym_document = ID2SYM(rb_intern("document"));
|
1227
|
-
sym_blockquote = ID2SYM(rb_intern("blockquote"));
|
1228
|
-
sym_list = ID2SYM(rb_intern("list"));
|
1229
|
-
sym_list_item = ID2SYM(rb_intern("list_item"));
|
1230
|
-
sym_code_block = ID2SYM(rb_intern("code_block"));
|
1231
|
-
sym_html = ID2SYM(rb_intern("html"));
|
1232
|
-
sym_paragraph = ID2SYM(rb_intern("paragraph"));
|
1233
|
-
sym_header = ID2SYM(rb_intern("header"));
|
1234
|
-
sym_hrule = ID2SYM(rb_intern("hrule"));
|
1235
|
-
sym_text = ID2SYM(rb_intern("text"));
|
1236
|
-
sym_softbreak = ID2SYM(rb_intern("softbreak"));
|
1237
|
-
sym_linebreak = ID2SYM(rb_intern("linebreak"));
|
1238
|
-
sym_code = ID2SYM(rb_intern("code"));
|
1239
|
-
sym_inline_html = ID2SYM(rb_intern("inline_html"));
|
1240
|
-
sym_emph = ID2SYM(rb_intern("emph"));
|
1241
|
-
sym_strong = ID2SYM(rb_intern("strong"));
|
1242
|
-
sym_link = ID2SYM(rb_intern("link"));
|
1243
|
-
sym_image = ID2SYM(rb_intern("image"));
|
1244
|
-
sym_footnote_reference = ID2SYM(rb_intern("footnote_reference"));
|
1245
|
-
sym_footnote_definition = ID2SYM(rb_intern("footnote_definition"));
|
1246
|
-
|
1247
|
-
sym_bullet_list = ID2SYM(rb_intern("bullet_list"));
|
1248
|
-
sym_ordered_list = ID2SYM(rb_intern("ordered_list"));
|
1249
|
-
|
1250
|
-
sym_left = ID2SYM(rb_intern("left"));
|
1251
|
-
sym_right = ID2SYM(rb_intern("right"));
|
1252
|
-
sym_center = ID2SYM(rb_intern("center"));
|
1253
|
-
|
1254
|
-
module = rb_define_module("CommonMarker");
|
1255
|
-
rb_define_singleton_method(module, "extensions", rb_extensions, 0);
|
1256
|
-
rb_eNodeError = rb_define_class_under(module, "NodeError", rb_eStandardError);
|
1257
|
-
rb_cNode = rb_define_class_under(module, "Node", rb_cObject);
|
1258
|
-
rb_undef_alloc_func(rb_cNode);
|
1259
|
-
rb_define_singleton_method(rb_cNode, "markdown_to_html", rb_markdown_to_html,
|
1260
|
-
3);
|
1261
|
-
rb_define_singleton_method(rb_cNode, "markdown_to_xml", rb_markdown_to_xml,
|
1262
|
-
3);
|
1263
|
-
rb_define_singleton_method(rb_cNode, "new", rb_node_new, 1);
|
1264
|
-
rb_define_singleton_method(rb_cNode, "parse_document", rb_parse_document, 4);
|
1265
|
-
rb_define_method(rb_cNode, "string_content", rb_node_get_string_content, 0);
|
1266
|
-
rb_define_method(rb_cNode, "string_content=", rb_node_set_string_content, 1);
|
1267
|
-
rb_define_method(rb_cNode, "type", rb_node_get_type, 0);
|
1268
|
-
rb_define_method(rb_cNode, "type_string", rb_node_get_type_string, 0);
|
1269
|
-
rb_define_method(rb_cNode, "sourcepos", rb_node_get_sourcepos, 0);
|
1270
|
-
rb_define_method(rb_cNode, "delete", rb_node_unlink, 0);
|
1271
|
-
rb_define_method(rb_cNode, "first_child", rb_node_first_child, 0);
|
1272
|
-
rb_define_method(rb_cNode, "next", rb_node_next, 0);
|
1273
|
-
rb_define_method(rb_cNode, "insert_before", rb_node_insert_before, 1);
|
1274
|
-
rb_define_method(rb_cNode, "_render_html", rb_render_html, 2);
|
1275
|
-
rb_define_method(rb_cNode, "_render_xml", rb_render_xml, 1);
|
1276
|
-
rb_define_method(rb_cNode, "_render_commonmark", rb_render_commonmark, -1);
|
1277
|
-
rb_define_method(rb_cNode, "_render_plaintext", rb_render_plaintext, -1);
|
1278
|
-
rb_define_method(rb_cNode, "insert_after", rb_node_insert_after, 1);
|
1279
|
-
rb_define_method(rb_cNode, "prepend_child", rb_node_prepend_child, 1);
|
1280
|
-
rb_define_method(rb_cNode, "append_child", rb_node_append_child, 1);
|
1281
|
-
rb_define_method(rb_cNode, "last_child", rb_node_last_child, 0);
|
1282
|
-
rb_define_method(rb_cNode, "parent", rb_node_parent, 0);
|
1283
|
-
rb_define_method(rb_cNode, "previous", rb_node_previous, 0);
|
1284
|
-
rb_define_method(rb_cNode, "url", rb_node_get_url, 0);
|
1285
|
-
rb_define_method(rb_cNode, "url=", rb_node_set_url, 1);
|
1286
|
-
rb_define_method(rb_cNode, "title", rb_node_get_title, 0);
|
1287
|
-
rb_define_method(rb_cNode, "title=", rb_node_set_title, 1);
|
1288
|
-
rb_define_method(rb_cNode, "header_level", rb_node_get_header_level, 0);
|
1289
|
-
rb_define_method(rb_cNode, "header_level=", rb_node_set_header_level, 1);
|
1290
|
-
rb_define_method(rb_cNode, "list_type", rb_node_get_list_type, 0);
|
1291
|
-
rb_define_method(rb_cNode, "list_type=", rb_node_set_list_type, 1);
|
1292
|
-
rb_define_method(rb_cNode, "list_start", rb_node_get_list_start, 0);
|
1293
|
-
rb_define_method(rb_cNode, "list_start=", rb_node_set_list_start, 1);
|
1294
|
-
rb_define_method(rb_cNode, "list_tight", rb_node_get_list_tight, 0);
|
1295
|
-
rb_define_method(rb_cNode, "list_tight=", rb_node_set_list_tight, 1);
|
1296
|
-
rb_define_method(rb_cNode, "fence_info", rb_node_get_fence_info, 0);
|
1297
|
-
rb_define_method(rb_cNode, "fence_info=", rb_node_set_fence_info, 1);
|
1298
|
-
rb_define_method(rb_cNode, "table_alignments", rb_node_get_table_alignments, 0);
|
1299
|
-
rb_define_method(rb_cNode, "tasklist_state", rb_node_get_tasklist_state, 0);
|
1300
|
-
rb_define_method(rb_cNode, "tasklist_item_checked?", rb_node_get_tasklist_item_checked, 0);
|
1301
|
-
rb_define_method(rb_cNode, "tasklist_item_checked=", rb_node_set_tasklist_item_checked, 1);
|
1302
|
-
|
1303
|
-
rb_define_method(rb_cNode, "html_escape_href", rb_html_escape_href, 1);
|
1304
|
-
rb_define_method(rb_cNode, "html_escape_html", rb_html_escape_html, 1);
|
1305
|
-
|
1306
|
-
cmark_gfm_core_extensions_ensure_registered();
|
1307
|
-
cmark_init_standard_node_flags();
|
1308
|
-
}
|