rs-rich-ext 0.0.21

Extensions for the `rich` Rust port: diagnostics, structured data, clap and tracing integration, diffs, workflow renderables and a plugin registry
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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
//! Documentation formats: Markdown, reStructuredText, Org, AsciiDoc and man
//! pages, behind one [`DocumentFormat`] interface.
//!
//! Not in upstream `rich`, which renders Markdown only. Each format is a
//! *provider*: an id (`"org"`), the file names it claims, a content sniffer
//! that says how confident it is a text is in that format, and a renderer
//! from source text to a renderable, with [`Diagnostic`]s for what it could
//! not show as written. A [`DocumentRegistry`] holds providers, finds one by
//! id, file name or content ([`DocumentRegistry::detect`]), and renders.
//! Nothing here depends on an output backend: a renderable prints to a
//! terminal, an HTML or SVG export, or any other console target alike.
//!
//! Markdown and reStructuredText wrap core's `Markdown` and
//! [`rst::RestructuredText`](crate::rst::RestructuredText). Org, AsciiDoc and
//! man pages parse into a small shared [model] (headings, paragraphs with
//! inline spans, lists, code, tables, quotes, admonitions, definition lists,
//! rules, image placeholders) that [`DocumentView`] draws with core's
//! renderables and Markdown's theme styles, so the five formats look alike.
//! The format-specific styles (TODO keywords, admonitions, drawers) are the
//! theme keys in [`STYLES`].
//!
//! ```
//! use rich::Console;
//! use rich_ext::document::{DocumentRegistry, RenderOptions};
//!
//! let registry = DocumentRegistry::with_builtins();
//! let detected = registry.detect(Some("notes.org"), "* Heading", 0.9).unwrap();
//! assert_eq!(detected.format, "org");
//!
//! let rendered = registry
//!     .render("= Title\n\nNOTE: Mind the gap.", "asciidoc", &RenderOptions::new())
//!     .unwrap();
//! let console = Console::builder().width(30).color_system(None).build();
//! let out = console.render_to_string(rendered.renderable.as_ref());
//! assert!(out.contains("Note") && out.contains("Mind the gap."));
//! ```
//!
//! # Third-party formats
//!
//! Register any [`DocumentFormat`] with [`DocumentRegistry::register`]. A
//! plugin cannot declare a document format through `PluginRegistrar` (that
//! would need a new registrar method in `rich-plugin-api`); it can register a
//! `SourceRenderer` with `renderer(name, …)`, and a host wraps that in a
//! [`SourceRendererFormat`] (adding the extensions it claims) to put it in a
//! registry. See `docs/PLUGINS.md`.

pub mod asciidoc;
pub mod man;
pub mod model;
pub mod org;
mod render;
mod scan;

use std::fmt;
use std::sync::Arc;

use rich::{Renderable, Text};

pub use model::{Block, Document, Inline};
pub use render::{DocumentView, RenderOptions};

/// Theme keys this module adds, chained into
/// [`extended_theme`](crate::theme::extended_theme). The renderer falls back
/// to these values when a console's theme lacks them, and reads the
/// `markdown.*` keys for everything Markdown also draws.
pub const STYLES: &[(&str, &str)] = &[
    ("document.todo", "bold red"),
    ("document.done", "bold green"),
    ("document.priority", "bold yellow"),
    ("document.tag", "dim"),
    ("document.byline", "dim"),
    ("document.caption", "dim italic"),
    ("document.properties", "dim"),
    ("document.properties.key", "bold"),
    ("document.term", "bold"),
    ("document.underline", "underline"),
    ("document.callout", "bold reverse"),
    ("document.checkbox", "bold"),
    ("document.checkbox.checked", "bold green"),
    ("document.checkbox.partial", "bold yellow"),
    ("document.attribution", "italic"),
    ("document.timestamp", "cyan"),
    ("document.footnote", "dim"),
    ("document.mark", "black on yellow"),
    ("document.banner", "dim"),
    ("document.aside", "dim"),
    ("document.admonition", "bold"),
    ("document.admonition.note", "bold cyan"),
    ("document.admonition.tip", "bold green"),
    ("document.admonition.important", "bold magenta"),
    ("document.admonition.warning", "bold yellow"),
    ("document.admonition.caution", "bold red"),
];

