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.
@@ -1,48 +1,29 @@
1
- use std::borrow::Cow;
1
+ use std::sync::LazyLock;
2
2
 
3
+ use magnus::value::ReprValue;
3
4
  use magnus::TryConvert;
4
- use magnus::{r_hash::ForEach, RHash, Symbol, Value};
5
+ use magnus::{r_hash::ForEach, Error, RHash, Ruby, Symbol, Value};
5
6
 
6
- use crate::utils::try_convert_string;
7
+ use crate::nesting::DEFAULT_MAX_NESTING_DEPTH;
8
+
9
+ /// Commonmarker's defaults are comrak's
10
+ /// A `nil` option value also means "the default", so the two never disagree.
11
+ pub static DEFAULT_OPTIONS: LazyLock<comrak::Options> = LazyLock::new(comrak::Options::default);
12
+
13
+ const PARSE: &str = "parse";
14
+ const RENDER: &str = "render";
15
+ const EXTENSION: &str = "extension";
7
16
 
8
17
  const PARSE_SMART: &str = "smart";
9
18
  const PARSE_DEFAULT_INFO_STRING: &str = "default_info_string";
10
19
  const PARSE_RELAXED_TASKLIST_MATCHING: &str = "relaxed_tasklist_matching";
11
20
  const PARSE_RELAXED_AUTOLINKS: &str = "relaxed_autolinks";
12
21
  const PARSE_LEAVE_FOOTNOTE_DEFINITIONS: &str = "leave_footnote_definitions";
22
+ const PARSE_IGNORE_SETEXT: &str = "ignore_setext";
13
23
  const PARSE_SOURCEPOS_CHARS: &str = "sourcepos_chars";
14
-
15
- pub fn iterate_parse_options(comrak_options: &mut comrak::options::Parse, options_hash: RHash) {
16
- options_hash
17
- .foreach(|key: Symbol, value: Value| {
18
- match key.name()? {
19
- Cow::Borrowed(PARSE_SMART) => {
20
- comrak_options.smart = TryConvert::try_convert(value)?;
21
- }
22
- Cow::Borrowed(PARSE_DEFAULT_INFO_STRING) => {
23
- comrak_options.default_info_string = try_convert_string(value);
24
- }
25
- Cow::Borrowed(PARSE_RELAXED_TASKLIST_MATCHING) => {
26
- comrak_options.relaxed_tasklist_matching = TryConvert::try_convert(value)?;
27
- }
28
- Cow::Borrowed(RENDER_IGNORE_SETEXT) => {
29
- comrak_options.ignore_setext = TryConvert::try_convert(value)?;
30
- }
31
- Cow::Borrowed(PARSE_RELAXED_AUTOLINKS) => {
32
- comrak_options.relaxed_autolinks = TryConvert::try_convert(value)?;
33
- }
34
- Cow::Borrowed(PARSE_LEAVE_FOOTNOTE_DEFINITIONS) => {
35
- comrak_options.leave_footnote_definitions = TryConvert::try_convert(value)?;
36
- }
37
- Cow::Borrowed(PARSE_SOURCEPOS_CHARS) => {
38
- comrak_options.sourcepos_chars = TryConvert::try_convert(value)?;
39
- }
40
- _ => {}
41
- }
42
- Ok(ForEach::Continue)
43
- })
44
- .unwrap();
45
- }
24
+ const PARSE_MAX_NESTING_DEPTH: &str = "max_nesting_depth";
25
+ const PARSE_TASKLIST_IN_TABLE: &str = "tasklist_in_table";
26
+ const PARSE_ESCAPED_CHAR_SPANS: &str = "escaped_char_spans";
46
27
 
47
28
  const RENDER_HARDBREAKS: &str = "hardbreaks";
48
29
  const RENDER_GITHUB_PRE_LANG: &str = "github_pre_lang";
@@ -52,72 +33,24 @@ const RENDER_UNSAFE: &str = "unsafe";
52
33
  const RENDER_ESCAPE: &str = "escape";
53
34
  const RENDER_SOURCEPOS: &str = "sourcepos";
54
35
  const RENDER_ESCAPED_CHAR_SPANS: &str = "escaped_char_spans";
55
- const RENDER_IGNORE_SETEXT: &str = "ignore_setext";
56
36
  const RENDER_IGNORE_EMPTY_LINKS: &str = "ignore_empty_links";
57
37
  const RENDER_GFM_QUIRKS: &str = "gfm_quirks";
58
38
  const RENDER_PREFER_FENCED: &str = "prefer_fenced";
59
39
  const RENDER_TASKLIST_CLASSES: &str = "tasklist_classes";
60
40
  const RENDER_COMPACT_HTML: &str = "compact_html";
61
41
  const RENDER_ALERT_STYLE: &str = "alert_style";
42
+ const RENDER_LIST_STYLE: &str = "list_style";
43
+ const RENDER_FIGURE_WITH_CAPTION: &str = "figure_with_caption";
44
+ const RENDER_OL_WIDTH: &str = "ol_width";
45
+ const RENDER_EXPERIMENTAL_MINIMIZE_COMMONMARK: &str = "experimental_minimize_commonmark";
46
+ const RENDER_ATTRIBUTES: &str = "attributes";
62
47
 
63
- pub fn iterate_render_options(comrak_options: &mut comrak::options::Render, options_hash: RHash) {
64
- options_hash
65
- .foreach(|key: Symbol, value: Value| {
66
- match key.name()? {
67
- Cow::Borrowed(RENDER_HARDBREAKS) => {
68
- comrak_options.hardbreaks = TryConvert::try_convert(value)?;
69
- }
70
- Cow::Borrowed(RENDER_GITHUB_PRE_LANG) => {
71
- comrak_options.github_pre_lang = TryConvert::try_convert(value)?;
72
- }
73
- Cow::Borrowed(RENDER_FULL_INFO_STRING) => {
74
- comrak_options.full_info_string = TryConvert::try_convert(value)?;
75
- }
76
- Cow::Borrowed(RENDER_WIDTH) => {
77
- comrak_options.width = TryConvert::try_convert(value)?;
78
- }
79
- Cow::Borrowed(RENDER_UNSAFE) => {
80
- comrak_options.r#unsafe = TryConvert::try_convert(value)?;
81
- }
82
- Cow::Borrowed(RENDER_ESCAPE) => {
83
- comrak_options.escape = TryConvert::try_convert(value)?;
84
- }
85
- Cow::Borrowed(RENDER_SOURCEPOS) => {
86
- comrak_options.sourcepos = TryConvert::try_convert(value)?;
87
- }
88
- Cow::Borrowed(RENDER_ESCAPED_CHAR_SPANS) => {
89
- comrak_options.escaped_char_spans = TryConvert::try_convert(value)?;
90
- }
91
- Cow::Borrowed(RENDER_IGNORE_EMPTY_LINKS) => {
92
- comrak_options.ignore_empty_links = TryConvert::try_convert(value)?;
93
- }
94
- Cow::Borrowed(RENDER_GFM_QUIRKS) => {
95
- comrak_options.gfm_quirks = TryConvert::try_convert(value)?;
96
- }
97
- Cow::Borrowed(RENDER_PREFER_FENCED) => {
98
- comrak_options.prefer_fenced = TryConvert::try_convert(value)?;
99
- }
100
- Cow::Borrowed(RENDER_TASKLIST_CLASSES) => {
101
- comrak_options.tasklist_classes = TryConvert::try_convert(value)?;
102
- }
103
- Cow::Borrowed(RENDER_COMPACT_HTML) => {
104
- comrak_options.compact_html = TryConvert::try_convert(value)?;
105
- }
106
- Cow::Borrowed(RENDER_ALERT_STYLE) => {
107
- comrak_options.alert_style = match try_convert_string(value).as_deref() {
108
- Some("semantic") => comrak::options::AlertStyleType::Semantic,
109
- _ => comrak::options::AlertStyleType::Specific,
110
- };
111
- }
112
- _ => {}
113
- }
114
- Ok(ForEach::Continue)
115
- })
116
- .unwrap();
117
- }
48
+ /// The Symbols `alert_style` accepts.
49
+ const ALERT_STYLES: &[&str] = &["specific", "semantic"];
50
+ /// The Symbols `list_style` accepts.
51
+ const LIST_STYLES: &[&str] = &["dash", "plus", "star"];
118
52
 
