adoctra-core 0.0.1

Core parser and document model for the adoctra AsciiDoc toolchain
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
//! Extension API: adoctra's own processors at the points where Asciidoctor
//! invokes its extensions (user decision 2026-11-05: an own Rust API, not
//! Ruby API compatibility).
//!
//! * A [`BlockProcessor`] handles a block whose style names it: a delimited
//!   block of one of its [contexts](BlockProcessor::contexts) or a paragraph
//!   (Ruby's `registered_for_block?` in `next_block`).
//! * A [`BlockMacroProcessor`] handles a `name::target[attrlist]` line (Ruby's
//!   `CustomBlockMacroRx`).
//! * An [`InlineMacroProcessor`] handles a `name:target[attrlist]` macro in
//!   text (Ruby's custom inline macros, first in `sub_macros`).
//! * A [`TreeProcessor`] receives the finished document.
//!
//! Processors are registered on a [`Registry`] that a host puts into
//! [`ParseOptions::extensions`](crate::ParseOptions::extensions). Core still
//! performs no I/O: what a processor reads, runs or writes is its own policy.
//! A processor answers with a [`NewBlock`], which then goes through the same
//! completion as a built-in block (title, caption, id, roles, options), or
//! with `None`, which drops the block like Ruby's `nil`. An inline macro
//! processor answers with a [`NewInline`] (an image or quoted text) or with
//! `None`, which removes the macro.

use crate::ast::{Document, Span};
use crate::attributes::Attributes;
use crate::context::{OutputTarget, SafeMode};
use crate::diagnostic::{Diagnostic, DiagnosticCursor, DiagnosticLocation, Severity};
use std::sync::Arc;

/// The context of the block a [`BlockProcessor`] replaces: the delimited
/// block's own context or a paragraph (Ruby's `cloaked-context`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BlockContext {
    Paragraph,
    Listing,
    Literal,
    Open,
    Example,
    Sidebar,
    Quote,
    Pass,
}

impl BlockContext {
    /// Ruby's context name (`listing`, `open`, …).
    pub fn as_str(self) -> &'static str {
        match self {
            BlockContext::Paragraph => "paragraph",
            BlockContext::Listing => "listing",
            BlockContext::Literal => "literal",
            BlockContext::Open => "open",
            BlockContext::Example => "example",
            BlockContext::Sidebar => "sidebar",
            BlockContext::Quote => "quote",
            BlockContext::Pass => "pass",
        }
    }

    /// The context of a delimited block's tip (`----`, `--`, …).
    pub(crate) fn of_tip(tip: &str) -> Option<Self> {
        Some(match tip {
            "----" => BlockContext::Listing,
            "...." => BlockContext::Literal,
            "--" => BlockContext::Open,
            "====" => BlockContext::Example,
            "****" => BlockContext::Sidebar,
            "____" => BlockContext::Quote,
            "++++" => BlockContext::Pass,
            _ => return None,
        })
    }

    /// Whether `style` is one Ruby handles itself on this delimited block (the
    /// context's own name or a masquerade), before any extension.
    pub(crate) fn builtin_style(self, style: &str) -> bool {
        const ADMONITIONS: [&str; 5] = ["NOTE", "TIP", "IMPORTANT", "WARNING", "CAUTION"];
        style == self.as_str()
            || match self {
                BlockContext::Listing => matches!(style, "literal" | "source"),
                BlockContext::Literal => matches!(style, "listing" | "source"),
                BlockContext::Open => {
                    matches!(
                        style,
                        "comment"
                            | "example"
                            | "literal"
                            | "listing"
                            | "pass"
                            | "quote"
                            | "sidebar"
                            | "source"
                            | "verse"
                            | "admonition"
                            | "abstract"
                            | "partintro"
                    ) || ADMONITIONS.contains(&style)
                }
                BlockContext::Example => style == "admonition" || ADMONITIONS.contains(&style),
                BlockContext::Quote => style == "verse",
                BlockContext::Pass => matches!(style, "stem" | "latexmath" | "asciimath"),
                BlockContext::Sidebar | BlockContext::Paragraph => false,
            }
    }
}

/// How a [`BlockProcessor`] receives the block's lines (Ruby's
/// `content_model`). The lines are always the preprocessed lines of the
/// block (includes and conditionals applied).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ContentModel {
    /// The lines as read (Ruby's `:simple`).
    #[default]
    Simple,
    /// The same lines; Ruby distinguishes it only for substitutions.
    Raw,
    /// The lines with tabs expanded and the indentation adjusted by the
    /// `indent` and `tabsize` attributes, like a listing block.
    Verbatim,
    /// The lines are read and discarded; the processor is not invoked.
    Skip,
}