/// A content-detection confidence at or above which a format is a likely
/// match: what `rich --format auto` routes on.
pub const LIKELY: f32 = 0.6;

/// A confidence at or above which a format is all but certain: what routes
/// an extensionless file with no flag.
pub const CERTAIN: f32 = 0.9;

/// How serious a [`Diagnostic`] is.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Severity {
    /// Something not drawn that a reader would not miss: a formatting
    /// request, an export-only block.
    Info,
    /// Content not shown as written: an `include::` not followed, an
    /// equation left out, a block that never closed.
    Warning,
}

/// Something a parser could not show as written.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Diagnostic {
    /// The 1-based source line, when known.
    pub line: Option<usize>,
    pub severity: Severity,
    pub message: String,
}

impl fmt::Display for Diagnostic {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.line {
            Some(line) => write!(f, "line {line}: {}", self.message),
            None => f.write_str(&self.message),
        }
    }
}

/// A parsed document and what the parser could not show.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Parsed {
    pub document: Document,
    pub diagnostics: Vec<Diagnostic>,
}

/// A rendered document: the renderable to print and the diagnostics.
pub struct Rendered {
    pub renderable: Box<dyn Renderable + Send + Sync>,
    pub diagnostics: Vec<Diagnostic>,
}

impl fmt::Debug for Rendered {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Rendered")
            .field("diagnostics", &self.diagnostics)
            .finish_non_exhaustive()
    }
}

/// How a file name relates to a format.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NameMatch {
    /// Not this format's name.
    No,
    /// Possibly this format, if the content agrees: `ls.1` may be a man
    /// page or a rotated log, `key.asc` AsciiDoc or a PGP signature.
    Maybe,
    /// This format's own extension (`.org`, `.adoc`).
    Yes,
}

/// A documentation format: how to recognise it and how to draw it.
pub trait DocumentFormat: Send + Sync {
    /// The format's id: lowercase, stable, usable as a CLI value (`"org"`).
    fn id(&self) -> &str;

    /// The name people call it (`"Org"`).
    fn name(&self) -> &str;

    /// Other ids accepted for it (`"adoc"` for AsciiDoc).
    fn aliases(&self) -> Vec<String> {
        Vec::new()
    }

    /// The file extensions it claims, lowercase, without the dot.
    fn extensions(&self) -> Vec<String> {
        Vec::new()
    }

    /// How `file_name` (a base name) relates to this format. By default a
    /// name whose extension is one of [`extensions`](Self::extensions) is
    /// [`NameMatch::Yes`].
    fn match_name(&self, file_name: &str) -> NameMatch {
        match extension(file_name) {
            Some(ext)
                if self
                    .extensions()
                    .iter()
                    .any(|own| own.eq_ignore_ascii_case(ext)) =>
            {
                NameMatch::Yes
            }
            _ => NameMatch::No,
        }
    }

    /// How confident it is that `text` is in this format, from 0 to 1. See
    /// [`LIKELY`] and [`CERTAIN`].
    fn sniff(&self, _text: &str) -> f32 {
        0.0
    }

    /// What the format supports (`"tables"`, `"admonitions"`, …), for
    /// listings and for callers that want to know before rendering.
    fn features(&self) -> Vec<String> {
        Vec::new()
    }

    /// Parse `source` into the shared [model], for formats built on it.
    fn parse(&self, _source: &str) -> Option<Parsed> {
        None
    }

    /// Render `source`. The default draws [`parse`](Self::parse)'s document
    /// with [`DocumentView`], or the source as plain text when the format
    /// does not parse into the model.
    fn render(&self, source: &str, options: &RenderOptions) -> Rendered {
        match self.parse(source) {
            Some(parsed) => {
                let diagnostics = parsed.diagnostics.clone();
                Rendered {
                    renderable: Box::new(
                        DocumentView::from_parsed(parsed).options(options.clone()),
                    ),
                    diagnostics,
                }
            }
            None => Rendered {
                renderable: Box::new(Text::new(source)),
                diagnostics: Vec::new(),
            },
        }
    }
}

