commonmarker 2.10.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.lock +83 -40
- data/README.md +264 -116
- data/UPGRADING.md +102 -0
- data/ext/commonmarker/Cargo.toml +6 -5
- data/ext/commonmarker/src/lib.rs +212 -110
- data/ext/commonmarker/src/nesting.rs +52 -0
- data/ext/commonmarker/src/node.rs +554 -365
- data/ext/commonmarker/src/options.rs +1319 -189
- data/ext/commonmarker/src/plugins/syntax_highlighting.rs +116 -151
- data/ext/commonmarker/src/plugins.rs +115 -1
- data/ext/commonmarker/src/rctree.rs +841 -0
- data/ext/commonmarker/src/utils.rs +5 -3
- data/lib/commonmarker/errors.rb +7 -0
- data/lib/commonmarker/node/ast.rb +1 -1
- data/lib/commonmarker/node/inspect.rb +15 -10
- data/lib/commonmarker/node.rb +23 -16
- data/lib/commonmarker/version.rb +2 -2
- data/lib/commonmarker.rb +80 -21
- metadata +6 -6
- data/lib/commonmarker/config.rb +0 -119
- data/lib/commonmarker/constants.rb +0 -7
- data/lib/commonmarker/renderer.rb +0 -9
- data/lib/commonmarker/utils.rb +0 -22
|
@@ -1,20 +1,28 @@
|
|
|
1
|
+
use crate::rctree::Node;
|
|
1
2
|
use comrak::arena_tree::Node as ComrakNode;
|
|
2
3
|
use comrak::nodes::{
|
|
3
|
-
AlertType, Ast as ComrakAst, AstNode as ComrakAstNode,
|
|
4
|
-
NodeCode, NodeCodeBlock,
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
AlertType, Ast as ComrakAst, AstNode as ComrakAstNode, CalloutFold, ListDelimType, NodeAlert,
|
|
5
|
+
NodeBlockId, NodeCallout, NodeCode, NodeCodeBlock, NodeComment, NodeCommentBlock,
|
|
6
|
+
NodeDescriptionItem, NodeEmph, NodeFootnoteDefinition, NodeFootnoteReference, NodeHeading,
|
|
7
|
+
NodeHtmlBlock, NodeLink, NodeList, NodeMath, NodeMathBlock, NodeMultilineBlockQuote,
|
|
8
|
+
NodeShortCode, NodeStrikethrough, NodeStrong, NodeTable, NodeTableRow, NodeTaskItem,
|
|
9
|
+
NodeThematicBreak, NodeValue as ComrakNodeValue, NodeWikiLink, TableAlignment,
|
|
10
|
+
};
|
|
11
|
+
use magnus::r_hash::ForEach;
|
|
12
|
+
use magnus::value::ReprValue;
|
|
13
|
+
use magnus::{
|
|
14
|
+
function, method, scan_args, Module, Object, RClass, RHash, Symbol, TryConvert, Value,
|
|
8
15
|
};
|
|
9
|
-
use magnus::{function, method, scan_args, Module, Object, RHash, RModule, Symbol, Value};
|
|
10
16
|
use magnus::{RArray, Ruby};
|
|
11
|
-
use
|
|
17
|
+
use std::sync::Arc;
|
|
12
18
|
use typed_arena::Arena;
|
|
13
19
|
|
|
14
20
|
use std::cell::RefCell;
|
|
15
21
|
|
|
16
|
-
use crate::options::{
|
|
22
|
+
use crate::options::{format_options, Options};
|
|
17
23
|
use crate::plugins::syntax_highlighting::construct_syntax_highlighter_from_plugin;
|
|
24
|
+
use crate::plugins::{comrak_plugins, format_plugins};
|
|
25
|
+
use comrak::plugins::syntect::SyntectAdapter;
|
|
18
26
|
|
|
19
27
|
#[derive(Debug, Clone)]
|
|
20
28
|
#[magnus::wrap(class = "Commonmarker::Node::Ast", size, mark)]
|
|
@@ -51,13 +59,17 @@ fn value_supports(value: &ComrakNodeValue, property: &str) -> Option<bool> {
|
|
|
51
59
|
| ComrakNodeValue::HtmlInline(_)
|
|
52
60
|
| ComrakNodeValue::Raw(_)
|
|
53
61
|
| ComrakNodeValue::Math(_)
|
|
62
|
+
| ComrakNodeValue::MathBlock(_)
|
|
54
63
|
| ComrakNodeValue::FrontMatter(_)
|
|
64
|
+
| ComrakNodeValue::CommentInline(_)
|
|
65
|
+
| ComrakNodeValue::MultilineComment(_)
|
|
55
66
|
),
|
|
56
67
|
"url" | "title" => matches!(value, ComrakNodeValue::Link(_) | ComrakNodeValue::Image(_)),
|
|
57
68
|
"header_level" => matches!(value, ComrakNodeValue::Heading(_)),
|
|
58
69
|
"list_type" | "list_start" | "list_tight" => matches!(value, ComrakNodeValue::List(_)),
|
|
59
70
|
"fenced" | "fence_info" => matches!(value, ComrakNodeValue::CodeBlock(_)),
|
|
60
71
|
"alert_type" => matches!(value, ComrakNodeValue::Alert(_)),
|
|
72
|
+
"callout_type" => matches!(value, ComrakNodeValue::Callout(_)),
|
|
61
73
|
_ => return None,
|
|
62
74
|
};
|
|
63
75
|
|
|
@@ -93,12 +105,11 @@ impl CommonmarkerNode {
|
|
|
93
105
|
));
|
|
94
106
|
};
|
|
95
107
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
})
|
|
108
|
+
let mut footnote_definition = NodeFootnoteDefinition::new(name);
|
|
109
|
+
// Total number of references to this footnote
|
|
110
|
+
footnote_definition.total_references = total_references;
|
|
111
|
+
|
|
112
|
+
ComrakNodeValue::FootnoteDefinition(footnote_definition)
|
|
102
113
|
}
|
|
103
114
|
"list" => {
|
|
104
115
|
let kwargs = scan_args::get_kwargs::<
|
|
@@ -141,35 +152,30 @@ impl CommonmarkerNode {
|
|
|
141
152
|
));
|
|
142
153
|
}
|
|
143
154
|
|
|
144
|
-
|
|
145
|
-
ListType::Ordered
|
|
146
|
-
} else {
|
|
147
|
-
ListType::Bullet
|
|
148
|
-
};
|
|
149
|
-
|
|
155
|
+
// For ordered lists, the delimiter after each number.
|
|
150
156
|
let comrak_delimiter = match delimiter.unwrap_or("".to_string()).as_str() {
|
|
151
157
|
")" => ListDelimType::Paren,
|
|
152
158
|
_ => ListDelimType::Period,
|
|
153
159
|
};
|
|
154
160
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
// Number of characters between the start of the list marker and the item text (including the list marker(s)).
|
|
161
|
-
padding: padding.unwrap_or(0),
|
|
161
|
+
// Whether the list is [tight](https://github.github.com/gfm/#tight), i.e. whether the
|
|
162
|
+
// paragraphs are wrapped in `<p>` tags when formatted as HTML.
|
|
163
|
+
let tight = tight.unwrap_or(false);
|
|
164
|
+
|
|
165
|
+
let mut list = if commonmark_list_type == "ordered" {
|
|
162
166
|
// For ordered lists, the ordinal the list starts at.
|
|
163
|
-
start
|
|
164
|
-
|
|
165
|
-
delimiter: comrak_delimiter,
|
|
167
|
+
NodeList::ordered(start.unwrap_or(0), comrak_delimiter, tight)
|
|
168
|
+
} else {
|
|
166
169
|
// For bullet lists, the character used for each bullet.
|
|
167
|
-
bullet_char
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
170
|
+
NodeList::bullet(bullet_char.unwrap_or(0), tight)
|
|
171
|
+
};
|
|
172
|
+
// Number of spaces before the list marker.
|
|
173
|
+
list.marker_offset = marker_offset.unwrap_or(0);
|
|
174
|
+
// Number of characters between the start of the list marker and the item text (including the list marker(s)).
|
|
175
|
+
list.padding = padding.unwrap_or(0);
|
|
176
|
+
list.is_task_list = task_list.unwrap_or(false);
|
|
177
|
+
|
|
178
|
+
ComrakNodeValue::List(list)
|
|
173
179
|
}
|
|
174
180
|
"description_list" => ComrakNodeValue::DescriptionList,
|
|
175
181
|
"description_item" => {
|
|
@@ -184,15 +190,15 @@ impl CommonmarkerNode {
|
|
|
184
190
|
|
|
185
191
|
let (marker_offset, padding, tight) = kwargs.optional;
|
|
186
192
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
193
|
+
// Whether the list is [tight](https://github.github.com/gfm/#tight), i.e. whether the
|
|
194
|
+
// paragraphs are wrapped in `<p>` tags when formatted as HTML.
|
|
195
|
+
let mut description_item = NodeDescriptionItem::new(tight.unwrap_or(false));
|
|
196
|
+
// Number of spaces before the list marker.
|
|
197
|
+
description_item.marker_offset = marker_offset.unwrap_or(0);
|
|
198
|
+
// Number of characters between the start of the list marker and the item text (including the list marker(s)).
|
|
199
|
+
description_item.padding = padding.unwrap_or(0);
|
|
200
|
+
|
|
201
|
+
ComrakNodeValue::DescriptionItem(description_item)
|
|
196
202
|
}
|
|
197
203
|
"description_term" => ComrakNodeValue::DescriptionTerm,
|
|
198
204
|
"description_details" => ComrakNodeValue::DescriptionDetails,
|
|
@@ -225,30 +231,30 @@ impl CommonmarkerNode {
|
|
|
225
231
|
let (fence_char, fence_length, fence_offset, info, literal, closed) =
|
|
226
232
|
kwargs.optional;
|
|
227
233
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
fence_char: fence_char.unwrap_or(b'`'),
|
|
233
|
-
// For fenced code blocks, the length of the fence.
|
|
234
|
-
fence_length: fence_length.unwrap_or(0),
|
|
235
|
-
// For fenced code blocks, the indentation level of the code within the block.
|
|
236
|
-
fence_offset: fence_offset.unwrap_or(0),
|
|
234
|
+
// The literal contents of the code block. As the contents are not interpreted as Markdown at
|
|
235
|
+
// all, they are contained within this structure, rather than inserted into a child inline of
|
|
236
|
+
// any kind.
|
|
237
|
+
let literal = literal.unwrap_or_default();
|
|
237
238
|
|
|
239
|
+
let mut code_block = if fenced {
|
|
238
240
|
// For fenced code blocks, the [info string](https://github.github.com/gfm/#info-string) after
|
|
239
241
|
// the opening fence, if any.
|
|
240
|
-
info
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
242
|
+
NodeCodeBlock::fenced(info.unwrap_or_default(), literal)
|
|
243
|
+
} else {
|
|
244
|
+
NodeCodeBlock::indented(literal)
|
|
245
|
+
};
|
|
246
|
+
// For fenced code blocks, the fence character itself (`` ` `` or `~`).
|
|
247
|
+
code_block.fence_char = fence_char.unwrap_or(b'`');
|
|
248
|
+
// For fenced code blocks, the length of the fence.
|
|
249
|
+
code_block.fence_length = fence_length.unwrap_or(0);
|
|
250
|
+
// For fenced code blocks, the indentation level of the code within the block.
|
|
251
|
+
code_block.fence_offset = fence_offset.unwrap_or(0);
|
|
252
|
+
// For fenced code blocks, whether the code block is closed. (If not, it was terminated by
|
|
253
|
+
// some other condition, like the end of the document, or the end of the indentation level the
|
|
254
|
+
// code block was introduced at.)
|
|
255
|
+
code_block.closed = closed.unwrap_or(true);
|
|
256
|
+
|
|
257
|
+
ComrakNodeValue::CodeBlock(Box::new(code_block))
|
|
252
258
|
}
|
|
253
259
|
"html_block" => {
|
|
254
260
|
let kwargs = scan_args::get_kwargs::<_, (), (Option<u8>, Option<String>), ()>(
|
|
@@ -259,12 +265,12 @@ impl CommonmarkerNode {
|
|
|
259
265
|
|
|
260
266
|
let (block_type, literal) = kwargs.optional;
|
|
261
267
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
+
// The literal contents of the HTML block.
|
|
269
|
+
let mut html_block = NodeHtmlBlock::new(literal.unwrap_or_default());
|
|
270
|
+
// The HTML block's type.
|
|
271
|
+
html_block.block_type = block_type.unwrap_or(0);
|
|
272
|
+
|
|
273
|
+
ComrakNodeValue::HtmlBlock(html_block)
|
|
268
274
|
}
|
|
269
275
|
"paragraph" => ComrakNodeValue::Paragraph,
|
|
270
276
|
"heading" => {
|
|
@@ -277,18 +283,18 @@ impl CommonmarkerNode {
|
|
|
277
283
|
let (level,) = kwargs.required;
|
|
278
284
|
let (setext, closed) = kwargs.optional;
|
|
279
285
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
286
|
+
// The heading level. For setext headings, an underline made of "=" characters is level 1, and made of
|
|
287
|
+
// "-" is level 2. For ATX headings, the number of leading "#" characters (from 1 to 6) is its level.
|
|
288
|
+
let mut heading = NodeHeading::new(level);
|
|
289
|
+
// Whether the heading is setext style (marked by an underline). If not, it is ATX style (marked by leading,
|
|
290
|
+
// and possibly trailing, "#" characters).
|
|
291
|
+
heading.setext = setext.unwrap_or(false);
|
|
292
|
+
// For ATX headings, whether the the leading "#" are terminated by a sequence of closing "#" characters.
|
|
293
|
+
heading.closed = closed.unwrap_or(false);
|
|
294
|
+
|
|
295
|
+
ComrakNodeValue::Heading(heading)
|
|
290
296
|
}
|
|
291
|
-
"thematic_break" => ComrakNodeValue::ThematicBreak,
|
|
297
|
+
"thematic_break" => ComrakNodeValue::ThematicBreak(NodeThematicBreak::default()),
|
|
292
298
|
"table" => {
|
|
293
299
|
let kwargs = scan_args::get_kwargs::<_, (RArray, usize, usize, usize), (), ()>(
|
|
294
300
|
args.keywords,
|
|
@@ -320,19 +326,16 @@ impl CommonmarkerNode {
|
|
|
320
326
|
comrak_alignments.push(TableAlignment::None);
|
|
321
327
|
}
|
|
322
328
|
});
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
// Number of non-empty, non-autocompleted cells
|
|
334
|
-
num_nonempty_cells,
|
|
335
|
-
}))
|
|
329
|
+
// The table alignments
|
|
330
|
+
let mut table = NodeTable::new(comrak_alignments);
|
|
331
|
+
// Number of columns of the table
|
|
332
|
+
table.num_columns = num_columns;
|
|
333
|
+
// Number of rows of the table
|
|
334
|
+
table.num_rows = num_rows;
|
|
335
|
+
// Number of non-empty, non-autocompleted cells
|
|
336
|
+
table.num_nonempty_cells = num_nonempty_cells;
|
|
337
|
+
|
|
338
|
+
ComrakNodeValue::Table(Box::new(table))
|
|
336
339
|
}
|
|
337
340
|
"table_row" => {
|
|
338
341
|
let kwargs =
|
|
@@ -340,7 +343,7 @@ impl CommonmarkerNode {
|
|
|
340
343
|
|
|
341
344
|
let (header,) = kwargs.required;
|
|
342
345
|
|
|
343
|
-
ComrakNodeValue::TableRow(header)
|
|
346
|
+
ComrakNodeValue::TableRow(NodeTableRow::new(header))
|
|
344
347
|
}
|
|
345
348
|
"table_cell" => ComrakNodeValue::TableCell,
|
|
346
349
|
"text" => {
|
|
@@ -363,10 +366,7 @@ impl CommonmarkerNode {
|
|
|
363
366
|
|
|
364
367
|
let (mark,) = kwargs.optional;
|
|
365
368
|
|
|
366
|
-
ComrakNodeValue::TaskItem(NodeTaskItem
|
|
367
|
-
symbol: mark,
|
|
368
|
-
symbol_sourcepos: (0, 0, 0, 0).into(),
|
|
369
|
-
})
|
|
369
|
+
ComrakNodeValue::TaskItem(NodeTaskItem::new(mark))
|
|
370
370
|
}
|
|
371
371
|
"softbreak" => ComrakNodeValue::SoftBreak,
|
|
372
372
|
"linebreak" => ComrakNodeValue::LineBreak,
|
|
@@ -379,15 +379,15 @@ impl CommonmarkerNode {
|
|
|
379
379
|
|
|
380
380
|
let (num_backticks, literal) = kwargs.optional;
|
|
381
381
|
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
382
|
+
// The content of the inline code span.
|
|
383
|
+
// As the contents are not interpreted as Markdown at all,
|
|
384
|
+
// they are contained within this structure,
|
|
385
|
+
// rather than inserted into a child inline of any kind
|
|
386
|
+
let mut code = NodeCode::new(literal.unwrap_or_default());
|
|
387
|
+
// The number of backticks
|
|
388
|
+
code.num_backticks = num_backticks.unwrap_or(1);
|
|
389
|
+
|
|
390
|
+
ComrakNodeValue::Code(code)
|
|
391
391
|
}
|
|
392
392
|
"html_inline" => {
|
|
393
393
|
let kwargs = scan_args::get_kwargs::<_, (), (Option<String>,), ()>(
|
|
@@ -400,9 +400,9 @@ impl CommonmarkerNode {
|
|
|
400
400
|
|
|
401
401
|
ComrakNodeValue::HtmlInline(content.unwrap_or_default())
|
|
402
402
|
}
|
|
403
|
-
"emph" => ComrakNodeValue::Emph,
|
|
404
|
-
"strong" => ComrakNodeValue::Strong,
|
|
405
|
-
"strikethrough" => ComrakNodeValue::Strikethrough,
|
|
403
|
+
"emph" => ComrakNodeValue::Emph(NodeEmph::default()),
|
|
404
|
+
"strong" => ComrakNodeValue::Strong(NodeStrong::default()),
|
|
405
|
+
"strikethrough" => ComrakNodeValue::Strikethrough(NodeStrikethrough::default()),
|
|
406
406
|
"superscript" => ComrakNodeValue::Superscript,
|
|
407
407
|
"subscript" => ComrakNodeValue::Subscript,
|
|
408
408
|
"link" => {
|
|
@@ -415,15 +415,8 @@ impl CommonmarkerNode {
|
|
|
415
415
|
let (url,) = kwargs.required;
|
|
416
416
|
let (title,) = kwargs.optional;
|
|
417
417
|
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
url,
|
|
421
|
-
// The title for the link or image.
|
|
422
|
-
//
|
|
423
|
-
// Note this field is used for the `title` attribute by the HTML formatter even for images;
|
|
424
|
-
// `alt` text is supplied in the image inline text.
|
|
425
|
-
title: title.unwrap_or_default(),
|
|
426
|
-
}))
|
|
418
|
+
// The URL for the link destination, and the title for the link.
|
|
419
|
+
ComrakNodeValue::Link(Box::new(NodeLink::new(url, title.unwrap_or_default())))
|
|
427
420
|
}
|
|
428
421
|
"image" => {
|
|
429
422
|
let kwargs = scan_args::get_kwargs::<_, (String,), (Option<String>,), ()>(
|
|
@@ -435,15 +428,11 @@ impl CommonmarkerNode {
|
|
|
435
428
|
let (url,) = kwargs.required;
|
|
436
429
|
let (title,) = kwargs.optional;
|
|
437
430
|
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
// Note this field is used for the `title` attribute by the HTML formatter even for images;
|
|
444
|
-
// `alt` text is supplied in the image inline text.
|
|
445
|
-
title: title.unwrap_or_default(),
|
|
446
|
-
}))
|
|
431
|
+
// The URL for the image source, and the title for the image.
|
|
432
|
+
//
|
|
433
|
+
// Note the title is used for the `title` attribute by the HTML formatter even for images;
|
|
434
|
+
// `alt` text is supplied in the image inline text.
|
|
435
|
+
ComrakNodeValue::Image(Box::new(NodeLink::new(url, title.unwrap_or_default())))
|
|
447
436
|
}
|
|
448
437
|
"footnote_reference" => {
|
|
449
438
|
let kwargs = scan_args::get_kwargs::<_, (String,), (Option<u32>, Option<u32>), ()>(
|
|
@@ -455,16 +444,14 @@ impl CommonmarkerNode {
|
|
|
455
444
|
let (name,) = kwargs.required;
|
|
456
445
|
let (ref_num, ix) = kwargs.optional;
|
|
457
446
|
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
ix: ix.unwrap_or(0),
|
|
467
|
-
}))
|
|
447
|
+
// The name of the footnote.
|
|
448
|
+
let mut footnote_reference = NodeFootnoteReference::new(name);
|
|
449
|
+
// The index of reference to the same footnote
|
|
450
|
+
footnote_reference.ref_num = ref_num.unwrap_or(0);
|
|
451
|
+
// The index of the footnote in the document.
|
|
452
|
+
footnote_reference.ix = ix.unwrap_or(0);
|
|
453
|
+
|
|
454
|
+
ComrakNodeValue::FootnoteReference(Box::new(footnote_reference))
|
|
468
455
|
}
|
|
469
456
|
// #[cfg(feature = "shortcodes")]
|
|
470
457
|
"shortcode" => {
|
|
@@ -492,20 +479,18 @@ impl CommonmarkerNode {
|
|
|
492
479
|
|
|
493
480
|
let (dollar_math, display_math, literal) = kwargs.required;
|
|
494
481
|
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
literal,
|
|
508
|
-
})
|
|
482
|
+
// The literal contents of the math span.
|
|
483
|
+
// As the contents are not interpreted as Markdown at all,
|
|
484
|
+
// they are contained within this structure,
|
|
485
|
+
// rather than inserted into a child inline of any kind.
|
|
486
|
+
//
|
|
487
|
+
// Whether this is display math (using `$$`)
|
|
488
|
+
let mut math = NodeMath::new(literal, display_math);
|
|
489
|
+
// Whether this is dollar math (`$` or `$$`).
|
|
490
|
+
// `false` indicates it is code math
|
|
491
|
+
math.dollar_math = dollar_math;
|
|
492
|
+
|
|
493
|
+
ComrakNodeValue::Math(math)
|
|
509
494
|
}
|
|
510
495
|
"multiline_block_quote" => {
|
|
511
496
|
let kwargs = scan_args::get_kwargs::<_, (usize, usize), (), ()>(
|
|
@@ -516,23 +501,30 @@ impl CommonmarkerNode {
|
|
|
516
501
|
|
|
517
502
|
let (fence_length, fence_offset) = kwargs.required;
|
|
518
503
|
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
504
|
+
let mut multiline_block_quote = NodeMultilineBlockQuote::default();
|
|
505
|
+
// The length of the fence.
|
|
506
|
+
multiline_block_quote.fence_length = fence_length;
|
|
507
|
+
// The indentation level of the fence marker.
|
|
508
|
+
multiline_block_quote.fence_offset = fence_offset;
|
|
509
|
+
|
|
510
|
+
ComrakNodeValue::MultilineBlockQuote(multiline_block_quote)
|
|
525
511
|
}
|
|
526
512
|
|
|
527
513
|
"escaped" => ComrakNodeValue::Escaped,
|
|
528
514
|
|
|
529
515
|
"wikilink" => {
|
|
530
|
-
let kwargs =
|
|
531
|
-
|
|
516
|
+
let kwargs = scan_args::get_kwargs::<_, (String,), (Option<bool>,), ()>(
|
|
517
|
+
args.keywords,
|
|
518
|
+
&["url"],
|
|
519
|
+
&["embed"],
|
|
520
|
+
)?;
|
|
532
521
|
|
|
533
522
|
let (url,) = kwargs.required;
|
|
523
|
+
let (embed,) = kwargs.optional;
|
|
534
524
|
|
|
535
|
-
|
|
525
|
+
// The URL for the link destination, and whether this is an
|
|
526
|
+
// embed (`![[embed]]`) rather than a normal link (`[[link]]`).
|
|
527
|
+
ComrakNodeValue::WikiLink(NodeWikiLink::new(url, embed.unwrap_or(false)))
|
|
536
528
|
}
|
|
537
529
|
|
|
538
530
|
"raw" => {
|
|
@@ -576,20 +568,114 @@ impl CommonmarkerNode {
|
|
|
576
568
|
}
|
|
577
569
|
};
|
|
578
570
|
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
571
|
+
// Overridden title. If None, then use the default title.
|
|
572
|
+
let mut alert = NodeAlert::new(alert_type, title);
|
|
573
|
+
// Originated from a multiline blockquote.
|
|
574
|
+
alert.multiline = multiline.unwrap_or(false);
|
|
575
|
+
// The length of the fence (multiline only).
|
|
576
|
+
alert.fence_length = fence_length.unwrap_or(0);
|
|
577
|
+
// The indentation level of the fence marker (multiline only)
|
|
578
|
+
alert.fence_offset = fence_offset.unwrap_or(0);
|
|
579
|
+
|
|
580
|
+
ComrakNodeValue::Alert(Box::new(alert))
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
"math_block" => {
|
|
584
|
+
let kwargs =
|
|
585
|
+
scan_args::get_kwargs::<_, (String,), (Option<usize>, Option<bool>), ()>(
|
|
586
|
+
args.keywords,
|
|
587
|
+
&["literal"],
|
|
588
|
+
&["fence_offset", "closed"],
|
|
589
|
+
)?;
|
|
590
|
+
|
|
591
|
+
let (literal,) = kwargs.required;
|
|
592
|
+
let (fence_offset, closed) = kwargs.optional;
|
|
593
|
+
|
|
594
|
+
// The literal contents of the math block, excluding the `$$` delimiter lines.
|
|
595
|
+
let mut math_block = NodeMathBlock::new(literal);
|
|
596
|
+
// The indentation level of the opening fence marker.
|
|
597
|
+
math_block.fence_offset = fence_offset.unwrap_or(0);
|
|
598
|
+
// Whether the math block was explicitly closed by a closing fence.
|
|
599
|
+
math_block.closed = closed.unwrap_or(true);
|
|
600
|
+
|
|
601
|
+
ComrakNodeValue::MathBlock(Box::new(math_block))
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
"callout" => {
|
|
605
|
+
let kwargs = scan_args::get_kwargs::<
|
|
606
|
+
_,
|
|
607
|
+
(Symbol,),
|
|
608
|
+
(Option<String>, Option<Symbol>),
|
|
609
|
+
(),
|
|
610
|
+
>(args.keywords, &["type"], &["metadata", "fold"])?;
|
|
611
|
+
|
|
612
|
+
let (callout_type,) = kwargs.required;
|
|
613
|
+
let (metadata, fold) = kwargs.optional;
|
|
614
|
+
|
|
615
|
+
// The initial fold state: not foldable, or foldable and initially expanded or collapsed.
|
|
616
|
+
let fold = match fold.map(|fold| fold.to_string()).as_deref() {
|
|
617
|
+
None | Some("none") => CalloutFold::None,
|
|
618
|
+
Some("expanded") => CalloutFold::Expanded,
|
|
619
|
+
Some("collapsed") => CalloutFold::Collapsed,
|
|
620
|
+
Some(_) => {
|
|
621
|
+
return Err(magnus::Error::new(
|
|
622
|
+
ruby.exception_arg_error(),
|
|
623
|
+
"callout fold must be `none`, `expanded`, or `collapsed`",
|
|
624
|
+
));
|
|
625
|
+
}
|
|
626
|
+
};
|
|
627
|
+
|
|
628
|
+
// The callout type, plus the raw metadata after the `|` separator, if any.
|
|
629
|
+
ComrakNodeValue::Callout(Box::new(NodeCallout::new(
|
|
630
|
+
callout_type.to_string(),
|
|
631
|
+
metadata,
|
|
632
|
+
fold,
|
|
633
|
+
)))
|
|
634
|
+
}
|
|
635
|
+
"callout_title" => ComrakNodeValue::CalloutTitle,
|
|
636
|
+
|
|
637
|
+
"block_id" => {
|
|
638
|
+
let kwargs =
|
|
639
|
+
scan_args::get_kwargs::<_, (String,), (), ()>(args.keywords, &["name"], &[])?;
|
|
640
|
+
|
|
641
|
+
let (name,) = kwargs.required;
|
|
642
|
+
|
|
643
|
+
// The identifier itself, without the leading `^`.
|
|
644
|
+
ComrakNodeValue::BlockId(Box::new(NodeBlockId::new(name)))
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
"comment" => {
|
|
648
|
+
let kwargs = scan_args::get_kwargs::<
|
|
649
|
+
_,
|
|
650
|
+
(),
|
|
651
|
+
(Option<String>, Option<bool>, Option<bool>),
|
|
652
|
+
(),
|
|
653
|
+
>(
|
|
654
|
+
args.keywords, &[], &["literal", "multiline", "closed"]
|
|
655
|
+
)?;
|
|
656
|
+
|
|
657
|
+
let (literal, multiline, closed) = kwargs.optional;
|
|
658
|
+
|
|
659
|
+
// The literal contents of the comment, without the `%%` markers.
|
|
660
|
+
let literal = literal.unwrap_or_default();
|
|
661
|
+
|
|
662
|
+
if multiline.unwrap_or(false) {
|
|
663
|
+
let mut comment = NodeCommentBlock::new(literal);
|
|
664
|
+
// Whether the comment block was explicitly closed by a closing `%%`.
|
|
665
|
+
comment.closed = closed.unwrap_or(true);
|
|
666
|
+
|
|
667
|
+
ComrakNodeValue::MultilineComment(comment)
|
|
668
|
+
} else {
|
|
669
|
+
ComrakNodeValue::CommentInline(NodeComment::new(literal))
|
|
670
|
+
}
|
|
590
671
|
}
|
|
591
672
|
|
|
592
|
-
_ =>
|
|
673
|
+
_ => {
|
|
674
|
+
return Err(magnus::Error::new(
|
|
675
|
+
ruby.exception_arg_error(),
|
|
676
|
+
format!("unknown node type {node_type}"),
|
|
677
|
+
));
|
|
678
|
+
}
|
|
593
679
|
};
|
|
594
680
|
|
|
595
681
|
Ok(CommonmarkerNode {
|
|
@@ -599,42 +685,50 @@ impl CommonmarkerNode {
|
|
|
599
685
|
})
|
|
600
686
|
}
|
|
601
687
|
|
|
688
|
+
/// Copies a comrak tree into a Ruby-visible tree, failing if any node sits
|
|
689
|
+
/// more than `max_nesting_depth` levels below the root.
|
|
602
690
|
pub fn new_from_comrak_node<'a>(
|
|
691
|
+
ruby: &Ruby,
|
|
603
692
|
comrak_root_node: &'a ComrakAstNode<'a>,
|
|
693
|
+
max_nesting_depth: usize,
|
|
604
694
|
) -> Result<CommonmarkerNode, magnus::Error> {
|
|
605
|
-
let
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
695
|
+
let commonmarker_root_node = Node::new(CommonmarkerAst {
|
|
696
|
+
data: comrak_root_node.data.borrow().clone(),
|
|
697
|
+
});
|
|
698
|
+
|
|
699
|
+
// Depth-first walk with an explicit stack instead of recursion, so that
|
|
700
|
+
// deeply nested input (e.g. thousands of nested blockquotes) cannot
|
|
701
|
+
// overflow the native stack. Each entry pairs an iterator over a comrak
|
|
702
|
+
// node's remaining children with the copy of that node the children get
|
|
703
|
+
// appended to.
|
|
704
|
+
let mut stack = vec![(comrak_root_node.children(), commonmarker_root_node.clone())];
|
|
705
|
+
|
|
706
|
+
loop {
|
|
707
|
+
// The stack holds one entry per ancestor, so its length is the
|
|
708
|
+
// depth of the next child produced.
|
|
709
|
+
let depth = stack.len();
|
|
710
|
+
let Some((children, parent)) = stack.last_mut() else {
|
|
711
|
+
break;
|
|
712
|
+
};
|
|
713
|
+
let Some(comrak_child) = children.next() else {
|
|
714
|
+
stack.pop();
|
|
715
|
+
continue;
|
|
613
716
|
};
|
|
614
717
|
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
Ok(_) => {}
|
|
618
|
-
Err(e) => {
|
|
619
|
-
panic!("cannot append node: {}", e);
|
|
620
|
-
}
|
|
621
|
-
}
|
|
718
|
+
if depth > max_nesting_depth {
|
|
719
|
+
return Err(crate::nesting::exceeded_error(ruby, max_nesting_depth));
|
|
622
720
|
}
|
|
623
721
|
|
|
624
|
-
|
|
722
|
+
let node = Node::new(CommonmarkerAst {
|
|
723
|
+
data: comrak_child.data.borrow().clone(),
|
|
724
|
+
});
|
|
725
|
+
parent.append(node.clone());
|
|
726
|
+
stack.push((comrak_child.children(), node));
|
|
625
727
|
}
|
|
626
728
|
|
|
627
|
-
|
|
628
|
-
inner:
|
|
629
|
-
}
|
|
630
|
-
|
|
631
|
-
for child in comrak_root_node.children() {
|
|
632
|
-
let new_child = iter_nodes(child);
|
|
633
|
-
|
|
634
|
-
commonmarker_root_node.append_child_node(&new_child)?;
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
Ok(commonmarker_root_node)
|
|
729
|
+
Ok(CommonmarkerNode {
|
|
730
|
+
inner: commonmarker_root_node,
|
|
731
|
+
})
|
|
638
732
|
}
|
|
639
733
|
|
|
640
734
|
fn type_to_symbol(ruby: &Ruby, rb_self: &Self) -> Symbol {
|
|
@@ -839,6 +933,9 @@ impl CommonmarkerNode {
|
|
|
839
933
|
ComrakNodeValue::HtmlInline(html_inline) => Ok(html_inline.to_string()),
|
|
840
934
|
ComrakNodeValue::Raw(raw) => Ok(raw.to_string()),
|
|
841
935
|
ComrakNodeValue::Math(math) => Ok(math.literal.to_string()),
|
|
936
|
+
ComrakNodeValue::MathBlock(math_block) => Ok(math_block.literal.to_string()),
|
|
937
|
+
ComrakNodeValue::CommentInline(comment) => Ok(comment.literal.to_string()),
|
|
938
|
+
ComrakNodeValue::MultilineComment(comment) => Ok(comment.literal.to_string()),
|
|
842
939
|
ComrakNodeValue::FrontMatter(front_matter) => Ok(front_matter.to_string()),
|
|
843
940
|
_ => Err(magnus::Error::new(
|
|
844
941
|
ruby.exception_type_error(),
|
|
@@ -862,6 +959,9 @@ impl CommonmarkerNode {
|
|
|
862
959
|
ComrakNodeValue::HtmlInline(ref mut html_inline) => *html_inline = new_literal,
|
|
863
960
|
ComrakNodeValue::Raw(ref mut raw) => *raw = new_literal,
|
|
864
961
|
ComrakNodeValue::Math(ref mut math) => math.literal = new_literal,
|
|
962
|
+
ComrakNodeValue::MathBlock(ref mut math_block) => math_block.literal = new_literal,
|
|
963
|
+
ComrakNodeValue::CommentInline(ref mut comment) => comment.literal = new_literal,
|
|
964
|
+
ComrakNodeValue::MultilineComment(ref mut comment) => comment.literal = new_literal,
|
|
865
965
|
ComrakNodeValue::FrontMatter(ref mut front_matter) => *front_matter = new_literal,
|
|
866
966
|
_ => {
|
|
867
967
|
return Err(magnus::Error::new(
|
|
@@ -956,7 +1056,12 @@ impl CommonmarkerNode {
|
|
|
956
1056
|
match new_type.to_string().as_str() {
|
|
957
1057
|
"bullet" => list.list_type = comrak::nodes::ListType::Bullet,
|
|
958
1058
|
"ordered" => list.list_type = comrak::nodes::ListType::Ordered,
|
|
959
|
-
_ =>
|
|
1059
|
+
_ => {
|
|
1060
|
+
return Err(magnus::Error::new(
|
|
1061
|
+
ruby.exception_arg_error(),
|
|
1062
|
+
"list type must be `bullet` or `ordered`",
|
|
1063
|
+
));
|
|
1064
|
+
}
|
|
960
1065
|
}
|
|
961
1066
|
Ok(true)
|
|
962
1067
|
}
|
|
@@ -1083,6 +1188,112 @@ impl CommonmarkerNode {
|
|
|
1083
1188
|
}
|
|
1084
1189
|
}
|
|
1085
1190
|
|
|
1191
|
+
/// The node's parsed attributes (`{#id .class key=value}`), as a Hash of
|
|
1192
|
+
/// `id`, `classes` and `pairs`, or `nil` when the node has none.
|
|
1193
|
+
fn get_attributes(ruby: &Ruby, rb_self: &Self) -> Result<Option<RHash>, magnus::Error> {
|
|
1194
|
+
let node = rb_self.inner.borrow();
|
|
1195
|
+
|
|
1196
|
+
let Some(attrs) = &node.data.attrs else {
|
|
1197
|
+
return Ok(None);
|
|
1198
|
+
};
|
|
1199
|
+
|
|
1200
|
+
let pairs = ruby.hash_new();
|
|
1201
|
+
for (key, value) in &attrs.pairs {
|
|
1202
|
+
pairs.aset(key.as_str(), value.as_str())?;
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
let result = ruby.hash_new();
|
|
1206
|
+
result.aset(ruby.to_symbol("id"), attrs.id.as_deref())?;
|
|
1207
|
+
result.aset(ruby.to_symbol("classes"), attrs.classes.clone())?;
|
|
1208
|
+
result.aset(ruby.to_symbol("pairs"), pairs)?;
|
|
1209
|
+
|
|
1210
|
+
Ok(Some(result))
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
/// Replaces the node's attributes. Accepts `nil` to clear them, or a Hash
|
|
1214
|
+
/// with any of `id` (String), `classes` (Array) and `pairs` (Hash). Strings
|
|
1215
|
+
/// and Symbols are both accepted for names and values.
|
|
1216
|
+
fn set_attributes(ruby: &Ruby, rb_self: &Self, value: Value) -> Result<bool, magnus::Error> {
|
|
1217
|
+
fn as_string(ruby: &Ruby, value: Value, what: &str) -> Result<String, magnus::Error> {
|
|
1218
|
+
if let Some(symbol) = Symbol::from_value(value) {
|
|
1219
|
+
return Ok(symbol.name()?.into_owned());
|
|
1220
|
+
}
|
|
1221
|
+
String::try_convert(value).map_err(|_| {
|
|
1222
|
+
magnus::Error::new(
|
|
1223
|
+
ruby.exception_type_error(),
|
|
1224
|
+
format!(
|
|
1225
|
+
"{} must be a String or Symbol; got {}",
|
|
1226
|
+
what,
|
|
1227
|
+
crate::utils::get_classname(value)
|
|
1228
|
+
),
|
|
1229
|
+
)
|
|
1230
|
+
})
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
let mut node = rb_self.inner.borrow_mut();
|
|
1234
|
+
|
|
1235
|
+
if value.is_nil() {
|
|
1236
|
+
node.data.attrs = None;
|
|
1237
|
+
return Ok(true);
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
let hash: RHash = TryConvert::try_convert(value).map_err(|_| {
|
|
1241
|
+
magnus::Error::new(
|
|
1242
|
+
ruby.exception_type_error(),
|
|
1243
|
+
format!(
|
|
1244
|
+
"attributes must be a Hash or nil; got {}",
|
|
1245
|
+
crate::utils::get_classname(value)
|
|
1246
|
+
),
|
|
1247
|
+
)
|
|
1248
|
+
})?;
|
|
1249
|
+
|
|
1250
|
+
let mut attrs = comrak::nodes::Attributes::default();
|
|
1251
|
+
|
|
1252
|
+
if let Some(id) = hash.get(ruby.to_symbol("id")).filter(|id| !id.is_nil()) {
|
|
1253
|
+
attrs.id = Some(as_string(ruby, id, "attribute id")?);
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
if let Some(classes) = hash.get(ruby.to_symbol("classes")).filter(|c| !c.is_nil()) {
|
|
1257
|
+
let classes: RArray = TryConvert::try_convert(classes).map_err(|_| {
|
|
1258
|
+
magnus::Error::new(
|
|
1259
|
+
ruby.exception_type_error(),
|
|
1260
|
+
format!(
|
|
1261
|
+
"attribute classes must be an Array; got {}",
|
|
1262
|
+
crate::utils::get_classname(classes)
|
|
1263
|
+
),
|
|
1264
|
+
)
|
|
1265
|
+
})?;
|
|
1266
|
+
for class in classes.into_iter() {
|
|
1267
|
+
attrs
|
|
1268
|
+
.classes
|
|
1269
|
+
.push(as_string(ruby, class, "attribute class")?);
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
if let Some(pairs) = hash.get(ruby.to_symbol("pairs")).filter(|p| !p.is_nil()) {
|
|
1274
|
+
let pairs: RHash = TryConvert::try_convert(pairs).map_err(|_| {
|
|
1275
|
+
magnus::Error::new(
|
|
1276
|
+
ruby.exception_type_error(),
|
|
1277
|
+
format!(
|
|
1278
|
+
"attribute pairs must be a Hash; got {}",
|
|
1279
|
+
crate::utils::get_classname(pairs)
|
|
1280
|
+
),
|
|
1281
|
+
)
|
|
1282
|
+
})?;
|
|
1283
|
+
pairs.foreach(|key: Value, value: Value| {
|
|
1284
|
+
attrs.pairs.push((
|
|
1285
|
+
as_string(ruby, key, "attribute name")?,
|
|
1286
|
+
as_string(ruby, value, "attribute value")?,
|
|
1287
|
+
));
|
|
1288
|
+
Ok(ForEach::Continue)
|
|
1289
|
+
})?;
|
|
1290
|
+
}
|
|
1291
|
+
|
|
1292
|
+
node.data.attrs = Some(Box::new(attrs));
|
|
1293
|
+
|
|
1294
|
+
Ok(true)
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1086
1297
|
fn get_alert_type(ruby: &Ruby, rb_self: &Self) -> Result<Symbol, magnus::Error> {
|
|
1087
1298
|
let node = rb_self.inner.borrow();
|
|
1088
1299
|
|
|
@@ -1093,6 +1304,10 @@ impl CommonmarkerNode {
|
|
|
1093
1304
|
AlertType::Important => Ok(ruby.to_symbol("important")),
|
|
1094
1305
|
AlertType::Warning => Ok(ruby.to_symbol("warning")),
|
|
1095
1306
|
AlertType::Caution => Ok(ruby.to_symbol("caution")),
|
|
1307
|
+
_ => Err(magnus::Error::new(
|
|
1308
|
+
ruby.exception_type_error(),
|
|
1309
|
+
"node has an unknown alert type",
|
|
1310
|
+
)),
|
|
1096
1311
|
},
|
|
1097
1312
|
_ => Err(magnus::Error::new(
|
|
1098
1313
|
ruby.exception_type_error(),
|
|
@@ -1116,7 +1331,12 @@ impl CommonmarkerNode {
|
|
|
1116
1331
|
"important" => alert.alert_type = AlertType::Important,
|
|
1117
1332
|
"warning" => alert.alert_type = AlertType::Warning,
|
|
1118
1333
|
"caution" => alert.alert_type = AlertType::Caution,
|
|
1119
|
-
_ =>
|
|
1334
|
+
_ => {
|
|
1335
|
+
return Err(magnus::Error::new(
|
|
1336
|
+
ruby.exception_arg_error(),
|
|
1337
|
+
"alert type must be `note`, `tip`, `important`, `warning`, or `caution`",
|
|
1338
|
+
));
|
|
1339
|
+
}
|
|
1120
1340
|
}
|
|
1121
1341
|
Ok(true)
|
|
1122
1342
|
}
|
|
@@ -1127,201 +1347,160 @@ impl CommonmarkerNode {
|
|
|
1127
1347
|
}
|
|
1128
1348
|
}
|
|
1129
1349
|
|
|
1130
|
-
fn
|
|
1131
|
-
let
|
|
1132
|
-
|
|
1133
|
-
let kwargs = scan_args::get_kwargs::<
|
|
1134
|
-
_,
|
|
1135
|
-
(),
|
|
1136
|
-
(Option<RHash>, Option<RHash>, Option<RHash>, Option<RHash>),
|
|
1137
|
-
(),
|
|
1138
|
-
>(
|
|
1139
|
-
args.keywords,
|
|
1140
|
-
&[],
|
|
1141
|
-
&["parse", "render", "extension", "plugins"],
|
|
1142
|
-
)?;
|
|
1143
|
-
let (rb_parse, rb_render, rb_extension, rb_plugins) = kwargs.optional;
|
|
1144
|
-
|
|
1145
|
-
let mut comrak_parse_options = comrak::options::Parse::default();
|
|
1146
|
-
let mut comrak_render_options = comrak::options::Render::default();
|
|
1147
|
-
let mut comrak_extension_options = comrak::options::Extension::default();
|
|
1350
|
+
fn get_callout_type(ruby: &Ruby, rb_self: &Self) -> Result<Symbol, magnus::Error> {
|
|
1351
|
+
let node = rb_self.inner.borrow();
|
|
1148
1352
|
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
if let Some(rb_extension) = rb_extension {
|
|
1156
|
-
iterate_extension_options(&mut comrak_extension_options, rb_extension);
|
|
1353
|
+
match &node.data.value {
|
|
1354
|
+
ComrakNodeValue::Callout(callout) => Ok(ruby.to_symbol(callout.callout_type.as_str())),
|
|
1355
|
+
_ => Err(magnus::Error::new(
|
|
1356
|
+
ruby.exception_type_error(),
|
|
1357
|
+
"node is not a callout node",
|
|
1358
|
+
)),
|
|
1157
1359
|
}
|
|
1360
|
+
}
|
|
1158
1361
|
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
let mut comrak_plugins = comrak::options::Plugins::default();
|
|
1166
|
-
|
|
1167
|
-
let syntect_adapter = match construct_syntax_highlighter_from_plugin(ruby, rb_plugins) {
|
|
1168
|
-
Ok(Some(adapter)) => Some(adapter),
|
|
1169
|
-
Ok(None) => None,
|
|
1170
|
-
Err(err) => return Err(err),
|
|
1171
|
-
};
|
|
1172
|
-
|
|
1173
|
-
match syntect_adapter {
|
|
1174
|
-
Some(ref adapter) => comrak_plugins.render.codefence_syntax_highlighter = Some(adapter),
|
|
1175
|
-
None => comrak_plugins.render.codefence_syntax_highlighter = None,
|
|
1176
|
-
}
|
|
1362
|
+
fn set_callout_type(
|
|
1363
|
+
ruby: &Ruby,
|
|
1364
|
+
rb_self: &Self,
|
|
1365
|
+
new_type: Symbol,
|
|
1366
|
+
) -> Result<bool, magnus::Error> {
|
|
1367
|
+
let mut node = rb_self.inner.borrow_mut();
|
|
1177
1368
|
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
) -> &'a comrak::arena_tree::Node<'a, std::cell::RefCell<comrak::nodes::Ast>> {
|
|
1183
|
-
let comrak_node: &'a mut ComrakAstNode = arena.alloc(ComrakNode::new(RefCell::new(
|
|
1184
|
-
node.inner.borrow().data.clone(),
|
|
1185
|
-
)));
|
|
1186
|
-
|
|
1187
|
-
for c in node.inner.children() {
|
|
1188
|
-
let child = CommonmarkerNode { inner: c };
|
|
1189
|
-
let child_node = iter_nodes(arena, &child);
|
|
1190
|
-
comrak_node.append(child_node);
|
|
1369
|
+
match node.data.value {
|
|
1370
|
+
ComrakNodeValue::Callout(ref mut callout) => {
|
|
1371
|
+
callout.callout_type = new_type.to_string();
|
|
1372
|
+
Ok(true)
|
|
1191
1373
|
}
|
|
1192
|
-
|
|
1193
|
-
|
|
1374
|
+
_ => Err(magnus::Error::new(
|
|
1375
|
+
ruby.exception_type_error(),
|
|
1376
|
+
"node is not a callout node",
|
|
1377
|
+
)),
|
|
1194
1378
|
}
|
|
1379
|
+
}
|
|
1195
1380
|
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1381
|
+
/// Copies this node and its descendants into `arena` as a comrak tree so that
|
|
1382
|
+
/// comrak's formatters can render it.
|
|
1383
|
+
///
|
|
1384
|
+
/// Depth-first walk with an explicit stack instead of recursion, so that
|
|
1385
|
+
/// deeply nested input cannot overflow the native stack. Each entry pairs an
|
|
1386
|
+
/// iterator over a node's remaining children with the copy of that node the
|
|
1387
|
+
/// children get appended to.
|
|
1388
|
+
fn to_comrak_node<'a>(&self, arena: &'a Arena<ComrakAstNode<'a>>) -> &'a ComrakAstNode<'a> {
|
|
1389
|
+
let comrak_root_node: &'a ComrakAstNode<'a> = arena.alloc(ComrakNode::new(RefCell::new(
|
|
1390
|
+
self.inner.borrow().data.clone(),
|
|
1391
|
+
)));
|
|
1392
|
+
|
|
1393
|
+
let mut stack = vec![(self.inner.children(), comrak_root_node)];
|
|
1394
|
+
|
|
1395
|
+
loop {
|
|
1396
|
+
let Some((children, parent)) = stack.last_mut() else {
|
|
1397
|
+
break;
|
|
1398
|
+
};
|
|
1399
|
+
let parent: &'a ComrakAstNode<'a> = parent;
|
|
1400
|
+
let Some(child) = children.next() else {
|
|
1401
|
+
stack.pop();
|
|
1402
|
+
continue;
|
|
1403
|
+
};
|
|
1206
1404
|
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
&mut output,
|
|
1212
|
-
&comrak_plugins,
|
|
1213
|
-
) {
|
|
1214
|
-
Ok(_) => {}
|
|
1215
|
-
Err(e) => {
|
|
1216
|
-
return Err(magnus::Error::new(
|
|
1217
|
-
ruby.exception_runtime_error(),
|
|
1218
|
-
format!("cannot convert into html: {}", e),
|
|
1219
|
-
));
|
|
1220
|
-
}
|
|
1405
|
+
let comrak_node: &'a ComrakAstNode<'a> =
|
|
1406
|
+
arena.alloc(ComrakNode::new(RefCell::new(child.borrow().data.clone())));
|
|
1407
|
+
parent.append(comrak_node);
|
|
1408
|
+
stack.push((child.children(), comrak_node));
|
|
1221
1409
|
}
|
|
1222
1410
|
|
|
1223
|
-
|
|
1411
|
+
comrak_root_node
|
|
1224
1412
|
}
|
|
1225
1413
|
|
|
1226
|
-
|
|
1414
|
+
/// Reads the `options:` and `plugins:` keyword arguments shared by
|
|
1415
|
+
/// `to_html` and `to_commonmark`.
|
|
1416
|
+
fn render_args(
|
|
1417
|
+
ruby: &Ruby,
|
|
1418
|
+
args: &[Value],
|
|
1419
|
+
) -> Result<(Options, Option<Arc<SyntectAdapter>>), magnus::Error> {
|
|
1227
1420
|
let args = scan_args::scan_args::<(), (), (), (), _, ()>(args)?;
|
|
1228
1421
|
|
|
1229
|
-
let kwargs = scan_args::get_kwargs::<
|
|
1230
|
-
_,
|
|
1231
|
-
(),
|
|
1232
|
-
(Option<RHash>, Option<RHash>, Option<RHash>, Option<RHash>),
|
|
1233
|
-
(),
|
|
1234
|
-
>(
|
|
1422
|
+
let kwargs = scan_args::get_kwargs::<_, (), (Option<Value>, Option<Value>), ()>(
|
|
1235
1423
|
args.keywords,
|
|
1236
1424
|
&[],
|
|
1237
|
-
&["
|
|
1425
|
+
&["options", "plugins"],
|
|
1238
1426
|
)?;
|
|
1239
|
-
let (
|
|
1427
|
+
let (rb_options, rb_plugins) = kwargs.optional;
|
|
1240
1428
|
|
|
1241
|
-
let
|
|
1242
|
-
let
|
|
1243
|
-
let
|
|
1429
|
+
let options = format_options(ruby, rb_options)?;
|
|
1430
|
+
let formatted_plugins = format_plugins(ruby, rb_plugins)?;
|
|
1431
|
+
let syntect_adapter = construct_syntax_highlighter_from_plugin(ruby, formatted_plugins)?;
|
|
1244
1432
|
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
}
|
|
1248
|
-
if let Some(rb_render) = rb_render {
|
|
1249
|
-
iterate_render_options(&mut comrak_render_options, rb_render);
|
|
1250
|
-
}
|
|
1251
|
-
if let Some(rb_extension) = rb_extension {
|
|
1252
|
-
iterate_extension_options(&mut comrak_extension_options, rb_extension);
|
|
1253
|
-
}
|
|
1433
|
+
Ok((options, syntect_adapter))
|
|
1434
|
+
}
|
|
1254
1435
|
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1436
|
+
/// Renders this node and its descendants as HTML.
|
|
1437
|
+
pub fn render_html(
|
|
1438
|
+
&self,
|
|
1439
|
+
ruby: &Ruby,
|
|
1440
|
+
options: &Options,
|
|
1441
|
+
plugins: &comrak::options::Plugins,
|
|
1442
|
+
) -> Result<String, magnus::Error> {
|
|
1443
|
+
let arena: Arena<ComrakAstNode> = Arena::new();
|
|
1444
|
+
let comrak_root_node = self.to_comrak_node(&arena);
|
|
1260
1445
|
|
|
1261
|
-
|
|
1446
|
+
crate::nesting::check(ruby, comrak_root_node, options.max_nesting_depth)?;
|
|
1262
1447
|
|
|
1263
|
-
let
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1448
|
+
let mut output = String::new();
|
|
1449
|
+
comrak::format_html_with_plugins(comrak_root_node, &options.comrak, &mut output, plugins)
|
|
1450
|
+
.map_err(|e| {
|
|
1451
|
+
magnus::Error::new(
|
|
1452
|
+
ruby.exception_runtime_error(),
|
|
1453
|
+
format!("cannot convert into html: {e}"),
|
|
1454
|
+
)
|
|
1455
|
+
})?;
|
|
1268
1456
|
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
None => comrak_plugins.render.codefence_syntax_highlighter = None,
|
|
1272
|
-
}
|
|
1457
|
+
Ok(output)
|
|
1458
|
+
}
|
|
1273
1459
|
|
|
1460
|
+
/// Renders this node and its descendants as CommonMark.
|
|
1461
|
+
pub fn render_commonmark(
|
|
1462
|
+
&self,
|
|
1463
|
+
ruby: &Ruby,
|
|
1464
|
+
options: &Options,
|
|
1465
|
+
plugins: &comrak::options::Plugins,
|
|
1466
|
+
) -> Result<String, magnus::Error> {
|
|
1274
1467
|
let arena: Arena<ComrakAstNode> = Arena::new();
|
|
1275
|
-
|
|
1276
|
-
arena: &'a Arena<comrak::arena_tree::Node<'a, RefCell<ComrakAst>>>,
|
|
1277
|
-
node: &CommonmarkerNode,
|
|
1278
|
-
) -> &'a comrak::arena_tree::Node<'a, std::cell::RefCell<comrak::nodes::Ast>> {
|
|
1279
|
-
let comrak_node: &'a mut ComrakAstNode = arena.alloc(ComrakNode::new(RefCell::new(
|
|
1280
|
-
node.inner.borrow().data.clone(),
|
|
1281
|
-
)));
|
|
1282
|
-
|
|
1283
|
-
for c in node.inner.children() {
|
|
1284
|
-
let child = CommonmarkerNode { inner: c };
|
|
1285
|
-
let child_node = iter_nodes(arena, &child);
|
|
1286
|
-
comrak_node.append(child_node);
|
|
1287
|
-
}
|
|
1468
|
+
let comrak_root_node = self.to_comrak_node(&arena);
|
|
1288
1469
|
|
|
1289
|
-
|
|
1290
|
-
}
|
|
1470
|
+
crate::nesting::check(ruby, comrak_root_node, options.max_nesting_depth)?;
|
|
1291
1471
|
|
|
1292
|
-
let
|
|
1293
|
-
|
|
1472
|
+
let mut output = String::new();
|
|
1473
|
+
comrak::format_commonmark_with_plugins(
|
|
1474
|
+
comrak_root_node,
|
|
1475
|
+
&options.comrak,
|
|
1476
|
+
&mut output,
|
|
1477
|
+
plugins,
|
|
1478
|
+
)
|
|
1479
|
+
.map_err(|e| {
|
|
1480
|
+
magnus::Error::new(
|
|
1481
|
+
ruby.exception_runtime_error(),
|
|
1482
|
+
format!("cannot convert into commonmark: {e}"),
|
|
1483
|
+
)
|
|
1484
|
+
})?;
|
|
1294
1485
|
|
|
1295
|
-
|
|
1296
|
-
|
|
1486
|
+
Ok(output)
|
|
1487
|
+
}
|
|
1297
1488
|
|
|
1298
|
-
|
|
1489
|
+
fn to_html(ruby: &Ruby, rb_self: &Self, args: &[Value]) -> Result<String, magnus::Error> {
|
|
1490
|
+
let (options, syntect_adapter) = Self::render_args(ruby, args)?;
|
|
1299
1491
|
|
|
1300
|
-
|
|
1301
|
-
|
|
1492
|
+
rb_self.render_html(ruby, &options, &comrak_plugins(syntect_adapter.as_deref()))
|
|
1493
|
+
}
|
|
1302
1494
|
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
&comrak_root_node,
|
|
1306
|
-
&comrak_options,
|
|
1307
|
-
&mut output,
|
|
1308
|
-
&comrak_plugins,
|
|
1309
|
-
) {
|
|
1310
|
-
Ok(_) => {}
|
|
1311
|
-
Err(e) => {
|
|
1312
|
-
return Err(magnus::Error::new(
|
|
1313
|
-
ruby.exception_runtime_error(),
|
|
1314
|
-
format!("cannot convert into html: {}", e),
|
|
1315
|
-
));
|
|
1316
|
-
}
|
|
1317
|
-
}
|
|
1495
|
+
fn to_commonmark(ruby: &Ruby, rb_self: &Self, args: &[Value]) -> Result<String, magnus::Error> {
|
|
1496
|
+
let (options, syntect_adapter) = Self::render_args(ruby, args)?;
|
|
1318
1497
|
|
|
1319
|
-
|
|
1498
|
+
rb_self.render_commonmark(ruby, &options, &comrak_plugins(syntect_adapter.as_deref()))
|
|
1320
1499
|
}
|
|
1321
1500
|
}
|
|
1322
1501
|
|
|
1323
|
-
pub fn init(ruby: &Ruby,
|
|
1324
|
-
let c_node =
|
|
1502
|
+
pub fn init(ruby: &Ruby, c_commonmarker: RClass) -> Result<(), magnus::Error> {
|
|
1503
|
+
let c_node = c_commonmarker
|
|
1325
1504
|
.define_class("Node", ruby.class_object())
|
|
1326
1505
|
.expect("cannot define class Commonmarker::Node");
|
|
1327
1506
|
|
|
@@ -1413,8 +1592,18 @@ pub fn init(ruby: &Ruby, m_commonmarker: RModule) -> Result<(), magnus::Error> {
|
|
|
1413
1592
|
c_node.define_method("fenced=", method!(CommonmarkerNode::set_fenced, 1))?;
|
|
1414
1593
|
c_node.define_method("fence_info", method!(CommonmarkerNode::get_fence_info, 0))?;
|
|
1415
1594
|
c_node.define_method("fence_info=", method!(CommonmarkerNode::set_fence_info, 1))?;
|
|
1595
|
+
c_node.define_method("attributes", method!(CommonmarkerNode::get_attributes, 0))?;
|
|
1596
|
+
c_node.define_method("attributes=", method!(CommonmarkerNode::set_attributes, 1))?;
|
|
1416
1597
|
c_node.define_method("alert_type", method!(CommonmarkerNode::get_alert_type, 0))?;
|
|
1417
1598
|
c_node.define_method("alert_type=", method!(CommonmarkerNode::set_alert_type, 1))?;
|
|
1599
|
+
c_node.define_method(
|
|
1600
|
+
"callout_type",
|
|
1601
|
+
method!(CommonmarkerNode::get_callout_type, 0),
|
|
1602
|
+
)?;
|
|
1603
|
+
c_node.define_method(
|
|
1604
|
+
"callout_type=",
|
|
1605
|
+
method!(CommonmarkerNode::set_callout_type, 1),
|
|
1606
|
+
)?;
|
|
1418
1607
|
|
|
1419
1608
|
Ok(())
|
|
1420
1609
|
}
|