/// How a [`BlockMacroProcessor`] receives the text in its brackets.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum MacroContent {
    /// Parsed as an attribute list (Ruby's `:attributes`, the default).
    #[default]
    Attributes,
    /// Kept whole in the `text` attribute.
    Text,
}

/// A key of [`ExtensionAttributes`]: Ruby's integer keys for positional
/// values and string keys for named ones.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AttrKey {
    Positional(usize),
    Named(String),
}

/// Block attributes in Ruby's insertion order (a Ruby `Hash`): metadata lines
/// in reading order, then the keys the extension point adds (positional names,
/// default attributes, `cloaked-context`). Setting an existing key keeps its
/// position. A key can hold Ruby's `nil`: an empty positional value
/// (`[,svg]`) is a key without a value, and Ruby code that walks the hash
/// (asciidoctor-diagram's checksum) sees it.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ExtensionAttributes {
    entries: Vec<(AttrKey, Option<String>)>,
}

impl ExtensionAttributes {
    pub fn new() -> Self {
        Self::default()
    }

    /// The value of a named attribute (`None` when missing or `nil`, like
    /// Ruby's `attributes[name]`).
    pub fn get(&self, name: &str) -> Option<&str> {
        self.entries.iter().find_map(|(key, value)| match key {
            AttrKey::Named(key) if key == name => value.as_deref(),
            _ => None,
        })
    }

    /// The value of a positional attribute (1-based, like Ruby; `None` when
    /// missing or `nil`).
    pub fn positional(&self, index: usize) -> Option<&str> {
        self.entries.iter().find_map(|(key, value)| match key {
            AttrKey::Positional(key) if *key == index => value.as_deref(),
            _ => None,
        })
    }

    /// Whether the named attribute has a value (a `nil` value counts as
    /// missing, like Ruby's `attributes[name]` truthiness).
    pub fn contains(&self, name: &str) -> bool {
        self.get(name).is_some()
    }

    /// Set a named attribute; an existing key keeps its position.
    pub fn set(&mut self, name: impl Into<String>, value: impl Into<String>) {
        self.set_key(AttrKey::Named(name.into()), value.into());
    }

    /// Set any key; an existing key keeps its position.
    pub fn set_key(&mut self, key: AttrKey, value: String) {
        self.set_value(key, Some(value));
    }

    /// Set any key to a value or to Ruby's `nil`; an existing key keeps its
    /// position.
    pub fn set_value(&mut self, key: AttrKey, value: Option<String>) {
        match self.entries.iter_mut().find(|(k, _)| *k == key) {
            Some((_, slot)) => *slot = value,
            None => self.entries.push((key, value)),
        }
    }

    /// Remove a named attribute and return its value.
    pub fn remove(&mut self, name: &str) -> Option<String> {
        let index = self
            .entries
            .iter()
            .position(|(key, _)| matches!(key, AttrKey::Named(key) if key == name))?;
        self.entries.remove(index).1
    }

    /// Whether the block has the option (`name-option`).
    pub fn has_option(&self, name: &str) -> bool {
        self.contains(&format!("{name}-option"))
    }

    /// The keys with their values in insertion order (`None` is Ruby's
    /// `nil`).
    pub fn iter(&self) -> impl Iterator<Item = (&AttrKey, Option<&str>)> {
        self.entries
            .iter()
            .map(|(key, value)| (key, value.as_deref()))
    }

    pub fn len(&self) -> usize {
        self.entries.len()
    }

    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}

/// A block handed to a [`BlockProcessor`].
#[derive(Debug, Clone)]
pub struct BlockRequest {
    /// The style that selected the processor.
    pub name: String,
    /// The delimited block's context, or [`BlockContext::Paragraph`].
    pub context: BlockContext,
    /// The block's lines (see [`ContentModel`]).
    pub lines: Vec<String>,
    /// The block's attributes, positional names applied, with
    /// `cloaked-context`; the style is removed, like Ruby's `build_block`.
    pub attributes: ExtensionAttributes,
    /// The source range of the whole block.
    pub span: Span,
}