/// The extension of a file name: after its last dot, unless that is the
/// first character (`.bashrc` has none).
pub fn extension(file_name: &str) -> Option<&str> {
    let (stem, ext) = file_name.rsplit_once('.')?;
    (!stem.is_empty() && !ext.is_empty()).then_some(ext)
}

/// The base name of a path or URL: its last segment, without a query or
/// fragment.
pub fn base_name(path: &str) -> &str {
    let name = path.rsplit(['/', '\\']).next().unwrap_or(path);
    name.split(['?', '#']).next().unwrap_or(name)
}

/// Why a format was chosen.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DetectedBy {
    /// The file name's extension.
    Name,
    /// An extension that may mean this format, confirmed by the content.
    NameAndContent,
    /// The content alone.
    Content,
}

/// A detected format.
#[derive(Clone, Debug, PartialEq)]
pub struct Detection {
    /// The format's id.
    pub format: String,
    /// From 0 to 1; 1 for a format's own extension.
    pub confidence: f32,
    pub by: DetectedBy,
}

/// Why a format could not be registered or used.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DocumentError {
    /// No registered format has this id or alias.
    Unknown { name: String, known: Vec<String> },
    /// The id or alias is taken.
    Conflict { name: String },
    /// The id or alias is not a lowercase name (see `rich_plugin_api::is_valid_name`).
    InvalidName { name: String },
}

impl fmt::Display for DocumentError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            DocumentError::Unknown { name, known } => write!(
                f,
                "unknown document format {name:?}; known: {}",
                known.join(", ")
            ),
            DocumentError::Conflict { name } => {
                write!(f, "a document format is already registered as {name:?}")
            }
            DocumentError::InvalidName { name } => {
                write!(f, "{name:?} is not a valid document format name")
            }
        }
    }
}

impl std::error::Error for DocumentError {}

/// Document formats by id, alias, file name and content.
#[derive(Clone, Default)]
pub struct DocumentRegistry {
    formats: Vec<Arc<dyn DocumentFormat>>,
}

impl fmt::Debug for DocumentRegistry {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list()
            .entries(self.formats.iter().map(|format| format.id().to_string()))
            .finish()
    }
}

impl DocumentRegistry {
    /// An empty registry.
    pub fn new() -> Self {
        DocumentRegistry::default()
    }

    /// The built-in formats, in detection order: Markdown (with the
    /// `markdown` feature), reStructuredText, Org, AsciiDoc and man pages.
    pub fn with_builtins() -> Self {
        let mut registry = DocumentRegistry::new();
        for format in builtins() {
            registry
                .register(format)
                .expect("the built-in formats have distinct names");
        }
        registry
    }

    /// Add `format`. Its id and aliases must be valid names that no
    /// registered format uses.
    pub fn register(&mut self, format: Arc<dyn DocumentFormat>) -> Result<(), DocumentError> {
        let mut names = vec![format.id().to_string()];
        names.extend(format.aliases());
        for name in &names {
            if !rich_plugin_api::is_valid_name(name) || name.chars().any(|c| c.is_uppercase()) {
                return Err(DocumentError::InvalidName { name: name.clone() });
            }
            if self.get(name).is_some() {
                return Err(DocumentError::Conflict { name: name.clone() });
            }
        }
        self.formats.push(format);
        Ok(())
    }

    /// The registered formats, in registration order.
    pub fn formats(&self) -> &[Arc<dyn DocumentFormat>] {
        &self.formats
    }

    /// The format with this id or alias (case-insensitive).
    pub fn get(&self, name: &str) -> Option<Arc<dyn DocumentFormat>> {
        let name = name.to_ascii_lowercase();
        self.formats
            .iter()
            .find(|format| format.id() == name || format.aliases().contains(&name))
            .cloned()
    }

    /// Every id, in registration order.
    pub fn ids(&self) -> Vec<String> {
        self.formats
            .iter()
            .map(|format| format.id().to_string())
            .collect()
    }

    /// Each format's confidence that `text` is in it, most confident first
    /// (ties keep registration order).
    pub fn sniff(&self, text: &str) -> Vec<(String, f32)> {
        let mut scores: Vec<(String, f32)> = self
            .formats
            .iter()
            .map(|format| (format.id().to_string(), format.sniff(text).clamp(0.0, 1.0)))
            .collect();
        scores.sort_by(|a, b| b.1.total_cmp(&a.1));
        scores
    }

