carta 0.0.7

Document converter library: parse a source format and render it to a target format.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
//! Parsing of format specifiers of the form `base[+ext][-ext]…`.
//!
//! A specifier names a base format and a sequence of extension toggles applied onto that format's
//! default extension set: `+name` enables an extension, `-name` disables it. The base name (the text
//! before the first `+`/`-`) is what [`reader_for`](crate::reader_for)/[`writer_for`](crate::writer_for)
//! resolve; the resulting [`Extensions`] is merged into the reader/writer options.

use carta_core::{Error, Extension, Extensions, Result, presets};

/// The extensions enabled by default for `base`, before any `+`/`-` toggles.
#[allow(clippy::match_same_arms)] // Formats sharing a default set stay separate arms for their own notes.
fn default_extensions(base: &str) -> Extensions {
    match base {
        "commonmark" => Extensions::from_list(&[Extension::RawHtml]),
        "commonmark_x" => presets::COMMONMARK_X,
        "markdown" => presets::MARKDOWN,
        "markdown_github" => presets::MARKDOWN_GITHUB,
        "markdown_phpextra" => presets::MARKDOWN_PHPEXTRA,
        "markdown_mmd" => presets::MARKDOWN_MMD,
        "markdown_strict" => presets::MARKDOWN_STRICT,
        "gfm" => presets::GFM,
        // Both default to `smart`: quotes, dashes, and ellipses are folded into their typographic
        // glyphs unless `-smart` asks for the literal Unicode punctuation instead. Typst additionally
        // recognizes `[@key]` citations by default.
        "typst" => Extensions::from_list(&[Extension::Smart, Extension::Citations]),
        "dokuwiki" => Extensions::from_list(&[Extension::Smart]),
        // The LaTeX family additionally assigns header identifiers from title text and expands
        // user-defined macros by default; both can be turned off with a `-` toggle.
        "latex" | "beamer" => Extensions::from_list(&[
            Extension::Smart,
            Extension::AutoIdentifiers,
            Extension::LatexMacros,
        ]),
        "html" | "html5" | "html4" => Extensions::from_list(&[
            Extension::AutoIdentifiers,
            Extension::LineBlocks,
            Extension::NativeDivs,
            Extension::NativeSpans,
        ]),
        // An EPUB's content documents are XHTML, so reading one preserves the same structural HTML
        // constructs: native divs and spans, and raw HTML passthrough.
        "epub" | "epub2" | "epub3" => Extensions::from_list(&[
            Extension::NativeDivs,
            Extension::NativeSpans,
            Extension::RawHtml,
        ]),
        "rst" | "mediawiki" | "man" => Extensions::from_list(&[Extension::AutoIdentifiers]),
        // Writing DOCX assigns header identifiers by default; the remaining extensions in its set —
        // custom styles, native figure/table numbering, and empty-paragraph preservation among them —
        // are opt-in `+` toggles.
        "docx" => Extensions::from_list(&[Extension::AutoIdentifiers]),
        // Writing ODT assigns header identifiers by default; empty-paragraph preservation and custom
        // styles are opt-in `+` toggles.
        "odt" => Extensions::from_list(&[Extension::AutoIdentifiers]),
        // Org assigns header identifiers, recognizes `[cite:@key]` citations, and reads task-list
        // checkboxes by default; `smart`, `fancy_lists`, and the alternate identifier shapes are
        // opt-in toggles.
        "org" => Extensions::from_list(&[
            Extension::AutoIdentifiers,
            Extension::Citations,
            Extension::TaskLists,
        ]),
        // A notebook's markdown cells are parsed and rendered in a GitHub-flavored dialect with dollar
        // math and auto identifiers on by default; a hash run needs a following space to open a
        // heading (so a bare `#.` is a list marker, not a heading).
        "ipynb" => Extensions::from_list(&[
            Extension::AllSymbolsEscapable,
            Extension::AutoIdentifiers,
            Extension::GfmAutoIdentifiers,
            Extension::Autolink,
            Extension::BacktickCodeBlocks,
            Extension::FencedCodeBlocks,
            Extension::IntrawordUnderscores,
            Extension::ListsWithoutPrecedingBlankline,
            Extension::PipeTables,
            Extension::RawHtml,
            Extension::ShortcutReferenceLinks,
            Extension::SpaceInAtxHeader,
            Extension::Strikeout,
            Extension::TaskLists,
            Extension::TexMathDollars,
        ]),
        // Writing AsciiDoc assigns header identifiers from title text by default.
        "asciidoc" => Extensions::from_list(&[Extension::AutoIdentifiers]),
        // Plain text is the Markdown writer stripped of inline and block markup, but it keeps the
        // structural extensions that shape how blocks are laid out: the list, table, definition, and
        // figure forms, blank-line spacing rules, intra-word underscores, and macro expansion.
        "plain" => Extensions::from_list(&[
            Extension::BlankBeforeBlockquote,
            Extension::BlankBeforeHeader,
            Extension::DefinitionLists,
            Extension::ExampleLists,
            Extension::FancyLists,
            Extension::GridTables,
            Extension::ImplicitFigures,
            Extension::IntrawordUnderscores,
            Extension::LatexMacros,
            Extension::MultilineTables,
            Extension::SimpleTables,
            Extension::Startnum,
            Extension::Strikeout,
            Extension::TableCaptions,
        ]),
        _ => Extensions::empty(),
    }
}