/// A block macro handed to a [`BlockMacroProcessor`].
#[derive(Debug, Clone)]
pub struct BlockMacroRequest {
    pub name: String,
    /// The target after attribute references.
    pub target: String,
    /// The preceding metadata merged with the macro's own attribute list
    /// (or `text`, see [`MacroContent`]).
    pub attributes: ExtensionAttributes,
    /// The macro line.
    pub span: Span,
}

/// How an [`InlineMacroProcessor`] is written (Ruby's `format`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum InlineMacroFormat {
    /// `name:target[attrlist]` (Ruby's `:long`, the default).
    #[default]
    Long,
    /// `name:[attrlist]`; the bracket text is also the target (`:short`).
    Short,
}

/// An inline macro handed to an [`InlineMacroProcessor`].
#[derive(Debug, Clone)]
pub struct InlineMacroRequest {
    pub name: String,
    /// The target as the macros stage sees it (after the earlier
    /// substitutions: special characters, quotes, attribute references and
    /// replacements). A short macro's target is its normalized bracket text.
    pub target: String,
    /// The default attributes, then the bracket text parsed as an attribute
    /// list (or in `text`, see [`MacroContent`]) after Ruby's
    /// `normalize_text` (stripped, line feeds as spaces, `\]` unescaped).
    pub attributes: ExtensionAttributes,
    /// The whole macro.
    pub span: Span,
}

/// What a processor can see of the document and how it reports problems.
pub struct ProcessContext<'a> {
    attributes: &'a Attributes,
    safe: SafeMode,
    base_dir: Option<&'a str>,
    output: Option<&'a OutputTarget>,
    span: Span,
    /// Where reports are located when it differs from `span` (Ruby's
    /// document reader mark while converting).
    cursor: Option<DiagnosticCursor>,
    diagnostics: std::cell::RefCell<Vec<Diagnostic>>,
    /// The converter's markup for [`ProcessContext::apply_subs`].
    markup: Option<std::sync::Arc<dyn Markup>>,
    /// The document's substitution state while reading (counters, footnotes),
    /// else substitutions read the attributes only.
    host: std::cell::RefCell<Option<&'a mut dyn SubstitutionHost>>,
}

/// The document converter's markup for substituted text: Ruby's
/// `apply_subs` returns what the converter makes of quotes, macros, line
/// breaks and callouts (`<strong>…</strong>` for HTML). A processor that
/// feeds substituted text to a tool sees this markup
/// ([`ProcessContext::apply_subs`]).
pub trait Markup: Send + Sync + std::fmt::Debug {
    /// The parts as the converter writes them, with the document's
    /// attributes (`htmlsyntax`, …).
    fn markup(&self, parts: &[crate::AttributePart], attributes: &Attributes) -> String;
}

/// Runs substitution stages with the reading document's state.
pub(crate) trait SubstitutionHost {
    fn substitute(
        &mut self,
        text: &str,
        stages: &[crate::pass_subs::Stage],
        span: Span,
    ) -> Vec<crate::AttributePart>;
}

impl<'a> ProcessContext<'a> {
    pub(crate) fn new(
        attributes: &'a Attributes,
        safe: SafeMode,
        base_dir: Option<&'a str>,
        output: Option<&'a OutputTarget>,
        span: Span,
    ) -> Self {
        Self {
            attributes,
            safe,
            base_dir,
            output,
            span,
            cursor: None,
            diagnostics: Default::default(),
            markup: None,
            host: Default::default(),
        }
    }

    /// Substituted text in the converter's markup (see [`Markup`]).
    pub(crate) fn with_markup(mut self, markup: Option<std::sync::Arc<dyn Markup>>) -> Self {
        self.markup = markup;
        self
    }

    /// Substitutions with the reading document's state.
    pub(crate) fn with_host(self, host: &'a mut dyn SubstitutionHost) -> Self {
        *self.host.borrow_mut() = Some(host);
        self
    }