    /// The format of a document, from its path (a file path or URL; `None`
    /// for standard input) and its text:
    ///
    /// 1. A format's own extension wins outright (`notes.org`).
    /// 2. An extension a format may use (`ls.1`, `key.asc`) needs the
    ///    content to agree at [`LIKELY`] or above.
    /// 3. Any other extension is never a document here: `main.c` stays
    ///    source code whatever it contains.
    /// 4. Without an extension, the content alone decides, when the most
    ///    confident format reaches `threshold` and leads the next by 0.1.
    pub fn detect(&self, path: Option<&str>, text: &str, threshold: f32) -> Option<Detection> {
        if let Some(path) = path {
            let name = base_name(path);
            let mut maybe: Vec<&Arc<dyn DocumentFormat>> = Vec::new();
            for format in &self.formats {
                match format.match_name(name) {
                    NameMatch::Yes => {
                        return Some(Detection {
                            format: format.id().to_string(),
                            confidence: 1.0,
                            by: DetectedBy::Name,
                        });
                    }
                    NameMatch::Maybe => maybe.push(format),
                    NameMatch::No => {}
                }
            }
            if !maybe.is_empty() {
                return maybe
                    .into_iter()
                    .map(|format| (format, format.sniff(text)))
                    .filter(|(_, confidence)| *confidence >= LIKELY)
                    .max_by(|a, b| a.1.total_cmp(&b.1))
                    .map(|(format, confidence)| Detection {
                        format: format.id().to_string(),
                        confidence: confidence.min(1.0),
                        by: DetectedBy::NameAndContent,
                    });
            }
            if extension(name).is_some() {
                return None;
            }
        }
        let scores = self.sniff(text);
        let (best, confidence) = scores.first()?.clone();
        let runner_up = scores.get(1).map_or(0.0, |(_, score)| *score);
        (confidence >= threshold && confidence - runner_up >= 0.1).then_some(Detection {
            format: best,
            confidence,
            by: DetectedBy::Content,
        })
    }

    /// Render `source` as the format named `format` (an id or alias).
    pub fn render(
        &self,
        source: &str,
        format: &str,
        options: &RenderOptions,
    ) -> Result<Rendered, DocumentError> {
        let provider = self.get(format).ok_or_else(|| DocumentError::Unknown {
            name: format.to_string(),
            known: self.ids(),
        })?;
        Ok(provider.render(source, options))
    }
}

/// The built-in providers, in detection order.
pub fn builtins() -> Vec<Arc<dyn DocumentFormat>> {
    let mut formats: Vec<Arc<dyn DocumentFormat>> = vec![
        Arc::new(RstFormat),
        Arc::new(OrgFormat),
        Arc::new(AsciiDocFormat),
        Arc::new(ManFormat),
    ];
    #[cfg(feature = "markdown")]
    formats.insert(0, Arc::new(MarkdownFormat));
    formats
}

fn strings(values: &[&str]) -> Vec<String> {
    values.iter().map(|value| value.to_string()).collect()
}

/// Markdown, drawn by core's `Markdown` (a faithful port of upstream's).
#[cfg(feature = "markdown")]
#[derive(Clone, Copy, Debug, Default)]
pub struct MarkdownFormat;

#[cfg(feature = "markdown")]
impl DocumentFormat for MarkdownFormat {
    fn id(&self) -> &str {
        "markdown"
    }

    fn name(&self) -> &str {
        "Markdown"
    }

    fn aliases(&self) -> Vec<String> {
        strings(&["md"])
    }

    fn extensions(&self) -> Vec<String> {
        strings(&["md", "markdown"])
    }

    fn sniff(&self, text: &str) -> f32 {
        sniff_markdown(text)
    }

    fn features(&self) -> Vec<String> {
        strings(&[
            "headings", "emphasis", "lists", "code", "tables", "quotes", "links", "images", "rules",
        ])
    }