/// The extensions enabled by default for `base` when it is read, before any `+`/`-` toggles.
///
/// A reader enables every construct its dialect can parse, which for the Markdown variants is a
/// broader set than the writer-shaping subset in [`default_extensions`]. Every other format reads and
/// writes with the same defaults.
fn reader_default_extensions(base: &str) -> Extensions {
    match base {
        // An OPML outline embeds Markdown in its node text, so reading one parses that text with the
        // extended Markdown dialect's defaults.
        "markdown" | "opml" => presets::MARKDOWN_READ,
        "gfm" => presets::GFM_READ,
        "commonmark_x" => presets::COMMONMARK_X_READ,
        "markdown_strict" => presets::MARKDOWN_STRICT_READ,
        "markdown_github" => presets::MARKDOWN_GITHUB_READ,
        "markdown_phpextra" => presets::MARKDOWN_PHPEXTRA_READ,
        "markdown_mmd" => presets::MARKDOWN_MMD_READ,
        _ => default_extensions(base),
    }
}

/// The fixed set of extensions a base format accepts, when it declares one.
///
/// `Some(set)` means the format admits exactly these extensions: a `+`/`-` toggle naming anything
/// outside the set is rejected, and only members appear in `--list-extensions`. `None` means the
/// format declares no fixed set, so any modeled extension may be toggled.
#[allow(clippy::too_many_lines)] // A per-format lookup table: one arm per accepted set.
#[allow(clippy::match_same_arms)] // Formats sharing an accepted set stay separate arms for their own notes.
pub(crate) fn supported_extensions(base: &str) -> Option<Extensions> {
    let set = match base {
        "commonmark" | "commonmark_x" | "gfm" => Extensions::from_list(&[
            // not modeled: sourcepos
            Extension::Alerts,
            Extension::AsciiIdentifiers,
            Extension::Attributes,
            Extension::Autolink,
            Extension::BracketedSpans,
            Extension::DefinitionLists,
            Extension::EastAsianLineBreaks,
            Extension::Emoji,
            Extension::FancyLists,
            Extension::FencedDivs,
            Extension::Footnotes,
            Extension::GfmAutoIdentifiers,
            Extension::HardLineBreaks,
            Extension::ImplicitFigures,
            Extension::ImplicitHeaderReferences,
            Extension::PipeTables,
            Extension::RawAttribute,
            Extension::RawHtml,
            Extension::RebaseRelativePaths,
            Extension::Smart,
            Extension::Strikeout,
            Extension::Subscript,
            Extension::Superscript,
            Extension::TaskLists,
            Extension::TexMathDollars,
            Extension::TexMathGfm,
            Extension::WikilinksTitleAfterPipe,
            Extension::WikilinksTitleBeforePipe,
            Extension::YamlMetadataBlock,
        ]),
        "html" | "html4" | "html5" | "epub" | "epub2" | "epub3" => Extensions::from_list(&[
            // not modeled: epub_html_exts
            Extension::AsciiIdentifiers,
            Extension::AutoIdentifiers,
            Extension::EastAsianLineBreaks,
            Extension::EmptyParagraphs,
            Extension::GfmAutoIdentifiers,
            Extension::LineBlocks,
            Extension::LiterateHaskell,
            Extension::NativeDivs,
            Extension::NativeSpans,
            Extension::RawHtml,
            Extension::RawTex,
            Extension::Smart,
            Extension::TaskLists,
            Extension::TexMathDollars,
            Extension::TexMathDoubleBackslash,
            Extension::TexMathSingleBackslash,
        ]),
        "latex" | "beamer" => Extensions::from_list(&[
            Extension::AsciiIdentifiers,
            Extension::AutoIdentifiers,
            Extension::EastAsianLineBreaks,
            Extension::EmptyParagraphs,
            Extension::GfmAutoIdentifiers,
            Extension::LatexMacros,
            Extension::LiterateHaskell,
            Extension::RawTex,
            Extension::Smart,
            Extension::TaskLists,
        ]),
        "rst" => Extensions::from_list(&[
            Extension::AsciiIdentifiers,
            Extension::AutoIdentifiers,
            Extension::EastAsianLineBreaks,
            Extension::GfmAutoIdentifiers,
            Extension::LiterateHaskell,
            Extension::Smart,
        ]),
        "docx" => Extensions::from_list(&[
            Extension::AsciiIdentifiers,
            Extension::AutoIdentifiers,
            Extension::Citations,
            Extension::EastAsianLineBreaks,
            Extension::EmptyParagraphs,
            Extension::GfmAutoIdentifiers,
            Extension::NativeNumbering,
            Extension::Styles,
        ]),
        "odt" => Extensions::from_list(&[
            // not modeled: xrefs_name, xrefs_number
            Extension::AsciiIdentifiers,
            Extension::AutoIdentifiers,
            Extension::EastAsianLineBreaks,
            Extension::EmptyParagraphs,
            Extension::GfmAutoIdentifiers,
            Extension::NativeNumbering,
        ]),
        "dokuwiki" => Extensions::from_list(&[
            Extension::AsciiIdentifiers,
            Extension::AutoIdentifiers,
            Extension::EastAsianLineBreaks,
            Extension::GfmAutoIdentifiers,
            Extension::RawHtml,
            Extension::Smart,
            Extension::TexMathDollars,
        ]),
        "org" => Extensions::from_list(&[
            // not modeled: smart_quotes, special_strings
            Extension::AsciiIdentifiers,
            Extension::AutoIdentifiers,
            Extension::Citations,
            Extension::EastAsianLineBreaks,
            Extension::FancyLists,
            Extension::GfmAutoIdentifiers,
            Extension::Smart,
            Extension::TaskLists,
        ]),
        "man" => Extensions::from_list(&[
            Extension::AsciiIdentifiers,
            Extension::AutoIdentifiers,
            Extension::EastAsianLineBreaks,
            Extension::GfmAutoIdentifiers,
        ]),
        "mediawiki" => Extensions::from_list(&[
            Extension::AsciiIdentifiers,
            Extension::AutoIdentifiers,
            Extension::EastAsianLineBreaks,
            Extension::GfmAutoIdentifiers,
            Extension::Smart,
        ]),
        "typst" => Extensions::from_list(&[
            Extension::Citations,
            Extension::EastAsianLineBreaks,
            Extension::Smart,
        ]),
        "asciidoc" => Extensions::from_list(&[
            Extension::AsciiIdentifiers,
            Extension::AutoIdentifiers,
            Extension::EastAsianLineBreaks,
            Extension::GfmAutoIdentifiers,
        ]),
        "markdown" | "markdown_github" | "markdown_mmd" | "markdown_phpextra"
        | "markdown_strict" | "opml" | "plain" => Extensions::from_list(&[
            Extension::Abbreviations,
            Extension::Alerts,
            Extension::AllSymbolsEscapable,
            Extension::AngleBracketsEscapable,
            Extension::AsciiIdentifiers,
            Extension::AutoIdentifiers,
            Extension::Autolink,
            Extension::BacktickCodeBlocks,
            Extension::BlankBeforeBlockquote,
            Extension::BlankBeforeHeader,
            Extension::BracketedSpans,
            Extension::Citations,
            Extension::DefinitionLists,
            Extension::EastAsianLineBreaks,
            Extension::Emoji,
            Extension::EscapedLineBreaks,
            Extension::ExampleLists,
            Extension::FancyLists,
            Extension::FencedCodeAttributes,
            Extension::FencedCodeBlocks,
            Extension::FencedDivs,
            Extension::Footnotes,
            Extension::FourSpaceRule,
            Extension::GfmAutoIdentifiers,
            Extension::GridTables,
            Extension::Gutenberg,
            Extension::HardLineBreaks,
            Extension::HeaderAttributes,
            Extension::IgnoreLineBreaks,
            Extension::ImplicitFigures,
            Extension::ImplicitHeaderReferences,
            Extension::InlineCodeAttributes,
            Extension::InlineNotes,
            Extension::IntrawordUnderscores,
            Extension::LatexMacros,
            Extension::LineBlocks,
            Extension::LinkAttributes,
            Extension::ListsWithoutPrecedingBlankline,
            Extension::LiterateHaskell,
            Extension::Mark,
            Extension::MarkdownAttribute,
            Extension::MarkdownInHtmlBlocks,
            Extension::MmdHeaderIdentifiers,
            Extension::MmdLinkAttributes,
            Extension::MmdTitleBlock,
            Extension::MultilineTables,
            Extension::NativeDivs,
            Extension::NativeSpans,
            Extension::OldDashes,
            Extension::PandocTitleBlock,
            Extension::PipeTables,
            Extension::RawAttribute,
            Extension::RawHtml,
            Extension::RawTex,
            Extension::RebaseRelativePaths,
            Extension::ShortSubsuperscripts,
            Extension::ShortcutReferenceLinks,
            Extension::SimpleTables,
            Extension::Smart,
            Extension::SpaceInAtxHeader,
            Extension::SpacedReferenceLinks,
            Extension::Startnum,
            Extension::Strikeout,
            Extension::Subscript,
            Extension::Superscript,
            Extension::TableAttributes,
            Extension::TableCaptions,
            Extension::TaskLists,
            Extension::TexMathDollars,
            Extension::TexMathDoubleBackslash,
            Extension::TexMathSingleBackslash,
            Extension::WikilinksTitleAfterPipe,
            Extension::WikilinksTitleBeforePipe,
            Extension::YamlMetadataBlock,
        ]),
        "ipynb" => Extensions::from_list(&[
            Extension::Abbreviations,
            Extension::Alerts,
            Extension::AllSymbolsEscapable,
            Extension::AngleBracketsEscapable,
            Extension::AsciiIdentifiers,
            Extension::AutoIdentifiers,
            Extension::Autolink,
            Extension::BacktickCodeBlocks,
            Extension::BlankBeforeBlockquote,
            Extension::BlankBeforeHeader,
            Extension::BracketedSpans,
            Extension::Citations,
            Extension::DefinitionLists,
            Extension::EastAsianLineBreaks,
            Extension::Emoji,
            Extension::EscapedLineBreaks,
            Extension::ExampleLists,
            Extension::FancyLists,
            Extension::FencedCodeAttributes,
            Extension::FencedCodeBlocks,
            Extension::FencedDivs,
            Extension::Footnotes,
            Extension::FourSpaceRule,
            Extension::GfmAutoIdentifiers,
            Extension::GridTables,
            Extension::Gutenberg,
            Extension::HardLineBreaks,
            Extension::HeaderAttributes,
            Extension::IgnoreLineBreaks,
            Extension::ImplicitFigures,
            Extension::ImplicitHeaderReferences,
            Extension::InlineCodeAttributes,
            Extension::InlineNotes,
            Extension::IntrawordUnderscores,
            Extension::LatexMacros,
            Extension::LineBlocks,
            Extension::LinkAttributes,
            Extension::ListsWithoutPrecedingBlankline,
            Extension::LiterateHaskell,
            Extension::Mark,
            Extension::MarkdownAttribute,
            Extension::MarkdownInHtmlBlocks,
            Extension::MmdHeaderIdentifiers,
            Extension::MmdLinkAttributes,
            Extension::MmdTitleBlock,
            Extension::MultilineTables,
            Extension::NativeDivs,
            Extension::NativeSpans,
            Extension::OldDashes,
            Extension::PandocTitleBlock,
            Extension::PipeTables,
            Extension::RawAttribute,
            Extension::RawHtml,
            Extension::RawMarkdown,
            Extension::RawTex,
            Extension::RebaseRelativePaths,
            Extension::ShortSubsuperscripts,
            Extension::ShortcutReferenceLinks,
            Extension::SimpleTables,
            Extension::Smart,
            Extension::SpaceInAtxHeader,
            Extension::SpacedReferenceLinks,
            Extension::Startnum,
            Extension::Strikeout,
            Extension::Subscript,
            Extension::Superscript,
            Extension::TableAttributes,
            Extension::TableCaptions,
            Extension::TaskLists,
            Extension::TexMathDollars,
            Extension::TexMathDoubleBackslash,
            Extension::TexMathSingleBackslash,
            Extension::WikilinksTitleAfterPipe,
            Extension::WikilinksTitleBeforePipe,
            Extension::YamlMetadataBlock,
        ]),
        "jira" | "native" | "json" | "csv" | "tsv" | "rtf" | "revealjs" => {
            Extensions::from_list(&[Extension::EastAsianLineBreaks])
        }
        _ => return None,
    };
    Some(set)
}