    /// Ruby's `parent.apply_subs(lines, parent.resolve_block_subs(subs, nil,
    /// subject))`: the substitutions a block's `subs` attribute names
    /// (without defaults; unknown names warn like Ruby), applied to the
    /// lines joined with line feeds, in the converter's markup (plain text
    /// without a [`Markup`]). Ruby runs them while reading, so attribute
    /// counters advance and footnotes register in the document.
    pub fn apply_subs(&self, lines: &[String], subs: &str, subject: &str) -> Vec<String> {
        let mut warnings = Vec::new();
        let stages = crate::pass_subs::resolve_block(
            subs,
            crate::pass_subs::Defaults::None,
            subject,
            self.span,
            &mut warnings,
        );
        self.diagnostics.borrow_mut().extend(warnings);
        let text = lines.join("\n");
        if stages.is_empty() || text.is_empty() {
            return lines.to_vec();
        }
        let parts = match self.host.borrow_mut().as_mut() {
            Some(host) => host.substitute(&text, &stages, self.span),
            None => {
                let mut diagnostics = Vec::new();
                let parts =
                    crate::docinfo::substitute(&text, &stages, self.attributes, &mut diagnostics);
                self.diagnostics.borrow_mut().extend(diagnostics);
                parts
            }
        };
        let text = match &self.markup {
            Some(markup) => markup.markup(&parts, self.attributes),
            None => crate::attributes::parts_text(&parts),
        };
        text.split('\n').map(str::to_owned).collect()
    }

    /// Reports located at this reader cursor instead of the span.
    pub(crate) fn with_cursor(mut self, cursor: Option<DiagnosticCursor>) -> Self {
        self.cursor = cursor;
        self
    }

    /// A document attribute as the block sees it.
    pub fn attribute(&self, name: &str) -> Option<&str> {
        self.attributes.get(name)
    }

    /// All document attributes as the block sees them.
    pub fn attributes(&self) -> &Attributes {
        self.attributes
    }

    pub fn safe(&self) -> SafeMode {
        self.safe
    }

    /// Ruby's `base_dir` of the document, when the host supplied one.
    pub fn base_dir(&self) -> Option<&str> {
        self.base_dir
    }

    /// Where the host writes the converted document (Ruby's `:to_file` or
    /// `:to_dir`), if it writes a file.
    pub fn output(&self) -> Option<&OutputTarget> {
        self.output
    }

    /// The source range of the block or macro being processed.
    pub fn span(&self) -> Span {
        self.span
    }

    /// Report an error at the block (Ruby's `logger.error` with the block's
    /// source location). An inline macro converted after reading reports at
    /// Ruby's document reader mark: the last block read from the document's
    /// own reader (its span stays the macro's for editors).
    pub fn error(&mut self, message: impl Into<String>) {
        self.report(Severity::Error, message.into());
    }

    /// Report a warning at the block.
    pub fn warning(&mut self, message: impl Into<String>) {
        self.report(Severity::Warning, message.into());
    }

    /// Report a warning without a source location (Ruby's messages logged
    /// without `source_location`, e.g. the path resolver's).
    pub fn warning_unlocated(&mut self, message: impl Into<String>) {
        self.report(Severity::Warning, message.into());
        if let Some(last) = self.diagnostics.get_mut().last_mut() {
            last.report.location = crate::diagnostic::DiagnosticLocation::Unlocated;
        }
    }

    fn report(&mut self, severity: Severity, message: String) {
        self.diagnostics.get_mut().push(Diagnostic {
            omitted_span: None,
            report: crate::DiagnosticReport {
                location: self
                    .cursor
                    .map_or(DiagnosticLocation::Source, DiagnosticLocation::Cursor),
                ..Default::default()
            },
            span: self.span,
            severity,
            message,
        });
    }

    pub(crate) fn into_diagnostics(self) -> Vec<Diagnostic> {
        self.diagnostics.into_inner()
    }
}

/// The kind of block a processor creates. Text sources are split into
/// lines with trailing whitespace removed, like Ruby's `Block.new` with a
/// string `:source`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NewBlockKind {
    /// An image block (`target` as an `image::` macro would give it).
    Image { target: String },
    /// A listing block with this text (verbatim, no further substitutions).
    Listing { source: String },
    /// A literal block with this text (verbatim, no further substitutions).
    Literal { source: String },
    /// A passthrough block with this raw text.
    Pass { source: String },
}

/// A block created by a processor, with its attributes. `title`, `caption`,
/// `id`, `role`, `style`, `reftext` and `*-option` keys become the block's
/// metadata; other named keys stay block attributes; positional keys are
/// dropped.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NewBlock {
    pub kind: NewBlockKind,
    pub attributes: ExtensionAttributes,
    /// Caption contexts (`figure`, …) of titled blocks the processor
    /// created and then dropped after Ruby's `assign_caption` numbered
    /// them: each advances `<context>-number` before this block is
    /// numbered, under the same conditions (a title, no `caption`
    /// attribute, a `<context>-caption` prefix).
    pub discarded_captions: Vec<String>,
}