    fn render(&self, source: &str, options: &RenderOptions) -> Rendered {
        let mut markdown = rich::markdown::Markdown::new(source).hyperlinks(options.hyperlinks);
        if let Some(theme) = &options.code_theme {
            markdown = markdown.code_theme(theme.as_str());
        }
        if let Some(justify) = options.justify {
            markdown = markdown.justify(justify);
        }
        for fence in &options.fences {
            markdown = markdown.fence_renderer(fence.clone());
        }
        Rendered {
            renderable: Box::new(markdown),
            diagnostics: Vec::new(),
        }
    }
}

/// reStructuredText, drawn as `rich-rst` draws it ([`crate::rst`]).
#[derive(Clone, Copy, Debug, Default)]
pub struct RstFormat;

impl DocumentFormat for RstFormat {
    fn id(&self) -> &str {
        "rst"
    }

    fn name(&self) -> &str {
        "reStructuredText"
    }

    fn aliases(&self) -> Vec<String> {
        strings(&["rest", "restructuredtext"])
    }

    fn extensions(&self) -> Vec<String> {
        strings(&["rst"])
    }

    fn sniff(&self, text: &str) -> f32 {
        sniff_rst(text)
    }

    fn features(&self) -> Vec<String> {
        strings(&[
            "headings",
            "emphasis",
            "lists",
            "code",
            "tables",
            "quotes",
            "links",
            "images",
            "admonitions",
            "definition-lists",
            "field-lists",
            "footnotes",
        ])
    }

    fn render(&self, source: &str, options: &RenderOptions) -> Rendered {
        let mut document = crate::rst::RestructuredText::new(source).default_lexer(
            options
                .default_language
                .clone()
                .unwrap_or_else(|| "python".into()),
        );
        if let Some(theme) = &options.code_theme {
            document = document.code_theme(theme.as_str());
        }
        Rendered {
            renderable: Box::new(document),
            diagnostics: Vec::new(),
        }
    }
}

/// Org mode ([`org`]).
#[derive(Clone, Copy, Debug, Default)]
pub struct OrgFormat;

impl DocumentFormat for OrgFormat {
    fn id(&self) -> &str {
        "org"
    }

    fn name(&self) -> &str {
        "Org"
    }

    fn aliases(&self) -> Vec<String> {
        strings(&["orgmode", "org-mode"])
    }

    fn extensions(&self) -> Vec<String> {
        strings(&["org"])
    }

    fn sniff(&self, text: &str) -> f32 {
        org::sniff(text)
    }

    fn features(&self) -> Vec<String> {
        strings(&[
            "headings",
            "todo-keywords",
            "priorities",
            "tags",
            "emphasis",
            "lists",
            "checkboxes",
            "definition-lists",
            "code",
            "tables",
            "quotes",
            "links",
            "images",
            "drawers",
            "keywords",
            "footnotes",
            "timestamps",
            "admonitions",
            "rules",
        ])
    }

    fn parse(&self, source: &str) -> Option<Parsed> {
        Some(org::parse(source))
    }
}

/// AsciiDoc ([`asciidoc`]).
#[derive(Clone, Copy, Debug, Default)]
pub struct AsciiDocFormat;

impl DocumentFormat for AsciiDocFormat {
    fn id(&self) -> &str {
        "asciidoc"
    }

    fn name(&self) -> &str {
        "AsciiDoc"
    }

    fn aliases(&self) -> Vec<String> {
        strings(&["adoc"])
    }

    fn extensions(&self) -> Vec<String> {
        strings(&["adoc", "asciidoc"])
    }

    /// `.adoc` and `.asciidoc` are AsciiDoc; `.asc` and `.ad` are when the
    /// content says so (`.asc` is also an armoured PGP file).
    fn match_name(&self, file_name: &str) -> NameMatch {
        match extension(file_name).map(str::to_ascii_lowercase).as_deref() {
            Some("adoc" | "asciidoc") => NameMatch::Yes,
            Some("asc" | "ad") => NameMatch::Maybe,
            _ => NameMatch::No,
        }
    }

    fn sniff(&self, text: &str) -> f32 {
        asciidoc::sniff(text)
    }

    fn features(&self) -> Vec<String> {
        strings(&[
            "headings",
            "attributes",
            "emphasis",
            "lists",
            "checklists",
            "definition-lists",
            "callouts",
            "code",
            "tables",
            "quotes",
            "admonitions",
            "sidebars",
            "links",
            "images",
            "footnotes",
            "rules",
        ])
    }