119
53
  const EXTENSION_STRIKETHROUGH: &str = "strikethrough";
120
- const EXTENSION_TAGFILTER: &str = "tagfilter";
121
54
  const EXTENSION_TABLE: &str = "table";
122
55
  const EXTENSION_AUTOLINK: &str = "autolink";
123
56
  const EXTENSION_TASKLIST: &str = "tasklist";
@@ -145,108 +78,1305 @@ const EXTENSION_CJK_FRIENDLY_EMPHASIS: &str = "cjk_friendly_emphasis";
145
78
  const EXTENSION_HIGHLIGHT: &str = "highlight";
146
79
  const EXTENSION_INSERT: &str = "insert";
147
80
  const EXTENSION_BLOCK_DIRECTIVE: &str = "block_directive";
81
+ const EXTENSION_MATH_DOLLARS_BLOCK: &str = "math_dollars_block";
82
+ const EXTENSION_WIKILINKS_IGNORE_EMBEDS: &str = "wikilinks_ignore_embeds";
83
+ const EXTENSION_OFM_CALLOUTS: &str = "ofm_callouts";
84
+ const EXTENSION_OFM_COMMENTS: &str = "ofm_comments";
85
+ const EXTENSION_OFM_BLOCK_IDS: &str = "ofm_block_ids";
86
+ const EXTENSION_HEADER_ATTRIBUTES: &str = "header_attributes";
87
+ const EXTENSION_FENCED_CODE_ATTRIBUTES: &str = "fenced_code_attributes";
88
+ const EXTENSION_INLINE_CODE_ATTRIBUTES: &str = "inline_code_attributes";
89
+ const EXTENSION_LINK_ATTRIBUTES: &str = "link_attributes";
148
90
 