/// Splits a format specifier into its base name and the [`Extensions`] it selects.
///
/// The base is the text up to the first `+` or `-`; the remainder is a run of `+name`/`-name`
/// toggles applied onto `default_extensions`.
///
/// # Errors
/// [`Error::UnknownExtension`] if a toggle names an extension this build does not recognize.
pub fn parse_format_spec(spec: &str) -> Result<(String, Extensions)> {
    parse_format_spec_with(spec, default_extensions)
}

/// Splits a reading format specifier into its base name and the [`Extensions`] it selects, seeding
/// the toggles from the reader defaults (which for the Markdown variants are broader than the writer
/// defaults; see [`reader_default_extensions`]).
///
/// # Errors
/// [`Error::UnknownExtension`] if a toggle names an extension this build does not recognize.
pub(crate) fn parse_reader_format_spec(spec: &str) -> Result<(String, Extensions)> {
    parse_format_spec_with(spec, reader_default_extensions)
}

fn parse_format_spec_with(
    spec: &str,
    defaults: impl Fn(&str) -> Extensions,
) -> Result<(String, Extensions)> {
    let base_end = spec.find(['+', '-']).unwrap_or(spec.len());
    let (base, mut rest) = spec.split_at(base_end);
    let mut extensions = defaults(base);
    let supported = supported_extensions(base);

    while !rest.is_empty() {
        let (enable, after_sign) = match rest.strip_prefix('+') {
            Some(tail) => (true, tail),
            None => match rest.strip_prefix('-') {
                Some(tail) => (false, tail),
                // `find(['+', '-'])` bounds the base, so a non-empty `rest` always starts with one.
                None => break,
            },
        };
        let token_end = after_sign.find(['+', '-']).unwrap_or(after_sign.len());
        let (name, remainder) = after_sign.split_at(token_end);
        rest = remainder;

        // A format that declares a fixed extension set admits only its members; anything else —
        // including a name no extension answers to — is unsupported for that format. A format
        // without a declared set accepts any modeled extension and rejects only unknown names.
        let extension = match &supported {
            Some(set) => Extension::from_name(name)
                .filter(|ext| set.contains(*ext))
                .ok_or_else(|| Error::UnsupportedExtension {
                    extension: name.to_owned(),
                    format: base.to_owned(),
                })?,
            None => Extension::from_name(name)
                .ok_or_else(|| Error::UnknownExtension(name.to_owned()))?,
        };
        if enable {
            extensions.insert(extension);
        } else {
            extensions.remove(extension);
        }
    }

    Ok((base.to_owned(), extensions))
}