    fn parse(&self, source: &str) -> Option<Parsed> {
        Some(asciidoc::parse(source))
    }
}

/// Man pages in roff: the man and mdoc macros ([`man`]).
#[derive(Clone, Copy, Debug, Default)]
pub struct ManFormat;

impl DocumentFormat for ManFormat {
    fn id(&self) -> &str {
        "man"
    }

    fn name(&self) -> &str {
        "man page"
    }

    fn aliases(&self) -> Vec<String> {
        strings(&["roff", "troff", "groff", "nroff", "mdoc"])
    }

    fn extensions(&self) -> Vec<String> {
        strings(&[
            "man", "roff", "mdoc", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        ])
    }

    /// `.man`, `.roff` and `.mdoc` are man pages; a manual section (`.1` to
    /// `.9`, optionally with a suffix such as `.3pm` or `.1ssl`) is one when
    /// the content says so (`syslog.1` is a rotated log).
    fn match_name(&self, file_name: &str) -> NameMatch {
        let Some(ext) = extension(file_name).map(str::to_ascii_lowercase) else {
            return NameMatch::No;
        };
        if matches!(ext.as_str(), "man" | "roff" | "mdoc") {
            return NameMatch::Yes;
        }
        let mut chars = ext.chars();
        let section = chars.next().is_some_and(|c| ('1'..='9').contains(&c));
        let suffix: String = chars.collect();
        let suffix_ok = suffix.len() <= 8
            && suffix.chars().next().is_none_or(|c| c.is_ascii_lowercase())
            && suffix.chars().all(|c| c.is_ascii_alphanumeric());
        if section && suffix_ok {
            NameMatch::Maybe
        } else {
            NameMatch::No
        }
    }

    fn sniff(&self, text: &str) -> f32 {
        man::sniff(text)
    }

    fn features(&self) -> Vec<String> {
        strings(&[
            "headings",
            "paragraphs",
            "fonts",
            "tagged-paragraphs",
            "lists",
            "indentation",
            "examples",
            "links",
            "tables",
            "mdoc",
            "conditionals",
        ])
    }

    fn parse(&self, source: &str) -> Option<Parsed> {
        Some(man::parse(source))
    }
}

/// A plugin's `SourceRenderer` as a document format: the renderer draws the
/// source; the id, name, extensions and an optional sniffer come from the
/// host that wraps it.
pub struct SourceRendererFormat {
    id: String,
    name: String,
    extensions: Vec<String>,
    renderer: Arc<dyn rich_plugin_api::SourceRenderer>,
    sniffer: Option<Sniffer>,
}

/// A content sniffer: how confident it is a text is in the format.
type Sniffer = Box<dyn Fn(&str) -> f32 + Send + Sync>;

impl SourceRendererFormat {
    /// Wrap `renderer` as the format `id`.
    pub fn new(id: impl Into<String>, renderer: Arc<dyn rich_plugin_api::SourceRenderer>) -> Self {
        let id = id.into();
        SourceRendererFormat {
            name: id.clone(),
            id,
            extensions: Vec::new(),
            renderer,
            sniffer: None,
        }
    }

    /// The name people call the format.
    #[must_use]
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = name.into();
        self
    }

    /// The extensions it claims, without the dot.
    #[must_use]
    pub fn extensions(mut self, extensions: &[&str]) -> Self {
        self.extensions = extensions
            .iter()
            .map(|ext| ext.trim_start_matches('.').to_ascii_lowercase())
            .collect();
        self
    }

    /// A content sniffer (0 to 1).
    #[must_use]
    pub fn sniffer(mut self, sniffer: impl Fn(&str) -> f32 + Send + Sync + 'static) -> Self {
        self.sniffer = Some(Box::new(sniffer));
        self
    }
}

impl DocumentFormat for SourceRendererFormat {
    fn id(&self) -> &str {
        &self.id
    }

    fn name(&self) -> &str {
        &self.name
    }

    fn extensions(&self) -> Vec<String> {
        self.extensions.clone()
    }

    fn sniff(&self, text: &str) -> f32 {
        self.sniffer.as_ref().map_or(0.0, |sniff| sniff(text))
    }