impl NewBlock {
    pub fn image(target: impl Into<String>, attributes: ExtensionAttributes) -> Self {
        Self {
            kind: NewBlockKind::Image {
                target: target.into(),
            },
            attributes,
            discarded_captions: Vec::new(),
        }
    }

    pub fn listing(source: impl Into<String>, attributes: ExtensionAttributes) -> Self {
        Self {
            kind: NewBlockKind::Listing {
                source: source.into(),
            },
            attributes,
            discarded_captions: Vec::new(),
        }
    }

    pub fn literal(source: impl Into<String>, attributes: ExtensionAttributes) -> Self {
        Self {
            kind: NewBlockKind::Literal {
                source: source.into(),
            },
            attributes,
            discarded_captions: Vec::new(),
        }
    }

    pub fn pass(source: impl Into<String>, attributes: ExtensionAttributes) -> Self {
        Self {
            kind: NewBlockKind::Pass {
                source: source.into(),
            },
            attributes,
            discarded_captions: Vec::new(),
        }
    }
}

/// What an [`InlineMacroProcessor`] puts in place of the macro.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NewInlineKind {
    /// An inline image of `target`, resolved like an `image:` macro's
    /// (`imagesdir`, `data-uri`, SVG options). The attributes give `alt`,
    /// `width`, `height`, `title`, `link`, `window`, `role`, `float`,
    /// `imagesdir` and `*-option` flags.
    Image { target: String },
    /// Quoted text (Ruby's `:quoted` inline). Like Ruby's, the text is
    /// written as is, without substitutions or escaping; the `role`
    /// attribute becomes the element's class.
    Quoted {
        format: crate::AttributeFormat,
        text: String,
    },
}

/// An inline node created by an [`InlineMacroProcessor`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NewInline {
    pub kind: NewInlineKind,
    pub attributes: ExtensionAttributes,
}

impl NewInline {
    pub fn image(target: impl Into<String>, attributes: ExtensionAttributes) -> Self {
        Self {
            kind: NewInlineKind::Image {
                target: target.into(),
            },
            attributes,
        }
    }

    pub fn quoted(
        format: crate::AttributeFormat,
        text: impl Into<String>,
        attributes: ExtensionAttributes,
    ) -> Self {
        Self {
            kind: NewInlineKind::Quoted {
                format,
                text: text.into(),
            },
            attributes,
        }
    }
}