#[cfg(test)]
mod tests {
    use super::parse_format_spec;
    use carta_core::{Error, Extension};

    #[test]
    fn bare_commonmark_enables_raw_html_by_default() {
        let (base, ext) = parse_format_spec("commonmark").unwrap();
        assert_eq!(base, "commonmark");
        assert!(ext.contains(Extension::RawHtml));
        assert!(!ext.contains(Extension::Strikeout));
    }

    #[test]
    fn bare_non_markdown_format_has_no_extensions() {
        let (base, ext) = parse_format_spec("json").unwrap();
        assert_eq!(base, "json");
        assert!(ext.is_empty());
    }

    #[test]
    fn plus_enables_minus_disables() {
        let (base, ext) = parse_format_spec("commonmark+strikeout+subscript").unwrap();
        assert_eq!(base, "commonmark");
        assert!(ext.contains(Extension::Strikeout));
        assert!(ext.contains(Extension::Subscript));
        assert!(ext.contains(Extension::RawHtml));

        let (_, ext) = parse_format_spec("commonmark-raw_html").unwrap();
        assert!(!ext.contains(Extension::RawHtml));
        assert!(ext.is_empty());
    }

    #[test]
    fn unknown_extension_is_an_error() {
        // A base with no declared accepted set rejects an unrecognized name as unknown.
        let err = parse_format_spec("unregistered_format+bogus").unwrap_err();
        assert!(matches!(err, Error::UnknownExtension(name) if name == "bogus"));

        // A base that declares an accepted set reports the same name as unsupported for it.
        let err = parse_format_spec("commonmark+bogus").unwrap_err();
        assert!(matches!(
            err,
            Error::UnsupportedExtension { extension, format }
                if extension == "bogus" && format == "commonmark"
        ));
    }