    fn render(&self, source: &str, _options: &RenderOptions) -> Rendered {
        match self.renderer.render(source) {
            Ok(renderable) => Rendered {
                renderable,
                diagnostics: Vec::new(),
            },
            Err(error) => Rendered {
                renderable: Box::new(Text::new(source)),
                diagnostics: vec![Diagnostic {
                    line: None,
                    severity: Severity::Warning,
                    message: format!("the {} renderer failed: {error}; shown as text", self.id),
                }],
            },
        }
    }
}

/// Markdown's tells. Capped below [`CERTAIN`]: Markdown-looking text is
/// everywhere (a `#` comment, a `*` bullet), so content alone only ever
/// makes it likely.
fn sniff_markdown(text: &str) -> f32 {
    let text = scan::sample(text);
    if text.starts_with("#!") {
        return 0.0;
    }
    let (mut fences, mut headings, mut links, mut emphasis, mut lists) = (0, 0, 0, 0, 0);
    for line in text.lines().take(4000) {
        let trimmed = line.trim_start();
        if trimmed.starts_with("```") || trimmed.starts_with("~~~") {
            fences += 1;
        }
        let hashes = trimmed.bytes().take_while(|&b| b == b'#').count();
        if (1..=6).contains(&hashes) && trimmed[hashes..].starts_with(' ') && line.starts_with('#')
        {
            headings += 1;
        }
        if line.contains("](") && line.contains('[') {
            links += 1;
        }
        if line.contains("**") || line.contains("`") {
            emphasis += 1;
        }
        if trimmed.starts_with("- ") || trimmed.starts_with("* ") || trimmed.starts_with("1. ") {
            lists += 1;
        }
    }
    let mut score: f32 = 0.0;
    let mut signals = 0;
    for (present, weight) in [
        (fences >= 2, 0.7),
        (links > 0, 0.6),
        (headings >= 2, 0.6),
        (headings == 1, 0.45),
        (emphasis > 0, 0.35),
        (lists >= 2, 0.3),
    ] {
        if present {
            score = score.max(weight);
            signals += 1;
        }
    }
    if signals > 1 {
        score += 0.05 * (signals - 1) as f32;
    }
    score.min(0.8)
}