149
- pub fn iterate_extension_options(
150
- comrak_options: &mut comrak::options::Extension,
91
+ /// Applies a validated option: on `Ok` sets the field, on `Err` records the
92
+ /// error in `error` and stops iteration. Expands inside an `RHash::foreach`
93
+ /// closure; pass the closure's `error` local as the first argument.
94
+ macro_rules! opt {
95
+ ($error:expr, $field:expr, $result:expr) => {
96
+ match $result {
97
+ Ok(v) => $field = v,
98
+ Err(e) => {
99
+ *$error = Some(e);
100
+ return Ok(ForEach::Stop);
101
+ }
102
+ }
103
+ };
104
+ }
105
+
106
+ /// Validates a boolean option value.
107
+ /// Returns the value if valid (true or false), default_for_nil if nil, Err if invalid type.
108
+ fn validate_bool(
109
+ ruby: &Ruby,
110
+ value: Value,
111
+ key: &str,
112
+ category: &str,
113
+ default_for_nil: bool,
114
+ ) -> Result<bool, Error> {
115
+ if value.is_nil() {
116
+ return Ok(default_for_nil);
117
+ }
118
+ // Strictly check for TrueClass or FalseClass, reject other types
119
+ let class_name = crate::utils::get_classname(value);
120
+ match class_name.as_ref() {
121
+ "TrueClass" => Ok(true),
122
+ "FalseClass" => Ok(false),
123
+ _ => Err(Error::new(
124
+ ruby.exception_type_error(),
125
+ format!(
126
+ "{} option `{}` must be Boolean; got {}",
127
+ category,
128
+ format_args!(":{}", key),
129
+ class_name
130
+ ),
131
+ )),
132
+ }
133
+ }
134
+
135
+ /// Validates a non-negative integer option value.
136
+ /// Returns the value if valid, default_for_nil if nil, Err if invalid type or negative.
137
+ fn validate_usize(
138
+ ruby: &Ruby,
139
+ value: Value,
140
+ key: &str,
141
+ category: &str,
142
+ default_for_nil: usize,
143
+ ) -> Result<usize, Error> {
144
+ if value.is_nil() {
145
+ return Ok(default_for_nil);
146
+ }
147
+ let n: i64 = TryConvert::try_convert(value).map_err(|_| {
148
+ Error::new(
149
+ ruby.exception_type_error(),
150
+ format!(
151
+ "{} option `{}` must be Integer; got {}",
152
+ category,
153
+ format_args!(":{}", key),
154
+ crate::utils::get_classname(value)
155
+ ),
156
+ )
157
+ })?;
158
+ usize::try_from(n).map_err(|_| {
159
+ Error::new(
160
+ ruby.exception_arg_error(),
161
+ format!(
162
+ "{} option `{}` must be a non-negative Integer; got {}",
163
+ category,
164
+ format_args!(":{}", key),
165
+ n
166
+ ),
167
+ )
168
+ })
169
+ }
170
+
171
+ /// Validates an optional string option value (for Option<String> fields).
172
+ /// nil means None (disable the feature), valid string means Some(string).
173
+ fn validate_optional_string(
174
+ ruby: &Ruby,
175
+ value: Value,
176
+ key: &str,
177
+ category: &str,
178
+ ) -> Result<Option<String>, Error> {
179
+ if value.is_nil() {
180
+ return Ok(None); // nil means disable the feature
181
+ }
182
+ let s: String = TryConvert::try_convert(value).map_err(|_| {
183
+ Error::new(
184
+ ruby.exception_type_error(),
185
+ format!(
186
+ "{} option `{}` must be String; got {}",
187
+ category,
188
+ format_args!(":{}", key),
189
+ crate::utils::get_classname(value)
190
+ ),
191
+ )
192
+ })?;
193
+ Ok(Some(s))
194
+ }
195
+
196
+ /// Validates an option that takes one of a fixed set of Symbols (for enum-like
197
+ /// fields such as `list_style`). nil means "keep the default". Returns the
198
+ /// matched name, or Err if the value is not a Symbol or is not one of
199
+ /// `allowed`.
200
+ fn validate_enum(
201
+ ruby: &Ruby,
202
+ value: Value,
203
+ key: &str,
204
+ category: &str,
205
+ allowed: &[&str],
206
+ ) -> Result<Option<String>, Error> {
207
+ if value.is_nil() {
208
+ return Ok(None);
209
+ }
210
+ let Some(symbol) = Symbol::from_value(value) else {
211
+ return Err(Error::new(
212
+ ruby.exception_type_error(),
213
+ format!(
214
+ "{} option `{}` must be Symbol; got {}",
215
+ category,
216
+ format_args!(":{}", key),
217
+ crate::utils::get_classname(value)
218
+ ),
219
+ ));
220
+ };
221
+ let name = symbol.name()?.into_owned();
222
+ if allowed.contains(&name.as_str()) {
223
+ return Ok(Some(name));
224
+ }
225
+ Err(Error::new(
226
+ ruby.exception_arg_error(),
227
+ format!(
228
+ "{} option `{}` must be one of {}; got :{}",
229
+ category,
230
+ format_args!(":{}", key),
231
+ allowed
232
+ .iter()
233
+ .map(|a| format!(":{a}"))
234
+ .collect::<Vec<_>>()
235
+ .join(", "),
236
+ name
237
+ ),
238
+ ))
239
+ }
240
+
241
+ /// The options Commonmarker accepts: comrak's own, plus `max_nesting_depth`,
242
+ /// which Commonmarker enforces itself (see `crate::nesting`).
243
+ pub struct Options {
244
+ pub comrak: comrak::Options<'static>,
245
+ pub max_nesting_depth: usize,
246
+ }
247
+
248
+ impl Options {
249
+ /// comrak's defaults, plus Commonmarker's own nesting limit.
250
+ fn defaults() -> Self {
251
+ Options {
252
+ comrak: DEFAULT_OPTIONS.clone(),
253
+ max_nesting_depth: DEFAULT_MAX_NESTING_DEPTH,
254
+ }
255
+ }
256
+ }
257
+
258
+ /// Formats and validates options from Ruby, merging with defaults.
259
+ /// Accepts Option<Value> and returns Options.
260
+ ///
261
+ /// Logic:
262
+ /// - If no options: use DEFAULT_OPTIONS (comrak's defaults)
263
+ /// - If options provided but category absent: use DEFAULT_OPTIONS for that category
264
+ /// - If category provided: start with the defaults, apply non-nil values from user
265
+ /// (nil values mean "keep default for this field")
266
+ pub fn format_options(ruby: &Ruby, rb_options: Option<Value>) -> Result<Options, Error> {
267
+ let rb_options = match rb_options {
268
+ None => return Ok(Options::defaults()),
269
+ Some(v) if v.is_nil() => return Ok(Options::defaults()),
270
+ Some(v) => v,
271
+ };
272
+
273
+ let options_hash: RHash = TryConvert::try_convert(rb_options).map_err(|_| {
274
+ Error::new(
275
+ ruby.exception_type_error(),
276
+ format!(
277
+ "options must be a Hash; got {}",
278
+ crate::utils::get_classname(rb_options)
279
+ ),
280
+ )
281
+ })?;
282
+
283
+ if options_hash.is_empty() {
284
+ return Ok(Options::defaults());
285
+ }
286
+
287
+ // Start with the defaults
288
+ let mut options = Options::defaults();
289
+
290
+ // Process parse options
291
+ match options_hash.get(ruby.to_symbol(PARSE)) {
292
+ None => {
293
+ // Category not provided, keep the defaults (already set)
294
+ }
295
+ Some(parse_value) if parse_value.is_nil() => {
296
+ // Category explicitly nil, keep the defaults (already set)
297
+ }
298
+ Some(parse_value) => {
299
+ // Category provided, apply user values over the defaults
300
+ let parse_hash: RHash = TryConvert::try_convert(parse_value).map_err(|_| {
301
+ Error::new(
302
+ ruby.exception_type_error(),
303
+ format!(
304
+ "parse options must be a Hash; got {}",
305
+ crate::utils::get_classname(parse_value)
306
+ ),
307
+ )
308
+ })?;
309
+ iterate_parse_options_with_validation(
310
+ ruby,
311
+ &mut options.comrak.parse,
312
+ &mut options.max_nesting_depth,
313
+ parse_hash,
314
+ )?;
315
+ }
316
+ }
317
+
318
+ // Process render options
319
+ match options_hash.get(ruby.to_symbol(RENDER)) {
320
+ None => {
321
+ // Keep the defaults (already set)
322
+ }
323
+ Some(render_value) if render_value.is_nil() => {
324
+ // Category explicitly nil, keep the defaults (already set)
325
+ }
326
+ Some(render_value) => {
327
+ let render_hash: RHash = TryConvert::try_convert(render_value).map_err(|_| {
328
+ Error::new(
329
+ ruby.exception_type_error(),
330
+ format!(
331
+ "render options must be a Hash; got {}",
332
+ crate::utils::get_classname(render_value)
333
+ ),
334
+ )
335
+ })?;
336
+ iterate_render_options_with_validation(ruby, &mut options.comrak.render, render_hash)?;
337
+ }
338
+ }
339
+
340
+ // Process extension options
341
+ match options_hash.get(ruby.to_symbol(EXTENSION)) {
342
+ None => {
343
+ // Keep the defaults (already set)
344
+ }
345
+ Some(extension_value) if extension_value.is_nil() => {
346
+ // Category explicitly nil, keep the defaults (already set)
347
+ }
348
+ Some(extension_value) => {
349
+ let extension_hash: RHash = TryConvert::try_convert(extension_value).map_err(|_| {
350
+ Error::new(
351
+ ruby.exception_type_error(),
352
+ format!(
353
+ "extension options must be a Hash; got {}",
354
+ crate::utils::get_classname(extension_value)
355
+ ),
356
+ )
357
+ })?;
358
+ iterate_extension_options_with_validation(
359
+ ruby,
360
+ &mut options.comrak.extension,
361
+ extension_hash,
362
+ )?;
363
+ }
364
+ }
365
+
366
+ Ok(options)
367
+ }
368
+
369
+ fn iterate_parse_options_with_validation(
370
+ ruby: &Ruby,
371
+ comrak_options: &mut comrak::options::Parse,
372
+ max_nesting_depth: &mut usize,
151
373
  options_hash: RHash,
152
- ) {
153
- options_hash
154
- .foreach(|key: Symbol, value: Value| {
155
- match key.name()? {
156
- Cow::Borrowed(EXTENSION_STRIKETHROUGH) => {
157
- comrak_options.strikethrough = TryConvert::try_convert(value)?;
158
- }
159
- Cow::Borrowed(EXTENSION_TAGFILTER) => {
160
- comrak_options.tagfilter = TryConvert::try_convert(value)?;
161
- }
162
- Cow::Borrowed(EXTENSION_TABLE) => {
163
- comrak_options.table = TryConvert::try_convert(value)?;
164
- }
165
- Cow::Borrowed(EXTENSION_AUTOLINK) => {
166
- comrak_options.autolink = TryConvert::try_convert(value)?;
167
- }
168
- Cow::Borrowed(EXTENSION_TASKLIST) => {
169
- comrak_options.tasklist = TryConvert::try_convert(value)?;
170
- }
171
- Cow::Borrowed(EXTENSION_SUPERSCRIPT) => {
172
- comrak_options.superscript = TryConvert::try_convert(value)?;
173
- }
174
- Cow::Borrowed(EXTENSION_HEADER_IDS) => {
175
- comrak_options.header_id_prefix = try_convert_string(value);
176
- }
177
- Cow::Borrowed(EXTENSION_HEADER_ID_PREFIX_IN_HREF) => {
178
- comrak_options.header_id_prefix_in_href = TryConvert::try_convert(value)?;
179
- }
180
- Cow::Borrowed(EXTENSION_FOOTNOTES) => {
181
- comrak_options.footnotes = TryConvert::try_convert(value)?;
182
- }
183
- Cow::Borrowed(EXTENSION_INLINE_FOOTNOTES) => {
184
- comrak_options.inline_footnotes = TryConvert::try_convert(value)?;
185
- }
186
- Cow::Borrowed(EXTENSION_DESCRIPTION_LISTS) => {
187
- comrak_options.description_lists = TryConvert::try_convert(value)?;
188
- }
189
- Cow::Borrowed(EXTENSION_FRONT_MATTER_DELIMITER) => {
190
- if let Some(option) = try_convert_string(value) {
191
- if !option.is_empty() {
192
- comrak_options.front_matter_delimiter = Some(option);
374
+ ) -> Result<(), Error> {
375
+ let mut error: Option<Error> = None;
376
+ let defaults = &DEFAULT_OPTIONS.parse;
377
+
378
+ options_hash.foreach(|key: Symbol, value: Value| {
379
+ match key.name()?.as_ref() {
380
+ PARSE_SMART => opt!(
381
+ &mut error,
382
+ comrak_options.smart,
383
+ validate_bool(ruby, value, PARSE_SMART, "parse", defaults.smart)
384
+ ),
385
+ PARSE_DEFAULT_INFO_STRING => opt!(
386
+ &mut error,
387
+ comrak_options.default_info_string,
388
+ validate_optional_string(ruby, value, PARSE_DEFAULT_INFO_STRING, "parse")
389
+ ),
390
+ PARSE_RELAXED_TASKLIST_MATCHING => opt!(
391
+ &mut error,
392
+ comrak_options.relaxed_tasklist_matching,
393
+ validate_bool(
394
+ ruby,
395
+ value,
396
+ PARSE_RELAXED_TASKLIST_MATCHING,
397
+ "parse",
398
+ defaults.relaxed_tasklist_matching,
399
+ )
400
+ ),
401
+ PARSE_IGNORE_SETEXT => opt!(
402
+ &mut error,
403
+ comrak_options.ignore_setext,
404
+ validate_bool(
405
+ ruby,
406
+ value,
407
+ PARSE_IGNORE_SETEXT,
408
+ "parse",
409
+ defaults.ignore_setext,
410
+ )
411
+ ),
412
+ PARSE_RELAXED_AUTOLINKS => opt!(
413
+ &mut error,
414
+ comrak_options.relaxed_autolinks,
415
+ validate_bool(
416
+ ruby,
417
+ value,
418
+ PARSE_RELAXED_AUTOLINKS,
419
+ "parse",
420
+ defaults.relaxed_autolinks,
421
+ )
422
+ ),
423
+ PARSE_LEAVE_FOOTNOTE_DEFINITIONS => opt!(
424
+ &mut error,
425
+ comrak_options.leave_footnote_definitions,
426
+ validate_bool(
427
+ ruby,
428
+ value,
429
+ PARSE_LEAVE_FOOTNOTE_DEFINITIONS,
430
+ "parse",
431
+ defaults.leave_footnote_definitions,
432
+ )
433
+ ),
434
+ PARSE_SOURCEPOS_CHARS => opt!(
435
+ &mut error,
436
+ comrak_options.sourcepos_chars,
437
+ validate_bool(
438
+ ruby,
439
+ value,
440
+ PARSE_SOURCEPOS_CHARS,
441
+ "parse",
442
+ defaults.sourcepos_chars,
443
+ )
444
+ ),
445
+ PARSE_MAX_NESTING_DEPTH => opt!(
446
+ &mut error,
447
+ *max_nesting_depth,
448
+ validate_usize(
449
+ ruby,
450
+ value,
451
+ PARSE_MAX_NESTING_DEPTH,
452
+ "parse",
453
+ DEFAULT_MAX_NESTING_DEPTH,
454
+ )
455
+ ),
456
+ PARSE_TASKLIST_IN_TABLE => opt!(
457
+ &mut error,
458
+ comrak_options.tasklist_in_table,
459
+ validate_bool(
460
+ ruby,
461
+ value,
462
+ PARSE_TASKLIST_IN_TABLE,
463
+ "parse",
464
+ defaults.tasklist_in_table,
465
+ )
466
+ ),
467
+ PARSE_ESCAPED_CHAR_SPANS => opt!(
468
+ &mut error,
469
+ comrak_options.escaped_char_spans,
470
+ validate_bool(
471
+ ruby,
472
+ value,
473
+ PARSE_ESCAPED_CHAR_SPANS,
474
+ "parse",
475
+ defaults.escaped_char_spans,
476
+ )
477
+ ),
478
+ _ => {}
479
+ }
480
+ Ok(ForEach::Continue)
481
+ })?;
482
+
483
+ match error {
484
+ Some(e) => Err(e),
485
+ None => Ok(()),
486
+ }
487
+ }
488
+
489
+ fn iterate_render_options_with_validation(
490
+ ruby: &Ruby,
491
+ comrak_options: &mut comrak::options::Render,
492
+ options_hash: RHash,
493
+ ) -> Result<(), Error> {
494
+ let mut error: Option<Error> = None;
495
+ let defaults = &DEFAULT_OPTIONS.render;
496
+
497
+ options_hash.foreach(|key: Symbol, value: Value| {
498
+ match key.name()?.as_ref() {
499
+ RENDER_HARDBREAKS => opt!(
500
+ &mut error,
501
+ comrak_options.hardbreaks,
502
+ validate_bool(
503
+ ruby,
504
+ value,
505
+ RENDER_HARDBREAKS,
506
+ "render",
507
+ defaults.hardbreaks,
508
+ )
509
+ ),
510
+ RENDER_GITHUB_PRE_LANG => opt!(
511
+ &mut error,
512
+ comrak_options.github_pre_lang,
513
+ validate_bool(
514
+ ruby,
515
+ value,
516
+ RENDER_GITHUB_PRE_LANG,
517
+ "render",
518
+ defaults.github_pre_lang,
519
+ )
520
+ ),
521
+ RENDER_FULL_INFO_STRING => opt!(
522
+ &mut error,
523
+ comrak_options.full_info_string,
524
+ validate_bool(
525
+ ruby,
526
+ value,
527
+ RENDER_FULL_INFO_STRING,
528
+ "render",
529
+ defaults.full_info_string,
530
+ )
531
+ ),
532
+ RENDER_WIDTH => opt!(
533
+ &mut error,
534
+ comrak_options.width,
535
+ validate_usize(ruby, value, RENDER_WIDTH, "render", defaults.width)
536
+ ),
537
+ RENDER_UNSAFE => opt!(
538
+ &mut error,
539
+ comrak_options.r#unsafe,
540
+ validate_bool(ruby, value, RENDER_UNSAFE, "render", defaults.r#unsafe)
541
+ ),
542
+ RENDER_ESCAPE => opt!(
543
+ &mut error,
544
+ comrak_options.escape,
545
+ validate_bool(ruby, value, RENDER_ESCAPE, "render", defaults.escape)
546
+ ),
547
+ RENDER_SOURCEPOS => opt!(
548
+ &mut error,
549
+ comrak_options.sourcepos,
550
+ validate_bool(ruby, value, RENDER_SOURCEPOS, "render", defaults.sourcepos)
551
+ ),
552
+ RENDER_ESCAPED_CHAR_SPANS => opt!(
553
+ &mut error,
554
+ comrak_options.escaped_char_spans,
555
+ validate_bool(
556
+ ruby,
557
+ value,
558
+ RENDER_ESCAPED_CHAR_SPANS,
559
+ "render",
560
+ defaults.escaped_char_spans,
561
+ )
562
+ ),
563
+ RENDER_IGNORE_EMPTY_LINKS => opt!(
564
+ &mut error,
565
+ comrak_options.ignore_empty_links,
566
+ validate_bool(
567
+ ruby,
568
+ value,
569
+ RENDER_IGNORE_EMPTY_LINKS,
570
+ "render",
571
+ defaults.ignore_empty_links,
572
+ )
573
+ ),
574
+ RENDER_GFM_QUIRKS => opt!(
575
+ &mut error,
576
+ comrak_options.gfm_quirks,
577
+ validate_bool(
578
+ ruby,
579
+ value,
580
+ RENDER_GFM_QUIRKS,
581
+ "render",
582
+ defaults.gfm_quirks,
583
+ )
584
+ ),
585
+ RENDER_PREFER_FENCED => opt!(
586
+ &mut error,
587
+ comrak_options.prefer_fenced,
588
+ validate_bool(
589
+ ruby,
590
+ value,
591
+ RENDER_PREFER_FENCED,
592
+ "render",
593
+ defaults.prefer_fenced,
594
+ )
595
+ ),
596
+ RENDER_TASKLIST_CLASSES => opt!(
597
+ &mut error,
598
+ comrak_options.tasklist_classes,
599
+ validate_bool(
600
+ ruby,
601
+ value,
602
+ RENDER_TASKLIST_CLASSES,
603
+ "render",
604
+ defaults.tasklist_classes,
605
+ )
606
+ ),
607
+ RENDER_COMPACT_HTML => opt!(
608
+ &mut error,
609
+ comrak_options.compact_html,
610
+ validate_bool(
611
+ ruby,
612
+ value,
613
+ RENDER_COMPACT_HTML,
614
+ "render",
615
+ defaults.compact_html,
616
+ )
617
+ ),
618
+ RENDER_ALERT_STYLE => {
619
+ match validate_enum(ruby, value, RENDER_ALERT_STYLE, "render", ALERT_STYLES) {
620
+ Ok(v) => {
621
+ comrak_options.alert_style = match v.as_deref() {
622
+ Some("semantic") => comrak::options::AlertStyleType::Semantic,
623
+ Some(_) => comrak::options::AlertStyleType::Specific,
624
+ None => defaults.alert_style,
193
625
  }
194
626
  }
627
+ Err(e) => {
628
+ error = Some(e);
629
+ return Ok(ForEach::Stop);
630
+ }
195
631
  }
196
- Cow::Borrowed(EXTENSION_MULTILINE_BLOCK_QUOTES) => {
197
- comrak_options.multiline_block_quotes = TryConvert::try_convert(value)?;
198
- }
199
- Cow::Borrowed(EXTENSION_MATH_DOLLARS) => {
200
- comrak_options.math_dollars = TryConvert::try_convert(value)?;
201
- }
202
- Cow::Borrowed(EXTENSION_MATH_CODE) => {
203
- comrak_options.math_code = TryConvert::try_convert(value)?;
204
- }
205
- Cow::Borrowed(EXTENSION_MATH_LATEX) => {
206
- comrak_options.math_latex = TryConvert::try_convert(value)?;
207
- }
208
- Cow::Borrowed(EXTENSION_SHORTCODES) => {
209
- comrak_options.shortcodes = TryConvert::try_convert(value)?;
210
- }
211
- Cow::Borrowed(EXTENSION_WIKILINKS_TITLE_AFTER_PIPE) => {
212
- comrak_options.wikilinks_title_after_pipe = TryConvert::try_convert(value)?;
213
- }
214
- Cow::Borrowed(EXTENSION_WIKILINKS_TITLE_BEFORE_PIPE) => {
215
- comrak_options.wikilinks_title_before_pipe = TryConvert::try_convert(value)?;
216
- }
217
- Cow::Borrowed(EXTENSION_UNDERLINE) => {
218
- comrak_options.underline = TryConvert::try_convert(value)?;
219
- }
220
- Cow::Borrowed(EXTENSION_SPOILER) => {
221
- comrak_options.spoiler = TryConvert::try_convert(value)?;
222
- }
223
- Cow::Borrowed(EXTENSION_GREENTEXT) => {
224
- comrak_options.greentext = TryConvert::try_convert(value)?;
225
- }
226
- Cow::Borrowed(EXTENSION_SUBSCRIPT) => {
227
- comrak_options.subscript = TryConvert::try_convert(value)?;
228
- }
229
- Cow::Borrowed(EXTENSION_SUBTEXT) => {
230
- comrak_options.subtext = TryConvert::try_convert(value)?;
231
- }
232
- Cow::Borrowed(EXTENSION_ALERTS) => {
233
- comrak_options.alerts = TryConvert::try_convert(value)?;
234
- }
235
- Cow::Borrowed(EXTENSION_CJK_FRIENDLY_EMPHASIS) => {
236
- comrak_options.cjk_friendly_emphasis = TryConvert::try_convert(value)?;
237
- }
238
- Cow::Borrowed(EXTENSION_HIGHLIGHT) => {
239
- comrak_options.highlight = TryConvert::try_convert(value)?;
240
- }
241
- Cow::Borrowed(EXTENSION_INSERT) => {
242
- comrak_options.insert = TryConvert::try_convert(value)?;
243
- }
244
- Cow::Borrowed(EXTENSION_BLOCK_DIRECTIVE) => {
245
- comrak_options.block_directive = TryConvert::try_convert(value)?;
632
+ }
633
+ RENDER_LIST_STYLE => {
634
+ match validate_enum(ruby, value, RENDER_LIST_STYLE, "render", LIST_STYLES) {
635
+ Ok(v) => {
636
+ comrak_options.list_style = match v.as_deref() {
637
+ Some("plus") => comrak::options::ListStyleType::Plus,
638
+ Some("star") => comrak::options::ListStyleType::Star,
639
+ Some(_) => comrak::options::ListStyleType::Dash,
640
+ None => defaults.list_style,
641
+ }
642
+ }
643
+ Err(e) => {
644
+ error = Some(e);
645
+ return Ok(ForEach::Stop);
646
+ }
246
647
  }
247
- _ => {}
248
648
  }
249
- Ok(ForEach::Continue)
250
- })
251
- .unwrap();
649
+ RENDER_FIGURE_WITH_CAPTION => opt!(
650
+ &mut error,
651
+ comrak_options.figure_with_caption,
652
+ validate_bool(
653
+ ruby,
654
+ value,
655
+ RENDER_FIGURE_WITH_CAPTION,
656
+ "render",
657
+ defaults.figure_with_caption,
658
+ )
659
+ ),
660
+ RENDER_OL_WIDTH => opt!(
661
+ &mut error,
662
+ comrak_options.ol_width,
663
+ validate_usize(ruby, value, RENDER_OL_WIDTH, "render", defaults.ol_width)
664
+ ),
665
+ RENDER_EXPERIMENTAL_MINIMIZE_COMMONMARK => opt!(
666
+ &mut error,
667
+ comrak_options.experimental_minimize_commonmark,
668
+ validate_bool(
669
+ ruby,
670
+ value,
671
+ RENDER_EXPERIMENTAL_MINIMIZE_COMMONMARK,
672
+ "render",
673
+ defaults.experimental_minimize_commonmark,
674
+ )
675
+ ),
676
+ RENDER_ATTRIBUTES => opt!(
677
+ &mut error,
678
+ comrak_options.attributes,
679
+ validate_bool(
680
+ ruby,
681
+ value,
682
+ RENDER_ATTRIBUTES,
683
+ "render",
684
+ defaults.attributes,
685
+ )
686
+ ),
687
+ _ => {}
688
+ }
689
+ Ok(ForEach::Continue)
690
+ })?;
691
+
692
+ match error {
693
+ Some(e) => Err(e),
694
+ None => Ok(()),
695
+ }
696
+ }
697
+
698
+ fn iterate_extension_options_with_validation(
699
+ ruby: &Ruby,
700
+ comrak_options: &mut comrak::options::Extension,
701
+ options_hash: RHash,
702
+ ) -> Result<(), Error> {
703
+ let mut error: Option<Error> = None;
704
+ let defaults = &DEFAULT_OPTIONS.extension;
705
+
706
+ options_hash.foreach(|key: Symbol, value: Value| {
707
+ match key.name()?.as_ref() {
708
+ EXTENSION_STRIKETHROUGH => opt!(
709
+ &mut error,
710
+ comrak_options.strikethrough,
711
+ validate_bool(
712
+ ruby,
713
+ value,
714
+ EXTENSION_STRIKETHROUGH,
715
+ "extension",
716
+ defaults.strikethrough,
717
+ )
718
+ ),
719
+ EXTENSION_TABLE => opt!(
720
+ &mut error,
721
+ comrak_options.table,
722
+ validate_bool(ruby, value, EXTENSION_TABLE, "extension", defaults.table)
723
+ ),
724
+ EXTENSION_AUTOLINK => opt!(
725
+ &mut error,
726
+ comrak_options.autolink,
727
+ validate_bool(
728
+ ruby,
729
+ value,
730
+ EXTENSION_AUTOLINK,
731
+ "extension",
732
+ defaults.autolink,
733
+ )
734
+ ),
735
+ EXTENSION_TASKLIST => opt!(
736
+ &mut error,
737
+ comrak_options.tasklist,
738
+ validate_bool(
739
+ ruby,
740
+ value,
741
+ EXTENSION_TASKLIST,
742
+ "extension",
743
+ defaults.tasklist,
744
+ )
745
+ ),
746
+ EXTENSION_SUPERSCRIPT => opt!(
747
+ &mut error,
748
+ comrak_options.superscript,
749
+ validate_bool(
750
+ ruby,
751
+ value,
752
+ EXTENSION_SUPERSCRIPT,
753
+ "extension",
754
+ defaults.superscript,
755
+ )
756
+ ),
757
+ EXTENSION_HEADER_IDS => opt!(
758
+ &mut error,
759
+ comrak_options.header_id_prefix,
760
+ validate_optional_string(ruby, value, EXTENSION_HEADER_IDS, "extension")
761
+ ),
762
+ EXTENSION_HEADER_ID_PREFIX_IN_HREF => opt!(
763
+ &mut error,
764
+ comrak_options.header_id_prefix_in_href,
765
+ validate_bool(
766
+ ruby,
767
+ value,
768
+ EXTENSION_HEADER_ID_PREFIX_IN_HREF,
769
+ "extension",
770
+ defaults.header_id_prefix_in_href,
771
+ )
772
+ ),
773
+ EXTENSION_FOOTNOTES => opt!(
774
+ &mut error,
775
+ comrak_options.footnotes,
776
+ validate_bool(
777
+ ruby,
778
+ value,
779
+ EXTENSION_FOOTNOTES,
780
+ "extension",
781
+ defaults.footnotes,
782
+ )
783
+ ),
784
+ EXTENSION_INLINE_FOOTNOTES => opt!(
785
+ &mut error,
786
+ comrak_options.inline_footnotes,
787
+ validate_bool(
788
+ ruby,
789
+ value,
790
+ EXTENSION_INLINE_FOOTNOTES,
791
+ "extension",
792
+ defaults.inline_footnotes,
793
+ )
794
+ ),
795
+ EXTENSION_DESCRIPTION_LISTS => opt!(
796
+ &mut error,
797
+ comrak_options.description_lists,
798
+ validate_bool(
799
+ ruby,
800
+ value,
801
+ EXTENSION_DESCRIPTION_LISTS,
802
+ "extension",
803
+ defaults.description_lists,
804
+ )
805
+ ),
806
+ EXTENSION_FRONT_MATTER_DELIMITER => opt!(
807
+ &mut error,
808
+ comrak_options.front_matter_delimiter,
809
+ validate_optional_string(
810
+ ruby,
811
+ value,
812
+ EXTENSION_FRONT_MATTER_DELIMITER,
813
+ "extension",
814
+ )
815
+ ),
816
+ EXTENSION_MULTILINE_BLOCK_QUOTES => opt!(
817
+ &mut error,
818
+ comrak_options.multiline_block_quotes,
819
+ validate_bool(
820
+ ruby,
821
+ value,
822
+ EXTENSION_MULTILINE_BLOCK_QUOTES,
823
+ "extension",
824
+ defaults.multiline_block_quotes,
825
+ )
826
+ ),
827
+ EXTENSION_MATH_DOLLARS => opt!(
828
+ &mut error,
829
+ comrak_options.math_dollars,
830
+ validate_bool(
831
+ ruby,
832
+ value,
833
+ EXTENSION_MATH_DOLLARS,
834
+ "extension",
835
+ defaults.math_dollars,
836
+ )
837
+ ),
838
+ EXTENSION_MATH_CODE => opt!(
839
+ &mut error,
840
+ comrak_options.math_code,
841
+ validate_bool(
842
+ ruby,
843
+ value,
844
+ EXTENSION_MATH_CODE,
845
+ "extension",
846
+ defaults.math_code,
847
+ )
848
+ ),
849
+ EXTENSION_MATH_LATEX => opt!(
850
+ &mut error,
851
+ comrak_options.math_latex,
852
+ validate_bool(
853
+ ruby,
854
+ value,
855
+ EXTENSION_MATH_LATEX,
856
+ "extension",
857
+ defaults.math_latex,
858
+ )
859
+ ),
860
+ EXTENSION_SHORTCODES => opt!(
861
+ &mut error,
862
+ comrak_options.shortcodes,
863
+ validate_bool(
864
+ ruby,
865
+ value,
866
+ EXTENSION_SHORTCODES,
867
+ "extension",
868
+ defaults.shortcodes,
869
+ )
870
+ ),
871
+ EXTENSION_WIKILINKS_TITLE_AFTER_PIPE => opt!(
872
+ &mut error,
873
+ comrak_options.wikilinks_title_after_pipe,
874
+ validate_bool(
875
+ ruby,
876
+ value,
877
+ EXTENSION_WIKILINKS_TITLE_AFTER_PIPE,
878
+ "extension",
879
+ defaults.wikilinks_title_after_pipe,
880
+ )
881
+ ),
882
+ EXTENSION_WIKILINKS_TITLE_BEFORE_PIPE => opt!(
883
+ &mut error,
884
+ comrak_options.wikilinks_title_before_pipe,
885
+ validate_bool(
886
+ ruby,
887
+ value,
888
+ EXTENSION_WIKILINKS_TITLE_BEFORE_PIPE,
889
+ "extension",
890
+ defaults.wikilinks_title_before_pipe,
891
+ )
892
+ ),
893
+ EXTENSION_UNDERLINE => opt!(
894
+ &mut error,
895
+ comrak_options.underline,
896
+ validate_bool(
897
+ ruby,
898
+ value,
899
+ EXTENSION_UNDERLINE,
900
+ "extension",
901
+ defaults.underline,
902
+ )
903
+ ),
904
+ EXTENSION_SPOILER => opt!(
905
+ &mut error,
906
+ comrak_options.spoiler,
907
+ validate_bool(
908
+ ruby,
909
+ value,
910
+ EXTENSION_SPOILER,
911
+ "extension",
912
+ defaults.spoiler,
913
+ )
914
+ ),
915
+ EXTENSION_GREENTEXT => opt!(
916
+ &mut error,
917
+ comrak_options.greentext,
918
+ validate_bool(
919
+ ruby,
920
+ value,
921
+ EXTENSION_GREENTEXT,
922
+ "extension",
923
+ defaults.greentext,
924
+ )
925
+ ),
926
+ EXTENSION_SUBSCRIPT => opt!(
927
+ &mut error,
928
+ comrak_options.subscript,
929
+ validate_bool(
930
+ ruby,
931
+ value,
932
+ EXTENSION_SUBSCRIPT,
933
+ "extension",
934
+ defaults.subscript,
935
+ )
936
+ ),
937
+ EXTENSION_SUBTEXT => opt!(
938
+ &mut error,
939
+ comrak_options.subtext,
940
+ validate_bool(
941
+ ruby,
942
+ value,
943
+ EXTENSION_SUBTEXT,
944
+ "extension",
945
+ defaults.subtext,
946
+ )
947
+ ),
948
+ EXTENSION_ALERTS => opt!(
949
+ &mut error,
950
+ comrak_options.alerts,
951
+ validate_bool(ruby, value, EXTENSION_ALERTS, "extension", defaults.alerts)
952
+ ),
953
+ EXTENSION_CJK_FRIENDLY_EMPHASIS => opt!(
954
+ &mut error,
955
+ comrak_options.cjk_friendly_emphasis,
956
+ validate_bool(
957
+ ruby,
958
+ value,
959
+ EXTENSION_CJK_FRIENDLY_EMPHASIS,
960
+ "extension",
961
+ defaults.cjk_friendly_emphasis,
962
+ )
963
+ ),
964
+ EXTENSION_HIGHLIGHT => opt!(
965
+ &mut error,
966
+ comrak_options.highlight,
967
+ validate_bool(
968
+ ruby,
969
+ value,
970
+ EXTENSION_HIGHLIGHT,
971
+ "extension",
972
+ defaults.highlight,
973
+ )
974
+ ),
975
+ EXTENSION_INSERT => opt!(
976
+ &mut error,
977
+ comrak_options.insert,
978
+ validate_bool(ruby, value, EXTENSION_INSERT, "extension", defaults.insert)
979
+ ),
980
+ EXTENSION_BLOCK_DIRECTIVE => opt!(
981
+ &mut error,
982
+ comrak_options.block_directive,
983
+ validate_bool(
984
+ ruby,
985
+ value,
986
+ EXTENSION_BLOCK_DIRECTIVE,
987
+ "extension",
988
+ defaults.block_directive,
989
+ )
990
+ ),
991
+ EXTENSION_MATH_DOLLARS_BLOCK => opt!(
992
+ &mut error,
993
+ comrak_options.math_dollars_block,
994
+ validate_bool(
995
+ ruby,
996
+ value,
997
+ EXTENSION_MATH_DOLLARS_BLOCK,
998
+ "extension",
999
+ defaults.math_dollars_block,
1000
+ )
1001
+ ),
1002
+ EXTENSION_WIKILINKS_IGNORE_EMBEDS => opt!(
1003
+ &mut error,
1004
+ comrak_options.wikilinks_ignore_embeds,
1005
+ validate_bool(
1006
+ ruby,
1007
+ value,
1008
+ EXTENSION_WIKILINKS_IGNORE_EMBEDS,
1009
+ "extension",
1010
+ defaults.wikilinks_ignore_embeds,
1011
+ )
1012
+ ),
1013
+ EXTENSION_OFM_CALLOUTS => opt!(
1014
+ &mut error,
1015
+ comrak_options.ofm_callouts,
1016
+ validate_bool(
1017
+ ruby,
1018
+ value,
1019
+ EXTENSION_OFM_CALLOUTS,
1020
+ "extension",
1021
+ defaults.ofm_callouts,
1022
+ )
1023
+ ),
1024
+ EXTENSION_OFM_COMMENTS => opt!(
1025
+ &mut error,
1026
+ comrak_options.ofm_comments,
1027
+ validate_bool(
1028
+ ruby,
1029
+ value,
1030
+ EXTENSION_OFM_COMMENTS,
1031
+ "extension",
1032
+ defaults.ofm_comments,
1033
+ )
1034
+ ),
1035
+ EXTENSION_OFM_BLOCK_IDS => opt!(
1036
+ &mut error,
1037
+ comrak_options.ofm_block_ids,
1038
+ validate_bool(
1039
+ ruby,
1040
+ value,
1041
+ EXTENSION_OFM_BLOCK_IDS,
1042
+ "extension",
1043
+ defaults.ofm_block_ids,
1044
+ )
1045
+ ),
1046
+ EXTENSION_HEADER_ATTRIBUTES => opt!(
1047
+ &mut error,
1048
+ comrak_options.header_attributes,
1049
+ validate_bool(
1050
+ ruby,
1051
+ value,
1052
+ EXTENSION_HEADER_ATTRIBUTES,
1053
+ "extension",
1054
+ defaults.header_attributes,
1055
+ )
1056
+ ),
1057
+ EXTENSION_FENCED_CODE_ATTRIBUTES => opt!(
1058
+ &mut error,
1059
+ comrak_options.fenced_code_attributes,
1060
+ validate_bool(
1061
+ ruby,
1062
+ value,
1063
+ EXTENSION_FENCED_CODE_ATTRIBUTES,
1064
+ "extension",
1065
+ defaults.fenced_code_attributes,
1066
+ )
1067
+ ),
1068
+ EXTENSION_INLINE_CODE_ATTRIBUTES => opt!(
1069
+ &mut error,
1070
+ comrak_options.inline_code_attributes,
1071
+ validate_bool(
1072
+ ruby,
1073
+ value,
1074
+ EXTENSION_INLINE_CODE_ATTRIBUTES,
1075
+ "extension",
1076
+ defaults.inline_code_attributes,
1077
+ )
1078
+ ),
1079
+ EXTENSION_LINK_ATTRIBUTES => opt!(
1080
+ &mut error,
1081
+ comrak_options.link_attributes,
1082
+ validate_bool(
1083
+ ruby,
1084
+ value,
1085
+ EXTENSION_LINK_ATTRIBUTES,
1086
+ "extension",
1087
+ defaults.link_attributes,
1088
+ )
1089
+ ),
1090
+ _ => {}
1091
+ }
1092
+ Ok(ForEach::Continue)
1093
+ })?;
1094
+
1095
+ match error {
1096
+ Some(e) => Err(e),
1097
+ None => Ok(()),
1098
+ }
1099
+ }
1100
+
1101
+ /// Returns the default options as a Ruby Hash for introspection. String
1102
+ /// options that are off by default appear as `nil`, which is also how they are
1103
+ /// turned off when passed back in.
1104
+ pub fn default_options_to_hash(ruby: &Ruby) -> Result<RHash, Error> {
1105
+ let options = ruby.hash_new();
1106
+
1107
+ // Parse options
1108
+ let parse = ruby.hash_new();
1109
+ parse.aset(ruby.to_symbol(PARSE_SMART), DEFAULT_OPTIONS.parse.smart)?;
1110
+ parse.aset(
1111
+ ruby.to_symbol(PARSE_DEFAULT_INFO_STRING),
1112
+ DEFAULT_OPTIONS.parse.default_info_string.as_deref(),
1113
+ )?;
1114
+ parse.aset(
1115
+ ruby.to_symbol(PARSE_RELAXED_TASKLIST_MATCHING),
1116
+ DEFAULT_OPTIONS.parse.relaxed_tasklist_matching,
1117
+ )?;
1118
+ parse.aset(
1119
+ ruby.to_symbol(PARSE_RELAXED_AUTOLINKS),
1120
+ DEFAULT_OPTIONS.parse.relaxed_autolinks,
1121
+ )?;
1122
+ parse.aset(
1123
+ ruby.to_symbol(PARSE_LEAVE_FOOTNOTE_DEFINITIONS),
1124
+ DEFAULT_OPTIONS.parse.leave_footnote_definitions,
1125
+ )?;
1126
+ parse.aset(
1127
+ ruby.to_symbol(PARSE_IGNORE_SETEXT),
1128
+ DEFAULT_OPTIONS.parse.ignore_setext,
1129
+ )?;
1130
+ parse.aset(
1131
+ ruby.to_symbol(PARSE_SOURCEPOS_CHARS),
1132
+ DEFAULT_OPTIONS.parse.sourcepos_chars,
1133
+ )?;
1134
+ parse.aset(
1135
+ ruby.to_symbol(PARSE_MAX_NESTING_DEPTH),
1136
+ DEFAULT_MAX_NESTING_DEPTH,
1137
+ )?;
1138
+ parse.aset(
1139
+ ruby.to_symbol(PARSE_TASKLIST_IN_TABLE),
1140
+ DEFAULT_OPTIONS.parse.tasklist_in_table,
1141
+ )?;
1142
+ parse.aset(
1143
+ ruby.to_symbol(PARSE_ESCAPED_CHAR_SPANS),
1144
+ DEFAULT_OPTIONS.parse.escaped_char_spans,
1145
+ )?;
1146
+ options.aset(ruby.to_symbol(PARSE), parse)?;
1147
+
1148
+ // Render options
1149
+ let render = ruby.hash_new();
1150
+ render.aset(
1151
+ ruby.to_symbol(RENDER_HARDBREAKS),
1152
+ DEFAULT_OPTIONS.render.hardbreaks,
1153
+ )?;
1154
+ render.aset(
1155
+ ruby.to_symbol(RENDER_GITHUB_PRE_LANG),
1156
+ DEFAULT_OPTIONS.render.github_pre_lang,
1157
+ )?;
1158
+ render.aset(
1159
+ ruby.to_symbol(RENDER_FULL_INFO_STRING),
1160
+ DEFAULT_OPTIONS.render.full_info_string,
1161
+ )?;
1162
+ render.aset(ruby.to_symbol(RENDER_WIDTH), DEFAULT_OPTIONS.render.width)?;
1163
+ render.aset(
1164
+ ruby.to_symbol(RENDER_UNSAFE),
1165
+ DEFAULT_OPTIONS.render.r#unsafe,
1166
+ )?;
1167
+ render.aset(ruby.to_symbol(RENDER_ESCAPE), DEFAULT_OPTIONS.render.escape)?;
1168
+ render.aset(
1169
+ ruby.to_symbol(RENDER_SOURCEPOS),
1170
+ DEFAULT_OPTIONS.render.sourcepos,
1171
+ )?;
1172
+ render.aset(
1173
+ ruby.to_symbol(RENDER_ESCAPED_CHAR_SPANS),
1174
+ DEFAULT_OPTIONS.render.escaped_char_spans,
1175
+ )?;
1176
+ render.aset(
1177
+ ruby.to_symbol(RENDER_IGNORE_EMPTY_LINKS),
1178
+ DEFAULT_OPTIONS.render.ignore_empty_links,
1179
+ )?;
1180
+ render.aset(
1181
+ ruby.to_symbol(RENDER_GFM_QUIRKS),
1182
+ DEFAULT_OPTIONS.render.gfm_quirks,
1183
+ )?;
1184
+ render.aset(
1185
+ ruby.to_symbol(RENDER_PREFER_FENCED),
1186
+ DEFAULT_OPTIONS.render.prefer_fenced,
1187
+ )?;
1188
+ render.aset(
1189
+ ruby.to_symbol(RENDER_TASKLIST_CLASSES),
1190
+ DEFAULT_OPTIONS.render.tasklist_classes,
1191
+ )?;
1192
+ render.aset(
1193
+ ruby.to_symbol(RENDER_COMPACT_HTML),
1194
+ DEFAULT_OPTIONS.render.compact_html,
1195
+ )?;
1196
+ render.aset(
1197
+ ruby.to_symbol(RENDER_ALERT_STYLE),
1198
+ ruby.to_symbol(match DEFAULT_OPTIONS.render.alert_style {
1199
+ comrak::options::AlertStyleType::Semantic => "semantic",
1200
+ _ => "specific",
1201
+ }),
1202
+ )?;
1203
+ render.aset(
1204
+ ruby.to_symbol(RENDER_LIST_STYLE),
1205
+ ruby.to_symbol(match DEFAULT_OPTIONS.render.list_style {
1206
+ comrak::options::ListStyleType::Plus => "plus",
1207
+ comrak::options::ListStyleType::Star => "star",
1208
+ _ => "dash",
1209
+ }),
1210
+ )?;
1211
+ render.aset(
1212
+ ruby.to_symbol(RENDER_FIGURE_WITH_CAPTION),
1213
+ DEFAULT_OPTIONS.render.figure_with_caption,
1214
+ )?;
1215
+ render.aset(
1216
+ ruby.to_symbol(RENDER_OL_WIDTH),
1217
+ DEFAULT_OPTIONS.render.ol_width,
1218
+ )?;
1219
+ render.aset(
1220
+ ruby.to_symbol(RENDER_EXPERIMENTAL_MINIMIZE_COMMONMARK),
1221
+ DEFAULT_OPTIONS.render.experimental_minimize_commonmark,
1222
+ )?;
1223
+ render.aset(
1224
+ ruby.to_symbol(RENDER_ATTRIBUTES),
1225
+ DEFAULT_OPTIONS.render.attributes,
1226
+ )?;
1227
+ options.aset(ruby.to_symbol(RENDER), render)?;
1228
+
1229
+ // Extension options
1230
+ let extension = ruby.hash_new();
1231
+ extension.aset(
1232
+ ruby.to_symbol(EXTENSION_STRIKETHROUGH),
1233
+ DEFAULT_OPTIONS.extension.strikethrough,
1234
+ )?;
1235
+ extension.aset(
1236
+ ruby.to_symbol(EXTENSION_TABLE),
1237
+ DEFAULT_OPTIONS.extension.table,
1238
+ )?;
1239
+ extension.aset(
1240
+ ruby.to_symbol(EXTENSION_AUTOLINK),
1241
+ DEFAULT_OPTIONS.extension.autolink,
1242
+ )?;
1243
+ extension.aset(
1244
+ ruby.to_symbol(EXTENSION_TASKLIST),
1245
+ DEFAULT_OPTIONS.extension.tasklist,
1246
+ )?;
1247
+ extension.aset(
1248
+ ruby.to_symbol(EXTENSION_SUPERSCRIPT),
1249
+ DEFAULT_OPTIONS.extension.superscript,
1250
+ )?;
1251
+ extension.aset(
1252
+ ruby.to_symbol(EXTENSION_HEADER_IDS),
1253
+ DEFAULT_OPTIONS.extension.header_id_prefix.as_deref(),
1254
+ )?;
1255
+ extension.aset(
1256
+ ruby.to_symbol(EXTENSION_HEADER_ID_PREFIX_IN_HREF),
1257
+ DEFAULT_OPTIONS.extension.header_id_prefix_in_href,
1258
+ )?;
1259
+ extension.aset(
1260
+ ruby.to_symbol(EXTENSION_FOOTNOTES),
1261
+ DEFAULT_OPTIONS.extension.footnotes,
1262
+ )?;
1263
+ extension.aset(
1264
+ ruby.to_symbol(EXTENSION_INLINE_FOOTNOTES),
1265
+ DEFAULT_OPTIONS.extension.inline_footnotes,
1266
+ )?;
1267
+ extension.aset(
1268
+ ruby.to_symbol(EXTENSION_DESCRIPTION_LISTS),
1269
+ DEFAULT_OPTIONS.extension.description_lists,
1270
+ )?;
1271
+ extension.aset(
1272
+ ruby.to_symbol(EXTENSION_FRONT_MATTER_DELIMITER),
1273
+ DEFAULT_OPTIONS.extension.front_matter_delimiter.as_deref(),
1274
+ )?;
1275
+ extension.aset(
1276
+ ruby.to_symbol(EXTENSION_MULTILINE_BLOCK_QUOTES),
1277
+ DEFAULT_OPTIONS.extension.multiline_block_quotes,
1278
+ )?;
1279
+ extension.aset(
1280
+ ruby.to_symbol(EXTENSION_MATH_DOLLARS),
1281
+ DEFAULT_OPTIONS.extension.math_dollars,
1282
+ )?;
1283
+ extension.aset(
1284
+ ruby.to_symbol(EXTENSION_MATH_CODE),
1285
+ DEFAULT_OPTIONS.extension.math_code,
1286
+ )?;
1287
+ extension.aset(
1288
+ ruby.to_symbol(EXTENSION_MATH_LATEX),
1289
+ DEFAULT_OPTIONS.extension.math_latex,
1290
+ )?;
1291
+ extension.aset(
1292
+ ruby.to_symbol(EXTENSION_SHORTCODES),
1293
+ DEFAULT_OPTIONS.extension.shortcodes,
1294
+ )?;
1295
+ extension.aset(
1296
+ ruby.to_symbol(EXTENSION_WIKILINKS_TITLE_BEFORE_PIPE),
1297
+ DEFAULT_OPTIONS.extension.wikilinks_title_before_pipe,
1298
+ )?;
1299
+ extension.aset(
1300
+ ruby.to_symbol(EXTENSION_WIKILINKS_TITLE_AFTER_PIPE),
1301
+ DEFAULT_OPTIONS.extension.wikilinks_title_after_pipe,
1302
+ )?;
1303
+ extension.aset(
1304
+ ruby.to_symbol(EXTENSION_UNDERLINE),
1305
+ DEFAULT_OPTIONS.extension.underline,
1306
+ )?;
1307
+ extension.aset(
1308
+ ruby.to_symbol(EXTENSION_SPOILER),
1309
+ DEFAULT_OPTIONS.extension.spoiler,
1310
+ )?;
1311
+ extension.aset(
1312
+ ruby.to_symbol(EXTENSION_GREENTEXT),
1313
+ DEFAULT_OPTIONS.extension.greentext,
1314
+ )?;
1315
+ extension.aset(
1316
+ ruby.to_symbol(EXTENSION_SUBSCRIPT),
1317
+ DEFAULT_OPTIONS.extension.subscript,
1318
+ )?;
1319
+ extension.aset(
1320
+ ruby.to_symbol(EXTENSION_SUBTEXT),
1321
+ DEFAULT_OPTIONS.extension.subtext,
1322
+ )?;
1323
+ extension.aset(
1324
+ ruby.to_symbol(EXTENSION_ALERTS),
1325
+ DEFAULT_OPTIONS.extension.alerts,
1326
+ )?;
1327
+ extension.aset(
1328
+ ruby.to_symbol(EXTENSION_CJK_FRIENDLY_EMPHASIS),
1329
+ DEFAULT_OPTIONS.extension.cjk_friendly_emphasis,
1330
+ )?;
1331
+ extension.aset(
1332
+ ruby.to_symbol(EXTENSION_HIGHLIGHT),
1333
+ DEFAULT_OPTIONS.extension.highlight,
1334
+ )?;
1335
+ extension.aset(
1336
+ ruby.to_symbol(EXTENSION_INSERT),
1337
+ DEFAULT_OPTIONS.extension.insert,
1338
+ )?;
1339
+ extension.aset(
1340
+ ruby.to_symbol(EXTENSION_BLOCK_DIRECTIVE),
1341
+ DEFAULT_OPTIONS.extension.block_directive,
1342
+ )?;
1343
+ extension.aset(
1344
+ ruby.to_symbol(EXTENSION_MATH_DOLLARS_BLOCK),
1345
+ DEFAULT_OPTIONS.extension.math_dollars_block,
1346
+ )?;
1347
+ extension.aset(
1348
+ ruby.to_symbol(EXTENSION_WIKILINKS_IGNORE_EMBEDS),
1349
+ DEFAULT_OPTIONS.extension.wikilinks_ignore_embeds,
1350
+ )?;
1351
+ extension.aset(
1352
+ ruby.to_symbol(EXTENSION_OFM_CALLOUTS),
1353
+ DEFAULT_OPTIONS.extension.ofm_callouts,
1354
+ )?;
1355
+ extension.aset(
1356
+ ruby.to_symbol(EXTENSION_OFM_COMMENTS),
1357
+ DEFAULT_OPTIONS.extension.ofm_comments,
1358
+ )?;
1359
+ extension.aset(
1360
+ ruby.to_symbol(EXTENSION_OFM_BLOCK_IDS),
1361
+ DEFAULT_OPTIONS.extension.ofm_block_ids,
1362
+ )?;
1363
+ extension.aset(
1364
+ ruby.to_symbol(EXTENSION_HEADER_ATTRIBUTES),
1365
+ DEFAULT_OPTIONS.extension.header_attributes,
1366
+ )?;
1367
+ extension.aset(
1368
+ ruby.to_symbol(EXTENSION_FENCED_CODE_ATTRIBUTES),
1369
+ DEFAULT_OPTIONS.extension.fenced_code_attributes,
1370
+ )?;
1371
+ extension.aset(
1372
+ ruby.to_symbol(EXTENSION_INLINE_CODE_ATTRIBUTES),
1373
+ DEFAULT_OPTIONS.extension.inline_code_attributes,
1374
+ )?;
1375
+ extension.aset(
1376
+ ruby.to_symbol(EXTENSION_LINK_ATTRIBUTES),
1377
+ DEFAULT_OPTIONS.extension.link_attributes,
1378
+ )?;
1379
+ options.aset(ruby.to_symbol(EXTENSION), extension)?;
1380
+
1381
+ Ok(options)
252
1382
  }