Skip to main content

ferromark_transforms/
typography.rs

1//! Explicit, language-aware typography for prose in a parsed document.
2
3use std::error::Error;
4use std::fmt;
5use std::ops::Range;
6use std::str::FromStr;
7
8use ferromark::ast::{Document, Node, Span};
9
10use crate::prose::raw_html_text_spans;
11use crate::{BoxError, TransformContext, TransformPass, text_runs};
12
13/// A supported language for the optional typography pass.
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15#[non_exhaustive]
16pub enum TypographyLanguage {
17    /// English (`en`).
18    English,
19    /// Spanish (`es`).
20    Spanish,
21    /// French (`fr`).
22    French,
23    /// Portuguese (`pt`).
24    Portuguese,
25    /// German (`de`).
26    German,
27    /// Italian (`it`).
28    Italian,
29    /// Dutch (`nl`).
30    Dutch,
31    /// Polish (`pl`).
32    Polish,
33    /// Russian (`ru`).
34    Russian,
35    /// Ukrainian (`uk`).
36    Ukrainian,
37    /// Czech (`cs`).
38    Czech,
39    /// Danish (`da`).
40    Danish,
41    /// Finnish (`fi`).
42    Finnish,
43    /// Norwegian Bokmål (`nb`).
44    NorwegianBokmal,
45    /// Swedish (`sv`).
46    Swedish,
47}
48
49impl TypographyLanguage {
50    /// Returns this language's lowercase ISO 639-1 code.
51    #[must_use]
52    pub const fn as_str(self) -> &'static str {
53        match self {
54            Self::English => "en",
55            Self::Spanish => "es",
56            Self::French => "fr",
57            Self::Portuguese => "pt",
58            Self::German => "de",
59            Self::Italian => "it",
60            Self::Dutch => "nl",
61            Self::Polish => "pl",
62            Self::Russian => "ru",
63            Self::Ukrainian => "uk",
64            Self::Czech => "cs",
65            Self::Danish => "da",
66            Self::Finnish => "fi",
67            Self::NorwegianBokmal => "nb",
68            Self::Swedish => "sv",
69        }
70    }
71
72    fn converts_apostrophes(self) -> bool {
73        !matches!(self, Self::Russian | Self::Ukrainian)
74    }
75
76    fn converts_double_hyphens(self) -> bool {
77        matches!(self, Self::English | Self::Russian)
78    }
79
80    fn converts_measurement_spaces(self) -> bool {
81        !matches!(self, Self::Russian | Self::Ukrainian)
82    }
83}
84
85impl FromStr for TypographyLanguage {
86    type Err = UnsupportedTypographyLanguage;
87
88    fn from_str(language: &str) -> Result<Self, Self::Err> {
89        match language {
90            "en" => Ok(Self::English),
91            "es" => Ok(Self::Spanish),
92            "fr" => Ok(Self::French),
93            "pt" => Ok(Self::Portuguese),
94            "de" => Ok(Self::German),
95            "it" => Ok(Self::Italian),
96            "nl" => Ok(Self::Dutch),
97            "pl" => Ok(Self::Polish),
98            "ru" => Ok(Self::Russian),
99            "uk" => Ok(Self::Ukrainian),
100            "cs" => Ok(Self::Czech),
101            "da" => Ok(Self::Danish),
102            "fi" => Ok(Self::Finnish),
103            "nb" => Ok(Self::NorwegianBokmal),
104            "sv" => Ok(Self::Swedish),
105            _ => Err(UnsupportedTypographyLanguage {
106                language: language.to_owned(),
107            }),
108        }
109    }
110}
111
112/// Error returned when a language code is not supported by the typography pass.
113#[derive(Debug, Clone, PartialEq, Eq)]
114pub struct UnsupportedTypographyLanguage {
115    language: String,
116}
117
118impl UnsupportedTypographyLanguage {
119    /// Returns the unsupported language code.
120    #[must_use]
121    pub fn language(&self) -> &str {
122        &self.language
123    }
124}
125
126impl fmt::Display for UnsupportedTypographyLanguage {
127    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
128        write!(
129            formatter,
130            "unsupported typography language {:?}; supported languages are cs, da, de, en, es, fi, fr, it, nb, nl, pl, pt, ru, sv, and uk",
131            self.language
132        )
133    }
134}
135
136impl Error for UnsupportedTypographyLanguage {}
137
138/// Configuration for the optional typography pass.
139#[derive(Debug, Clone, Copy, PartialEq, Eq)]
140pub struct TypographyOptions {
141    language: TypographyLanguage,
142    dashes: bool,
143    ellipses: bool,
144    guillemet_digraphs: bool,
145}
146
147impl TypographyOptions {
148    /// Creates typography options for one explicit language.
149    ///
150    /// Dash and ellipsis conversion are enabled. The pass never detects a
151    /// language or changes languages within a document.
152    #[must_use]
153    pub const fn new(language: TypographyLanguage) -> Self {
154        Self {
155            language,
156            dashes: true,
157            ellipses: true,
158            guillemet_digraphs: false,
159        }
160    }
161
162    /// Enables or disables dash conversion independently of other rules.
163    #[must_use]
164    pub const fn with_dashes(mut self, enabled: bool) -> Self {
165        self.dashes = enabled;
166        self
167    }
168
169    /// Enables or disables ellipsis conversion independently of other rules.
170    #[must_use]
171    pub const fn with_ellipses(mut self, enabled: bool) -> Self {
172        self.ellipses = enabled;
173        self
174    }
175
176    /// Enables conversion of parser-preserved `<<…>>` digraphs.
177    ///
178    /// The parser's `guillemet_digraphs` option must also be enabled so the
179    /// enclosed text is available to this pass.
180    #[must_use]
181    pub const fn with_guillemet_digraphs(mut self, enabled: bool) -> Self {
182        self.guillemet_digraphs = enabled;
183        self
184    }
185
186    /// Returns the configured language.
187    #[must_use]
188    pub const fn language(self) -> TypographyLanguage {
189        self.language
190    }
191
192    /// Returns whether dash conversion is enabled.
193    #[must_use]
194    pub const fn dashes(self) -> bool {
195        self.dashes
196    }
197
198    /// Returns whether ellipsis conversion is enabled.
199    #[must_use]
200    pub const fn ellipses(self) -> bool {
201        self.ellipses
202    }
203
204    /// Returns whether `<<…>>` digraphs are converted by this pass.
205    #[must_use]
206    pub const fn guillemet_digraphs(self) -> bool {
207        self.guillemet_digraphs
208    }
209}
210
211/// A pass that applies explicit language-aware typography to prose text.
212///
213/// Quotes pair across ordinary inline markup. Code, math, raw HTML, MDX
214/// expressions, image metadata, link destinations and detected renderer
215/// autolinks are protected. Paragraphs and other block containers have
216/// independent quote context. Existing Unicode punctuation is preserved.
217pub struct TypographyPass {
218    options: TypographyOptions,
219}
220
221impl TypographyPass {
222    /// Creates a typography pass with the given language and options.
223    #[must_use]
224    pub const fn new(options: TypographyOptions) -> Self {
225        Self { options }
226    }
227}
228
229impl TransformPass for TypographyPass {
230    fn name(&self) -> &'static str {
231        "typography"
232    }
233
234    fn apply<'arena>(
235        &mut self,
236        document: &mut Document<'arena>,
237        context: &TransformContext<'arena>,
238    ) -> Result<(), BoxError> {
239        let raw_html_spans = raw_html_text_spans(document, true);
240        if self.options.guillemet_digraphs {
241            transform_block_children::<true>(
242                &mut document.children,
243                context,
244                self.options,
245                &raw_html_spans,
246            );
247        } else {
248            transform_block_children::<false>(
249                &mut document.children,
250                context,
251                self.options,
252                &raw_html_spans,
253            );
254        }
255        Ok(())
256    }
257}
258
259enum InlineItem<'arena> {
260    Text {
261        value: &'arena str,
262        protected: Vec<Range<usize>>,
263        escaped: Vec<Range<usize>>,
264    },
265    Boundary,
266    HtmlBoundary,
267    SoftBreak,
268}
269
270#[derive(Clone, Copy)]
271struct GuillemetToken {
272    offset: usize,
273    kind: GuillemetTokenKind,
274}
275
276#[derive(Clone, Copy)]
277enum GuillemetTokenKind {
278    Opening,
279    Closing,
280    TrimPadding,
281}
282
283#[derive(Clone, Copy)]
284struct PendingGuillemet {
285    item: usize,
286    offset: usize,
287}
288
289#[derive(Default)]
290struct QuoteState {
291    stack: Vec<QuoteFrame>,
292    previous: Option<char>,
293}
294
295#[derive(Clone, Copy)]
296struct QuoteFrame {
297    source: char,
298    primary: bool,
299}
300
301#[derive(Clone, Copy)]
302struct QuoteMarks {
303    open: &'static str,
304    close: &'static str,
305}
306
307impl QuoteState {
308    fn reset(&mut self) {
309        self.stack.clear();
310        self.previous = None;
311    }
312
313    fn observe(&mut self, value: &str) {
314        if let Some(last) = value.chars().last() {
315            self.previous = Some(last);
316        }
317    }
318
319    fn replace_quote(
320        &mut self,
321        source: char,
322        next: Option<char>,
323        language: TypographyLanguage,
324    ) -> Option<&'static str> {
325        if self
326            .stack
327            .last()
328            .is_some_and(|frame| frame.source == source)
329            && self.previous.is_some_and(can_close_quote)
330            && let Some(frame) = self.stack.pop()
331        {
332            return Some(quote_marks(language, frame.primary).close);
333        }
334
335        if can_open_quote(self.previous, next) {
336            let primary = self
337                .stack
338                .last()
339                .map_or(source == '"', |frame| !frame.primary);
340            self.stack.push(QuoteFrame { source, primary });
341            return Some(quote_marks(language, primary).open);
342        }
343
344        if self.previous.is_some_and(can_close_quote) {
345            return Some(quote_marks(language, source == '"').close);
346        }
347
348        None
349    }
350
351    #[inline]
352    fn observe_authored_quote(
353        &mut self,
354        character: char,
355        next: Option<char>,
356        language: TypographyLanguage,
357    ) {
358        if let Some(primary) = symmetric_quote_level(character, language) {
359            if self
360                .stack
361                .last()
362                .is_some_and(|frame| frame.primary == primary)
363            {
364                if self.previous.is_some_and(can_close_quote)
365                    && next.is_none_or(|next| !next.is_alphanumeric())
366                {
367                    self.stack.pop();
368                }
369                return;
370            }
371
372            if can_open_quote(self.previous, next) {
373                self.stack.push(QuoteFrame {
374                    source: if primary { '"' } else { '\'' },
375                    primary,
376                });
377            }
378            return;
379        }
380
381        if let Some((opening, primary)) = authored_quote_event(character, language) {
382            if opening && can_open_quote(self.previous, next) {
383                self.stack.push(QuoteFrame {
384                    source: if primary { '"' } else { '\'' },
385                    primary,
386                });
387            } else if !opening
388                && self.stack.last().is_some_and(|frame| {
389                    frame.primary == primary && self.previous.is_some_and(can_close_quote)
390                })
391            {
392                self.stack.pop();
393            }
394        }
395    }
396}
397
398fn transform_block_children<'arena, const GUILLEMET_DIGRAPHS: bool>(
399    nodes: &mut [Node<'arena>],
400    context: &TransformContext<'arena>,
401    options: TypographyOptions,
402    raw_html_spans: &[Span],
403) {
404    let mut cursor = 0;
405    while cursor < nodes.len() {
406        if is_inline_node(&nodes[cursor]) {
407            let start = cursor;
408            cursor += 1;
409            while cursor < nodes.len() && is_inline_node(&nodes[cursor]) {
410                cursor += 1;
411            }
412            transform_inline_children::<GUILLEMET_DIGRAPHS>(
413                &mut nodes[start..cursor],
414                context,
415                options,
416                raw_html_spans,
417            );
418        } else {
419            transform_block_node::<GUILLEMET_DIGRAPHS>(
420                &mut nodes[cursor],
421                context,
422                options,
423                raw_html_spans,
424            );
425            cursor += 1;
426        }
427    }
428}
429
430fn transform_block_node<'arena, const GUILLEMET_DIGRAPHS: bool>(
431    node: &mut Node<'arena>,
432    context: &TransformContext<'arena>,
433    options: TypographyOptions,
434    raw_html_spans: &[Span],
435) {
436    match node {
437        Node::Span(node) => {
438            transform_inline_children::<GUILLEMET_DIGRAPHS>(
439                &mut node.children,
440                context,
441                options,
442                raw_html_spans,
443            );
444        }
445        Node::Paragraph(node) => {
446            transform_inline_children::<GUILLEMET_DIGRAPHS>(
447                &mut node.children,
448                context,
449                options,
450                raw_html_spans,
451            );
452        }
453        Node::Heading(node) => {
454            transform_inline_children::<GUILLEMET_DIGRAPHS>(
455                &mut node.children,
456                context,
457                options,
458                raw_html_spans,
459            );
460        }
461        Node::BlockQuote(node) => {
462            transform_block_children::<GUILLEMET_DIGRAPHS>(
463                &mut node.children,
464                context,
465                options,
466                raw_html_spans,
467            );
468        }
469        Node::List(node) => {
470            for item in &mut node.children {
471                transform_block_children::<GUILLEMET_DIGRAPHS>(
472                    &mut item.children,
473                    context,
474                    options,
475                    raw_html_spans,
476                );
477            }
478        }
479        Node::ListItem(node) => {
480            transform_block_children::<GUILLEMET_DIGRAPHS>(
481                &mut node.children,
482                context,
483                options,
484                raw_html_spans,
485            );
486        }
487        Node::Table(node) => {
488            if let Some(attributes) = &mut node.attributes {
489                transform_inline_children::<GUILLEMET_DIGRAPHS>(
490                    &mut attributes.caption,
491                    context,
492                    options,
493                    raw_html_spans,
494                );
495            }
496            for row in &mut node.children {
497                for cell in &mut row.children {
498                    transform_inline_children::<GUILLEMET_DIGRAPHS>(
499                        &mut cell.children,
500                        context,
501                        options,
502                        raw_html_spans,
503                    );
504                }
505            }
506        }
507        Node::Figure(node) => {
508            transform_block_node::<GUILLEMET_DIGRAPHS>(
509                &mut node.content,
510                context,
511                options,
512                raw_html_spans,
513            );
514            transform_inline_children::<GUILLEMET_DIGRAPHS>(
515                &mut node.caption,
516                context,
517                options,
518                raw_html_spans,
519            );
520        }
521        Node::DefinitionList(node) => {
522            transform_block_children::<GUILLEMET_DIGRAPHS>(
523                &mut node.children,
524                context,
525                options,
526                raw_html_spans,
527            );
528        }
529        Node::DefinitionListTerm(node) => {
530            transform_inline_children::<GUILLEMET_DIGRAPHS>(
531                &mut node.children,
532                context,
533                options,
534                raw_html_spans,
535            );
536        }
537        Node::DefinitionListDefinition(node) => {
538            transform_block_children::<GUILLEMET_DIGRAPHS>(
539                &mut node.children,
540                context,
541                options,
542                raw_html_spans,
543            );
544        }
545        Node::FootnoteDefinition(node) => {
546            transform_block_children::<GUILLEMET_DIGRAPHS>(
547                &mut node.children,
548                context,
549                options,
550                raw_html_spans,
551            );
552        }
553        Node::MdxJsxFlowElement(node) => {
554            transform_block_children::<GUILLEMET_DIGRAPHS>(
555                &mut node.children,
556                context,
557                options,
558                raw_html_spans,
559            );
560        }
561        Node::ThematicBreak(_)
562        | Node::CodeBlock(_)
563        | Node::MathBlock(_)
564        | Node::Html(_)
565        | Node::Text(_)
566        | Node::Emphasis(_)
567        | Node::Strong(_)
568        | Node::InlineCode(_)
569        | Node::InlineMath(_)
570        | Node::Break(_)
571        | Node::Link(_)
572        | Node::Image(_)
573        | Node::Highlight(_)
574        | Node::Delete(_)
575        | Node::Superscript(_)
576        | Node::Subscript(_)
577        | Node::FootnoteReference(_)
578        | Node::Definition(_)
579        | Node::MdxJsxTextElement(_)
580        | Node::MdxjsEsm(_)
581        | Node::MdxFlowExpression(_)
582        | Node::MdxTextExpression(_)
583        | Node::Insertion(_) => {}
584    }
585}
586
587fn is_inline_node(node: &Node<'_>) -> bool {
588    matches!(
589        node,
590        Node::Text(_)
591            | Node::Emphasis(_)
592            | Node::Strong(_)
593            | Node::InlineCode(_)
594            | Node::InlineMath(_)
595            | Node::Break(_)
596            | Node::Link(_)
597            | Node::Span(_)
598            | Node::Image(_)
599            | Node::Highlight(_)
600            | Node::Delete(_)
601            | Node::Insertion(_)
602            | Node::Superscript(_)
603            | Node::Subscript(_)
604            | Node::FootnoteReference(_)
605            | Node::Html(_)
606            | Node::MdxJsxTextElement(_)
607            | Node::MdxTextExpression(_)
608    )
609}
610
611fn transform_inline_children<'arena, const GUILLEMET_DIGRAPHS: bool>(
612    nodes: &mut [Node<'arena>],
613    context: &TransformContext<'arena>,
614    options: TypographyOptions,
615    raw_html_spans: &[Span],
616) {
617    let mut items = Vec::new();
618    let mut protected_urls = Vec::new();
619    collect_inline_items::<GUILLEMET_DIGRAPHS>(
620        nodes,
621        context,
622        raw_html_spans,
623        &mut items,
624        &mut protected_urls,
625    );
626    let next_after = next_char_after_items(&items);
627    let guillemet_tokens = if GUILLEMET_DIGRAPHS {
628        matched_guillemet_tokens(&items, &protected_urls)
629    } else {
630        Vec::new()
631    };
632    let mut state = QuoteState::default();
633    let mut replacements = Vec::new();
634
635    for (index, item) in items.iter().enumerate() {
636        match item {
637            InlineItem::Text {
638                value,
639                protected,
640                escaped,
641                ..
642            } => replacements.push(transform_text::<GUILLEMET_DIGRAPHS>(
643                value,
644                protected,
645                escaped,
646                next_after[index],
647                &items,
648                index,
649                &mut state,
650                options,
651                if GUILLEMET_DIGRAPHS {
652                    &guillemet_tokens[index]
653                } else {
654                    &[]
655                },
656            )),
657            InlineItem::Boundary => state.reset(),
658            InlineItem::HtmlBoundary if GUILLEMET_DIGRAPHS => {}
659            InlineItem::HtmlBoundary => state.reset(),
660            InlineItem::SoftBreak => state.previous = Some(' '),
661        }
662    }
663
664    let mut replacement_index = 0;
665    apply_inline_replacements(nodes, &replacements, &mut replacement_index, context);
666}
667
668/// Finds complete `<<` / `>>` pairs across ordinary inline text nodes.
669/// Protected nodes and URL ranges split quote context; raw HTML tags are
670/// transparent so a pair can enclose ordinary inline HTML.
671fn matched_guillemet_tokens(
672    items: &[InlineItem<'_>],
673    protected_urls: &[Option<Vec<Range<usize>>>],
674) -> Vec<Vec<GuillemetToken>> {
675    let mut tokens: Vec<Vec<GuillemetToken>> = (0..items.len()).map(|_| Vec::new()).collect();
676    let mut pending = Vec::new();
677
678    for (item_index, item) in items.iter().enumerate() {
679        let InlineItem::Text {
680            value,
681            protected,
682            escaped,
683            ..
684        } = item
685        else {
686            match item {
687                InlineItem::Boundary => pending.clear(),
688                InlineItem::HtmlBoundary | InlineItem::SoftBreak => {}
689                InlineItem::Text { .. } => unreachable!(),
690            }
691            continue;
692        };
693
694        let bytes = value.as_bytes();
695        let mut offset = 0;
696        while offset < bytes.len() {
697            let (marker, opening) = match bytes[offset] {
698                b'<' => (b'<', true),
699                b'>' => (b'>', false),
700                _ => {
701                    offset += next_char_at(value, offset).map_or(1, |(_, width)| width);
702                    continue;
703                }
704            };
705            if is_exact_doubled_marker(bytes, offset, marker)
706                && !is_blocked(offset, protected, escaped)
707                && !is_blocked(offset + 1, protected, escaped)
708            {
709                if opening {
710                    // A marker directly in front of an autolink stays
711                    // literal; otherwise the generated closing quote can
712                    // be emitted as part of the link destination.
713                    if !is_protected_immediately_after(items, item_index, offset + 2) {
714                        pending.push(PendingGuillemet {
715                            item: item_index,
716                            offset,
717                        });
718                    }
719                } else if let Some(open) = pending.pop()
720                    && has_guillemet_content(items, open.item, open.offset + 2, item_index, offset)
721                    && !is_url_protected_near_closer(items, protected_urls, item_index, offset)
722                {
723                    tokens[open.item].push(GuillemetToken {
724                        offset: open.offset,
725                        kind: GuillemetTokenKind::Opening,
726                    });
727                    tokens[item_index].push(GuillemetToken {
728                        offset,
729                        kind: GuillemetTokenKind::Closing,
730                    });
731                    for (padding_item, padding_offset) in
732                        next_ascii_padding_locations(items, open.item, open.offset + 2)
733                    {
734                        tokens[padding_item].push(GuillemetToken {
735                            offset: padding_offset,
736                            kind: GuillemetTokenKind::TrimPadding,
737                        });
738                    }
739                    for (padding_item, padding_offset) in
740                        previous_ascii_padding_locations(items, item_index, offset)
741                    {
742                        tokens[padding_item].push(GuillemetToken {
743                            offset: padding_offset,
744                            kind: GuillemetTokenKind::TrimPadding,
745                        });
746                    }
747                }
748                offset += 2;
749            } else {
750                offset += 1;
751            }
752        }
753    }
754
755    for item_tokens in &mut tokens {
756        item_tokens.sort_unstable_by_key(|token| token.offset);
757    }
758    tokens
759}
760
761fn has_guillemet_content(
762    items: &[InlineItem<'_>],
763    opening_item: usize,
764    opening_content: usize,
765    closing_item: usize,
766    closing_offset: usize,
767) -> bool {
768    for (index, item) in items
769        .iter()
770        .enumerate()
771        .take(closing_item + 1)
772        .skip(opening_item)
773    {
774        match item {
775            InlineItem::Text { value, .. } => {
776                let start = if index == opening_item {
777                    opening_content
778                } else {
779                    0
780                };
781                let end = if index == closing_item {
782                    closing_offset
783                } else {
784                    value.len()
785                };
786                if value.get(start..end).is_some_and(|content| {
787                    content.chars().any(|character| !character.is_whitespace())
788                }) {
789                    return true;
790                }
791            }
792            InlineItem::Boundary | InlineItem::SoftBreak => return true,
793            InlineItem::HtmlBoundary => {}
794        }
795    }
796    false
797}
798
799fn is_ascii_padding(byte: Option<u8>) -> bool {
800    matches!(byte, Some(b' ' | b'\t'))
801}
802
803fn next_ascii_padding_locations(
804    items: &[InlineItem<'_>],
805    item_index: usize,
806    offset: usize,
807) -> Vec<(usize, usize)> {
808    let mut locations = Vec::new();
809    let mut index = item_index;
810    let mut cursor = offset;
811    while let Some(item) = items.get(index) {
812        match item {
813            InlineItem::Text {
814                value,
815                protected,
816                escaped,
817                ..
818            } => {
819                while cursor < value.len()
820                    && is_ascii_padding(value.as_bytes().get(cursor).copied())
821                    && !is_blocked(cursor, protected, escaped)
822                {
823                    locations.push((index, cursor));
824                    cursor += 1;
825                }
826                if cursor < value.len() {
827                    return locations;
828                }
829                index += 1;
830                cursor = 0;
831            }
832            InlineItem::Boundary | InlineItem::HtmlBoundary | InlineItem::SoftBreak => {
833                return locations;
834            }
835        }
836    }
837    locations
838}
839
840fn previous_ascii_padding_locations(
841    items: &[InlineItem<'_>],
842    item_index: usize,
843    offset: usize,
844) -> Vec<(usize, usize)> {
845    let mut locations = Vec::new();
846    let mut index = item_index;
847    let mut cursor = offset;
848    loop {
849        match items.get(index) {
850            Some(InlineItem::Text {
851                value,
852                protected,
853                escaped,
854                ..
855            }) => {
856                let mut end = cursor.min(value.len());
857                while let Some((start, character)) = value[..end].char_indices().next_back() {
858                    if !character.is_ascii()
859                        || !is_ascii_padding(Some(character as u8))
860                        || is_blocked(start, protected, escaped)
861                    {
862                        return locations;
863                    }
864                    locations.push((index, start));
865                    end = start;
866                }
867                let Some(previous) = index.checked_sub(1) else {
868                    return locations;
869                };
870                index = previous;
871                cursor = usize::MAX;
872            }
873            Some(InlineItem::Boundary | InlineItem::HtmlBoundary | InlineItem::SoftBreak)
874            | None => {
875                return locations;
876            }
877        }
878    }
879}
880
881fn is_protected_immediately_after(
882    items: &[InlineItem<'_>],
883    item_index: usize,
884    offset: usize,
885) -> bool {
886    if let Some(InlineItem::Text {
887        value,
888        protected,
889        escaped,
890        ..
891    }) = items.get(item_index)
892        && offset < value.len()
893    {
894        return is_blocked(offset, protected, escaped);
895    }
896    let mut index = item_index + 1;
897    while let Some(item) = items.get(index) {
898        match item {
899            InlineItem::Text {
900                value,
901                protected,
902                escaped,
903                ..
904            } => {
905                if value.is_empty() {
906                    index += 1;
907                    continue;
908                }
909                return is_blocked(0, protected, escaped);
910            }
911            InlineItem::Boundary | InlineItem::HtmlBoundary | InlineItem::SoftBreak => {
912                return false;
913            }
914        }
915    }
916    false
917}
918
919fn is_url_protected_near_closer(
920    items: &[InlineItem<'_>],
921    protected_urls_by_item: &[Option<Vec<Range<usize>>>],
922    item_index: usize,
923    offset: usize,
924) -> bool {
925    let mut index = item_index;
926    let mut cursor = offset;
927    loop {
928        match items.get(index) {
929            Some(InlineItem::Text { value, .. }) => {
930                let mut end = cursor.min(value.len());
931                while let Some((start, character)) = value[..end].char_indices().next_back() {
932                    if protected_urls_by_item
933                        .get(index)
934                        .and_then(Option::as_deref)
935                        .unwrap_or_default()
936                        .iter()
937                        .any(|range| range.start <= start && start < range.end)
938                    {
939                        return true;
940                    }
941                    if !(character.is_ascii()
942                        && (character.is_ascii_punctuation()
943                            || is_ascii_padding(Some(character as u8))))
944                    {
945                        return false;
946                    }
947                    end = start;
948                }
949                let Some(previous) = index.checked_sub(1) else {
950                    return false;
951                };
952                index = previous;
953                cursor = usize::MAX;
954            }
955            Some(InlineItem::Boundary | InlineItem::HtmlBoundary | InlineItem::SoftBreak)
956            | None => return false,
957        }
958    }
959}
960
961fn is_exact_doubled_marker(bytes: &[u8], offset: usize, marker: u8) -> bool {
962    bytes.get(offset..offset + 2) == Some(&[marker, marker])
963        && (offset == 0 || bytes[offset - 1] != marker)
964        && bytes.get(offset + 2) != Some(&marker)
965}
966
967fn collect_inline_items<'arena, const GUILLEMET_DIGRAPHS: bool>(
968    nodes: &[Node<'arena>],
969    context: &TransformContext<'arena>,
970    raw_html_spans: &[Span],
971    items: &mut Vec<InlineItem<'arena>>,
972    protected_urls_by_item: &mut Vec<Option<Vec<Range<usize>>>>,
973) {
974    let mut protected_by_node: Vec<Vec<Range<usize>>> =
975        (0..nodes.len()).map(|_| Vec::new()).collect();
976    let mut protected_urls_by_node =
977        GUILLEMET_DIGRAPHS.then(|| (0..nodes.len()).map(|_| Vec::new()).collect::<Vec<_>>());
978
979    for run in text_runs(nodes) {
980        let node_range = run.node_range();
981        let run_span = run.source_span();
982        let is_raw_html = raw_html_spans
983            .iter()
984            .any(|span| run_span.start < span.end && span.start < run_span.end);
985        let protected_urls = GUILLEMET_DIGRAPHS.then(|| run.protected_url_ranges(context));
986        let protected = if is_raw_html {
987            std::iter::once(0..run.value().len()).collect()
988        } else if let Some(protected_urls) = &protected_urls {
989            protected_urls.clone()
990        } else {
991            run.protected_url_ranges(context)
992        };
993        let mut byte_cursor = 0;
994
995        for node_index in node_range.clone() {
996            let Node::Text(text) = &nodes[node_index] else {
997                continue;
998            };
999            let segment_end = byte_cursor + text.value.len();
1000            for range in &protected {
1001                let start = range.start.max(byte_cursor);
1002                let end = range.end.min(segment_end);
1003                if start < end {
1004                    protected_by_node[node_index].push(start - byte_cursor..end - byte_cursor);
1005                }
1006            }
1007            if let (Some(protected_urls), Some(protected_by_node)) =
1008                (&protected_urls, &mut protected_urls_by_node)
1009            {
1010                for range in protected_urls {
1011                    let start = range.start.max(byte_cursor);
1012                    let end = range.end.min(segment_end);
1013                    if start < end {
1014                        protected_by_node[node_index].push(start - byte_cursor..end - byte_cursor);
1015                    }
1016                }
1017            }
1018            byte_cursor = segment_end;
1019        }
1020    }
1021
1022    for (index, node) in nodes.iter().enumerate() {
1023        match node {
1024            Node::Span(node) => {
1025                collect_inline_items::<GUILLEMET_DIGRAPHS>(
1026                    &node.children,
1027                    context,
1028                    raw_html_spans,
1029                    items,
1030                    protected_urls_by_item,
1031                );
1032            }
1033            Node::Text(text) => {
1034                let protected_urls = protected_urls_by_node
1035                    .as_mut()
1036                    .map(|protected| std::mem::take(&mut protected[index]));
1037                items.push(InlineItem::Text {
1038                    value: text.value,
1039                    protected: std::mem::take(&mut protected_by_node[index]),
1040                    escaped: escaped_source_ranges::<GUILLEMET_DIGRAPHS>(
1041                        context.source(),
1042                        text.span,
1043                        text.value,
1044                    ),
1045                });
1046                if GUILLEMET_DIGRAPHS {
1047                    protected_urls_by_item.push(protected_urls);
1048                }
1049            }
1050            Node::Emphasis(node) => {
1051                collect_inline_items::<GUILLEMET_DIGRAPHS>(
1052                    &node.children,
1053                    context,
1054                    raw_html_spans,
1055                    items,
1056                    protected_urls_by_item,
1057                );
1058            }
1059            Node::Strong(node) => {
1060                collect_inline_items::<GUILLEMET_DIGRAPHS>(
1061                    &node.children,
1062                    context,
1063                    raw_html_spans,
1064                    items,
1065                    protected_urls_by_item,
1066                );
1067            }
1068            Node::Highlight(node) => {
1069                collect_inline_items::<GUILLEMET_DIGRAPHS>(
1070                    &node.children,
1071                    context,
1072                    raw_html_spans,
1073                    items,
1074                    protected_urls_by_item,
1075                );
1076            }
1077            Node::Delete(node) => {
1078                collect_inline_items::<GUILLEMET_DIGRAPHS>(
1079                    &node.children,
1080                    context,
1081                    raw_html_spans,
1082                    items,
1083                    protected_urls_by_item,
1084                );
1085            }
1086            Node::Insertion(node) => {
1087                collect_inline_items::<GUILLEMET_DIGRAPHS>(
1088                    &node.children,
1089                    context,
1090                    raw_html_spans,
1091                    items,
1092                    protected_urls_by_item,
1093                );
1094            }
1095            Node::Superscript(node) => {
1096                collect_inline_items::<GUILLEMET_DIGRAPHS>(
1097                    &node.children,
1098                    context,
1099                    raw_html_spans,
1100                    items,
1101                    protected_urls_by_item,
1102                );
1103            }
1104            Node::Subscript(node) => {
1105                collect_inline_items::<GUILLEMET_DIGRAPHS>(
1106                    &node.children,
1107                    context,
1108                    raw_html_spans,
1109                    items,
1110                    protected_urls_by_item,
1111                );
1112            }
1113            Node::MdxJsxTextElement(node) => {
1114                collect_inline_items::<GUILLEMET_DIGRAPHS>(
1115                    &node.children,
1116                    context,
1117                    raw_html_spans,
1118                    items,
1119                    protected_urls_by_item,
1120                );
1121            }
1122            Node::Link(node) if is_url_label(node.url, &node.children) => {
1123                items.push(InlineItem::Boundary);
1124                if GUILLEMET_DIGRAPHS {
1125                    protected_urls_by_item.push(None);
1126                }
1127            }
1128            Node::Link(node) => {
1129                collect_inline_items::<GUILLEMET_DIGRAPHS>(
1130                    &node.children,
1131                    context,
1132                    raw_html_spans,
1133                    items,
1134                    protected_urls_by_item,
1135                );
1136            }
1137            Node::Break(_) => {
1138                items.push(InlineItem::SoftBreak);
1139                if GUILLEMET_DIGRAPHS {
1140                    protected_urls_by_item.push(None);
1141                }
1142            }
1143            Node::Html(_) => {
1144                items.push(InlineItem::HtmlBoundary);
1145                if GUILLEMET_DIGRAPHS {
1146                    protected_urls_by_item.push(None);
1147                }
1148            }
1149            Node::InlineCode(_)
1150            | Node::InlineMath(_)
1151            | Node::Image(_)
1152            | Node::FootnoteReference(_)
1153            | Node::MdxTextExpression(_)
1154            | Node::ThematicBreak(_)
1155            | Node::Paragraph(_)
1156            | Node::Heading(_)
1157            | Node::BlockQuote(_)
1158            | Node::List(_)
1159            | Node::ListItem(_)
1160            | Node::CodeBlock(_)
1161            | Node::MathBlock(_)
1162            | Node::Table(_)
1163            | Node::Figure(_)
1164            | Node::DefinitionList(_)
1165            | Node::DefinitionListTerm(_)
1166            | Node::DefinitionListDefinition(_)
1167            | Node::Definition(_)
1168            | Node::FootnoteDefinition(_)
1169            | Node::MdxJsxFlowElement(_)
1170            | Node::MdxjsEsm(_)
1171            | Node::MdxFlowExpression(_) => {
1172                items.push(InlineItem::Boundary);
1173                if GUILLEMET_DIGRAPHS {
1174                    protected_urls_by_item.push(None);
1175                }
1176            }
1177        }
1178    }
1179}
1180
1181fn apply_inline_replacements<'arena>(
1182    nodes: &mut [Node<'arena>],
1183    replacements: &[Option<String>],
1184    replacement_index: &mut usize,
1185    context: &TransformContext<'arena>,
1186) {
1187    for node in nodes {
1188        match node {
1189            Node::Span(node) => {
1190                apply_inline_replacements(
1191                    &mut node.children,
1192                    replacements,
1193                    replacement_index,
1194                    context,
1195                );
1196            }
1197            Node::Text(text) => {
1198                if let Some(Some(value)) = replacements.get(*replacement_index) {
1199                    context.replace_text_value(text, value);
1200                }
1201                *replacement_index += 1;
1202            }
1203            Node::Emphasis(node) => {
1204                apply_inline_replacements(
1205                    &mut node.children,
1206                    replacements,
1207                    replacement_index,
1208                    context,
1209                );
1210            }
1211            Node::Strong(node) => {
1212                apply_inline_replacements(
1213                    &mut node.children,
1214                    replacements,
1215                    replacement_index,
1216                    context,
1217                );
1218            }
1219            Node::Highlight(node) => {
1220                apply_inline_replacements(
1221                    &mut node.children,
1222                    replacements,
1223                    replacement_index,
1224                    context,
1225                );
1226            }
1227            Node::Delete(node) => {
1228                apply_inline_replacements(
1229                    &mut node.children,
1230                    replacements,
1231                    replacement_index,
1232                    context,
1233                );
1234            }
1235            Node::Insertion(node) => {
1236                apply_inline_replacements(
1237                    &mut node.children,
1238                    replacements,
1239                    replacement_index,
1240                    context,
1241                );
1242            }
1243            Node::Superscript(node) => {
1244                apply_inline_replacements(
1245                    &mut node.children,
1246                    replacements,
1247                    replacement_index,
1248                    context,
1249                );
1250            }
1251            Node::Subscript(node) => {
1252                apply_inline_replacements(
1253                    &mut node.children,
1254                    replacements,
1255                    replacement_index,
1256                    context,
1257                );
1258            }
1259            Node::MdxJsxTextElement(node) => {
1260                apply_inline_replacements(
1261                    &mut node.children,
1262                    replacements,
1263                    replacement_index,
1264                    context,
1265                );
1266            }
1267            Node::Link(node) if is_url_label(node.url, &node.children) => {}
1268            Node::Link(node) => {
1269                apply_inline_replacements(
1270                    &mut node.children,
1271                    replacements,
1272                    replacement_index,
1273                    context,
1274                );
1275            }
1276            Node::ThematicBreak(_)
1277            | Node::Paragraph(_)
1278            | Node::Heading(_)
1279            | Node::BlockQuote(_)
1280            | Node::List(_)
1281            | Node::ListItem(_)
1282            | Node::CodeBlock(_)
1283            | Node::MathBlock(_)
1284            | Node::Html(_)
1285            | Node::InlineCode(_)
1286            | Node::InlineMath(_)
1287            | Node::Break(_)
1288            | Node::Image(_)
1289            | Node::Table(_)
1290            | Node::Figure(_)
1291            | Node::DefinitionList(_)
1292            | Node::DefinitionListTerm(_)
1293            | Node::DefinitionListDefinition(_)
1294            | Node::FootnoteReference(_)
1295            | Node::Definition(_)
1296            | Node::FootnoteDefinition(_)
1297            | Node::MdxJsxFlowElement(_)
1298            | Node::MdxjsEsm(_)
1299            | Node::MdxFlowExpression(_)
1300            | Node::MdxTextExpression(_) => {}
1301        }
1302    }
1303}
1304
1305fn is_url_label(url: &str, children: &[Node<'_>]) -> bool {
1306    matches!(children, [Node::Text(text)] if text.value == url)
1307}
1308
1309fn next_char_after_items(items: &[InlineItem<'_>]) -> Vec<Option<char>> {
1310    let mut next_after = vec![None; items.len()];
1311    let mut next = None;
1312
1313    for index in (0..items.len()).rev() {
1314        next_after[index] = next;
1315        match &items[index] {
1316            InlineItem::Text {
1317                value,
1318                protected,
1319                escaped,
1320                ..
1321            } => match first_context_char(value, protected, escaped) {
1322                ContextChar::Char(character) => next = Some(character),
1323                ContextChar::Empty => {}
1324                ContextChar::Boundary => next = None,
1325            },
1326            InlineItem::Boundary | InlineItem::HtmlBoundary => next = None,
1327            InlineItem::SoftBreak => next = Some(' '),
1328        }
1329    }
1330
1331    next_after
1332}
1333
1334enum ContextChar {
1335    Char(char),
1336    Empty,
1337    Boundary,
1338}
1339
1340fn first_context_char(
1341    value: &str,
1342    protected: &[Range<usize>],
1343    escaped: &[Range<usize>],
1344) -> ContextChar {
1345    if value.is_empty() {
1346        return ContextChar::Empty;
1347    }
1348    if is_blocked(0, protected, escaped) {
1349        return ContextChar::Boundary;
1350    }
1351    match value.chars().next() {
1352        Some(character) => ContextChar::Char(character),
1353        None => ContextChar::Empty,
1354    }
1355}
1356
1357#[allow(clippy::too_many_arguments)]
1358fn transform_text<const GUILLEMET_DIGRAPHS: bool>(
1359    value: &str,
1360    protected: &[Range<usize>],
1361    escaped: &[Range<usize>],
1362    next_after: Option<char>,
1363    items: &[InlineItem<'_>],
1364    item_index: usize,
1365    state: &mut QuoteState,
1366    options: TypographyOptions,
1367    guillemet_tokens: &[GuillemetToken],
1368) -> Option<String> {
1369    let mut result = String::with_capacity(value.len());
1370    let mut offset = 0;
1371    let mut protected_index = 0;
1372    let mut escaped_index = 0;
1373    let mut guillemet_index = 0;
1374    let mut changed = false;
1375
1376    while offset < value.len() {
1377        advance_range_index(protected, &mut protected_index, offset);
1378        advance_range_index(escaped, &mut escaped_index, offset);
1379        if GUILLEMET_DIGRAPHS {
1380            while guillemet_tokens
1381                .get(guillemet_index)
1382                .is_some_and(|token| token.offset < offset)
1383            {
1384                guillemet_index += 1;
1385            }
1386        }
1387
1388        if let Some(range) = protected.get(protected_index)
1389            && range.start <= offset
1390            && offset < range.end
1391        {
1392            let end = range.end.min(value.len());
1393            state.reset();
1394            result.push_str(&value[offset..end]);
1395            offset = end;
1396            state.reset();
1397            continue;
1398        }
1399
1400        let Some(character) = value[offset..].chars().next() else {
1401            break;
1402        };
1403        let character_end = offset + character.len_utf8();
1404
1405        if let Some(range) = escaped.get(escaped_index)
1406            && range.start <= offset
1407            && offset < range.end
1408        {
1409            result.push_str(&value[offset..character_end]);
1410            state.observe(&value[offset..character_end]);
1411            offset = character_end;
1412            continue;
1413        }
1414
1415        if GUILLEMET_DIGRAPHS
1416            && let Some(token) = guillemet_tokens.get(guillemet_index)
1417            && token.offset == offset
1418        {
1419            match token.kind {
1420                GuillemetTokenKind::TrimPadding => {
1421                    if is_ascii_padding(value.as_bytes().get(offset).copied()) {
1422                        offset += 1;
1423                        changed = true;
1424                    }
1425                    guillemet_index += 1;
1426                    continue;
1427                }
1428                GuillemetTokenKind::Opening | GuillemetTokenKind::Closing => {
1429                    let replacement = if matches!(token.kind, GuillemetTokenKind::Opening) {
1430                        let primary = state.stack.last().is_none_or(|frame| !frame.primary);
1431                        state.stack.push(QuoteFrame {
1432                            source: '<',
1433                            primary,
1434                        });
1435                        quote_marks(options.language(), primary).open
1436                    } else {
1437                        let frame = state
1438                            .stack
1439                            .iter()
1440                            .rposition(|frame| frame.source == '<')
1441                            .map(|index| state.stack.remove(index));
1442                        quote_marks(options.language(), frame.is_none_or(|frame| frame.primary))
1443                            .close
1444                    };
1445                    result.push_str(replacement);
1446                    state.observe(replacement);
1447                    offset += 2;
1448                    guillemet_index += 1;
1449                    changed = true;
1450                    continue;
1451                }
1452            }
1453        }
1454
1455        let next = next_context_char(
1456            value,
1457            character_end,
1458            protected,
1459            escaped,
1460            items,
1461            item_index,
1462            next_after,
1463        );
1464
1465        if options.ellipses()
1466            && value[offset..].starts_with("...")
1467            && !is_blocked(offset + 1, protected, escaped)
1468            && !is_blocked(offset + 2, protected, escaped)
1469        {
1470            result.push('…');
1471            state.observe("…");
1472            offset += 3;
1473            changed = true;
1474            continue;
1475        }
1476
1477        if options.dashes()
1478            && options.language().converts_double_hyphens()
1479            && value[offset..].starts_with("---")
1480            && dash_boundary(
1481                state.previous,
1482                next_context_char(
1483                    value,
1484                    offset + 3,
1485                    protected,
1486                    escaped,
1487                    items,
1488                    item_index,
1489                    next_after,
1490                ),
1491            )
1492        {
1493            result.push('—');
1494            state.observe("—");
1495            offset += 3;
1496            changed = true;
1497            continue;
1498        }
1499
1500        if options.dashes()
1501            && options.language().converts_double_hyphens()
1502            && value[offset..].starts_with("--")
1503            && dash_boundary(
1504                state.previous,
1505                next_context_char(
1506                    value,
1507                    offset + 2,
1508                    protected,
1509                    escaped,
1510                    items,
1511                    item_index,
1512                    next_after,
1513                ),
1514            )
1515        {
1516            result.push('—');
1517            state.observe("—");
1518            offset += 2;
1519            changed = true;
1520            continue;
1521        }
1522
1523        if character == ' '
1524            && options.language() == TypographyLanguage::French
1525            && is_french_punctuation(next)
1526        {
1527            result.push('\u{00a0}');
1528            state.observe("\u{00a0}");
1529            offset = character_end;
1530            changed = true;
1531            continue;
1532        }
1533
1534        if character == ' '
1535            && options.language().converts_measurement_spaces()
1536            && state
1537                .previous
1538                .is_some_and(|previous| previous.is_ascii_digit())
1539            && has_unit_after(value, character_end, protected, escaped)
1540        {
1541            result.push('\u{00a0}');
1542            state.observe("\u{00a0}");
1543            offset = character_end;
1544            changed = true;
1545            continue;
1546        }
1547
1548        let following_dash_length = dash_sequence_length(&value[character_end..]);
1549        if character == ' '
1550            && options.dashes()
1551            && options.language().converts_double_hyphens()
1552            && state
1553                .previous
1554                .is_some_and(|previous| !previous.is_whitespace())
1555            && following_dash_length > 0
1556            && next_context_char(
1557                value,
1558                character_end + following_dash_length,
1559                protected,
1560                escaped,
1561                items,
1562                item_index,
1563                next_after,
1564            )
1565            .is_none_or(|character| character.is_whitespace() || is_closing_punctuation(character))
1566        {
1567            result.push('\u{00a0}');
1568            state.observe("\u{00a0}");
1569            offset = character_end;
1570            changed = true;
1571            continue;
1572        }
1573
1574        if character == '\''
1575            && state.previous.is_some_and(char::is_alphanumeric)
1576            && next.is_some_and(char::is_alphanumeric)
1577        {
1578            if options.language().converts_apostrophes() {
1579                result.push('’');
1580                state.observe("’");
1581                changed = true;
1582            } else {
1583                result.push(character);
1584                state.observe(&value[offset..character_end]);
1585            }
1586            offset = character_end;
1587            continue;
1588        }
1589
1590        if (character == '"' || character == '\'')
1591            && let Some(replacement) = state.replace_quote(character, next, options.language())
1592        {
1593            result.push_str(replacement);
1594            state.observe(replacement);
1595            offset = character_end;
1596            changed = true;
1597            continue;
1598        }
1599
1600        state.observe_authored_quote(character, next, options.language());
1601        result.push(character);
1602        state.observe(&value[offset..character_end]);
1603        offset = character_end;
1604    }
1605
1606    changed.then_some(result)
1607}
1608
1609fn advance_range_index(ranges: &[Range<usize>], index: &mut usize, offset: usize) {
1610    while ranges.get(*index).is_some_and(|range| range.end <= offset) {
1611        *index += 1;
1612    }
1613}
1614
1615fn is_blocked(offset: usize, protected: &[Range<usize>], escaped: &[Range<usize>]) -> bool {
1616    protected
1617        .iter()
1618        .chain(escaped)
1619        .any(|range| range.start <= offset && offset < range.end)
1620}
1621
1622fn next_context_char(
1623    value: &str,
1624    offset: usize,
1625    protected: &[Range<usize>],
1626    escaped: &[Range<usize>],
1627    items: &[InlineItem<'_>],
1628    item_index: usize,
1629    next_after: Option<char>,
1630) -> Option<char> {
1631    if offset < value.len() {
1632        if is_blocked(offset, protected, escaped) {
1633            return None;
1634        }
1635        return value[offset..].chars().next();
1636    }
1637
1638    let mut index = item_index + 1;
1639    while let Some(item) = items.get(index) {
1640        match item {
1641            InlineItem::Text {
1642                value,
1643                protected,
1644                escaped,
1645                ..
1646            } => match first_context_char(value, protected, escaped) {
1647                ContextChar::Char(character) => return Some(character),
1648                ContextChar::Empty => index += 1,
1649                ContextChar::Boundary => return None,
1650            },
1651            InlineItem::Boundary | InlineItem::HtmlBoundary => return None,
1652            InlineItem::SoftBreak => return Some(' '),
1653        }
1654    }
1655    next_after
1656}
1657
1658fn can_open_quote(previous: Option<char>, next: Option<char>) -> bool {
1659    next.is_some_and(|character| !character.is_whitespace())
1660        && previous
1661            .is_none_or(|character| character.is_whitespace() || is_opening_punctuation(character))
1662}
1663
1664fn can_close_quote(previous: char) -> bool {
1665    previous.is_alphanumeric() || is_closing_punctuation(previous)
1666}
1667
1668fn is_opening_punctuation(character: char) -> bool {
1669    matches!(
1670        character,
1671        '(' | '[' | '{' | '<' | '«' | '„' | '‚' | '“' | '‘'
1672    )
1673}
1674
1675fn is_closing_punctuation(character: char) -> bool {
1676    matches!(
1677        character,
1678        ')' | ']' | '}' | '>' | ',' | '.' | '!' | '?' | ';' | ':' | '»' | '”' | '’' | '“' | '‘'
1679    )
1680}
1681
1682fn dash_boundary(previous: Option<char>, next: Option<char>) -> bool {
1683    previous.is_none_or(|character| character.is_whitespace() || is_opening_punctuation(character))
1684        && next
1685            .is_none_or(|character| character.is_whitespace() || is_closing_punctuation(character))
1686}
1687
1688fn dash_sequence_length(value: &str) -> usize {
1689    if value.starts_with("---") {
1690        3
1691    } else if value.starts_with("--") {
1692        2
1693    } else {
1694        0
1695    }
1696}
1697
1698fn quote_marks(language: TypographyLanguage, primary: bool) -> QuoteMarks {
1699    match (language, primary) {
1700        (TypographyLanguage::English, true) => QuoteMarks {
1701            open: "“",
1702            close: "”",
1703        },
1704        (TypographyLanguage::English, false) => QuoteMarks {
1705            open: "‘",
1706            close: "’",
1707        },
1708        (TypographyLanguage::Spanish, true) => QuoteMarks {
1709            open: "«",
1710            close: "»",
1711        },
1712        (TypographyLanguage::Spanish, false) => QuoteMarks {
1713            open: "“",
1714            close: "”",
1715        },
1716        (TypographyLanguage::French, true) => QuoteMarks {
1717            open: "«\u{202f}",
1718            close: "\u{202f}»",
1719        },
1720        (TypographyLanguage::French, false) => QuoteMarks {
1721            open: "“",
1722            close: "”",
1723        },
1724        (TypographyLanguage::Portuguese, true) => QuoteMarks {
1725            open: "«",
1726            close: "»",
1727        },
1728        (TypographyLanguage::Portuguese, false) => QuoteMarks {
1729            open: "“",
1730            close: "”",
1731        },
1732        (TypographyLanguage::German, true) => QuoteMarks {
1733            open: "„",
1734            close: "“",
1735        },
1736        (TypographyLanguage::German, false) => QuoteMarks {
1737            open: "‚",
1738            close: "‘",
1739        },
1740        (TypographyLanguage::Italian, true) => QuoteMarks {
1741            open: "«",
1742            close: "»",
1743        },
1744        (TypographyLanguage::Italian, false) => QuoteMarks {
1745            open: "“",
1746            close: "”",
1747        },
1748        (TypographyLanguage::Dutch, true) => QuoteMarks {
1749            open: "‘",
1750            close: "’",
1751        },
1752        (TypographyLanguage::Dutch, false) => QuoteMarks {
1753            open: "“",
1754            close: "”",
1755        },
1756        (TypographyLanguage::Polish, true) => QuoteMarks {
1757            open: "„",
1758            close: "”",
1759        },
1760        (TypographyLanguage::Polish, false) => QuoteMarks {
1761            open: "‚",
1762            close: "‘",
1763        },
1764        (TypographyLanguage::Russian, true) => QuoteMarks {
1765            open: "«",
1766            close: "»",
1767        },
1768        (TypographyLanguage::Russian, false) => QuoteMarks {
1769            open: "„",
1770            close: "“",
1771        },
1772        (TypographyLanguage::Ukrainian, true) => QuoteMarks {
1773            open: "«",
1774            close: "»",
1775        },
1776        (TypographyLanguage::Ukrainian, false) => QuoteMarks {
1777            open: "„",
1778            close: "“",
1779        },
1780        (TypographyLanguage::Czech, true) => QuoteMarks {
1781            open: "„",
1782            close: "“",
1783        },
1784        (TypographyLanguage::Czech, false) => QuoteMarks {
1785            open: "‚",
1786            close: "‘",
1787        },
1788        (TypographyLanguage::Danish, true) => QuoteMarks {
1789            open: "»",
1790            close: "«",
1791        },
1792        (TypographyLanguage::Danish, false) => QuoteMarks {
1793            open: "„",
1794            close: "“",
1795        },
1796        (TypographyLanguage::Finnish, true) => QuoteMarks {
1797            open: "”",
1798            close: "”",
1799        },
1800        (TypographyLanguage::Finnish, false) => QuoteMarks {
1801            open: "’",
1802            close: "’",
1803        },
1804        (TypographyLanguage::NorwegianBokmal, true) => QuoteMarks {
1805            open: "«",
1806            close: "»",
1807        },
1808        (TypographyLanguage::NorwegianBokmal, false) => QuoteMarks {
1809            open: "‘",
1810            close: "’",
1811        },
1812        (TypographyLanguage::Swedish, true) => QuoteMarks {
1813            open: "”",
1814            close: "”",
1815        },
1816        (TypographyLanguage::Swedish, false) => QuoteMarks {
1817            open: "’",
1818            close: "’",
1819        },
1820    }
1821}
1822
1823fn symmetric_quote_level(character: char, language: TypographyLanguage) -> Option<bool> {
1824    for primary in [true, false] {
1825        let marks = quote_marks(language, primary);
1826        if marks.open == marks.close && marks.open.starts_with(character) {
1827            return Some(primary);
1828        }
1829    }
1830    None
1831}
1832
1833fn authored_quote_event(character: char, language: TypographyLanguage) -> Option<(bool, bool)> {
1834    let primary = quote_marks(language, true);
1835    let secondary = quote_marks(language, false);
1836    let primary_open = primary.open.chars().next()?;
1837    let primary_close = primary.close.chars().last()?;
1838    let secondary_open = secondary.open.chars().next()?;
1839    let secondary_close = secondary.close.chars().last()?;
1840
1841    if character == primary_open {
1842        Some((true, true))
1843    } else if character == primary_close {
1844        Some((false, true))
1845    } else if character == secondary_open {
1846        Some((true, false))
1847    } else if character == secondary_close {
1848        Some((false, false))
1849    } else {
1850        None
1851    }
1852}
1853
1854fn is_french_punctuation(character: Option<char>) -> bool {
1855    matches!(character, Some(';' | ':' | '!' | '?'))
1856}
1857
1858fn has_unit_after(
1859    value: &str,
1860    offset: usize,
1861    protected: &[Range<usize>],
1862    escaped: &[Range<usize>],
1863) -> bool {
1864    const UNITS: &[&str] = &["km/h", "°C", "km", "cm", "mm", "kg", "mg", "m", "g", "%"];
1865    if is_blocked(offset, protected, escaped) {
1866        return false;
1867    }
1868    UNITS.iter().any(|unit| {
1869        let Some(end) = offset.checked_add(unit.len()) else {
1870            return false;
1871        };
1872        if value
1873            .get(offset..end)
1874            .is_none_or(|candidate| candidate != *unit)
1875            || protected
1876                .iter()
1877                .chain(escaped)
1878                .any(|range| range.start < end && range.end > offset)
1879        {
1880            return false;
1881        }
1882        value[end..]
1883            .chars()
1884            .next()
1885            .is_none_or(|character| !character.is_alphanumeric())
1886    })
1887}
1888
1889fn escaped_source_ranges<const GUILLEMET_DIGRAPHS: bool>(
1890    source: &str,
1891    span: Span,
1892    value: &str,
1893) -> Vec<Range<usize>> {
1894    let Some(raw) = source.get(span.start as usize..span.end as usize) else {
1895        return Vec::new();
1896    };
1897    let mut raw_quotes = Vec::new();
1898    let mut raw_offset = 0;
1899
1900    while raw_offset < raw.len() {
1901        if raw[raw_offset..].starts_with('\\')
1902            && let Some((character, width)) = next_char_at(raw, raw_offset + 1)
1903            && (matches!(character, '\'' | '"')
1904                || (GUILLEMET_DIGRAPHS && matches!(character, '<' | '>')))
1905            && character.is_ascii_punctuation()
1906        {
1907            raw_quotes.push((character, true));
1908            raw_offset += 1 + width;
1909            continue;
1910        }
1911
1912        if raw[raw_offset..].starts_with('&')
1913            && let Some((character, end)) = entity_at(raw, raw_offset)
1914            && (matches!(character, '\'' | '"')
1915                || (GUILLEMET_DIGRAPHS && matches!(character, '<' | '>')))
1916        {
1917            raw_quotes.push((character, true));
1918            raw_offset = end;
1919            continue;
1920        }
1921
1922        if let Some((character, width)) = next_char_at(raw, raw_offset) {
1923            if matches!(character, '\'' | '"')
1924                || (GUILLEMET_DIGRAPHS && matches!(character, '<' | '>'))
1925            {
1926                raw_quotes.push((character, false));
1927            }
1928            raw_offset += width;
1929        } else {
1930            break;
1931        }
1932    }
1933
1934    let text_quotes: Vec<_> = value
1935        .char_indices()
1936        .filter(|(_, character)| {
1937            matches!(character, '\'' | '"')
1938                || (GUILLEMET_DIGRAPHS && matches!(character, '<' | '>'))
1939        })
1940        .collect();
1941
1942    if raw_quotes.len() != text_quotes.len()
1943        || raw_quotes
1944            .iter()
1945            .zip(&text_quotes)
1946            .any(|((raw_character, _), (_, text_character))| raw_character != text_character)
1947    {
1948        return Vec::new();
1949    }
1950
1951    raw_quotes
1952        .iter()
1953        .zip(text_quotes)
1954        .filter_map(|((_, escaped), (offset, character))| {
1955            escaped.then_some(offset..offset + character.len_utf8())
1956        })
1957        .collect()
1958}
1959
1960fn next_char_at(value: &str, offset: usize) -> Option<(char, usize)> {
1961    let character = value.get(offset..)?.chars().next()?;
1962    Some((character, character.len_utf8()))
1963}
1964
1965fn entity_at(value: &str, start: usize) -> Option<(char, usize)> {
1966    let semicolon = value[start..].find(';')? + start;
1967    let name = &value[start + 1..semicolon];
1968    let character = if let Some(numeric) = name.strip_prefix('#') {
1969        if let Some(hexadecimal) = numeric
1970            .strip_prefix('x')
1971            .or_else(|| numeric.strip_prefix('X'))
1972        {
1973            u32::from_str_radix(hexadecimal, 16)
1974                .ok()
1975                .and_then(char::from_u32)?
1976        } else {
1977            numeric.parse::<u32>().ok().and_then(char::from_u32)?
1978        }
1979    } else {
1980        match name {
1981            "quot" => '"',
1982            "apos" => '\'',
1983            "lt" | "LT" => '<',
1984            "gt" | "GT" => '>',
1985            _ => return None,
1986        }
1987    };
1988    Some((character, semicolon + 1))
1989}
1990
1991#[cfg(test)]
1992mod tests {
1993    use super::{TypographyLanguage, TypographyOptions, UnsupportedTypographyLanguage};
1994
1995    #[test]
1996    fn parses_only_the_fifteen_reviewed_language_codes() {
1997        for language in [
1998            "cs", "da", "de", "en", "es", "fi", "fr", "it", "nb", "nl", "pl", "pt", "ru", "sv",
1999            "uk",
2000        ] {
2001            assert_eq!(
2002                language
2003                    .parse::<TypographyLanguage>()
2004                    .map(TypographyLanguage::as_str),
2005                Ok(language)
2006            );
2007        }
2008        assert_eq!(
2009            "en-US".parse::<TypographyLanguage>(),
2010            Err(UnsupportedTypographyLanguage {
2011                language: "en-US".to_owned(),
2012            })
2013        );
2014    }
2015
2016    #[test]
2017    fn dash_and_ellipsis_toggles_are_independent() {
2018        let options = TypographyOptions::new(TypographyLanguage::English)
2019            .with_dashes(false)
2020            .with_ellipses(true);
2021        assert!(!options.dashes());
2022        assert!(options.ellipses());
2023    }
2024}