Skip to main content

asciidoc_parser/parser/
parser.rs

1use std::{
2    cell::{Cell, RefCell},
3    collections::{HashMap, HashSet},
4    rc::Rc,
5    sync::Arc,
6};
7
8use crate::{
9    Document, HasSpan,
10    blocks::{SectionNumber, SectionType},
11    document::{Attribute, Catalog, InterpretedValue, RefType},
12    parser::{
13        AllowableValue, AttributeValue, DocinfoFileHandler, HtmlSubstitutionRenderer,
14        IncludeFileHandler, InlineSubstitutionRenderer, ModificationContext, PathResolver,
15        SafeMode, SvgFileHandler,
16        built_in_attrs::{built_in_attrs, built_in_default_values},
17        preprocessor::preprocess,
18    },
19    warnings::{Warning, WarningType},
20};
21
22/// The [`Parser`] struct and its related structs allow a caller to configure
23/// how AsciiDoc parsing occurs and then to initiate the parsing process.
24#[derive(Clone, Debug)]
25pub struct Parser {
26    /// Attribute values at current state of parsing.
27    ///
28    /// Shared (copy-on-write via [`Arc`]) with the immutable built-in attribute
29    /// table, so creating or cloning a parser does not deep-copy the table; the
30    /// map is only copied the first time this parser modifies an attribute.
31    pub(crate) attribute_values: Arc<HashMap<String, AttributeValue>>,
32
33    /// Default values for attributes if "set." Immutable after construction and
34    /// shared via [`Arc`] (never copied per parser).
35    default_attribute_values: Arc<HashMap<String, String>>,
36
37    /// Specifies how the basic raw text of a simple block will be converted to
38    /// the format which will ultimately be presented in the final output.
39    ///
40    /// Typically this is an [`HtmlSubstitutionRenderer`] but clients may
41    /// provide alternative implementations.
42    pub(crate) renderer: Rc<dyn InlineSubstitutionRenderer>,
43
44    /// Specifies the name of the primary file to be parsed.
45    pub(crate) primary_file_name: Option<String>,
46
47    /// Specifies how to generate clean and secure paths relative to the parsing
48    /// context.
49    pub path_resolver: PathResolver,
50
51    /// Handler for resolving include:: directives.
52    pub(crate) include_file_handler: Option<Rc<dyn IncludeFileHandler>>,
53
54    /// Handler for resolving docinfo files. If absent, no docinfo content is
55    /// resolved.
56    pub(crate) docinfo_file_handler: Option<Rc<dyn DocinfoFileHandler>>,
57
58    /// Handler for reading the contents of an SVG file requested by an inline
59    /// image with the `inline` option. If absent, inline SVG images fall back
60    /// to rendering their alt text.
61    pub(crate) svg_file_handler: Option<Rc<dyn SvgFileHandler>>,
62
63    /// The safe mode under which the document is parsed and rendered. Controls
64    /// security-sensitive rendering behavior (such as whether an interactive
65    /// SVG image is rendered as an `<object>` element). Defaults to
66    /// [`SafeMode::Secure`].
67    pub(crate) safe: SafeMode,
68
69    /// Document catalog for tracking referenceable elements during parsing.
70    /// This is created during parsing and transferred to the Document when
71    /// complete.
72    ///
73    /// Wrapped in a [`RefCell`] so that anchors and references discovered deep
74    /// inside inline substitution (where only a shared `&Parser` is available,
75    /// e.g. within a regex [`Replacer`](regex::Replacer)) can still be
76    /// registered.
77    catalog: RefCell<Catalog>,
78
79    /// Most recently-assigned section number.
80    pub(crate) last_section_number: SectionNumber,
81
82    /// Most recently-assigned appendix section number.
83    pub(crate) last_appendix_section_number: SectionNumber,
84
85    /// Saved copy of sectnumlevels at end of document header.
86    pub(crate) sectnumlevels: usize,
87
88    /// Section type of outermost section. (Used to determine whether to number
89    /// child sections as a normal section or appendix.)
90    pub(crate) topmost_section_type: SectionType,
91
92    /// True while parsing the direct block children of a section that carries
93    /// the `bibliography` style.
94    ///
95    /// A top-level unordered list parsed in this scope implicitly inherits the
96    /// `bibliography` style (matching Asciidoctor), even without its own
97    /// `[bibliography]` attribute. The flag is saved and restored around each
98    /// section body, so a non-bibliography subsection clears it for its own
99    /// children (the style does not propagate into subsections).
100    pub(crate) parsing_bibliography_section_body: bool,
101
102    /// True while the principal text of a bibliography list item is being
103    /// substituted.
104    ///
105    /// Read through a shared `&Parser` by the macros substitution step so it
106    /// recognizes a leading bibliography anchor (`[[[id]]]`). It is wrapped in
107    /// a [`Cell`] because the substitution code paths (e.g. a regex
108    /// [`Replacer`](regex::Replacer)) only hold a shared reference to the
109    /// parser.
110    pub(crate) in_bibliography_list_item: Cell<bool>,
111
112    /// Live values of [counter] attributes, keyed by counter name (e.g.
113    /// `index`, `example-number`, `table-number`).
114    ///
115    /// A counter is a specialized document attribute: its value is *also* the
116    /// value of the document attribute of the same name. Counters are resolved
117    /// (and advanced) deep inside the attribute-reference substitution step,
118    /// where only a shared `&Parser` is available, so the new value is recorded
119    /// here through a [`RefCell`] and read back as an attribute by
120    /// [`attribute_value()`]. An explicit attribute assignment to a counter's
121    /// name supersedes this overlay (and is what allows `:!name:` to reset a
122    /// counter), so every attribute setter clears the matching entry.
123    ///
124    /// Captioned blocks (example, table, …) are numbered with this same
125    /// mechanism: each context's caption number is the counter named
126    /// `<context>-number`, mirroring Asciidoctor's `Document#counter`.
127    ///
128    /// [counter]: https://docs.asciidoctor.org/asciidoc/latest/attributes/counters/
129    /// [`attribute_value()`]: Self::attribute_value
130    pub(crate) counter_values: RefCell<HashMap<String, String>>,
131
132    /// Canonical names of attributes that are locked against modification from
133    /// the document body for the current scope.
134    ///
135    /// An AsciiDoc table cell creates a nested document that inherits the
136    /// parent document's attributes. An attribute that is *set* in the
137    /// parent _cannot_ be modified inside the cell (matching Asciidoctor,
138    /// which here diverges from the spec's "set or explicitly unset" wording),
139    /// so while a cell is being parsed every inherited attribute name
140    /// (other than a handful of exceptions) is recorded here and a body
141    /// attribute assignment to such a name is silently ignored. The set is
142    /// saved and restored around each cell, so the lock applies only within
143    /// the cell (and nests correctly).
144    pub(crate) locked_attribute_names: HashSet<String>,
145
146    /// Number of AsciiDoc table cells currently being parsed in the call stack.
147    ///
148    /// An AsciiDoc table cell creates a nested, standalone AsciiDoc document.
149    /// While that document is being parsed this counter is greater than zero,
150    /// which (matching Asciidoctor's `Document#nested?`) changes the default
151    /// cell separator of any table found inside from the vertical bar (`|`) to
152    /// the exclamation mark (`!`), so a nested table needs no explicit
153    /// `separator` attribute. The counter is incremented and decremented around
154    /// each AsciiDoc cell, so it nests correctly.
155    pub(crate) nested_document_depth: usize,
156
157    /// Catalog of callout numbers registered by verbatim blocks, used to
158    /// validate the callout lists that annotate them.
159    ///
160    /// Wrapped in a [`RefCell`] because callouts are registered deep inside the
161    /// callouts substitution step, where only a shared `&Parser` is available.
162    callouts: RefCell<CalloutCatalog>,
163
164    /// Warnings produced while replacing attribute references (e.g. a reference
165    /// to a missing attribute when `attribute-missing` is `warn`).
166    ///
167    /// Wrapped in a [`RefCell`] because attribute references are replaced deep
168    /// inside the attributes substitution step, where only a shared `&Parser`
169    /// is available. Each entry stores the byte offset and length of the source
170    /// span the warning refers to (rather than a borrowed
171    /// [`Span`](crate::Span), which the lifetime-free `Parser` cannot
172    /// hold), so the warnings can be turned into
173    /// spanned [`Warning`]s once the document's owned source is available.
174    substitution_warnings: RefCell<Vec<DeferredWarning>>,
175}
176
177/// A warning recorded in a form that does not borrow the source so it can live
178/// on the [`Parser`] (or be returned from preprocessing), to be reconstituted
179/// into a spanned [`Warning`] once the document's owned source is available.
180///
181/// This is used both for warnings raised while replacing attribute references
182/// and for warnings raised during preprocessing (e.g. an unresolved include
183/// directive). The `offset`/`len` pair locates the relevant text within the
184/// (preprocessed) document source.
185#[derive(Clone, Debug)]
186pub(crate) struct DeferredWarning {
187    /// Byte offset into the document source of the span this warning refers to.
188    pub(crate) offset: usize,
189
190    /// Byte length of the span this warning refers to.
191    pub(crate) len: usize,
192
193    /// The type of warning, already carrying any owned data it needs (such as
194    /// the missing attribute's name).
195    pub(crate) warning: WarningType,
196}
197
198/// Tracks the callout numbers defined by verbatim blocks so that a callout list
199/// can be validated against the callouts it annotates.
200///
201/// This mirrors the relevant behavior of Asciidoctor's `Callouts` catalog: each
202/// verbatim block registers the callout numbers it defines into the current
203/// list, and each callout list checks its items against that list (warning
204/// about any item with no matching callout) before the list is closed.
205#[derive(Clone, Debug, Default)]
206struct CalloutCatalog {
207    /// Callout numbers registered (in document order) since the last callout
208    /// list was closed.
209    current: Vec<u32>,
210}
211
212impl Default for Parser {
213    fn default() -> Self {
214        Self {
215            attribute_values: built_in_attrs(),
216            default_attribute_values: built_in_default_values(),
217            renderer: Rc::new(HtmlSubstitutionRenderer {}),
218            primary_file_name: None,
219            path_resolver: PathResolver::default(),
220            include_file_handler: None,
221            docinfo_file_handler: None,
222            svg_file_handler: None,
223            safe: SafeMode::default(),
224            catalog: RefCell::new(Catalog::new()),
225            last_section_number: SectionNumber::default(),
226            last_appendix_section_number: SectionNumber {
227                section_type: SectionType::Appendix,
228                components: vec![],
229            },
230            sectnumlevels: 3,
231            topmost_section_type: SectionType::Normal,
232            parsing_bibliography_section_body: false,
233            in_bibliography_list_item: Cell::new(false),
234            counter_values: RefCell::new(HashMap::new()),
235            locked_attribute_names: HashSet::new(),
236            nested_document_depth: 0,
237            callouts: RefCell::new(CalloutCatalog::default()),
238            substitution_warnings: RefCell::new(vec![]),
239        }
240    }
241}
242
243impl Parser {
244    /// Parse a UTF-8 string as an AsciiDoc document.
245    ///
246    /// The [`Document`] data structure returned by this call has a '`static`
247    /// lifetime; this is an implementation detail. It retains a copy of the
248    /// `source` string that was passed in, but it is not tied to the lifetime
249    /// of that string.
250    ///
251    /// Nearly all of the data structures contained within the [`Document`]
252    /// structure are tied to the lifetime of the document and have a `'src`
253    /// lifetime to signal their dependency on the source document.
254    ///
255    /// **IMPORTANT:** The AsciiDoc language documentation states that UTF-16
256    /// encoding is allowed if a byte-order-mark (BOM) is present at the
257    /// start of a file. This format is not directly supported by the
258    /// `asciidoc-parser` crate. Any UTF-16 content must be re-encoded as
259    /// UTF-8 prior to parsing.
260    ///
261    /// The `Parser` struct will be updated with document attribute values
262    /// discovered during parsing. These values may be inspected using
263    /// [`attribute_value()`].
264    ///
265    /// # Warnings, not errors
266    ///
267    /// Any UTF-8 string is a valid AsciiDoc document, so this function does not
268    /// return an [`Option`] or [`Result`] data type. There may be any number of
269    /// character sequences that have ambiguous or potentially unintended
270    /// meanings. For that reason, a caller is advised to review the warnings
271    /// provided via the [`warnings()`] iterator.
272    ///
273    /// [`warnings()`]: Document::warnings
274    /// [`attribute_value()`]: Self::attribute_value
275    pub fn parse(&mut self, source: &str) -> Document<'static> {
276        let mut document = self.parse_deferred(source);
277
278        // Resolve cross-references against this document's own catalog. For
279        // multi-document workflows, use `parse_deferred` and resolve later with
280        // a caller-supplied resolver via `Document::resolve_references`.
281        document.resolve_against_own_catalog(&*self.renderer);
282
283        document
284    }
285
286    /// Parse a UTF-8 string as an AsciiDoc document, leaving cross-references
287    /// unresolved.
288    ///
289    /// This behaves like [`parse()`], except it does not resolve
290    /// cross-references (`<<id>>`, `xref:id[…]`). The returned [`Document`]
291    /// carries its references in a deferred state; resolve them later with
292    /// [`Document::resolve_references`].
293    ///
294    /// This is the entry point for multi-document workflows (e.g. Antora-style
295    /// site generation): parse every document with this method, build a
296    /// combined index from each document's [`catalog()`], then resolve each
297    /// document against that index. This crate does not merge catalogs
298    /// itself.
299    ///
300    /// [`parse()`]: Self::parse
301    /// [`catalog()`]: Document::catalog
302    pub fn parse_deferred(&mut self, source: &str) -> Document<'static> {
303        let (preprocessed_source, source_map, preprocessor_warnings) = preprocess(source, self);
304
305        // NOTE: `Document::parse` will transfer the catalog to itself at the end of the
306        // parsing operation. Start each parse with a fresh catalog.
307        *self.catalog.borrow_mut() = Catalog::new();
308
309        // Start each parse with an empty callout catalog.
310        *self.callouts.borrow_mut() = CalloutCatalog::default();
311
312        // Start each parse with no pending substitution warnings.
313        self.substitution_warnings.borrow_mut().clear();
314
315        // Reset section numbering for each new document.
316        self.last_section_number = SectionNumber::default();
317
318        // Reset counter (and captioned-block) numbering for each new document.
319        self.counter_values.borrow_mut().clear();
320
321        Document::parse(
322            &preprocessed_source,
323            source_map,
324            preprocessor_warnings,
325            self,
326        )
327    }
328
329    /// Retrieves the current interpreted value of a [document attribute].
330    ///
331    /// Each document holds a set of name-value pairs called document
332    /// attributes. These attributes provide a means of configuring the AsciiDoc
333    /// processor, declaring document metadata, and defining reusable content.
334    /// This page introduces document attributes and answers some questions
335    /// about the terminology used when referring to them.
336    ///
337    /// ## What are document attributes?
338    ///
339    /// Document attributes are effectively document-scoped variables for the
340    /// AsciiDoc language. The AsciiDoc language defines a set of built-in
341    /// attributes, and also allows the author (or extensions) to define
342    /// additional document attributes, which may replace built-in attributes
343    /// when permitted.
344    ///
345    /// Built-in attributes either provide access to read-only information about
346    /// the document and its environment or allow the author to configure
347    /// behavior of the AsciiDoc processor for a whole document or select
348    /// regions. Built-in attributes are effectively unordered. User-defined
349    /// attribute serve as a powerful text replacement tool. User-defined
350    /// attributes are stored in the order in which they are defined.
351    ///
352    /// [document attribute]: https://docs.asciidoctor.org/asciidoc/latest/attributes/document-attributes/
353    pub fn attribute_value<N: AsRef<str>>(&self, name: N) -> InterpretedValue {
354        // A counter's current value lives in the overlay and supersedes any
355        // earlier value of the attribute of the same name (see
356        // [`counter_values`](Self::counter_values)).
357        if let Some(value) = self.counter_values.borrow().get(name.as_ref()) {
358            return InterpretedValue::Value(value.clone());
359        }
360
361        self.attribute_values
362            .get(name.as_ref())
363            .map(|av| av.value.clone())
364            .map(|av| {
365                if let InterpretedValue::Set = av
366                    && let Some(default) = self.default_attribute_values.get(name.as_ref())
367                {
368                    InterpretedValue::Value(default.clone())
369                } else {
370                    av
371                }
372            })
373            .unwrap_or(InterpretedValue::Unset)
374    }
375
376    /// Returns `true` if the parser has a [document attribute] by this name.
377    ///
378    /// [document attribute]: https://docs.asciidoctor.org/asciidoc/latest/attributes/document-attributes/
379    pub fn has_attribute<N: AsRef<str>>(&self, name: N) -> bool {
380        self.counter_values.borrow().contains_key(name.as_ref())
381            || self.attribute_values.contains_key(name.as_ref())
382    }
383
384    /// Returns `true` if the parser has a [document attribute] by this name
385    /// which has been set (i.e. is present and not [unset]).
386    ///
387    /// [document attribute]: https://docs.asciidoctor.org/asciidoc/latest/attributes/document-attributes/
388    /// [unset]: https://docs.asciidoctor.org/asciidoc/latest/attributes/unset-attributes/
389    pub fn is_attribute_set<N: AsRef<str>>(&self, name: N) -> bool {
390        // A counter always holds a concrete (set) value.
391        if self.counter_values.borrow().contains_key(name.as_ref()) {
392            return true;
393        }
394
395        self.attribute_values
396            .get(name.as_ref())
397            .map(|a| a.value != InterpretedValue::Unset)
398            .unwrap_or(false)
399    }
400
401    /// Resolves whether a document title should be displayed, from the
402    /// `showtitle`/`notitle` attribute pair (which are complements).
403    ///
404    /// `showtitle` takes precedence: if present, the title shows precisely when
405    /// it is set. Otherwise `notitle`, if present, hides the title when set.
406    /// When neither attribute is present, `default_shown` decides — a
407    /// standalone document (such as a nested AsciiDoc table cell) shows its
408    /// title, while an embedded document does not.
409    pub(crate) fn resolve_show_title(&self, default_shown: bool) -> bool {
410        if self.has_attribute("showtitle") {
411            self.is_attribute_set("showtitle")
412        } else if self.has_attribute("notitle") {
413            !self.is_attribute_set("notitle")
414        } else {
415            default_shown
416        }
417    }
418
419    /// Forces the `doctype` attribute to `value`, refreshing the derived
420    /// `backend-html5-doctype-*` attribute.
421    ///
422    /// Used when a nested AsciiDoc table cell resets its doctype to the default
423    /// (a cell does not inherit the parent's doctype). The value stays
424    /// modifiable from the document body so the cell may still set its own
425    /// doctype.
426    pub(crate) fn force_doctype(&mut self, value: &str) {
427        Arc::make_mut(&mut self.attribute_values).insert(
428            "doctype".to_string(),
429            AttributeValue {
430                allowable_value: AllowableValue::Any,
431                modification_context: ModificationContext::ApiOrDocumentBody,
432                value: InterpretedValue::Value(value.to_string()),
433            },
434        );
435        self.refresh_doctype_derived_attr();
436    }
437
438    /// Recomputes the `backend-html5-doctype-{doctype}` intrinsic attribute so
439    /// exactly one exists — for the active doctype — resolving to an empty
440    /// (defined) value. References to any other doctype stay undefined and so
441    /// render literally.
442    pub(crate) fn refresh_doctype_derived_attr(&mut self) {
443        Arc::make_mut(&mut self.attribute_values)
444            .retain(|name, _| !name.starts_with("backend-html5-doctype-"));
445
446        if let InterpretedValue::Value(doctype) = self.attribute_value("doctype") {
447            Arc::make_mut(&mut self.attribute_values).insert(
448                format!("backend-html5-doctype-{doctype}"),
449                AttributeValue {
450                    allowable_value: AllowableValue::Any,
451                    modification_context: ModificationContext::Anywhere,
452                    value: InterpretedValue::Value(String::new()),
453                },
454            );
455        }
456    }
457
458    /// Sets the value of an [intrinsic attribute].
459    ///
460    /// Intrinsic attributes are set automatically by the processor. These
461    /// attributes provide information about the document being processed (e.g.,
462    /// `docfile`), the security mode under which the processor is running
463    /// (e.g., `safe-mode-name`), and information about the user’s environment
464    /// (e.g., `user-home`).
465    ///
466    /// The [`modification_context`](ModificationContext) establishes whether
467    /// the value can be subsequently modified by the document header and/or in
468    /// the document body.
469    ///
470    /// Subsequent calls to this function or [`with_intrinsic_attribute_bool()`]
471    /// are always permitted. The last such call for any given attribute name
472    /// takes precendence.
473    ///
474    /// [intrinsic attribute]: https://docs.asciidoctor.org/asciidoc/latest/attributes/document-attributes-ref/#intrinsic-attributes
475    ///
476    /// [`with_intrinsic_attribute_bool()`]: Self::with_intrinsic_attribute_bool
477    pub fn with_intrinsic_attribute<N: AsRef<str>, V: AsRef<str>>(
478        mut self,
479        name: N,
480        value: V,
481        modification_context: ModificationContext,
482    ) -> Self {
483        let attribute_value = AttributeValue {
484            allowable_value: AllowableValue::Any,
485            modification_context,
486            value: InterpretedValue::Value(value.as_ref().to_string()),
487        };
488
489        Arc::make_mut(&mut self.attribute_values)
490            .insert(name.as_ref().to_lowercase(), attribute_value);
491
492        self
493    }
494
495    /// Register a referenceable element (anchor, section, bibliography entry)
496    /// in the document catalog.
497    ///
498    /// This takes `&self` (rather than `&mut self`) so that it can be called
499    /// from inline-substitution code paths that only hold a shared reference to
500    /// the parser, such as a regex [`Replacer`](regex::Replacer).
501    pub(crate) fn register_ref(
502        &self,
503        id: &str,
504        reftext: Option<&str>,
505        ref_type: RefType,
506    ) -> Result<(), crate::document::DuplicateIdError> {
507        self.catalog
508            .borrow_mut()
509            .register_ref(id, reftext, ref_type)
510    }
511
512    /// Registers a callout number defined by a verbatim block.
513    ///
514    /// Takes `&self` so it can be called from the callouts substitution step,
515    /// which only holds a shared reference to the parser.
516    pub(crate) fn register_callout(&self, number: u32) {
517        self.callouts.borrow_mut().current.push(number);
518    }
519
520    /// Returns `true` if a callout numbered `number` was registered for the
521    /// current (not-yet-closed) callout list.
522    pub(crate) fn callout_defined(&self, number: u32) -> bool {
523        self.callouts.borrow().current.contains(&number)
524    }
525
526    /// Closes the current callout list, so callouts registered afterward belong
527    /// to the next list.
528    pub(crate) fn close_callout_list(&self) {
529        self.callouts.borrow_mut().current.clear();
530    }
531
532    /// Returns the number of an already-defined footnote with the given ID, if
533    /// one exists in the current document's footnote registry.
534    ///
535    /// Takes `&self` so it can be called from the macros substitution step,
536    /// which only holds a shared reference to the parser.
537    pub(crate) fn footnote_index_for_id(&self, id: &str) -> Option<String> {
538        self.catalog
539            .borrow()
540            .footnote_with_id(id)
541            .map(|f| f.index.clone())
542    }
543
544    /// Defines a new footnote, advancing the `footnote-number` counter and
545    /// registering the footnote in the current document's registry. Returns the
546    /// number assigned to the footnote.
547    ///
548    /// Takes `&self` so it can be called from the macros substitution step.
549    pub(crate) fn define_footnote(
550        &self,
551        id: Option<&str>,
552        text: String,
553        xrefs: Vec<crate::content::XrefSegment>,
554    ) -> String {
555        // A footnote's text is extracted out of the block during macro
556        // substitution, so any cross-reference inside it never reaches the
557        // document-level resolution pass over block content. Those
558        // cross-references are captured (as placeholders in `text` plus the
559        // `xrefs` segments) so they can be resolved alongside the block
560        // references. The stored `text` is the unresolved fallback rendering
561        // until then, so it is always clean.
562        let (text, deferred) = if xrefs.is_empty() {
563            (text, None)
564        } else {
565            let deferred = crate::content::FootnoteDeferred::new(text, xrefs);
566            let rendered = deferred.render(&*self.renderer);
567            (rendered, Some(Box::new(deferred)))
568        };
569
570        // Footnotes are numbered consecutively throughout the document via the
571        // `footnote-number` counter, which is seeded to `0` so the first
572        // footnote is numbered `1`. The counter is a document-wide attribute, so
573        // numbering continues across nested documents (AsciiDoc table cells)
574        // even though the footnote *list* does not. The counter honors any seed
575        // the document sets, so a non-integer seed yields a non-integer number
576        // (matching Asciidoctor); the value is therefore kept as a string.
577        let index = self.counter("footnote-number", None);
578
579        self.catalog
580            .borrow_mut()
581            .register_footnote(crate::document::Footnote {
582                index: index.clone(),
583                id: id.map(|s| s.to_owned()),
584                text,
585                deferred,
586            });
587
588        index
589    }
590
591    /// Removes and returns the current document's footnote list, leaving an
592    /// empty list behind. Used to give a nested document (an AsciiDoc table
593    /// cell) its own footnote registry; see [`restore_footnotes`].
594    ///
595    /// [`restore_footnotes`]: Self::restore_footnotes
596    pub(crate) fn take_footnotes(&self) -> Vec<crate::document::Footnote> {
597        self.catalog.borrow_mut().take_footnotes()
598    }
599
600    /// Restores a previously-[taken](Self::take_footnotes) footnote list,
601    /// discarding any footnotes registered in the meantime (i.e. those defined
602    /// inside the nested document).
603    pub(crate) fn restore_footnotes(&self, footnotes: Vec<crate::document::Footnote>) {
604        self.catalog.borrow_mut().restore_footnotes(footnotes);
605    }
606
607    /// Records a warning produced while replacing attribute references.
608    ///
609    /// Takes `&self` so it can be called from the attributes substitution step,
610    /// which only holds a shared reference to the parser. `source` locates the
611    /// text the warning refers to; its byte offset and length are stored so a
612    /// spanned [`Warning`] can be reconstructed later (see
613    /// [`take_substitution_warnings`](Self::take_substitution_warnings)).
614    pub(crate) fn record_substitution_warning(
615        &self,
616        source: crate::Span<'_>,
617        warning: WarningType,
618    ) {
619        self.substitution_warnings
620            .borrow_mut()
621            .push(DeferredWarning {
622                offset: source.byte_offset(),
623                len: source.len(),
624                warning,
625            });
626    }
627
628    /// Returns the number of substitution warnings recorded so far.
629    ///
630    /// Used together with [`truncate_substitution_warnings`] to discard
631    /// warnings recorded while parsing an owned (e.g. include-expanded) source,
632    /// whose offsets do not refer to the primary document source.
633    ///
634    /// [`truncate_substitution_warnings`]: Self::truncate_substitution_warnings
635    pub(crate) fn substitution_warnings_len(&self) -> usize {
636        self.substitution_warnings.borrow().len()
637    }
638
639    /// Discards any substitution warnings recorded since the buffer held `len`
640    /// entries.
641    pub(crate) fn truncate_substitution_warnings(&self, len: usize) {
642        self.substitution_warnings.borrow_mut().truncate(len);
643    }
644
645    /// Takes the substitution warnings recorded during parsing, leaving the
646    /// buffer empty.
647    pub(crate) fn take_substitution_warnings(&self) -> Vec<DeferredWarning> {
648        std::mem::take(&mut *self.substitution_warnings.borrow_mut())
649    }
650
651    /// Generate a unique ID derived from `base_id` and register it in the
652    /// document catalog, returning the ID that was assigned.
653    pub(crate) fn generate_and_register_unique_id(
654        &self,
655        base_id: &str,
656        reftext: Option<&str>,
657        ref_type: RefType,
658    ) -> String {
659        self.catalog
660            .borrow_mut()
661            .generate_and_register_unique_id(base_id, reftext, ref_type)
662    }
663
664    /// Takes the catalog from the parser, transferring ownership and leaving an
665    /// empty catalog in its place.
666    ///
667    /// This is used by `Document::parse` to transfer the catalog from the
668    /// parser to the document at the end of parsing.
669    pub(crate) fn take_catalog(&mut self) -> Catalog {
670        std::mem::take(&mut *self.catalog.borrow_mut())
671    }
672
673    /* Comment out until we're prepared to use and test this.
674        /// Sets the default value for an [intrinsic attribute].
675        ///
676        /// Default values for attributes are provided automatically by the
677        /// processor. These values provide a falllback textual value for an
678        /// attribute when it is merely "set" by the document via API, header, or
679        /// document body.
680        ///
681        /// Calling this does not imply that the value is set automatically by
682        /// default, nor does it establish any policy for where the value may be
683        /// modified. For that, please use [`with_intrinsic_attribute`].
684        ///
685        /// [intrinsic attribute]: https://docs.asciidoctor.org/asciidoc/latest/attributes/document-attributes-ref/#intrinsic-attributes
686        /// [`with_intrinsic_attribute`]: Self::with_intrinsic_attribute
687        pub fn with_default_attribute_value<N: AsRef<str>, V: AsRef<str>>(
688            mut self,
689            name: N,
690            value: V,
691        ) -> Self {
692            self.default_attribute_values
693                .insert(name.as_ref().to_string(), value.as_ref().to_string());
694
695            self
696        }
697    */
698
699    /// Sets the value of an [intrinsic attribute] from a boolean flag.
700    ///
701    /// A boolean `true` is interpreted as "set." A boolean `false` is
702    /// interpreted as "unset."
703    ///
704    /// Intrinsic attributes are set automatically by the processor. These
705    /// attributes provide information about the document being processed (e.g.,
706    /// `docfile`), the security mode under which the processor is running
707    /// (e.g., `safe-mode-name`), and information about the user’s environment
708    /// (e.g., `user-home`).
709    ///
710    /// The [`modification_context`](ModificationContext) establishes whether
711    /// the value can be subsequently modified by the document header and/or in
712    /// the document body.
713    ///
714    /// Subsequent calls to this function or [`with_intrinsic_attribute()`] are
715    /// always permitted. The last such call for any given attribute name takes
716    /// precendence.
717    ///
718    /// [intrinsic attribute]: https://docs.asciidoctor.org/asciidoc/latest/attributes/document-attributes-ref/#intrinsic-attributes
719    ///
720    /// [`with_intrinsic_attribute()`]: Self::with_intrinsic_attribute
721    pub fn with_intrinsic_attribute_bool<N: AsRef<str>>(
722        mut self,
723        name: N,
724        value: bool,
725        modification_context: ModificationContext,
726    ) -> Self {
727        let attribute_value = AttributeValue {
728            allowable_value: AllowableValue::Any,
729            modification_context,
730            value: if value {
731                InterpretedValue::Set
732            } else {
733                InterpretedValue::Unset
734            },
735        };
736
737        Arc::make_mut(&mut self.attribute_values)
738            .insert(name.as_ref().to_lowercase(), attribute_value);
739
740        self
741    }
742
743    /// Replace the default [`InlineSubstitutionRenderer`] for this parser.
744    ///
745    /// The default implementation of [`InlineSubstitutionRenderer`] that is
746    /// provided is suitable for HTML5 rendering. If you are targeting a
747    /// different back-end rendering, you will need to provide your own
748    /// implementation and set it using this call before parsing.
749    pub fn with_inline_substitution_renderer<ISR: InlineSubstitutionRenderer + 'static>(
750        mut self,
751        renderer: ISR,
752    ) -> Self {
753        self.renderer = Rc::new(renderer);
754        self
755    }
756
757    /// Sets the name of the primary file to be parsed when [`parse()`] is
758    /// called.
759    ///
760    /// This name will be used for any error messages detected in this file and
761    /// also will be passed to [`IncludeFileHandler::resolve_target()`] as the
762    /// `source` argument for any `include::` file resolution requests from this
763    /// file.
764    ///
765    /// [`parse()`]: Self::parse
766    /// [`IncludeFileHandler::resolve_target()`]: crate::parser::IncludeFileHandler::resolve_target
767    pub fn with_primary_file_name<S: AsRef<str>>(mut self, name: S) -> Self {
768        self.primary_file_name = Some(name.as_ref().to_owned());
769        self
770    }
771
772    /// Sets the [`IncludeFileHandler`] for this parser.
773    ///
774    /// The include file handler is responsible for resolving `include::`
775    /// directives encountered during preprocessing. If no handler is provided,
776    /// include directives will be ignored.
777    ///
778    /// [`IncludeFileHandler`]: crate::parser::IncludeFileHandler
779    pub fn with_include_file_handler<IFH: IncludeFileHandler + 'static>(
780        mut self,
781        handler: IFH,
782    ) -> Self {
783        self.include_file_handler = Some(Rc::new(handler));
784        self
785    }
786
787    /// Sets the [`DocinfoFileHandler`] for this parser.
788    ///
789    /// The docinfo file handler is responsible for providing the content of
790    /// [docinfo files] requested while resolving a document's docinfo (see the
791    /// `docinfo` attribute). If no handler is provided, no docinfo content is
792    /// resolved and [`Document::docinfo`] returns an empty string for every
793    /// location.
794    ///
795    /// [`DocinfoFileHandler`]: crate::parser::DocinfoFileHandler
796    /// [docinfo files]: https://docs.asciidoctor.org/asciidoc/latest/docinfo/
797    /// [`Document::docinfo`]: crate::Document::docinfo
798    pub fn with_docinfo_file_handler<DFH: DocinfoFileHandler + 'static>(
799        mut self,
800        handler: DFH,
801    ) -> Self {
802        self.docinfo_file_handler = Some(Rc::new(handler));
803        self
804    }
805
806    /// Sets the [`SvgFileHandler`] for this parser.
807    ///
808    /// The SVG file handler is responsible for providing the raw contents of an
809    /// SVG file requested by an inline image with the `inline` option (e.g.
810    /// `image:diagram.svg[opts=inline]`). If no handler is provided, inline SVG
811    /// images fall back to rendering their alt text.
812    ///
813    /// [`SvgFileHandler`]: crate::parser::SvgFileHandler
814    pub fn with_svg_file_handler<SFH: SvgFileHandler + 'static>(mut self, handler: SFH) -> Self {
815        self.svg_file_handler = Some(Rc::new(handler));
816        self
817    }
818
819    /// Sets the [`SafeMode`] under which the document is parsed and rendered.
820    ///
821    /// The default is [`SafeMode::Secure`], the most conservative setting.
822    /// Relaxing the safe mode enables security-sensitive rendering behavior,
823    /// such as rendering an interactive SVG image as an `<object>` element.
824    ///
825    /// [`SafeMode`]: crate::SafeMode
826    pub fn with_safe_mode(mut self, safe: SafeMode) -> Self {
827        self.safe = safe;
828        self.apply_safe_mode_attributes();
829        self
830    }
831
832    /// Refreshes the `safe-mode-*` family of [intrinsic attributes] from the
833    /// current safe mode.
834    ///
835    /// These attributes let a document (or a downstream converter) inspect the
836    /// security mode under which it is being processed:
837    ///
838    /// * `safe-mode-level` — the numeric level (`0`, `1`, `10`, or `20`).
839    /// * `safe-mode-name` — the lowercase mode name (`unsafe`, `safe`,
840    ///   `server`, or `secure`).
841    /// * `safe-mode-<name>` — a single flag attribute (set to an empty value)
842    ///   naming the active mode; the flags for the other modes are left unset
843    ///   so that a reference to them resolves literally.
844    ///
845    /// All of these are read-only from the document's perspective (they can
846    /// only be established via the API), matching Ruby Asciidoctor.
847    ///
848    /// [intrinsic attributes]: https://docs.asciidoctor.org/asciidoc/latest/attributes/document-attributes-ref/#intrinsic-attributes
849    fn apply_safe_mode_attributes(&mut self) {
850        let attrs = Arc::make_mut(&mut self.attribute_values);
851
852        let intrinsic = |value: InterpretedValue| AttributeValue {
853            allowable_value: AllowableValue::Any,
854            modification_context: ModificationContext::ApiOnly,
855            value,
856        };
857
858        attrs.insert(
859            "safe-mode-level".to_string(),
860            intrinsic(InterpretedValue::Value(self.safe.level().to_string())),
861        );
862        attrs.insert(
863            "safe-mode-name".to_string(),
864            intrinsic(InterpretedValue::Value(self.safe.name().to_string())),
865        );
866
867        // Exactly one `safe-mode-<name>` flag is set (to an empty value); the
868        // rest are removed so that referencing them resolves literally.
869        for mode in [
870            SafeMode::Unsafe,
871            SafeMode::Safe,
872            SafeMode::Server,
873            SafeMode::Secure,
874        ] {
875            let name = format!("safe-mode-{}", mode.name());
876            if mode == self.safe {
877                attrs.insert(name, intrinsic(InterpretedValue::Set));
878            } else {
879                attrs.remove(&name);
880            }
881        }
882    }
883
884    /// Returns the [`SafeMode`] under which this parser operates.
885    ///
886    /// [`SafeMode`]: crate::SafeMode
887    pub fn safe_mode(&self) -> SafeMode {
888        self.safe
889    }
890
891    /// Returns the document name (`docname`): the base name of the primary
892    /// file, stripped of its directory and final extension.
893    ///
894    /// This is the `<docname>` used to build private docinfo file names (e.g.
895    /// `mydoc-docinfo.html` for `mydoc.adoc`). Returns `None` when no primary
896    /// file name has been set, in which case private docinfo files cannot be
897    /// resolved.
898    pub(crate) fn docname(&self) -> Option<String> {
899        let primary = self.primary_file_name.as_deref()?;
900
901        // Strip the directory portion (handling both separators, since the
902        // primary file name may have been supplied on either platform).
903        let base = primary.rsplit(['/', '\\']).next().unwrap_or(primary);
904
905        // Strip a single trailing extension, if present. A leading-dot name
906        // (e.g. `.adoc`) is treated as having no extension and is kept whole as
907        // the stem, matching Ruby's `File.basename(".adoc", ".*")`.
908        let stem = match base.rfind('.') {
909            Some(0) | None => base,
910            Some(idx) => &base[..idx],
911        };
912
913        if stem.is_empty() {
914            None
915        } else {
916            Some(stem.to_string())
917        }
918    }
919
920    /// Called from [`Header::parse()`] to accept or reject an attribute value.
921    ///
922    /// [`Header::parse()`]: crate::document::Header::parse
923    pub(crate) fn set_attribute_from_header<'src>(
924        &mut self,
925        attr: &Attribute<'src>,
926        warnings: &mut Vec<Warning<'src>>,
927    ) {
928        let attr_name = remap_attr_name(attr.name().data());
929
930        let existing_attr = self.attribute_values.get(&attr_name);
931
932        // Verify that we have permission to overwrite any existing attribute value.
933        if let Some(existing_attr) = existing_attr
934            && (existing_attr.modification_context == ModificationContext::ApiOnly
935                || existing_attr.modification_context == ModificationContext::ApiOrDocumentBody)
936        {
937            warnings.push(Warning {
938                source: attr.span(),
939                warning: WarningType::AttributeValueIsLocked(attr_name),
940            });
941            return;
942        }
943
944        let mut value = attr.value().clone();
945
946        if let InterpretedValue::Set = value
947            && let Some(default_value) = self.default_attribute_values.get(&attr_name)
948        {
949            value = InterpretedValue::Value(default_value.clone());
950        }
951
952        let attribute_value = AttributeValue {
953            allowable_value: AllowableValue::Any,
954            modification_context: ModificationContext::Anywhere,
955            value,
956        };
957
958        // An explicit assignment supersedes (and resets) any counter of the same
959        // name.
960        self.counter_values.borrow_mut().remove(&attr_name);
961
962        let is_doctype = attr_name == "doctype";
963        Arc::make_mut(&mut self.attribute_values).insert(attr_name, attribute_value);
964        if is_doctype {
965            self.refresh_doctype_derived_attr();
966        }
967    }
968
969    /// Called from [`Header::parse()`] for a value that is derived from parsing
970    /// the header (except for attribute lines).
971    ///
972    /// [`Header::parse()`]: crate::document::Header::parse
973    pub(crate) fn set_attribute_by_value_from_header<N: AsRef<str>, V: AsRef<str>>(
974        &mut self,
975        name: N,
976        value: V,
977    ) {
978        let attr_name = remap_attr_name(name);
979
980        let attribute_value = AttributeValue {
981            allowable_value: AllowableValue::Any,
982            modification_context: ModificationContext::Anywhere,
983            value: InterpretedValue::Value(value.as_ref().to_owned()),
984        };
985
986        self.counter_values.borrow_mut().remove(&attr_name);
987        Arc::make_mut(&mut self.attribute_values).insert(attr_name, attribute_value);
988    }
989
990    /// Applies the `imagesdir`-relative default for the `iconsdir` attribute.
991    ///
992    /// The `iconsdir` attribute defaults to `{imagesdir}/icons`; when
993    /// `imagesdir` is left empty this resolves to the built-in
994    /// [`DEFAULT_ICONSDIR`] (`./images/icons`). When `imagesdir` is set to a
995    /// non-empty value and `iconsdir` was left at its built-in default, the
996    /// icons directory is derived as `{imagesdir}/icons`.
997    ///
998    /// The derivation is skipped — so an explicit `iconsdir` wins — when either
999    /// the attribute was set in the header (`iconsdir_set_in_header`) or its
1000    /// resolved value differs from [`DEFAULT_ICONSDIR`] (which is how an
1001    /// override applied any other way, e.g. via the API, is detected). The one
1002    /// case this cannot detect is a non-header override whose value happens to
1003    /// equal the built-in default (e.g. an API caller setting `iconsdir` to
1004    /// exactly `./images/icons`): it is indistinguishable from the default and
1005    /// so is re-derived. That combination is contradictory in practice (it
1006    /// pins `iconsdir` to the value it would take were `imagesdir` unset) and
1007    /// is not worth a dedicated provenance flag.
1008    ///
1009    /// This is called once, after the document header is parsed, mirroring
1010    /// Asciidoctor's document-initialization timing (a later `imagesdir` change
1011    /// in the document body does not retroactively re-derive `iconsdir`). See
1012    /// icons-image.adoc.
1013    ///
1014    /// [`DEFAULT_ICONSDIR`]: super::built_in_attrs::DEFAULT_ICONSDIR
1015    pub(crate) fn apply_iconsdir_default(&mut self, iconsdir_set_in_header: bool) {
1016        if iconsdir_set_in_header {
1017            return;
1018        }
1019
1020        // Preserve any override whose value differs from the built-in default
1021        // (e.g. one applied via the API); only the built-in default itself is
1022        // eligible for `imagesdir`-relative derivation. See the doc comment for
1023        // the one indistinguishable corner case.
1024        if self.attribute_value("iconsdir").as_maybe_str()
1025            != Some(super::built_in_attrs::DEFAULT_ICONSDIR)
1026        {
1027            return;
1028        }
1029
1030        let imagesdir = self.attribute_value("imagesdir");
1031        let derived = match imagesdir.as_maybe_str().filter(|d| !d.is_empty()) {
1032            Some(dir) => format!("{}/icons", dir.trim_end_matches('/')),
1033            None => return,
1034        };
1035
1036        self.set_attribute_by_value_from_header("iconsdir", derived);
1037    }
1038
1039    /// Called while parsing a block (see [`Block::parse_with_outcome()`]) to
1040    /// accept or reject an attribute value from a document (body) attribute.
1041    ///
1042    /// [`Block::parse_with_outcome()`]: crate::blocks::Block::parse_with_outcome
1043    pub(crate) fn set_attribute_from_body<'src>(
1044        &mut self,
1045        attr: &Attribute<'src>,
1046        warnings: &mut Vec<Warning<'src>>,
1047    ) {
1048        let attr_name = remap_attr_name(attr.name().data());
1049
1050        // An attribute inherited from the parent document of an AsciiDoc table
1051        // cell is locked for the duration of that cell: a body assignment to it
1052        // is silently ignored (no warning), matching Asciidoctor.
1053        if self.locked_attribute_names.contains(&attr_name) {
1054            return;
1055        }
1056
1057        // Verify that we have permission to overwrite any existing attribute value.
1058        if let Some(existing_attr) = self.attribute_values.get(&attr_name)
1059            && (existing_attr.modification_context != ModificationContext::Anywhere
1060                && existing_attr.modification_context != ModificationContext::ApiOrDocumentBody)
1061        {
1062            warnings.push(Warning {
1063                source: attr.span(),
1064                warning: WarningType::AttributeValueIsLocked(attr_name),
1065            });
1066            return;
1067        }
1068
1069        let attribute_value = AttributeValue {
1070            allowable_value: AllowableValue::Any,
1071            modification_context: ModificationContext::Anywhere,
1072            value: attr.value().clone(),
1073        };
1074
1075        // An explicit assignment supersedes (and resets) any counter of the same
1076        // name. This is what lets `:!name:` reset a counter.
1077        self.counter_values.borrow_mut().remove(&attr_name);
1078
1079        let is_doctype = attr_name == "doctype";
1080        Arc::make_mut(&mut self.attribute_values).insert(attr_name, attribute_value);
1081        if is_doctype {
1082            self.refresh_doctype_derived_attr();
1083        }
1084    }
1085
1086    /// Assign the next section number for a given level.
1087    pub(crate) fn assign_section_number(&mut self, level: usize) -> SectionNumber {
1088        match self.topmost_section_type {
1089            SectionType::Normal => {
1090                self.last_section_number.assign_next_number(level);
1091                self.last_section_number.clone()
1092            }
1093            SectionType::Appendix => {
1094                self.last_appendix_section_number.assign_next_number(level);
1095                self.last_appendix_section_number.clone()
1096            }
1097            SectionType::Discrete => {
1098                // Shouldn't happen, but ignore if it does.
1099                self.last_section_number.clone()
1100            }
1101        }
1102    }
1103
1104    /// Resolves a [counter] of the given `name`, advancing it to the next value
1105    /// in its sequence and returning that value.
1106    ///
1107    /// A counter is a specialized document attribute: its value is stored as
1108    /// (and read back from) the attribute of the same name, so a later
1109    /// `{name}` reference shows the current value and an attribute assignment
1110    /// such as `:!name:` resets it. Each resolution advances the counter:
1111    ///
1112    /// * an integer value is incremented (`1` -> `2`);
1113    /// * any other value is advanced like Ruby's `String#succ` (`a` -> `b`, `z`
1114    ///   -> `aa`, `Az` -> `Ba`), matching Asciidoctor.
1115    ///
1116    /// `seed` (from the `{counter:name:seed}` form) supplies the first value,
1117    /// but only when the counter is currently unset; otherwise it is ignored.
1118    /// With no seed the sequence starts at `1`.
1119    ///
1120    /// This mirrors Asciidoctor's `Document#counter`.
1121    ///
1122    /// [counter]: https://docs.asciidoctor.org/asciidoc/latest/attributes/counters/
1123    pub(crate) fn counter(&self, name: &str, seed: Option<&str>) -> String {
1124        let next = match self.attribute_value(name) {
1125            InterpretedValue::Value(current) if !current.is_empty() => next_counter_value(&current),
1126            _ => match seed {
1127                Some(seed) if !seed.is_empty() => seed.to_string(),
1128                _ => "1".to_string(),
1129            },
1130        };
1131
1132        self.counter_values
1133            .borrow_mut()
1134            .insert(name.to_string(), next.clone());
1135
1136        next
1137    }
1138}
1139
1140/// Advances a counter value to the next value in its sequence, mirroring
1141/// Asciidoctor's `Helpers.nextval`.
1142///
1143/// A canonical integer string (one that round-trips through integer parsing,
1144/// e.g. `7` but not `07` or `+7`) is incremented numerically. Anything else is
1145/// advanced with [`string_succ`].
1146fn next_counter_value(current: &str) -> String {
1147    if let Ok(n) = current.parse::<i64>()
1148        && n.to_string() == current
1149    {
1150        // `saturating_add` keeps a counter that has somehow reached `i64::MAX`
1151        // pinned there rather than panicking (debug) or wrapping (release).
1152        return n.saturating_add(1).to_string();
1153    }
1154
1155    string_succ(current)
1156}
1157
1158/// Returns the successor of a string, mirroring Ruby's `String#succ` for the
1159/// ASCII cases that AsciiDoc counters can produce.
1160///
1161/// The right-most alphanumeric character is incremented within its own class
1162/// (digits, lowercase letters, uppercase letters), carrying leftward on
1163/// wrap-around (`9` -> `0`, `z` -> `a`, `Z` -> `A`) and prepending a fresh
1164/// leading character (`1`, `a`, or `A`) when the carry runs off the front
1165/// (`z` -> `aa`, `Zz` -> `AAa`). A string with no alphanumeric characters has
1166/// the code point of its last character incremented.
1167fn string_succ(current: &str) -> String {
1168    let chars: Vec<char> = current.chars().collect();
1169
1170    // Without an alphanumeric to carry through, Ruby increments the code point
1171    // of the final character.
1172    if !chars.iter().any(char::is_ascii_alphanumeric) {
1173        let mut chars = chars;
1174        if let Some(last) = chars.last_mut() {
1175            *last = char::from_u32(*last as u32 + 1).unwrap_or(*last);
1176        }
1177        return chars.into_iter().collect();
1178    }
1179
1180    // Walk right to left. `carrying` stays true while we are still looking for
1181    // (or carrying through) the alphanumeric run: trailing non-alphanumeric
1182    // characters are passed over unchanged, then the right-most alphanumeric is
1183    // incremented within its class and any wrap-around carries leftward to the
1184    // next alphanumeric. When the carry runs off the front, a fresh leading
1185    // character of the same class is prepended (`z` -> `aa`, `9` -> `10`).
1186    let mut out_rev: Vec<char> = Vec::with_capacity(chars.len() + 1);
1187    let mut carrying = true;
1188    let mut lead = '1';
1189
1190    for &c in chars.iter().rev() {
1191        if carrying && c.is_ascii_alphanumeric() {
1192            // Increment within the character's class, carrying on wrap-around.
1193            // The arms are exhaustive over ASCII alphanumerics, so the catch-all
1194            // can only be `Z` (the one value not matched above).
1195            let (next, carry) = match c {
1196                '0'..='8' | 'a'..='y' | 'A'..='Y' => ((c as u8 + 1) as char, false),
1197                '9' => ('0', true),
1198                'z' => ('a', true),
1199                _ => ('A', true),
1200            };
1201            out_rev.push(next);
1202            carrying = carry;
1203            // On a carry, remember the class of leading character to prepend if
1204            // the carry runs off the front; `next` is `0`, `a`, or `A` here.
1205            lead = match next {
1206                '0' => '1',
1207                'a' => 'a',
1208                _ => 'A',
1209            };
1210        } else {
1211            // Either the carry is spent, or this is a trailing non-alphanumeric
1212            // we pass over while still searching for the run to increment.
1213            out_rev.push(c);
1214        }
1215    }
1216
1217    if carrying {
1218        out_rev.push(lead);
1219    }
1220
1221    out_rev.into_iter().rev().collect()
1222}
1223
1224fn remap_attr_name<N: AsRef<str>>(raw_attr_name: N) -> String {
1225    let attr_name = raw_attr_name.as_ref().to_lowercase();
1226
1227    // Some attribute names have aliases. Remap to the primary name.
1228    match attr_name.as_str() {
1229        "hardbreaks" => "hardbreaks-option".to_string(),
1230        _ => attr_name,
1231    }
1232}
1233
1234#[cfg(test)]
1235mod tests {
1236    #![allow(clippy::panic)]
1237    #![allow(clippy::unwrap_used)]
1238
1239    use crate::{
1240        attributes::Attrlist,
1241        blocks::Block,
1242        parser::{
1243            CharacterReplacementType, IconRenderParams, ImageRenderParams,
1244            InlineSubstitutionRenderer, LinkRenderParams, QuoteScope, QuoteType, SpecialCharacter,
1245        },
1246        tests::prelude::*,
1247    };
1248
1249    #[test]
1250    fn default_is_unset() {
1251        let p = Parser::default();
1252        assert_eq!(p.attribute_value("foo"), InterpretedValue::Unset);
1253    }
1254
1255    #[test]
1256    fn creates_catalog_if_needed() {
1257        let mut p = Parser::default();
1258        let doc = p.parse("= Hello, World!\n\n== First Section Title");
1259        let cat = doc.catalog();
1260        assert!(cat.refs.contains_key("_first_section_title"));
1261
1262        let doc = p.parse("= Hello, World!\n\n== Second Section Title");
1263        let cat = doc.catalog();
1264        assert!(!cat.refs.contains_key("_first_section_title"));
1265        assert!(cat.refs.contains_key("_second_section_title"));
1266    }
1267
1268    #[test]
1269    fn with_intrinsic_attribute() {
1270        let p =
1271            Parser::default().with_intrinsic_attribute("foo", "bar", ModificationContext::Anywhere);
1272
1273        assert_eq!(p.attribute_value("foo"), InterpretedValue::Value("bar"));
1274        assert_eq!(p.attribute_value("foo2"), InterpretedValue::Unset);
1275
1276        assert!(p.is_attribute_set("foo"));
1277        assert!(!p.is_attribute_set("foo2"));
1278        assert!(!p.is_attribute_set("xyz"));
1279    }
1280
1281    #[test]
1282    fn with_intrinsic_attribute_set() {
1283        let p = Parser::default().with_intrinsic_attribute_bool(
1284            "foo",
1285            true,
1286            ModificationContext::Anywhere,
1287        );
1288
1289        assert_eq!(p.attribute_value("foo"), InterpretedValue::Set);
1290        assert_eq!(p.attribute_value("foo2"), InterpretedValue::Unset);
1291
1292        assert!(p.is_attribute_set("foo"));
1293        assert!(!p.is_attribute_set("foo2"));
1294        assert!(!p.is_attribute_set("xyz"));
1295    }
1296
1297    #[test]
1298    fn with_intrinsic_attribute_unset() {
1299        let p = Parser::default().with_intrinsic_attribute_bool(
1300            "foo",
1301            false,
1302            ModificationContext::Anywhere,
1303        );
1304
1305        assert_eq!(p.attribute_value("foo"), InterpretedValue::Unset);
1306        assert_eq!(p.attribute_value("foo2"), InterpretedValue::Unset);
1307
1308        assert!(!p.is_attribute_set("foo"));
1309        assert!(!p.is_attribute_set("foo2"));
1310        assert!(!p.is_attribute_set("xyz"));
1311    }
1312
1313    #[test]
1314    fn can_not_override_locked_default_value() {
1315        let mut parser = Parser::default();
1316
1317        let doc = parser.parse(":sp: not a space!");
1318
1319        assert_eq!(
1320            doc.warnings().next().unwrap().warning,
1321            WarningType::AttributeValueIsLocked("sp".to_owned())
1322        );
1323
1324        assert_eq!(parser.attribute_value("sp"), InterpretedValue::Value(" "));
1325    }
1326
1327    #[test]
1328    fn catalog_transferred_to_document() {
1329        let mut parser = Parser::default();
1330        let doc = parser.parse("= Test Document\n\nSome content");
1331
1332        let catalog = doc.catalog();
1333        assert!(catalog.is_empty());
1334
1335        // The catalog was transferred to the document, leaving the parser with
1336        // an empty catalog.
1337        assert!(parser.catalog.borrow().is_empty());
1338    }
1339
1340    #[test]
1341    fn block_ids_registered_in_catalog() {
1342        let mut parser = Parser::default();
1343        let doc = parser.parse("= Test Document\n\n[#my-block]\nSome content with an ID");
1344
1345        let catalog = doc.catalog();
1346        assert!(!catalog.is_empty());
1347        assert!(catalog.contains_id("my-block"));
1348
1349        let entry = catalog.get_ref("my-block").unwrap();
1350        assert_eq!(entry.id, "my-block");
1351        assert_eq!(entry.ref_type, crate::document::RefType::Anchor);
1352    }
1353
1354    /// A simple test renderer that modifies special characters differently
1355    /// from the default HTML renderer.
1356    #[derive(Debug)]
1357    struct TestRenderer;
1358
1359    impl InlineSubstitutionRenderer for TestRenderer {
1360        fn render_special_character(&self, type_: SpecialCharacter, dest: &mut String) {
1361            // Custom rendering: wrap special characters in brackets.
1362            match type_ {
1363                SpecialCharacter::Lt => dest.push_str("[LT]"),
1364                SpecialCharacter::Gt => dest.push_str("[GT]"),
1365                SpecialCharacter::Ampersand => dest.push_str("[AMP]"),
1366            }
1367        }
1368
1369        fn render_quoted_substitition(
1370            &self,
1371            _type_: QuoteType,
1372            _scope: QuoteScope,
1373            _attrlist: Option<Attrlist<'_>>,
1374            _id: Option<String>,
1375            body: &str,
1376            dest: &mut String,
1377        ) {
1378            dest.push_str(body);
1379        }
1380
1381        fn render_character_replacement(
1382            &self,
1383            _type_: CharacterReplacementType,
1384            dest: &mut String,
1385        ) {
1386            dest.push_str("[CHAR]");
1387        }
1388
1389        fn render_line_break(&self, dest: &mut String) {
1390            dest.push_str("[BR]");
1391        }
1392
1393        fn render_image(&self, _params: &ImageRenderParams, dest: &mut String) {
1394            dest.push_str("[IMAGE]");
1395        }
1396
1397        fn image_uri(
1398            &self,
1399            target_image_path: &str,
1400            _parser: &Parser,
1401            _asset_dir_key: Option<&str>,
1402        ) -> String {
1403            target_image_path.to_string()
1404        }
1405
1406        fn render_icon(&self, _params: &IconRenderParams, dest: &mut String) {
1407            dest.push_str("[ICON]");
1408        }
1409
1410        fn render_link(&self, _params: &LinkRenderParams, dest: &mut String) {
1411            dest.push_str("[LINK]");
1412        }
1413
1414        fn render_anchor(&self, id: &str, _reftext: Option<String>, dest: &mut String) {
1415            dest.push_str(&format!("[ANCHOR:{}]", id));
1416        }
1417
1418        fn render_xref(&self, params: &crate::parser::XrefRenderParams, dest: &mut String) {
1419            dest.push_str(&format!("[XREF:{}]", params.target));
1420        }
1421
1422        fn render_callout(&self, params: &crate::parser::CalloutRenderParams, dest: &mut String) {
1423            dest.push_str(&format!("[CALLOUT:{}]", params.number));
1424        }
1425
1426        fn render_index_term(
1427            &self,
1428            params: &crate::parser::IndexTermRenderParams,
1429            dest: &mut String,
1430        ) {
1431            match params.visible_term {
1432                Some(term) => dest.push_str(&format!("[INDEXTERM:{term}]")),
1433                None => dest.push_str("[INDEXTERM]"),
1434            }
1435        }
1436
1437        fn render_button(&self, text: &str, dest: &mut String) {
1438            dest.push_str(&format!("[BUTTON:{text}]"));
1439        }
1440
1441        fn render_keyboard(&self, keys: &[String], dest: &mut String) {
1442            dest.push_str(&format!("[KBD:{}]", keys.join("+")));
1443        }
1444
1445        fn render_menu(&self, params: &crate::parser::MenuRenderParams, dest: &mut String) {
1446            dest.push_str(&format!("[MENU:{}]", params.menu));
1447        }
1448
1449        fn render_footnote(&self, params: &crate::parser::FootnoteRenderParams, dest: &mut String) {
1450            match params.index {
1451                Some(index) => dest.push_str(&format!("[FOOTNOTE:{index}]")),
1452                None => dest.push_str(&format!("[FOOTNOTE:{}]", params.text)),
1453            }
1454        }
1455    }
1456
1457    #[test]
1458    fn with_inline_substitution_renderer() {
1459        let mut parser = Parser::default().with_inline_substitution_renderer(TestRenderer);
1460
1461        // Parse a simple document with special characters and a footnote.
1462        let doc = parser.parse("Hello & goodbye < world > test footnote:[a note]");
1463
1464        // The document should parse successfully.
1465        assert_eq!(doc.warnings().count(), 0);
1466
1467        // Get the first block from the document.
1468        let block = doc.nested_blocks().next().unwrap();
1469
1470        let Block::Simple(simple_block) = block else {
1471            panic!("Expected simple block, got: {block:?}");
1472        };
1473
1474        // Our custom renderer should show [AMP], [LT], and [GT] instead of HTML
1475        // entities, and a resolved footnote as [FOOTNOTE:<index>].
1476        assert_eq!(
1477            simple_block.content().rendered(),
1478            "Hello [AMP] goodbye [LT] world [GT] test [FOOTNOTE:1]"
1479        );
1480    }
1481
1482    #[test]
1483    fn custom_renderer_renders_unresolved_footnote() {
1484        let mut parser = Parser::default().with_inline_substitution_renderer(TestRenderer);
1485
1486        // An unresolved footnote reference exercises the renderer's `None`
1487        // (no index) branch, which our custom renderer shows as
1488        // [FOOTNOTE:<text>].
1489        let doc = parser.parse("test.footnote:missing[]");
1490
1491        let block = doc.nested_blocks().next().unwrap();
1492        let Block::Simple(simple_block) = block else {
1493            panic!("Expected simple block, got: {block:?}");
1494        };
1495
1496        assert_eq!(simple_block.content().rendered(), "test.[FOOTNOTE:missing]");
1497    }
1498
1499    mod resolve_show_title {
1500        use crate::parser::{ModificationContext, Parser};
1501
1502        fn with(name: &str, set: bool) -> Parser {
1503            Parser::default().with_intrinsic_attribute_bool(
1504                name,
1505                set,
1506                ModificationContext::Anywhere,
1507            )
1508        }
1509
1510        #[test]
1511        fn neither_present_uses_default() {
1512            assert!(Parser::default().resolve_show_title(true));
1513            assert!(!Parser::default().resolve_show_title(false));
1514        }
1515
1516        #[test]
1517        fn showtitle_takes_precedence_and_decides() {
1518            // Present and set -> shown; present and unset -> hidden, regardless
1519            // of the default.
1520            assert!(with("showtitle", true).resolve_show_title(false));
1521            assert!(!with("showtitle", false).resolve_show_title(true));
1522        }
1523
1524        #[test]
1525        fn notitle_is_the_complement_when_showtitle_absent() {
1526            // notitle set -> hidden; notitle unset -> shown.
1527            assert!(!with("notitle", true).resolve_show_title(true));
1528            assert!(with("notitle", false).resolve_show_title(false));
1529        }
1530    }
1531
1532    mod refresh_doctype_derived_attr {
1533        use crate::{document::InterpretedValue, parser::Parser};
1534
1535        #[test]
1536        fn tracks_the_active_doctype() {
1537            let mut parser = Parser::default();
1538
1539            // The default doctype is `article`, so only its derived attribute is
1540            // defined (to an empty value).
1541            assert_eq!(
1542                parser.attribute_value("backend-html5-doctype-article"),
1543                InterpretedValue::Value(String::new())
1544            );
1545            assert_eq!(
1546                parser.attribute_value("backend-html5-doctype-book"),
1547                InterpretedValue::Unset
1548            );
1549
1550            // Forcing a new doctype moves the derived attribute with it.
1551            parser.force_doctype("book");
1552            assert_eq!(
1553                parser.attribute_value("backend-html5-doctype-book"),
1554                InterpretedValue::Value(String::new())
1555            );
1556            assert_eq!(
1557                parser.attribute_value("backend-html5-doctype-article"),
1558                InterpretedValue::Unset
1559            );
1560        }
1561
1562        #[test]
1563        fn defines_no_derived_attr_when_doctype_is_not_a_value() {
1564            let mut parser = Parser::default();
1565
1566            // The default article derived attribute starts out defined.
1567            assert_eq!(
1568                parser.attribute_value("backend-html5-doctype-article"),
1569                InterpretedValue::Value(String::new())
1570            );
1571
1572            // With `doctype` unset (no `Value`), a refresh clears any existing
1573            // derived attribute and defines none.
1574            std::sync::Arc::make_mut(&mut parser.attribute_values).remove("doctype");
1575            parser.refresh_doctype_derived_attr();
1576
1577            assert_eq!(parser.attribute_value("doctype"), InterpretedValue::Unset);
1578            assert_eq!(
1579                parser.attribute_value("backend-html5-doctype-article"),
1580                InterpretedValue::Unset
1581            );
1582        }
1583    }
1584
1585    mod docname {
1586        use crate::Parser;
1587
1588        #[test]
1589        fn none_without_primary_file_name() {
1590            assert_eq!(Parser::default().docname(), None);
1591        }
1592
1593        #[test]
1594        fn strips_directory_and_extension() {
1595            assert_eq!(
1596                Parser::default()
1597                    .with_primary_file_name("mydoc.adoc")
1598                    .docname()
1599                    .as_deref(),
1600                Some("mydoc")
1601            );
1602            assert_eq!(
1603                Parser::default()
1604                    .with_primary_file_name("docs/guide/mydoc.adoc")
1605                    .docname()
1606                    .as_deref(),
1607                Some("mydoc")
1608            );
1609            // A Windows-style separator is handled too, since the primary file
1610            // name may be supplied on either platform.
1611            assert_eq!(
1612                Parser::default()
1613                    .with_primary_file_name(r"docs\guide\mydoc.adoc")
1614                    .docname()
1615                    .as_deref(),
1616                Some("mydoc")
1617            );
1618        }
1619
1620        #[test]
1621        fn keeps_name_with_no_extension() {
1622            assert_eq!(
1623                Parser::default()
1624                    .with_primary_file_name("README")
1625                    .docname()
1626                    .as_deref(),
1627                Some("README")
1628            );
1629        }
1630
1631        #[test]
1632        fn none_when_path_has_no_file_component() {
1633            // A primary file name that ends in a separator has an empty base
1634            // name, which yields no document name.
1635            assert_eq!(
1636                Parser::default()
1637                    .with_primary_file_name("docs/guide/")
1638                    .docname(),
1639                None
1640            );
1641        }
1642
1643        #[test]
1644        fn leading_dot_name_is_kept_whole() {
1645            // A leading-dot name (e.g. `.adoc`) is treated as a dotfile with no
1646            // extension and kept whole, matching Ruby's
1647            // `File.basename(".adoc", ".*")`.
1648            assert_eq!(
1649                Parser::default()
1650                    .with_primary_file_name(".adoc")
1651                    .docname()
1652                    .as_deref(),
1653                Some(".adoc")
1654            );
1655        }
1656    }
1657
1658    mod counter {
1659        use super::super::next_counter_value;
1660        use crate::{document::InterpretedValue, tests::prelude::*};
1661
1662        #[test]
1663        fn next_counter_value_integer() {
1664            assert_eq!(next_counter_value("1"), "2");
1665            assert_eq!(next_counter_value("9"), "10");
1666            assert_eq!(next_counter_value("0"), "1");
1667            assert_eq!(next_counter_value("-1"), "0");
1668        }
1669
1670        #[test]
1671        fn next_counter_value_non_canonical_integer_is_advanced_as_a_string() {
1672            // A leading zero (or sign) does not round-trip through integer
1673            // parsing, so it is advanced like a string instead.
1674            assert_eq!(next_counter_value("07"), "08");
1675            assert_eq!(next_counter_value("+5"), "+6");
1676            // A leading-zero value still carries digit-to-digit like a string.
1677            assert_eq!(next_counter_value("09"), "10");
1678            assert_eq!(next_counter_value("099"), "100");
1679        }
1680
1681        #[test]
1682        fn next_counter_value_saturates_at_i64_max() {
1683            // A counter pinned at `i64::MAX` stays there rather than panicking
1684            // (debug) or wrapping (release).
1685            let max = i64::MAX.to_string();
1686            assert_eq!(next_counter_value(&max), max);
1687        }
1688
1689        #[test]
1690        fn next_counter_value_characters() {
1691            assert_eq!(next_counter_value("a"), "b");
1692            assert_eq!(next_counter_value("A"), "B");
1693            assert_eq!(next_counter_value("z"), "aa");
1694            assert_eq!(next_counter_value("Z"), "AA");
1695            assert_eq!(next_counter_value("az"), "ba");
1696            assert_eq!(next_counter_value("zz"), "aaa");
1697            assert_eq!(next_counter_value("Zz"), "AAa");
1698        }
1699
1700        #[test]
1701        fn next_counter_value_trailing_non_alphanumeric() {
1702            // The right-most alphanumeric is incremented; trailing punctuation is
1703            // left in place.
1704            assert_eq!(next_counter_value("a)"), "b)");
1705        }
1706
1707        #[test]
1708        fn next_counter_value_no_alphanumeric() {
1709            // With nothing alphanumeric to carry, the final code point advances.
1710            assert_eq!(next_counter_value("{"), "|");
1711        }
1712
1713        #[test]
1714        fn counter_defaults_to_one() {
1715            let p = Parser::default();
1716            assert_eq!(p.counter("x", None), "1");
1717            assert_eq!(p.counter("x", None), "2");
1718            assert_eq!(
1719                p.attribute_value("x"),
1720                InterpretedValue::Value("2".to_string())
1721            );
1722            assert!(p.has_attribute("x"));
1723            assert!(p.is_attribute_set("x"));
1724        }
1725
1726        #[test]
1727        fn counter_seed_used_only_while_unset() {
1728            let p = Parser::default();
1729            assert_eq!(p.counter("c", Some("A")), "A");
1730            // Once set, a later seed is ignored.
1731            assert_eq!(p.counter("c", Some("Q")), "B");
1732        }
1733
1734        #[test]
1735        fn counter_empty_seed_falls_back_to_one() {
1736            let p = Parser::default();
1737            assert_eq!(p.counter("c", Some("")), "1");
1738        }
1739    }
1740}