    #[test]
    fn markdown_default_is_the_broad_dialect() {
        let (base, ext) = parse_format_spec("markdown").unwrap();
        assert_eq!(base, "markdown");
        assert!(ext.contains(Extension::Smart));
        assert!(ext.contains(Extension::DefinitionLists));
        assert!(ext.contains(Extension::PipeTables));
    }

    #[test]
    fn gfm_and_commonmark_x_presets_resolve() {
        let (base, ext) = parse_format_spec("gfm").unwrap();
        assert_eq!(base, "gfm");
        assert!(ext.contains(Extension::Strikeout));
        assert!(ext.contains(Extension::PipeTables));
        assert!(!ext.contains(Extension::DefinitionLists));

        let (base, ext) = parse_format_spec("commonmark_x").unwrap();
        assert_eq!(base, "commonmark_x");
        assert!(ext.contains(Extension::FencedDivs));
    }

    #[test]
    fn toggles_apply_over_a_preset() {
        let (_, ext) = parse_format_spec("markdown-smart-pipe_tables").unwrap();
        assert!(!ext.contains(Extension::Smart));
        assert!(!ext.contains(Extension::PipeTables));
        assert!(ext.contains(Extension::DefinitionLists));
    }

    #[test]
    fn html_enables_its_structural_defaults() {
        for spec in ["html", "html5", "html4"] {
            let (base, ext) = parse_format_spec(spec).unwrap();
            assert_eq!(base, spec);
            assert!(ext.contains(Extension::AutoIdentifiers));
            assert!(ext.contains(Extension::LineBlocks));
            assert!(ext.contains(Extension::NativeDivs));
            assert!(ext.contains(Extension::NativeSpans));
            // The text extensions are opt-in, not part of the default set.
            assert!(!ext.contains(Extension::Smart));
            assert!(!ext.contains(Extension::TexMathDollars));
        }
    }