/// reStructuredText's tells: directives, title adornments, roles. Capped
/// below [`CERTAIN`], as Markdown's are.
fn sniff_rst(text: &str) -> f32 {
    let text = scan::sample(text);
    if text.starts_with("#!") {
        return 0.0;
    }
    let lines: Vec<&str> = text.lines().take(4000).collect();
    let (mut directives, mut adornments, mut roles, mut fields) = (0, 0, 0, 0);
    for (index, line) in lines.iter().enumerate() {
        let trimmed = line.trim_start();
        if let Some(rest) = trimmed.strip_prefix(".. ")
            && rest.contains("::")
        {
            directives += 1;
        }
        let first = line.chars().next();
        let adornment = line.len() >= 3
            && first.is_some_and(|c| "=-~^\"'`#*+".contains(c))
            && line.chars().all(|c| Some(c) == first);
        if adornment && index > 0 {
            let title = lines[index - 1].trim_end();
            if !title.is_empty() && title.chars().count() <= line.chars().count() {
                adornments += 1;
            }
        }
        if line.contains(":ref:`") || line.contains(":doc:`") || line.contains("``") {
            roles += 1;
        }
        if trimmed.starts_with(':') && trimmed[1..].contains(": ") {
            fields += 1;
        }
    }
    let mut score: f32 = 0.0;
    let mut signals = 0;
    for (present, weight) in [
        (directives > 0, 0.8),
        (roles > 0, 0.5),
        (adornments >= 2, 0.5),
        (fields >= 2, 0.4),
    ] {
        if present {
            score = score.max(weight);
            signals += 1;
        }
    }
    if signals > 1 {
        score += 0.05 * (signals - 1) as f32;
    }
    score.min(0.85)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn builtins_are_found_by_id_alias_and_extension() {
        let registry = DocumentRegistry::with_builtins();
        assert_eq!(registry.get("adoc").unwrap().id(), "asciidoc");
        assert_eq!(registry.get("ROFF").unwrap().id(), "man");
        for (path, format) in [
            ("notes.org", "org"),
            ("guide.adoc", "asciidoc"),
            ("README.rst", "rst"),
            ("dir/ls.man", "man"),
            ("https://example.com/a.ORG?raw=1", "org"),
        ] {
            let detected = registry.detect(Some(path), "", CERTAIN).unwrap();
            assert_eq!(
                (detected.format.as_str(), detected.by),
                (format, DetectedBy::Name)
            );
        }
    }

    #[test]
    fn a_section_extension_needs_roff_inside() {
        let registry = DocumentRegistry::with_builtins();
        let page = ".TH LS 1\n.SH NAME\nls \\- list\n";
        let detected = registry.detect(Some("ls.1"), page, CERTAIN).unwrap();
        assert_eq!(
            (detected.format.as_str(), detected.by),
            ("man", DetectedBy::NameAndContent)
        );
        assert_eq!(
            registry.detect(Some("syslog.1"), "Jan 1 boot\n", CERTAIN),
            None
        );
        assert_eq!(registry.detect(Some("report.2024"), page, CERTAIN), None);
    }

    #[test]
    fn source_code_is_never_a_document() {
        let registry = DocumentRegistry::with_builtins();
        let looks_like_man = ".TH X 1\n.SH NAME\nx\n";
        assert_eq!(registry.detect(Some("main.c"), looks_like_man, 0.0), None);
        assert_eq!(registry.detect(Some("x.txt"), "#+TITLE: x\n", 0.0), None);
    }

    #[test]
    fn content_decides_without_an_extension() {
        let registry = DocumentRegistry::with_builtins();
        let org = "#+TITLE: Notes\n* TODO Write\n";
        let detected = registry.detect(None, org, CERTAIN).unwrap();
        assert_eq!(
            (detected.format.as_str(), detected.by),
            ("org", DetectedBy::Content)
        );
        // Markdown never reaches CERTAIN on content alone.
        assert_eq!(
            registry.detect(Some("README"), "# Title\n\nSee [x](y).\n", CERTAIN),
            None
        );
        assert_eq!(
            registry
                .detect(
                    None,
                    "# Title\n\nSee [x](y).\n\n```\ncode\n```\n```\n",
                    LIKELY
                )
                .map(|d| d.format),
            Some("markdown".to_string())
        );
        assert_eq!(registry.detect(None, "just some text\n", LIKELY), None);
    }

    #[test]
    fn registering_checks_names() {
        let mut registry = DocumentRegistry::with_builtins();
        assert_eq!(
            registry.register(Arc::new(OrgFormat)),
            Err(DocumentError::Conflict { name: "org".into() })
        );
        struct Bad;
        impl DocumentFormat for Bad {
            fn id(&self) -> &str {
                "Not Valid"
            }
            fn name(&self) -> &str {
                "bad"
            }
        }
        assert!(matches!(
            registry.register(Arc::new(Bad)),
            Err(DocumentError::InvalidName { .. })
        ));
    }

    #[test]
    fn a_source_renderer_becomes_a_format() {
        struct Upper;
        impl rich_plugin_api::SourceRenderer for Upper {
            fn render(
                &self,
                source: &str,
            ) -> Result<Box<dyn Renderable + Send + Sync>, rich_plugin_api::PluginError>
            {
                Ok(Box::new(Text::new(source.to_uppercase())))
            }
        }
        let mut registry = DocumentRegistry::with_builtins();
        registry
            .register(Arc::new(
                SourceRendererFormat::new("shout", Arc::new(Upper)).extensions(&["shout"]),
            ))
            .unwrap();
        let detected = registry.detect(Some("a.shout"), "", CERTAIN).unwrap();
        assert_eq!(detected.format, "shout");
        let rendered = registry
            .render("hi", "shout", &RenderOptions::new())
            .unwrap();
        let console = rich::Console::builder()
            .width(10)
            .color_system(None)
            .build();
        assert_eq!(console.render_to_string(rendered.renderable.as_ref()), "HI");
    }

    #[test]
    fn unknown_formats_name_the_known_ones() {
        let registry = DocumentRegistry::with_builtins();
        let error = registry
            .render("x", "textile", &RenderOptions::new())
            .unwrap_err();
        assert!(error.to_string().contains("org"));
    }
}