/// Handles a block whose style is its [name](BlockProcessor::name).
pub trait BlockProcessor: Send + Sync {
    /// The style the processor is registered for.
    fn name(&self) -> &str;
    /// The contexts it accepts (Ruby's `contexts`/`on_contexts`).
    fn contexts(&self) -> &[BlockContext];
    /// Names for the positional attributes after the style (Ruby's
    /// `name_positional_attributes`): the first names the second field.
    fn positional_attributes(&self) -> &[&str] {
        &[]
    }
    /// Attributes set unless the block has them (Ruby's `default_attrs`).
    fn default_attributes(&self) -> &[(&str, &str)] {
        &[]
    }
    fn content_model(&self) -> ContentModel {
        ContentModel::Simple
    }
    /// Create the replacement block, or `None` to drop the block.
    fn process(&self, request: BlockRequest, context: &mut ProcessContext<'_>) -> Option<NewBlock>;
}

/// Handles `name::target[attrlist]` lines.
pub trait BlockMacroProcessor: Send + Sync {
    fn name(&self) -> &str;
    /// Names for the positional attributes in the brackets.
    fn positional_attributes(&self) -> &[&str] {
        &[]
    }
    /// Attributes set unless the macro has them.
    fn default_attributes(&self) -> &[(&str, &str)] {
        &[]
    }
    fn content(&self) -> MacroContent {
        MacroContent::Attributes
    }
    /// Create the replacement block, or `None` to drop the macro.
    fn process(
        &self,
        request: BlockMacroRequest,
        context: &mut ProcessContext<'_>,
    ) -> Option<NewBlock>;
}

/// Handles `name:target[attrlist]` macros in text. Ruby runs custom inline
/// macros first in its macros substitution, each over the text the previous
/// one left, in registration order: `\\?name:(\S+?)\[(|.*?[^\\])\]` on one
/// line (`name:[…]` for [`InlineMacroFormat::Short`]); a leading backslash
/// escapes the macro.
pub trait InlineMacroProcessor: Send + Sync {
    fn name(&self) -> &str;
    fn format(&self) -> InlineMacroFormat {
        InlineMacroFormat::Long
    }
    /// Names for the positional attributes in the brackets.
    fn positional_attributes(&self) -> &[&str] {
        &[]
    }
    /// Attributes set before the bracket text is parsed (Ruby's
    /// `default_attrs`).
    fn default_attributes(&self) -> &[(&str, &str)] {
        &[]
    }
    fn content(&self) -> MacroContent {
        MacroContent::Attributes
    }
    /// Create the replacement, or `None` to remove the macro.
    fn process(
        &self,
        request: InlineMacroRequest,
        context: &mut ProcessContext<'_>,
    ) -> Option<NewInline>;
}

/// Runs on the finished document (after reading and conversion-time
/// substitutions), before a backend converts it.
pub trait TreeProcessor: Send + Sync {
    fn name(&self) -> &str;
    fn process(&self, document: &mut Document, context: &mut ProcessContext<'_>);
}

/// The processors a host registers for parsing.
#[derive(Default, Clone)]
pub struct Registry {
    blocks: Vec<Arc<dyn BlockProcessor>>,
    block_macros: Vec<Arc<dyn BlockMacroProcessor>>,
    inline_macros: Vec<Arc<dyn InlineMacroProcessor>>,
    tree_processors: Vec<Arc<dyn TreeProcessor>>,
}

impl std::fmt::Debug for Registry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Registry")
            .field(
                "blocks",
                &self.blocks.iter().map(|p| p.name()).collect::<Vec<_>>(),
            )
            .field(
                "block_macros",
                &self
                    .block_macros
                    .iter()
                    .map(|p| p.name())
                    .collect::<Vec<_>>(),
            )
            .field(
                "inline_macros",
                &self
                    .inline_macros
                    .iter()
                    .map(|p| p.name())
                    .collect::<Vec<_>>(),
            )
            .field(
                "tree_processors",
                &self
                    .tree_processors
                    .iter()
                    .map(|p| p.name())
                    .collect::<Vec<_>>(),
            )
            .finish()
    }
}

impl Registry {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn register_block(&mut self, processor: impl BlockProcessor + 'static) -> &mut Self {
        self.blocks.push(Arc::new(processor));
        self
    }

    pub fn register_block_macro(
        &mut self,
        processor: impl BlockMacroProcessor + 'static,
    ) -> &mut Self {
        self.block_macros.push(Arc::new(processor));
        self
    }

    /// Register an inline macro processor. Like Ruby's hash of inline
    /// macros, a second registration of a name replaces the first in its
    /// place.
    pub fn register_inline_macro(
        &mut self,
        processor: impl InlineMacroProcessor + 'static,
    ) -> &mut Self {
        let processor: Arc<dyn InlineMacroProcessor> = Arc::new(processor);
        match self
            .inline_macros
            .iter_mut()
            .find(|p| p.name() == processor.name())
        {
            Some(slot) => *slot = processor,
            None => self.inline_macros.push(processor),
        }
        self
    }

    pub fn register_tree_processor(
        &mut self,
        processor: impl TreeProcessor + 'static,
    ) -> &mut Self {
        self.tree_processors.push(Arc::new(processor));
        self
    }

    pub fn is_empty(&self) -> bool {
        self.blocks.is_empty()
            && self.block_macros.is_empty()
            && self.inline_macros.is_empty()
            && self.tree_processors.is_empty()
    }

    /// Ruby's `registered_for_block?`: the last registration wins.
    pub fn block(&self, name: &str, context: BlockContext) -> Option<&Arc<dyn BlockProcessor>> {
        self.blocks
            .iter()
            .rev()
            .find(|p| p.name() == name && p.contexts().contains(&context))
    }

    /// Ruby's `registered_for_block_macro?`: the last registration wins.
    pub fn block_macro(&self, name: &str) -> Option<&Arc<dyn BlockMacroProcessor>> {
        self.block_macros.iter().rev().find(|p| p.name() == name)
    }

    /// The inline macro processors in the order Ruby applies them.
    pub fn inline_macros(&self) -> impl Iterator<Item = &Arc<dyn InlineMacroProcessor>> {
        self.inline_macros.iter()
    }

    pub fn tree_processors(&self) -> impl Iterator<Item = &Arc<dyn TreeProcessor>> {
        self.tree_processors.iter()
    }
}