    #[test]
    fn html_toggles_add_text_and_remove_structural_extensions() {
        let (_, ext) = parse_format_spec("html+smart+tex_math_dollars").unwrap();
        assert!(ext.contains(Extension::Smart));
        assert!(ext.contains(Extension::TexMathDollars));
        assert!(ext.contains(Extension::NativeDivs));

        let (_, ext) = parse_format_spec("html-native_divs-auto_identifiers").unwrap();
        assert!(!ext.contains(Extension::NativeDivs));
        assert!(!ext.contains(Extension::AutoIdentifiers));
        assert!(ext.contains(Extension::NativeSpans));
        assert!(ext.contains(Extension::LineBlocks));
    }

    #[test]
    fn html_unknown_extension_is_an_error() {
        // HTML declares an accepted set, so an unrecognized name is unsupported for the format.
        let err = parse_format_spec("html+bogus").unwrap_err();
        assert!(matches!(
            err,
            Error::UnsupportedExtension { extension, format }
                if extension == "bogus" && format == "html"
        ));
    }

    #[test]
    fn recognized_dialect_toggle_names_parse_without_error() {
        // These extension names are part of the markdown-family vocabulary a format spec may toggle.
        // Each must be recognized so that toggling it on a base format succeeds rather than aborting,
        // and each toggle must apply in both directions: `+name` enables it and a trailing `-name`
        // disables it, regardless of whether the base enables it by default.
        let names = [
            "abbreviations",
            "all_symbols_escapable",
            "angle_brackets_escapable",
            "ascii_identifiers",
            "east_asian_line_breaks",
            "four_space_rule",
            "gutenberg",
            "ignore_line_breaks",
            "latex_macros",
            "literate_haskell",
            "mmd_link_attributes",
            "mmd_title_block",
            "old_dashes",
            "raw_markdown",
            "rebase_relative_paths",
            "short_subsuperscripts",
            "shortcut_reference_links",
            "space_in_atx_header",
            "spaced_reference_links",
            "wikilinks_title_after_pipe",
            "wikilinks_title_before_pipe",
        ];
        for name in names {
            let (_, enabled) = parse_format_spec(&format!("ipynb+{name}"))
                .unwrap_or_else(|err| panic!("ipynb+{name} should parse: {err:?}"));
            let extension =
                Extension::from_name(name).unwrap_or_else(|| panic!("{name} should be a variant"));
            assert!(enabled.contains(extension), "+{name} should enable it");

            let (_, disabled) = parse_format_spec(&format!("ipynb+{name}-{name}"))
                .unwrap_or_else(|err| panic!("ipynb+{name}-{name} should parse: {err:?}"));
            assert!(
                !disabled.contains(extension),
                "+{name}-{name} should disable it"
            );
        }
    }

