commonmarker 0.23.10 → 1.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Cargo.lock +1182 -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 +103 -0
- data/ext/commonmarker/src/node.rs +1151 -0
- data/ext/commonmarker/src/options.rs +175 -0
- data/ext/commonmarker/src/plugins/syntax_highlighting.rs +166 -0
- data/ext/commonmarker/src/plugins.rs +6 -0
- data/ext/commonmarker/src/utils.rs +8 -0
- data/lib/commonmarker/config.rb +92 -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
@@ -0,0 +1,1151 @@
|
|
1
|
+
use comrak::arena_tree::Node as ComrakNode;
|
2
|
+
use comrak::nodes::{
|
3
|
+
Ast as ComrakAst, AstNode as ComrakAstNode, ListDelimType, ListType, NodeCode, NodeCodeBlock,
|
4
|
+
NodeDescriptionItem, NodeFootnoteDefinition, NodeFootnoteReference, NodeHeading, NodeHtmlBlock,
|
5
|
+
NodeLink, NodeList, NodeMath, NodeMultilineBlockQuote, NodeShortCode, NodeTable,
|
6
|
+
NodeValue as ComrakNodeValue, NodeWikiLink, TableAlignment,
|
7
|
+
};
|
8
|
+
use magnus::RArray;
|
9
|
+
use magnus::{function, method, scan_args, Module, Object, RHash, RModule, Symbol, Value};
|
10
|
+
use rctree::Node;
|
11
|
+
use typed_arena::Arena;
|
12
|
+
|
13
|
+
use std::cell::RefCell;
|
14
|
+
|
15
|
+
use crate::format_options;
|
16
|
+
|
17
|
+
use crate::plugins::syntax_highlighting::construct_syntax_highlighter_from_plugin;
|
18
|
+
|
19
|
+
#[derive(Debug, Clone)]
|
20
|
+
#[magnus::wrap(class = "Commonmarker::Node::Ast", size, mark)]
|
21
|
+
pub struct CommonmarkerAst {
|
22
|
+
data: ComrakAst,
|
23
|
+
}
|
24
|
+
|
25
|
+
#[derive(Debug, Clone)]
|
26
|
+
#[magnus::wrap(class = "Commonmarker::Node", size, mark)]
|
27
|
+
pub struct CommonmarkerNode {
|
28
|
+
inner: Node<CommonmarkerAst>,
|
29
|
+
}
|
30
|
+
|
31
|
+
/// SAFETY: This is safe because we only access this data when the GVL is held.
|
32
|
+
unsafe impl Send for CommonmarkerNode {}
|
33
|
+
|
34
|
+
impl CommonmarkerNode {
|
35
|
+
pub fn new(args: &[Value]) -> Result<Self, magnus::Error> {
|
36
|
+
let args = scan_args::scan_args::<_, (), (), (), _, ()>(args)?;
|
37
|
+
let (node_type,): (Symbol,) = args.required;
|
38
|
+
|
39
|
+
let node = match node_type.to_string().as_str() {
|
40
|
+
"document" => ComrakNodeValue::Document,
|
41
|
+
"block_quote" => ComrakNodeValue::BlockQuote,
|
42
|
+
"footnote_definition" => {
|
43
|
+
let kwargs = scan_args::get_kwargs::<_, (String,), (Option<u32>,), ()>(
|
44
|
+
args.keywords,
|
45
|
+
&["name"],
|
46
|
+
&["total_references"],
|
47
|
+
)?;
|
48
|
+
let (name,) = kwargs.required;
|
49
|
+
let (total_reference,) = kwargs.optional;
|
50
|
+
|
51
|
+
ComrakNodeValue::FootnoteDefinition(NodeFootnoteDefinition {
|
52
|
+
// The name of the footnote.
|
53
|
+
name,
|
54
|
+
// Total number of references to this footnote
|
55
|
+
total_references: total_reference.unwrap_or(1),
|
56
|
+
})
|
57
|
+
}
|
58
|
+
"list" => {
|
59
|
+
let kwargs = scan_args::get_kwargs::<
|
60
|
+
_,
|
61
|
+
(Symbol,),
|
62
|
+
(
|
63
|
+
Option<usize>,
|
64
|
+
Option<usize>,
|
65
|
+
Option<usize>,
|
66
|
+
Option<String>,
|
67
|
+
Option<u8>,
|
68
|
+
Option<bool>,
|
69
|
+
),
|
70
|
+
(),
|
71
|
+
>(
|
72
|
+
args.keywords,
|
73
|
+
&["type"],
|
74
|
+
&[
|
75
|
+
"marker_offset",
|
76
|
+
"padding",
|
77
|
+
"start",
|
78
|
+
"delimiter",
|
79
|
+
"bullet_char",
|
80
|
+
"tight",
|
81
|
+
],
|
82
|
+
)?;
|
83
|
+
|
84
|
+
let (list_type,) = kwargs.required;
|
85
|
+
let (marker_offset, padding, start, delimiter, bullet_char, tight) =
|
86
|
+
kwargs.optional;
|
87
|
+
|
88
|
+
let commonmark_list_type = list_type.to_string();
|
89
|
+
|
90
|
+
if commonmark_list_type != "bullet" && commonmark_list_type != "ordered" {
|
91
|
+
return Err(magnus::Error::new(
|
92
|
+
magnus::exception::arg_error(),
|
93
|
+
"list type must be `bullet` or `ordered`",
|
94
|
+
));
|
95
|
+
}
|
96
|
+
|
97
|
+
let comrak_list_type = if commonmark_list_type == "ordered" {
|
98
|
+
ListType::Ordered
|
99
|
+
} else {
|
100
|
+
ListType::Bullet
|
101
|
+
};
|
102
|
+
|
103
|
+
let comrak_delimiter = match delimiter.unwrap_or("".to_string()).as_str() {
|
104
|
+
")" => ListDelimType::Paren,
|
105
|
+
_ => ListDelimType::Period,
|
106
|
+
};
|
107
|
+
|
108
|
+
ComrakNodeValue::List(NodeList {
|
109
|
+
// The kind of list (bullet (unordered) or ordered).
|
110
|
+
list_type: comrak_list_type,
|
111
|
+
// Number of spaces before the list marker.
|
112
|
+
marker_offset: marker_offset.unwrap_or(0),
|
113
|
+
// Number of characters between the start of the list marker and the item text (including the list marker(s)).
|
114
|
+
padding: padding.unwrap_or(0),
|
115
|
+
// For ordered lists, the ordinal the list starts at.
|
116
|
+
start: start.unwrap_or(0),
|
117
|
+
// For ordered lists, the delimiter after each number.
|
118
|
+
delimiter: comrak_delimiter,
|
119
|
+
// For bullet lists, the character used for each bullet.
|
120
|
+
bullet_char: bullet_char.unwrap_or(0),
|
121
|
+
// Whether the list is [tight](https://github.github.com/gfm/#tight), i.e. whether the
|
122
|
+
// paragraphs are wrapped in `<p>` tags when formatted as HTML.
|
123
|
+
tight: tight.unwrap_or(false),
|
124
|
+
})
|
125
|
+
}
|
126
|
+
"description_list" => ComrakNodeValue::DescriptionList,
|
127
|
+
"description_item" => {
|
128
|
+
let kwargs = scan_args::get_kwargs::<_, (), (Option<usize>, Option<usize>), ()>(
|
129
|
+
args.keywords,
|
130
|
+
&[],
|
131
|
+
&["marker_offset", "padding"],
|
132
|
+
)?;
|
133
|
+
|
134
|
+
let (marker_offset, padding) = kwargs.optional;
|
135
|
+
|
136
|
+
ComrakNodeValue::DescriptionItem(NodeDescriptionItem {
|
137
|
+
// Number of spaces before the list marker.
|
138
|
+
marker_offset: marker_offset.unwrap_or(0),
|
139
|
+
// Number of characters between the start of the list marker and the item text (including the list marker(s)).
|
140
|
+
padding: padding.unwrap_or(0),
|
141
|
+
})
|
142
|
+
}
|
143
|
+
"description_term" => ComrakNodeValue::DescriptionTerm,
|
144
|
+
"description_details" => ComrakNodeValue::DescriptionDetails,
|
145
|
+
"code_block" => {
|
146
|
+
let kwargs = scan_args::get_kwargs::<
|
147
|
+
_,
|
148
|
+
(bool,),
|
149
|
+
(
|
150
|
+
Option<u8>,
|
151
|
+
Option<usize>,
|
152
|
+
Option<usize>,
|
153
|
+
Option<String>,
|
154
|
+
Option<String>,
|
155
|
+
),
|
156
|
+
(),
|
157
|
+
>(
|
158
|
+
args.keywords,
|
159
|
+
&["fenced"],
|
160
|
+
&[
|
161
|
+
"fence_char",
|
162
|
+
"fence_length",
|
163
|
+
"fence_offset",
|
164
|
+
"info",
|
165
|
+
"literal",
|
166
|
+
],
|
167
|
+
)?;
|
168
|
+
let (fenced,) = kwargs.required;
|
169
|
+
let (fence_char, fence_length, fence_offset, info, literal) = kwargs.optional;
|
170
|
+
|
171
|
+
ComrakNodeValue::CodeBlock(NodeCodeBlock {
|
172
|
+
// Whether the code block is fenced.
|
173
|
+
fenced,
|
174
|
+
// For fenced code blocks, the fence character itself (`` ` `` or `~`).
|
175
|
+
fence_char: fence_char.unwrap_or(b'`'),
|
176
|
+
// For fenced code blocks, the length of the fence.
|
177
|
+
fence_length: fence_length.unwrap_or(0),
|
178
|
+
// For fenced code blocks, the indentation level of the code within the block.
|
179
|
+
fence_offset: fence_offset.unwrap_or(0),
|
180
|
+
|
181
|
+
// For fenced code blocks, the [info string](https://github.github.com/gfm/#info-string) after
|
182
|
+
// the opening fence, if any.
|
183
|
+
info: info.unwrap_or(String::with_capacity(10)),
|
184
|
+
|
185
|
+
// The literal contents of the code block. As the contents are not interpreted as Markdown at
|
186
|
+
// all, they are contained within this structure, rather than inserted into a child inline of
|
187
|
+
// any kind.
|
188
|
+
literal: literal.unwrap_or(String::new()),
|
189
|
+
})
|
190
|
+
}
|
191
|
+
"html_block" => {
|
192
|
+
let kwargs = scan_args::get_kwargs::<_, (), (Option<u8>, Option<String>), ()>(
|
193
|
+
args.keywords,
|
194
|
+
&[],
|
195
|
+
&["block_type", "literal"],
|
196
|
+
)?;
|
197
|
+
|
198
|
+
let (block_type, literal) = kwargs.optional;
|
199
|
+
|
200
|
+
ComrakNodeValue::HtmlBlock(NodeHtmlBlock {
|
201
|
+
// Number of spaces before the list marker.
|
202
|
+
block_type: block_type.unwrap_or(0),
|
203
|
+
// Number of characters between the start of the list marker and the item text (including the list marker(s)).
|
204
|
+
literal: literal.unwrap_or(String::new()),
|
205
|
+
})
|
206
|
+
}
|
207
|
+
"paragraph" => ComrakNodeValue::Paragraph,
|
208
|
+
"heading" => {
|
209
|
+
let kwargs = scan_args::get_kwargs::<_, (u8,), (Option<bool>,), ()>(
|
210
|
+
args.keywords,
|
211
|
+
&["level"],
|
212
|
+
&["setext"],
|
213
|
+
)?;
|
214
|
+
|
215
|
+
let (level,) = kwargs.required;
|
216
|
+
let (setext,) = kwargs.optional;
|
217
|
+
|
218
|
+
ComrakNodeValue::Heading(NodeHeading {
|
219
|
+
// Number of spaces before the list marker.
|
220
|
+
level,
|
221
|
+
// Number of characters between the start of the list marker and the item text (including the list marker(s)).
|
222
|
+
setext: setext.unwrap_or(false),
|
223
|
+
})
|
224
|
+
}
|
225
|
+
"thematic_break" => ComrakNodeValue::ThematicBreak,
|
226
|
+
"table" => {
|
227
|
+
let kwargs = scan_args::get_kwargs::<_, (RArray, usize, usize, usize), (), ()>(
|
228
|
+
args.keywords,
|
229
|
+
&[
|
230
|
+
"alignments",
|
231
|
+
"num_columns",
|
232
|
+
"num_rows",
|
233
|
+
"num_nonempty_cells",
|
234
|
+
],
|
235
|
+
&[],
|
236
|
+
)?;
|
237
|
+
|
238
|
+
let (alignments, num_columns, num_rows, num_nonempty_cells) = kwargs.required;
|
239
|
+
|
240
|
+
let mut comrak_alignments = vec![];
|
241
|
+
alignments.each().for_each(|alignment| {
|
242
|
+
match alignment.unwrap().to_string().as_str() {
|
243
|
+
"left" => {
|
244
|
+
comrak_alignments.push(TableAlignment::Left);
|
245
|
+
}
|
246
|
+
"right" => {
|
247
|
+
comrak_alignments.push(TableAlignment::Right);
|
248
|
+
}
|
249
|
+
"center" => {
|
250
|
+
comrak_alignments.push(TableAlignment::Center);
|
251
|
+
}
|
252
|
+
_ => {
|
253
|
+
comrak_alignments.push(TableAlignment::None);
|
254
|
+
}
|
255
|
+
}
|
256
|
+
});
|
257
|
+
ComrakNodeValue::Table(NodeTable {
|
258
|
+
// The table alignments
|
259
|
+
alignments: comrak_alignments,
|
260
|
+
|
261
|
+
// Number of columns of the table
|
262
|
+
num_columns,
|
263
|
+
|
264
|
+
// Number of rows of the table
|
265
|
+
num_rows,
|
266
|
+
|
267
|
+
// Number of non-empty, non-autocompleted cells
|
268
|
+
num_nonempty_cells,
|
269
|
+
})
|
270
|
+
}
|
271
|
+
"table_row" => {
|
272
|
+
let kwargs =
|
273
|
+
scan_args::get_kwargs::<_, (bool,), (), ()>(args.keywords, &["header"], &[])?;
|
274
|
+
|
275
|
+
let (header,) = kwargs.required;
|
276
|
+
|
277
|
+
ComrakNodeValue::TableRow(header)
|
278
|
+
}
|
279
|
+
"table_cell" => ComrakNodeValue::TableCell,
|
280
|
+
"text" => {
|
281
|
+
let kwargs = scan_args::get_kwargs::<_, (), (Option<String>,), ()>(
|
282
|
+
args.keywords,
|
283
|
+
&[],
|
284
|
+
&["content"],
|
285
|
+
)?;
|
286
|
+
|
287
|
+
let (content,) = kwargs.optional;
|
288
|
+
|
289
|
+
ComrakNodeValue::Text(content.unwrap_or("".to_string()))
|
290
|
+
}
|
291
|
+
"taskitem" => {
|
292
|
+
let kwargs = scan_args::get_kwargs::<_, (), (Option<char>,), ()>(
|
293
|
+
args.keywords,
|
294
|
+
&[],
|
295
|
+
&["mark"],
|
296
|
+
)?;
|
297
|
+
|
298
|
+
let (mark,) = kwargs.optional;
|
299
|
+
|
300
|
+
ComrakNodeValue::TaskItem(mark)
|
301
|
+
}
|
302
|
+
"softbreak" => ComrakNodeValue::SoftBreak,
|
303
|
+
"linebreak" => ComrakNodeValue::LineBreak,
|
304
|
+
"code" => {
|
305
|
+
let kwargs = scan_args::get_kwargs::<_, (), (Option<usize>, Option<String>), ()>(
|
306
|
+
args.keywords,
|
307
|
+
&[],
|
308
|
+
&["num_backticks", "literal"],
|
309
|
+
)?;
|
310
|
+
|
311
|
+
let (num_backticks, literal) = kwargs.optional;
|
312
|
+
|
313
|
+
ComrakNodeValue::Code(NodeCode {
|
314
|
+
// The number of backticks
|
315
|
+
num_backticks: num_backticks.unwrap_or(1),
|
316
|
+
// The content of the inline code span.
|
317
|
+
// As the contents are not interpreted as Markdown at all,
|
318
|
+
// they are contained within this structure,
|
319
|
+
// rather than inserted into a child inline of any kind
|
320
|
+
literal: literal.unwrap_or_default(),
|
321
|
+
})
|
322
|
+
}
|
323
|
+
"html_inline" => {
|
324
|
+
let kwargs = scan_args::get_kwargs::<_, (), (Option<String>,), ()>(
|
325
|
+
args.keywords,
|
326
|
+
&[],
|
327
|
+
&["content"],
|
328
|
+
)?;
|
329
|
+
|
330
|
+
let (content,) = kwargs.optional;
|
331
|
+
|
332
|
+
ComrakNodeValue::HtmlInline(content.unwrap_or_default())
|
333
|
+
}
|
334
|
+
"emph" => ComrakNodeValue::Emph,
|
335
|
+
"strong" => ComrakNodeValue::Strong,
|
336
|
+
"strikethrough" => ComrakNodeValue::Strikethrough,
|
337
|
+
"superscript" => ComrakNodeValue::Superscript,
|
338
|
+
"link" => {
|
339
|
+
let kwargs = scan_args::get_kwargs::<_, (String,), (Option<String>,), ()>(
|
340
|
+
args.keywords,
|
341
|
+
&["url"],
|
342
|
+
&["title"],
|
343
|
+
)?;
|
344
|
+
|
345
|
+
let (url,) = kwargs.required;
|
346
|
+
let (title,) = kwargs.optional;
|
347
|
+
|
348
|
+
ComrakNodeValue::Link(NodeLink {
|
349
|
+
// The URL for the link destination or image source.
|
350
|
+
url,
|
351
|
+
// The title for the link or image.
|
352
|
+
//
|
353
|
+
// Note this field is used for the `title` attribute by the HTML formatter even for images;
|
354
|
+
// `alt` text is supplied in the image inline text.
|
355
|
+
title: title.unwrap_or_default(),
|
356
|
+
})
|
357
|
+
}
|
358
|
+
"image" => {
|
359
|
+
let kwargs = scan_args::get_kwargs::<_, (String,), (Option<String>,), ()>(
|
360
|
+
args.keywords,
|
361
|
+
&["url"],
|
362
|
+
&["title"],
|
363
|
+
)?;
|
364
|
+
|
365
|
+
let (url,) = kwargs.required;
|
366
|
+
let (title,) = kwargs.optional;
|
367
|
+
|
368
|
+
ComrakNodeValue::Image(NodeLink {
|
369
|
+
// The URL for the link destination or image source.
|
370
|
+
url,
|
371
|
+
// The title for the link or image.
|
372
|
+
//
|
373
|
+
// Note this field is used for the `title` attribute by the HTML formatter even for images;
|
374
|
+
// `alt` text is supplied in the image inline text.
|
375
|
+
title: title.unwrap_or_default(),
|
376
|
+
})
|
377
|
+
}
|
378
|
+
"footnote_reference" => {
|
379
|
+
let kwargs = scan_args::get_kwargs::<_, (String,), (Option<u32>, Option<u32>), ()>(
|
380
|
+
args.keywords,
|
381
|
+
&["name"],
|
382
|
+
&["ref_num", "ix"],
|
383
|
+
)?;
|
384
|
+
|
385
|
+
let (name,) = kwargs.required;
|
386
|
+
let (ref_num, ix) = kwargs.optional;
|
387
|
+
|
388
|
+
ComrakNodeValue::FootnoteReference(NodeFootnoteReference {
|
389
|
+
// The name of the footnote.
|
390
|
+
name,
|
391
|
+
// The index of reference to the same footnote
|
392
|
+
ref_num: ref_num.unwrap_or(0),
|
393
|
+
// The index of the footnote in the document.
|
394
|
+
ix: ix.unwrap_or(0),
|
395
|
+
})
|
396
|
+
}
|
397
|
+
// #[cfg(feature = "shortcodes")]
|
398
|
+
"shortcode" => {
|
399
|
+
let kwargs =
|
400
|
+
scan_args::get_kwargs::<_, (String,), (), ()>(args.keywords, &["code"], &[])?;
|
401
|
+
|
402
|
+
let (code,) = kwargs.required;
|
403
|
+
|
404
|
+
match NodeShortCode::try_from(code.as_str()) {
|
405
|
+
Ok(shortcode) => ComrakNodeValue::ShortCode(shortcode),
|
406
|
+
_ => {
|
407
|
+
return Err(magnus::Error::new(
|
408
|
+
magnus::exception::arg_error(),
|
409
|
+
"list type must be `bullet` or `ordered`",
|
410
|
+
));
|
411
|
+
}
|
412
|
+
}
|
413
|
+
}
|
414
|
+
"math" => {
|
415
|
+
let kwargs = scan_args::get_kwargs::<_, (bool, bool, String), (), ()>(
|
416
|
+
args.keywords,
|
417
|
+
&["dollar_math", "display_math", "literal"],
|
418
|
+
&[],
|
419
|
+
)?;
|
420
|
+
|
421
|
+
let (dollar_math, display_math, literal) = kwargs.required;
|
422
|
+
|
423
|
+
ComrakNodeValue::Math(NodeMath {
|
424
|
+
// Whether this is dollar math (`$` or `$$`).
|
425
|
+
// `false` indicates it is code math
|
426
|
+
dollar_math,
|
427
|
+
|
428
|
+
// Whether this is display math (using `$$`)
|
429
|
+
display_math,
|
430
|
+
|
431
|
+
// The literal contents of the math span.
|
432
|
+
// As the contents are not interpreted as Markdown at all,
|
433
|
+
// they are contained within this structure,
|
434
|
+
// rather than inserted into a child inline of any kind.
|
435
|
+
literal,
|
436
|
+
})
|
437
|
+
}
|
438
|
+
"multiline_block_quote" => {
|
439
|
+
let kwargs = scan_args::get_kwargs::<_, (usize, usize), (), ()>(
|
440
|
+
args.keywords,
|
441
|
+
&["fence_length", "fence_offset"],
|
442
|
+
&[],
|
443
|
+
)?;
|
444
|
+
|
445
|
+
let (fence_length, fence_offset) = kwargs.required;
|
446
|
+
|
447
|
+
ComrakNodeValue::MultilineBlockQuote(NodeMultilineBlockQuote {
|
448
|
+
// The length of the fence.
|
449
|
+
fence_length,
|
450
|
+
// The indentation level of the fence marker.
|
451
|
+
fence_offset,
|
452
|
+
})
|
453
|
+
}
|
454
|
+
|
455
|
+
"escaped" => ComrakNodeValue::Escaped,
|
456
|
+
|
457
|
+
"wikilink" => {
|
458
|
+
let kwargs =
|
459
|
+
scan_args::get_kwargs::<_, (String,), (), ()>(args.keywords, &["url"], &[])?;
|
460
|
+
|
461
|
+
let (url,) = kwargs.required;
|
462
|
+
|
463
|
+
ComrakNodeValue::WikiLink(NodeWikiLink { url })
|
464
|
+
}
|
465
|
+
_ => panic!("unknown node type {}", node_type),
|
466
|
+
};
|
467
|
+
|
468
|
+
Ok(CommonmarkerNode {
|
469
|
+
inner: Node::new(CommonmarkerAst {
|
470
|
+
data: ComrakAst::new(node, (0, 0).into()),
|
471
|
+
}),
|
472
|
+
})
|
473
|
+
}
|
474
|
+
|
475
|
+
pub fn new_from_comrak_node<'a>(
|
476
|
+
comrak_root_node: &'a ComrakAstNode<'a>,
|
477
|
+
) -> Result<CommonmarkerNode, magnus::Error> {
|
478
|
+
let comrak_ast = comrak_root_node.data.clone().into_inner();
|
479
|
+
|
480
|
+
fn iter_nodes<'a>(comrak_node: &'a ComrakAstNode<'a>) -> CommonmarkerNode {
|
481
|
+
let comrak_node_ast = comrak_node.data.clone().into_inner();
|
482
|
+
let commonmark_node = CommonmarkerNode {
|
483
|
+
inner: Node::new(CommonmarkerAst {
|
484
|
+
data: comrak_node_ast,
|
485
|
+
}),
|
486
|
+
};
|
487
|
+
|
488
|
+
for c in comrak_node.children() {
|
489
|
+
match commonmark_node.append_child_node(&iter_nodes(c)) {
|
490
|
+
Ok(_) => {}
|
491
|
+
Err(e) => {
|
492
|
+
panic!("cannot append node: {}", e);
|
493
|
+
}
|
494
|
+
}
|
495
|
+
}
|
496
|
+
|
497
|
+
commonmark_node
|
498
|
+
}
|
499
|
+
|
500
|
+
let commonmarker_root_node = CommonmarkerNode {
|
501
|
+
inner: Node::new(CommonmarkerAst { data: comrak_ast }),
|
502
|
+
};
|
503
|
+
|
504
|
+
for child in comrak_root_node.children() {
|
505
|
+
let new_child = iter_nodes(child);
|
506
|
+
|
507
|
+
commonmarker_root_node.append_child_node(&new_child)?;
|
508
|
+
}
|
509
|
+
|
510
|
+
Ok(commonmarker_root_node)
|
511
|
+
}
|
512
|
+
|
513
|
+
fn type_to_symbol(&self) -> Symbol {
|
514
|
+
let node = self.inner.borrow();
|
515
|
+
match node.data.value {
|
516
|
+
ComrakNodeValue::Document => Symbol::new("document"),
|
517
|
+
ComrakNodeValue::BlockQuote => Symbol::new("block_quote"),
|
518
|
+
ComrakNodeValue::FootnoteDefinition(_) => Symbol::new("footnote_definition"),
|
519
|
+
ComrakNodeValue::List(..) => Symbol::new("list"),
|
520
|
+
ComrakNodeValue::DescriptionList => Symbol::new("description_list"),
|
521
|
+
ComrakNodeValue::DescriptionItem(_) => Symbol::new("description_item"),
|
522
|
+
ComrakNodeValue::DescriptionTerm => Symbol::new("description_term"),
|
523
|
+
ComrakNodeValue::DescriptionDetails => Symbol::new("description_details"),
|
524
|
+
ComrakNodeValue::Item(..) => Symbol::new("item"),
|
525
|
+
ComrakNodeValue::CodeBlock(..) => Symbol::new("code_block"),
|
526
|
+
ComrakNodeValue::HtmlBlock(..) => Symbol::new("html_block"),
|
527
|
+
ComrakNodeValue::Paragraph => Symbol::new("paragraph"),
|
528
|
+
ComrakNodeValue::Heading(..) => Symbol::new("heading"),
|
529
|
+
ComrakNodeValue::ThematicBreak => Symbol::new("thematic_break"),
|
530
|
+
ComrakNodeValue::Table(..) => Symbol::new("table"),
|
531
|
+
ComrakNodeValue::TableRow(..) => Symbol::new("table_row"),
|
532
|
+
ComrakNodeValue::TableCell => Symbol::new("table_cell"),
|
533
|
+
ComrakNodeValue::Text(..) => Symbol::new("text"),
|
534
|
+
ComrakNodeValue::SoftBreak => Symbol::new("softbreak"),
|
535
|
+
ComrakNodeValue::LineBreak => Symbol::new("linebreak"),
|
536
|
+
ComrakNodeValue::Image(..) => Symbol::new("image"),
|
537
|
+
ComrakNodeValue::Link(..) => Symbol::new("link"),
|
538
|
+
ComrakNodeValue::Emph => Symbol::new("emph"),
|
539
|
+
ComrakNodeValue::Strong => Symbol::new("strong"),
|
540
|
+
ComrakNodeValue::Code(..) => Symbol::new("code"),
|
541
|
+
ComrakNodeValue::HtmlInline(..) => Symbol::new("html_inline"),
|
542
|
+
ComrakNodeValue::Strikethrough => Symbol::new("strikethrough"),
|
543
|
+
ComrakNodeValue::FrontMatter(_) => Symbol::new("frontmatter"),
|
544
|
+
ComrakNodeValue::TaskItem { .. } => Symbol::new("taskitem"),
|
545
|
+
ComrakNodeValue::Superscript => Symbol::new("superscript"),
|
546
|
+
ComrakNodeValue::FootnoteReference(..) => Symbol::new("footnote_reference"),
|
547
|
+
ComrakNodeValue::ShortCode(_) => Symbol::new("shortcode"),
|
548
|
+
ComrakNodeValue::MultilineBlockQuote(_) => Symbol::new("multiline_block_quote"),
|
549
|
+
ComrakNodeValue::Escaped => Symbol::new("escaped"),
|
550
|
+
ComrakNodeValue::Math(..) => Symbol::new("math"),
|
551
|
+
ComrakNodeValue::WikiLink(..) => Symbol::new("wikilink"),
|
552
|
+
}
|
553
|
+
}
|
554
|
+
|
555
|
+
fn get_parent(&self) -> Option<CommonmarkerNode> {
|
556
|
+
self.inner.parent().map(|n| CommonmarkerNode { inner: n })
|
557
|
+
}
|
558
|
+
|
559
|
+
fn get_previous_sibling(&self) -> Option<CommonmarkerNode> {
|
560
|
+
self.inner
|
561
|
+
.previous_sibling()
|
562
|
+
.map(|n| CommonmarkerNode { inner: n })
|
563
|
+
}
|
564
|
+
|
565
|
+
fn get_next_sibling(&self) -> Option<CommonmarkerNode> {
|
566
|
+
self.inner
|
567
|
+
.next_sibling()
|
568
|
+
.map(|n| CommonmarkerNode { inner: n })
|
569
|
+
}
|
570
|
+
|
571
|
+
fn get_first_child(&self) -> Option<CommonmarkerNode> {
|
572
|
+
self.inner
|
573
|
+
.first_child()
|
574
|
+
.map(|n| CommonmarkerNode { inner: n })
|
575
|
+
}
|
576
|
+
|
577
|
+
fn get_last_child(&self) -> Option<CommonmarkerNode> {
|
578
|
+
self.inner
|
579
|
+
.last_child()
|
580
|
+
.map(|n| CommonmarkerNode { inner: n })
|
581
|
+
}
|
582
|
+
|
583
|
+
fn prepend_child_node(&self, new_child: &CommonmarkerNode) -> Result<bool, magnus::Error> {
|
584
|
+
let node = new_child.inner.clone();
|
585
|
+
node.detach();
|
586
|
+
self.inner.prepend(node);
|
587
|
+
|
588
|
+
Ok(true)
|
589
|
+
}
|
590
|
+
|
591
|
+
fn append_child_node(&self, new_child: &CommonmarkerNode) -> Result<bool, magnus::Error> {
|
592
|
+
let node = new_child.inner.clone();
|
593
|
+
node.detach();
|
594
|
+
self.inner.append(node);
|
595
|
+
|
596
|
+
Ok(true)
|
597
|
+
}
|
598
|
+
|
599
|
+
fn detach_node(&self) -> Result<CommonmarkerNode, magnus::Error> {
|
600
|
+
let node = self.inner.make_copy().borrow().data.clone();
|
601
|
+
self.inner.detach();
|
602
|
+
|
603
|
+
Ok(CommonmarkerNode {
|
604
|
+
inner: Node::new(CommonmarkerAst { data: node }),
|
605
|
+
})
|
606
|
+
}
|
607
|
+
|
608
|
+
fn get_sourcepos(&self) -> Result<RHash, magnus::Error> {
|
609
|
+
let node = self.inner.borrow();
|
610
|
+
|
611
|
+
let result = RHash::new();
|
612
|
+
result.aset(Symbol::new("start_line"), node.data.sourcepos.start.line)?;
|
613
|
+
result.aset(
|
614
|
+
Symbol::new("start_column"),
|
615
|
+
node.data.sourcepos.start.column,
|
616
|
+
)?;
|
617
|
+
result.aset(Symbol::new("end_line"), node.data.sourcepos.end.line)?;
|
618
|
+
result.aset(Symbol::new("end_column"), node.data.sourcepos.end.column)?;
|
619
|
+
|
620
|
+
Ok(result)
|
621
|
+
}
|
622
|
+
|
623
|
+
fn replace_node(&self, new_node: &CommonmarkerNode) -> Result<bool, magnus::Error> {
|
624
|
+
self.insert_node_after(new_node)?;
|
625
|
+
match self.detach_node() {
|
626
|
+
Ok(_) => Ok(true),
|
627
|
+
Err(e) => Err(e),
|
628
|
+
}
|
629
|
+
}
|
630
|
+
|
631
|
+
fn insert_node_before(&self, new_sibling: &CommonmarkerNode) -> Result<bool, magnus::Error> {
|
632
|
+
let node = new_sibling.inner.clone();
|
633
|
+
node.detach();
|
634
|
+
self.inner.insert_before(node);
|
635
|
+
|
636
|
+
Ok(true)
|
637
|
+
}
|
638
|
+
|
639
|
+
fn insert_node_after(&self, new_sibling: &CommonmarkerNode) -> Result<bool, magnus::Error> {
|
640
|
+
let node = new_sibling.inner.clone();
|
641
|
+
node.detach();
|
642
|
+
self.inner.insert_after(node);
|
643
|
+
|
644
|
+
Ok(true)
|
645
|
+
}
|
646
|
+
|
647
|
+
fn get_url(&self) -> Result<String, magnus::Error> {
|
648
|
+
let node = self.inner.borrow();
|
649
|
+
|
650
|
+
match &node.data.value {
|
651
|
+
ComrakNodeValue::Link(link) => Ok(link.url.to_string()),
|
652
|
+
ComrakNodeValue::Image(image) => Ok(image.url.to_string()),
|
653
|
+
_ => Err(magnus::Error::new(
|
654
|
+
magnus::exception::type_error(),
|
655
|
+
"node is not an image or link node",
|
656
|
+
)),
|
657
|
+
}
|
658
|
+
}
|
659
|
+
|
660
|
+
fn set_url(&self, new_url: String) -> Result<bool, magnus::Error> {
|
661
|
+
let mut node = self.inner.borrow_mut();
|
662
|
+
|
663
|
+
match node.data.value {
|
664
|
+
ComrakNodeValue::Link(ref mut link) => {
|
665
|
+
link.url = new_url;
|
666
|
+
Ok(true)
|
667
|
+
}
|
668
|
+
ComrakNodeValue::Image(ref mut image) => {
|
669
|
+
image.url = new_url;
|
670
|
+
Ok(true)
|
671
|
+
}
|
672
|
+
_ => Err(magnus::Error::new(
|
673
|
+
magnus::exception::type_error(),
|
674
|
+
"node is not an image or link node",
|
675
|
+
)),
|
676
|
+
}
|
677
|
+
}
|
678
|
+
|
679
|
+
fn get_string_content(&self) -> Result<String, magnus::Error> {
|
680
|
+
let node = self.inner.borrow();
|
681
|
+
|
682
|
+
match node.data.value {
|
683
|
+
ComrakNodeValue::Code(ref code) => return Ok(code.literal.to_string()),
|
684
|
+
ComrakNodeValue::CodeBlock(ref code_block) => {
|
685
|
+
return Ok(code_block.literal.to_string())
|
686
|
+
}
|
687
|
+
_ => {}
|
688
|
+
}
|
689
|
+
|
690
|
+
match node.data.value.text() {
|
691
|
+
Some(s) => Ok(s.to_string()),
|
692
|
+
None => Err(magnus::Error::new(
|
693
|
+
magnus::exception::type_error(),
|
694
|
+
"node does not have string content",
|
695
|
+
)),
|
696
|
+
}
|
697
|
+
}
|
698
|
+
|
699
|
+
fn set_string_content(&self, new_content: String) -> Result<bool, magnus::Error> {
|
700
|
+
let mut node = self.inner.borrow_mut();
|
701
|
+
|
702
|
+
match node.data.value {
|
703
|
+
ComrakNodeValue::Code(ref mut code) => {
|
704
|
+
code.literal = new_content;
|
705
|
+
return Ok(true);
|
706
|
+
}
|
707
|
+
ComrakNodeValue::CodeBlock(ref mut code_block) => {
|
708
|
+
code_block.literal = new_content;
|
709
|
+
return Ok(true);
|
710
|
+
}
|
711
|
+
_ => {}
|
712
|
+
}
|
713
|
+
|
714
|
+
match node.data.value.text_mut() {
|
715
|
+
Some(s) => {
|
716
|
+
*s = new_content;
|
717
|
+
Ok(true)
|
718
|
+
}
|
719
|
+
None => Err(magnus::Error::new(
|
720
|
+
magnus::exception::type_error(),
|
721
|
+
"node does not have string content",
|
722
|
+
)),
|
723
|
+
}
|
724
|
+
}
|
725
|
+
|
726
|
+
fn get_title(&self) -> Result<String, magnus::Error> {
|
727
|
+
let node = self.inner.borrow();
|
728
|
+
|
729
|
+
match &node.data.value {
|
730
|
+
ComrakNodeValue::Link(link) => Ok(link.title.to_string()),
|
731
|
+
ComrakNodeValue::Image(image) => Ok(image.title.to_string()),
|
732
|
+
_ => Err(magnus::Error::new(
|
733
|
+
magnus::exception::type_error(),
|
734
|
+
"node is not an image or link node",
|
735
|
+
)),
|
736
|
+
}
|
737
|
+
}
|
738
|
+
|
739
|
+
fn set_title(&self, new_title: String) -> Result<bool, magnus::Error> {
|
740
|
+
let mut node = self.inner.borrow_mut();
|
741
|
+
|
742
|
+
match node.data.value {
|
743
|
+
ComrakNodeValue::Link(ref mut link) => {
|
744
|
+
link.title = new_title;
|
745
|
+
Ok(true)
|
746
|
+
}
|
747
|
+
ComrakNodeValue::Image(ref mut image) => {
|
748
|
+
image.title = new_title;
|
749
|
+
Ok(true)
|
750
|
+
}
|
751
|
+
_ => Err(magnus::Error::new(
|
752
|
+
magnus::exception::type_error(),
|
753
|
+
"node is not an image or link node",
|
754
|
+
)),
|
755
|
+
}
|
756
|
+
}
|
757
|
+
|
758
|
+
fn get_header_level(&self) -> Result<u8, magnus::Error> {
|
759
|
+
let node = self.inner.borrow();
|
760
|
+
|
761
|
+
match &node.data.value {
|
762
|
+
ComrakNodeValue::Heading(heading) => Ok(heading.level),
|
763
|
+
_ => Err(magnus::Error::new(
|
764
|
+
magnus::exception::type_error(),
|
765
|
+
"node is not a heading node",
|
766
|
+
)),
|
767
|
+
}
|
768
|
+
}
|
769
|
+
|
770
|
+
fn set_header_level(&self, new_level: u8) -> Result<bool, magnus::Error> {
|
771
|
+
let mut node = self.inner.borrow_mut();
|
772
|
+
|
773
|
+
match node.data.value {
|
774
|
+
ComrakNodeValue::Heading(ref mut heading) => {
|
775
|
+
heading.level = new_level;
|
776
|
+
Ok(true)
|
777
|
+
}
|
778
|
+
_ => Err(magnus::Error::new(
|
779
|
+
magnus::exception::type_error(),
|
780
|
+
"node is not a heading node",
|
781
|
+
)),
|
782
|
+
}
|
783
|
+
}
|
784
|
+
|
785
|
+
fn get_list_type(&self) -> Result<Symbol, magnus::Error> {
|
786
|
+
let node = self.inner.borrow();
|
787
|
+
|
788
|
+
match &node.data.value {
|
789
|
+
ComrakNodeValue::List(list) => match list.list_type {
|
790
|
+
comrak::nodes::ListType::Bullet => Ok(Symbol::new("bullet")),
|
791
|
+
comrak::nodes::ListType::Ordered => Ok(Symbol::new("ordered")),
|
792
|
+
},
|
793
|
+
_ => Err(magnus::Error::new(
|
794
|
+
magnus::exception::type_error(),
|
795
|
+
"node is not a list node",
|
796
|
+
)),
|
797
|
+
}
|
798
|
+
}
|
799
|
+
|
800
|
+
fn set_list_type(&self, new_type: Symbol) -> Result<bool, magnus::Error> {
|
801
|
+
let mut node = self.inner.borrow_mut();
|
802
|
+
|
803
|
+
match node.data.value {
|
804
|
+
ComrakNodeValue::List(ref mut list) => {
|
805
|
+
match new_type.to_string().as_str() {
|
806
|
+
"bullet" => list.list_type = comrak::nodes::ListType::Bullet,
|
807
|
+
"ordered" => list.list_type = comrak::nodes::ListType::Ordered,
|
808
|
+
_ => return Ok(false),
|
809
|
+
}
|
810
|
+
Ok(true)
|
811
|
+
}
|
812
|
+
_ => Err(magnus::Error::new(
|
813
|
+
magnus::exception::type_error(),
|
814
|
+
"node is not a list node",
|
815
|
+
)),
|
816
|
+
}
|
817
|
+
}
|
818
|
+
|
819
|
+
fn get_list_start(&self) -> Result<usize, magnus::Error> {
|
820
|
+
let node = self.inner.borrow();
|
821
|
+
|
822
|
+
match &node.data.value {
|
823
|
+
ComrakNodeValue::List(list) => Ok(list.start),
|
824
|
+
_ => Err(magnus::Error::new(
|
825
|
+
magnus::exception::type_error(),
|
826
|
+
"node is not a list node",
|
827
|
+
)),
|
828
|
+
}
|
829
|
+
}
|
830
|
+
|
831
|
+
fn set_list_start(&self, new_start: usize) -> Result<bool, magnus::Error> {
|
832
|
+
let mut node = self.inner.borrow_mut();
|
833
|
+
|
834
|
+
match node.data.value {
|
835
|
+
ComrakNodeValue::List(ref mut list) => {
|
836
|
+
list.start = new_start;
|
837
|
+
Ok(true)
|
838
|
+
}
|
839
|
+
_ => Err(magnus::Error::new(
|
840
|
+
magnus::exception::type_error(),
|
841
|
+
"node is not a list node",
|
842
|
+
)),
|
843
|
+
}
|
844
|
+
}
|
845
|
+
|
846
|
+
fn get_list_tight(&self) -> Result<bool, magnus::Error> {
|
847
|
+
let node = self.inner.borrow();
|
848
|
+
|
849
|
+
match &node.data.value {
|
850
|
+
ComrakNodeValue::List(list) => Ok(list.tight),
|
851
|
+
_ => Err(magnus::Error::new(
|
852
|
+
magnus::exception::type_error(),
|
853
|
+
"node is not a list node",
|
854
|
+
)),
|
855
|
+
}
|
856
|
+
}
|
857
|
+
|
858
|
+
fn set_list_tight(&self, new_tight: bool) -> Result<bool, magnus::Error> {
|
859
|
+
let mut node = self.inner.borrow_mut();
|
860
|
+
|
861
|
+
match node.data.value {
|
862
|
+
ComrakNodeValue::List(ref mut list) => {
|
863
|
+
list.tight = new_tight;
|
864
|
+
Ok(true)
|
865
|
+
}
|
866
|
+
_ => Err(magnus::Error::new(
|
867
|
+
magnus::exception::type_error(),
|
868
|
+
"node is not a list node",
|
869
|
+
)),
|
870
|
+
}
|
871
|
+
}
|
872
|
+
|
873
|
+
fn get_fence_info(&self) -> Result<String, magnus::Error> {
|
874
|
+
let node = self.inner.borrow();
|
875
|
+
|
876
|
+
match &node.data.value {
|
877
|
+
ComrakNodeValue::CodeBlock(code_block) => Ok(code_block.info.to_string()),
|
878
|
+
_ => Err(magnus::Error::new(
|
879
|
+
magnus::exception::type_error(),
|
880
|
+
"node is not a code block node",
|
881
|
+
)),
|
882
|
+
}
|
883
|
+
}
|
884
|
+
|
885
|
+
fn set_fence_info(&self, new_info: String) -> Result<bool, magnus::Error> {
|
886
|
+
let mut node = self.inner.borrow_mut();
|
887
|
+
|
888
|
+
match node.data.value {
|
889
|
+
ComrakNodeValue::CodeBlock(ref mut code_block) => {
|
890
|
+
code_block.info = new_info;
|
891
|
+
Ok(true)
|
892
|
+
}
|
893
|
+
_ => Err(magnus::Error::new(
|
894
|
+
magnus::exception::type_error(),
|
895
|
+
"node is not a code block node",
|
896
|
+
)),
|
897
|
+
}
|
898
|
+
}
|
899
|
+
|
900
|
+
fn to_html(&self, args: &[Value]) -> Result<String, magnus::Error> {
|
901
|
+
let args = scan_args::scan_args::<(), (), (), (), _, ()>(args)?;
|
902
|
+
|
903
|
+
let kwargs = scan_args::get_kwargs::<_, (), (Option<RHash>, Option<RHash>), ()>(
|
904
|
+
args.keywords,
|
905
|
+
&[],
|
906
|
+
&["options", "plugins"],
|
907
|
+
)?;
|
908
|
+
let (rb_options, rb_plugins) = kwargs.optional;
|
909
|
+
|
910
|
+
let comrak_options = match format_options(rb_options) {
|
911
|
+
Ok(options) => options,
|
912
|
+
Err(err) => return Err(err),
|
913
|
+
};
|
914
|
+
|
915
|
+
let mut comrak_plugins = comrak::Plugins::default();
|
916
|
+
|
917
|
+
let syntect_adapter = match construct_syntax_highlighter_from_plugin(rb_plugins) {
|
918
|
+
Ok(Some(adapter)) => Some(adapter),
|
919
|
+
Ok(None) => None,
|
920
|
+
Err(err) => return Err(err),
|
921
|
+
};
|
922
|
+
|
923
|
+
match syntect_adapter {
|
924
|
+
Some(ref adapter) => comrak_plugins.render.codefence_syntax_highlighter = Some(adapter),
|
925
|
+
None => comrak_plugins.render.codefence_syntax_highlighter = None,
|
926
|
+
}
|
927
|
+
|
928
|
+
let arena: Arena<ComrakAstNode> = Arena::new();
|
929
|
+
fn iter_nodes<'a>(
|
930
|
+
arena: &'a Arena<comrak::arena_tree::Node<'a, RefCell<ComrakAst>>>,
|
931
|
+
node: &CommonmarkerNode,
|
932
|
+
) -> &'a comrak::arena_tree::Node<'a, std::cell::RefCell<comrak::nodes::Ast>> {
|
933
|
+
let comrak_node: &'a mut ComrakAstNode = arena.alloc(ComrakNode::new(RefCell::new(
|
934
|
+
node.inner.borrow().data.clone(),
|
935
|
+
)));
|
936
|
+
|
937
|
+
for c in node.inner.children() {
|
938
|
+
let child = CommonmarkerNode { inner: c };
|
939
|
+
let child_node = iter_nodes(arena, &child);
|
940
|
+
comrak_node.append(child_node);
|
941
|
+
}
|
942
|
+
|
943
|
+
comrak_node
|
944
|
+
}
|
945
|
+
|
946
|
+
let comrak_root_node: ComrakNode<RefCell<ComrakAst>> =
|
947
|
+
ComrakNode::new(RefCell::new(self.inner.borrow().data.clone()));
|
948
|
+
|
949
|
+
for c in self.inner.children() {
|
950
|
+
let child = CommonmarkerNode { inner: c };
|
951
|
+
|
952
|
+
let new_child = iter_nodes(&arena, &child);
|
953
|
+
|
954
|
+
comrak_root_node.append(new_child);
|
955
|
+
}
|
956
|
+
|
957
|
+
let mut output = vec![];
|
958
|
+
match comrak::format_html_with_plugins(
|
959
|
+
&comrak_root_node,
|
960
|
+
&comrak_options,
|
961
|
+
&mut output,
|
962
|
+
&comrak_plugins,
|
963
|
+
) {
|
964
|
+
Ok(_) => {}
|
965
|
+
Err(e) => {
|
966
|
+
return Err(magnus::Error::new(
|
967
|
+
magnus::exception::runtime_error(),
|
968
|
+
format!("cannot convert into html: {}", e),
|
969
|
+
));
|
970
|
+
}
|
971
|
+
}
|
972
|
+
|
973
|
+
match std::str::from_utf8(&output) {
|
974
|
+
Ok(s) => Ok(s.to_string()),
|
975
|
+
Err(_e) => Err(magnus::Error::new(
|
976
|
+
magnus::exception::runtime_error(),
|
977
|
+
"cannot convert into utf-8",
|
978
|
+
)),
|
979
|
+
}
|
980
|
+
}
|
981
|
+
|
982
|
+
fn to_commonmark(&self, args: &[Value]) -> Result<String, magnus::Error> {
|
983
|
+
let args = scan_args::scan_args::<(), (), (), (), _, ()>(args)?;
|
984
|
+
|
985
|
+
let kwargs = scan_args::get_kwargs::<_, (), (Option<RHash>, Option<RHash>), ()>(
|
986
|
+
args.keywords,
|
987
|
+
&[],
|
988
|
+
&["options", "plugins"],
|
989
|
+
)?;
|
990
|
+
let (rb_options, rb_plugins) = kwargs.optional;
|
991
|
+
|
992
|
+
let _comrak_options = format_options(rb_options);
|
993
|
+
let comrak_options = match format_options(rb_options) {
|
994
|
+
Ok(options) => options,
|
995
|
+
Err(err) => return Err(err),
|
996
|
+
};
|
997
|
+
|
998
|
+
let mut comrak_plugins = comrak::Plugins::default();
|
999
|
+
|
1000
|
+
let syntect_adapter = match construct_syntax_highlighter_from_plugin(rb_plugins) {
|
1001
|
+
Ok(Some(adapter)) => Some(adapter),
|
1002
|
+
Ok(None) => None,
|
1003
|
+
Err(err) => return Err(err),
|
1004
|
+
};
|
1005
|
+
|
1006
|
+
match syntect_adapter {
|
1007
|
+
Some(ref adapter) => comrak_plugins.render.codefence_syntax_highlighter = Some(adapter),
|
1008
|
+
None => comrak_plugins.render.codefence_syntax_highlighter = None,
|
1009
|
+
}
|
1010
|
+
|
1011
|
+
let arena: Arena<ComrakAstNode> = Arena::new();
|
1012
|
+
fn iter_nodes<'a>(
|
1013
|
+
arena: &'a Arena<comrak::arena_tree::Node<'a, RefCell<ComrakAst>>>,
|
1014
|
+
node: &CommonmarkerNode,
|
1015
|
+
) -> &'a comrak::arena_tree::Node<'a, std::cell::RefCell<comrak::nodes::Ast>> {
|
1016
|
+
let comrak_node: &'a mut ComrakAstNode = arena.alloc(ComrakNode::new(RefCell::new(
|
1017
|
+
node.inner.borrow().data.clone(),
|
1018
|
+
)));
|
1019
|
+
|
1020
|
+
for c in node.inner.children() {
|
1021
|
+
let child = CommonmarkerNode { inner: c };
|
1022
|
+
let child_node = iter_nodes(arena, &child);
|
1023
|
+
comrak_node.append(child_node);
|
1024
|
+
}
|
1025
|
+
|
1026
|
+
comrak_node
|
1027
|
+
}
|
1028
|
+
|
1029
|
+
let comrak_root_node: ComrakNode<RefCell<ComrakAst>> =
|
1030
|
+
ComrakNode::new(RefCell::new(self.inner.borrow().data.clone()));
|
1031
|
+
|
1032
|
+
for c in self.inner.children() {
|
1033
|
+
let child = CommonmarkerNode { inner: c };
|
1034
|
+
|
1035
|
+
let new_child = iter_nodes(&arena, &child);
|
1036
|
+
|
1037
|
+
comrak_root_node.append(new_child);
|
1038
|
+
}
|
1039
|
+
|
1040
|
+
let mut output = vec![];
|
1041
|
+
match comrak::format_commonmark_with_plugins(
|
1042
|
+
&comrak_root_node,
|
1043
|
+
&comrak_options,
|
1044
|
+
&mut output,
|
1045
|
+
&comrak_plugins,
|
1046
|
+
) {
|
1047
|
+
Ok(_) => {}
|
1048
|
+
Err(e) => {
|
1049
|
+
return Err(magnus::Error::new(
|
1050
|
+
magnus::exception::runtime_error(),
|
1051
|
+
format!("cannot convert into html: {}", e),
|
1052
|
+
));
|
1053
|
+
}
|
1054
|
+
}
|
1055
|
+
|
1056
|
+
match std::str::from_utf8(&output) {
|
1057
|
+
Ok(s) => Ok(s.to_string()),
|
1058
|
+
Err(_e) => Err(magnus::Error::new(
|
1059
|
+
magnus::exception::runtime_error(),
|
1060
|
+
"cannot convert into utf-8",
|
1061
|
+
)),
|
1062
|
+
}
|
1063
|
+
}
|
1064
|
+
}
|
1065
|
+
|
1066
|
+
pub fn init(m_commonmarker: RModule) -> Result<(), magnus::Error> {
|
1067
|
+
let c_node = m_commonmarker
|
1068
|
+
.define_class("Node", magnus::class::object())
|
1069
|
+
.expect("cannot define class Commonmarker::Node");
|
1070
|
+
|
1071
|
+
c_node.define_singleton_method("new", function!(CommonmarkerNode::new, -1))?;
|
1072
|
+
|
1073
|
+
c_node.define_method("type", method!(CommonmarkerNode::type_to_symbol, 0))?;
|
1074
|
+
c_node.define_method("parent", method!(CommonmarkerNode::get_parent, 0))?;
|
1075
|
+
c_node.define_method("first_child", method!(CommonmarkerNode::get_first_child, 0))?;
|
1076
|
+
c_node.define_method("last_child", method!(CommonmarkerNode::get_last_child, 0))?;
|
1077
|
+
c_node.define_method(
|
1078
|
+
"previous_sibling",
|
1079
|
+
method!(CommonmarkerNode::get_previous_sibling, 0),
|
1080
|
+
)?;
|
1081
|
+
c_node.define_method(
|
1082
|
+
"next_sibling",
|
1083
|
+
method!(CommonmarkerNode::get_next_sibling, 0),
|
1084
|
+
)?;
|
1085
|
+
|
1086
|
+
c_node.define_method("node_to_html", method!(CommonmarkerNode::to_html, -1))?;
|
1087
|
+
c_node.define_method(
|
1088
|
+
"node_to_commonmark",
|
1089
|
+
method!(CommonmarkerNode::to_commonmark, -1),
|
1090
|
+
)?;
|
1091
|
+
|
1092
|
+
c_node.define_method("replace", method!(CommonmarkerNode::replace_node, 1))?;
|
1093
|
+
|
1094
|
+
c_node.define_method(
|
1095
|
+
"insert_before",
|
1096
|
+
method!(CommonmarkerNode::insert_node_before, 1),
|
1097
|
+
)?;
|
1098
|
+
c_node.define_method(
|
1099
|
+
"insert_after",
|
1100
|
+
method!(CommonmarkerNode::insert_node_after, 1),
|
1101
|
+
)?;
|
1102
|
+
|
1103
|
+
c_node.define_method(
|
1104
|
+
"prepend_child",
|
1105
|
+
method!(CommonmarkerNode::prepend_child_node, 1),
|
1106
|
+
)?;
|
1107
|
+
c_node.define_method(
|
1108
|
+
"append_child",
|
1109
|
+
method!(CommonmarkerNode::append_child_node, 1),
|
1110
|
+
)?;
|
1111
|
+
|
1112
|
+
c_node.define_method("delete", method!(CommonmarkerNode::detach_node, 0))?;
|
1113
|
+
|
1114
|
+
c_node.define_method(
|
1115
|
+
"source_position",
|
1116
|
+
method!(CommonmarkerNode::get_sourcepos, 0),
|
1117
|
+
)?;
|
1118
|
+
|
1119
|
+
c_node.define_method(
|
1120
|
+
"string_content",
|
1121
|
+
method!(CommonmarkerNode::get_string_content, 0),
|
1122
|
+
)?;
|
1123
|
+
c_node.define_method(
|
1124
|
+
"string_content=",
|
1125
|
+
method!(CommonmarkerNode::set_string_content, 1),
|
1126
|
+
)?;
|
1127
|
+
|
1128
|
+
c_node.define_method("url", method!(CommonmarkerNode::get_url, 0))?;
|
1129
|
+
c_node.define_method("url=", method!(CommonmarkerNode::set_url, 1))?;
|
1130
|
+
c_node.define_method("title", method!(CommonmarkerNode::get_title, 0))?;
|
1131
|
+
c_node.define_method("title=", method!(CommonmarkerNode::set_title, 1))?;
|
1132
|
+
|
1133
|
+
c_node.define_method(
|
1134
|
+
"header_level",
|
1135
|
+
method!(CommonmarkerNode::get_header_level, 0),
|
1136
|
+
)?;
|
1137
|
+
c_node.define_method(
|
1138
|
+
"header_level=",
|
1139
|
+
method!(CommonmarkerNode::set_header_level, 1),
|
1140
|
+
)?;
|
1141
|
+
c_node.define_method("list_type", method!(CommonmarkerNode::get_list_type, 0))?;
|
1142
|
+
c_node.define_method("list_type=", method!(CommonmarkerNode::set_list_type, 1))?;
|
1143
|
+
c_node.define_method("list_start", method!(CommonmarkerNode::get_list_start, 0))?;
|
1144
|
+
c_node.define_method("list_start=", method!(CommonmarkerNode::set_list_start, 1))?;
|
1145
|
+
c_node.define_method("list_tight", method!(CommonmarkerNode::get_list_tight, 0))?;
|
1146
|
+
c_node.define_method("list_tight=", method!(CommonmarkerNode::set_list_tight, 1))?;
|
1147
|
+
c_node.define_method("fence_info", method!(CommonmarkerNode::get_fence_info, 0))?;
|
1148
|
+
c_node.define_method("fence_info=", method!(CommonmarkerNode::set_fence_info, 1))?;
|
1149
|
+
|
1150
|
+
Ok(())
|
1151
|
+
}
|