    #[test]
    fn latex_defaults_include_identifiers_and_macros() {
        let (base, ext) = parse_format_spec("latex").unwrap();
        assert_eq!(base, "latex");
        assert!(ext.contains(Extension::Smart));
        assert!(ext.contains(Extension::AutoIdentifiers));
        assert!(ext.contains(Extension::LatexMacros));
        assert!(!ext.contains(Extension::RawTex));

        let (_, ext) = parse_format_spec("latex-smart-latex_macros+raw_tex").unwrap();
        assert!(!ext.contains(Extension::Smart));
        assert!(!ext.contains(Extension::LatexMacros));
        assert!(ext.contains(Extension::RawTex));
        assert!(ext.contains(Extension::AutoIdentifiers));
    }

    #[test]
    fn dokuwiki_defaults_to_smart_only() {
        let (base, ext) = parse_format_spec("dokuwiki").unwrap();
        assert_eq!(base, "dokuwiki");
        assert!(ext.contains(Extension::Smart));
        assert!(!ext.contains(Extension::TexMathDollars));
    }

    #[test]
    fn dokuwiki_admits_its_declared_extensions() {
        let (_, ext) = parse_format_spec("dokuwiki+tex_math_dollars-smart").unwrap();
        assert!(ext.contains(Extension::TexMathDollars));
        assert!(!ext.contains(Extension::Smart));
    }

    #[test]
    fn dokuwiki_rejects_an_extension_outside_its_set() {
        // `pipe_tables` is a modeled extension, but not one dokuwiki accepts.
        let err = parse_format_spec("dokuwiki+pipe_tables").unwrap_err();
        assert!(matches!(
            err,
            Error::UnsupportedExtension { extension, format }
                if extension == "pipe_tables" && format == "dokuwiki"
        ));
        // An unknown name is reported the same way: unsupported for this format.
        let err = parse_format_spec("dokuwiki+bogus").unwrap_err();
        assert!(matches!(
            err,
            Error::UnsupportedExtension { extension, .. } if extension == "bogus"
        ));
    }

    #[test]
    fn every_registry_format_declares_its_accepted_set_status() {
        use super::supported_extensions;
        // Formats that declare an accepted extension set. A new format must be added to exactly
        // one of these two arrays, deliberately.
        const WITH_SET: &[&str] = &[
            "asciidoc",
            "beamer",
            "commonmark",
            "commonmark_x",
            "csv",
            "docx",
            "dokuwiki",
            "epub",
            "epub2",
            "gfm",
            "html",
            "html4",
            "ipynb",
            "jira",
            "json",
            "latex",
            "man",
            "markdown",
            "markdown_github",
            "markdown_mmd",
            "markdown_phpextra",
            "markdown_strict",
            "mediawiki",
            "native",
            "odt",
            "opml",
            "org",
            "plain",
            "revealjs",
            "rst",
            "rtf",
            "tsv",
            "typst",
        ];
        // Formats that intentionally declare NO set (any modeled toggle is admitted).
        const WITHOUT_SET: &[&str] = &[];

        let mut names: Vec<&str> = crate::supported_input_formats();
        names.extend(crate::supported_output_formats());
        names.sort_unstable();
        names.dedup();

        for name in names {
            let categorized = WITH_SET.contains(&name) || WITHOUT_SET.contains(&name);
            assert!(
                categorized,
                "format `{name}` is not categorized in WITH_SET/WITHOUT_SET — classify it: \
                 does format_spec::supported_extensions declare a set for it?"
            );
            assert_eq!(
                supported_extensions(name).is_some(),
                WITH_SET.contains(&name),
                "format `{name}`: supported_extensions presence disagrees with its category"
            );
        }
        // An unrecognized base declares no set, so any modeled toggle is admitted for it.
        assert!(supported_extensions("unregistered_format").is_none());
    }

    #[test]
    fn docx_defaults_to_auto_identifiers_only() {
        let (base, ext) = parse_format_spec("docx").unwrap();
        assert_eq!(base, "docx");
        assert!(ext.contains(Extension::AutoIdentifiers));
        // The three writer-shaping toggles are opt-in, off by default.
        assert!(!ext.contains(Extension::Styles));
        assert!(!ext.contains(Extension::NativeNumbering));
        assert!(!ext.contains(Extension::EmptyParagraphs));
    }

    #[test]
    fn docx_admits_its_declared_extensions_and_rejects_others() {
        let (_, ext) = parse_format_spec("docx+styles+native_numbering+empty_paragraphs").unwrap();
        assert!(ext.contains(Extension::Styles));
        assert!(ext.contains(Extension::NativeNumbering));
        assert!(ext.contains(Extension::EmptyParagraphs));
        assert!(ext.contains(Extension::AutoIdentifiers));

        // `pipe_tables` is a modeled extension, but not one docx accepts.
        let err = parse_format_spec("docx+pipe_tables").unwrap_err();
        assert!(matches!(
            err,
            Error::UnsupportedExtension { extension, format }
                if extension == "pipe_tables" && format == "docx"
        ));
    }

    #[test]
    fn odt_defaults_to_auto_identifiers_only() {
        let (base, ext) = parse_format_spec("odt").unwrap();
        assert_eq!(base, "odt");
        assert!(ext.contains(Extension::AutoIdentifiers));
        // Empty-paragraph preservation is an opt-in toggle, off by default.
        assert!(!ext.contains(Extension::EmptyParagraphs));
    }

    #[test]
    fn odt_admits_its_declared_extensions_and_rejects_others() {
        let (_, ext) = parse_format_spec("odt+empty_paragraphs+native_numbering").unwrap();
        assert!(ext.contains(Extension::EmptyParagraphs));
        assert!(ext.contains(Extension::NativeNumbering));
        assert!(ext.contains(Extension::AutoIdentifiers));

        // `styles` and `raw_html` are modeled extensions, but not ones odt accepts.
        for spec in ["odt+styles", "odt+raw_html"] {
            let err = parse_format_spec(spec).unwrap_err();
            assert!(matches!(
                err,
                Error::UnsupportedExtension { format, .. } if format == "odt"
            ));
        }
    }

    #[test]
    fn rst_admits_its_declared_extensions_and_rejects_others() {
        let (_, ext) = parse_format_spec("rst+smart-auto_identifiers").unwrap();
        assert!(ext.contains(Extension::Smart));
        assert!(!ext.contains(Extension::AutoIdentifiers));

        // `pipe_tables` is a modeled extension, but not one rst accepts.
        let err = parse_format_spec("rst+pipe_tables").unwrap_err();
        assert!(matches!(
            err,
            Error::UnsupportedExtension { extension, format }
                if extension == "pipe_tables" && format == "rst"
        ));
    }

    #[test]
    fn commonmark_admits_its_declared_extensions_and_rejects_others() {
        let (_, ext) = parse_format_spec("commonmark+strikeout+task_lists").unwrap();
        assert!(ext.contains(Extension::Strikeout));
        assert!(ext.contains(Extension::TaskLists));

        // `latex_macros` is a modeled extension, but not one commonmark accepts.
        let err = parse_format_spec("commonmark+latex_macros").unwrap_err();
        assert!(matches!(
            err,
            Error::UnsupportedExtension { extension, format }
                if extension == "latex_macros" && format == "commonmark"
        ));
    }
}