citum-engine 0.79.0

Citum citation and bibliography processor
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*
SPDX-License-Identifier: MIT OR Apache-2.0
SPDX-FileCopyrightText: © 2023-2026 Bruce D'Arcus and Citum contributors
*/

use super::format::QuoteMarks;
use citum_schema::options::{Config, bibliography::BibliographyConfig, titles::TitleRendering};
use citum_schema::template::{Rendering, TemplateComponent, TitleType};
use std::sync::Arc;

/// A processed template component with its rendered value.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ProcTemplateComponent {
    /// The original template component (for rendering instructions).
    pub template_component: TemplateComponent,
    /// The 0-based source index in the active layout template, when requested.
    pub template_index: Option<usize>,
    /// The processed values.
    pub value: String,
    /// Optional prefix from value extraction.
    pub prefix: Option<String>,
    /// Optional suffix from value extraction.
    pub suffix: Option<String>,
    /// Optional URL for hyperlinking.
    pub url: Option<String>,
    /// Reference type for type-specific overrides.
    pub ref_type: Option<String>,
    /// Optional global configuration.
    pub config: Option<Arc<Config>>,
    /// Optional bibliography-only configuration.
    pub bibliography_config: Option<Arc<BibliographyConfig>>,
    /// Effective language for this rendered component.
    pub item_language: Option<String>,
    /// Locale-resolved quote mark characters, threaded from the active [`Locale`]'s
    /// [`GrammarOptions`](citum_schema::locale::GrammarOptions) so `quote`/`wrap: quotes`
    /// render the style's actual quotation convention instead of a hardcoded default.
    ///
    /// [`Locale`]: citum_schema::locale::Locale
    pub quote_marks: QuoteMarks,
    /// Whether this component begins a sentence according to processor-owned render context.
    pub sentence_initial: bool,
    /// Whether the value is already pre-formatted (e.g. from a List or substitution).
    pub pre_formatted: bool,
    /// True when this component's rendered text is *only* a bibliography
    /// numeric label (`update_label_mode`'s synthetic `[label, following]`
    /// group with an empty `following`, e.g. no author). Bibliography
    /// assembly must still write the label text, but must not treat it as
    /// real preceding content for the *next* component's separator decision
    /// — the label attaches directly to whatever content actually opens the
    /// entry, exactly as if the label were not there.
    pub label_only: bool,
}

/// A processed template (list of rendered components).
pub type ProcTemplate = Vec<ProcTemplateComponent>;

/// A processed bibliography entry.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ProcEntry {
    /// The reference ID.
    pub id: String,
    /// The processed template components.
    pub template: ProcTemplate,
    /// Metadata for interactivity (tooltips, etc.)
    pub metadata: super::format::ProcEntryMetadata,
}

use super::format::{OutputFormat, SemanticAttribute};
use super::plain::PlainText;
use std::borrow::Cow;

/// Resolve the semantic CSS class for a rendered component based on its template type.
fn resolve_semantic_class(component: &ProcTemplateComponent) -> Option<String> {
    use citum_schema::template::{DateVariable, SimpleVariable};
    match &component.template_component {
        TemplateComponent::Title(t) => match t.title {
            TitleType::Primary => Some("citum-title".to_string()),
            TitleType::ContainerTitle
            | TitleType::ParentMonograph
            | TitleType::ParentSerial
            | TitleType::CollectionTitle => Some("citum-container-title".to_string()),
            _ => Some("citum-title".to_string()),
        },
        TemplateComponent::Contributor(c) => Some(format!(
            "citum-{}",
            c.contributor
                .as_slice()
                .iter()
                .map(citum_schema::template::ContributorRole::as_str)
                .collect::<Vec<_>>()
                .join("-")
        )),
        TemplateComponent::Date(d) => Some(format!(
            "citum-{}",
            match d.date {
                DateVariable::Issued => "issued",
                DateVariable::Accessed => "accessed",
                DateVariable::OriginalPublished => "original-published",
                DateVariable::Submitted => "submitted",
                DateVariable::EventDate => "event-date",
                DateVariable::Copyright => "copyright",
                DateVariable::Printing => "printing",
            }
        )),
        TemplateComponent::Number(n) => Some(format!("citum-{}", n.number.as_key())),
        TemplateComponent::Identifier(identifier) => Some(format!(
            "citum-identifier-{}",
            identifier.identifier.as_str()
        )),
        TemplateComponent::Variable(v) => Some(format!(
            "citum-{}",
            match v.variable {
                SimpleVariable::Doi => "doi",
                SimpleVariable::Url => "url",
                SimpleVariable::Isbn => "isbn",
                SimpleVariable::Issn => "issn",
                SimpleVariable::Pmid => "pmid",
                SimpleVariable::Note => "note",
                SimpleVariable::Publisher => "publisher",
                SimpleVariable::PublisherPlace => "publisher-place",
                SimpleVariable::ContainerTitleShort => "container-title-short",
                SimpleVariable::Archive => "archive",
                _ => "variable",
            }
        )),
        TemplateComponent::Message(m) => Some(format!(
            "citum-message-{}",
            m.message
                .chars()
                .map(|ch| if ch.is_ascii_alphanumeric() { ch } else { '-' })
                .collect::<String>()
                .trim_matches('-')
        )),
        _ => None,
    }
}

/// Render a single component to string using the default `PlainText` format.
#[must_use]
pub fn render_component(component: &ProcTemplateComponent) -> String {
    PlainText.finish(render_component_with_format::<PlainText>(component))
}

/// Render a single component using a specific output format.
#[must_use]
pub fn render_component_with_format<F: OutputFormat<Output = String>>(
    component: &ProcTemplateComponent,
) -> F::Output {
    render_component_with_format_and_renderer::<F>(component, &F::default(), true)
}

fn realized_component_affixes<'a>(
    rendering: &'a Rendering,
    script: crate::values::ScriptClass,
    realization: Option<&'a citum_schema::options::PunctuationRealization>,
) -> (Cow<'a, str>, Cow<'a, str>) {
    let realize = |punctuation: &'a citum_schema::template::DelimiterPunctuation, position| {
        super::format::realize_punctuation(punctuation, script, realization, position)
    };
    let prefix = rendering
        .prefix
        .as_ref()
        .map(|punctuation| realize(punctuation, super::format::PunctuationPosition::Prefix))
        .unwrap_or(Cow::Borrowed(""));
    let suffix = rendering
        .suffix
        .as_ref()
        .map(|punctuation| realize(punctuation, super::format::PunctuationPosition::Suffix))
        .unwrap_or(Cow::Borrowed(""));
    (prefix, suffix)
}

fn apply_component_semantics<F>(
    component: &ProcTemplateComponent,
    fmt: &F,
    show_semantics: bool,
    output: F::Output,
) -> F::Output
where
    F: OutputFormat<Output = String>,
{
    if !show_semantics {
        return output;
    }
    let Some(class) = resolve_semantic_class(component) else {
        return output;
    };
    let semantic_attributes = component
        .template_index
        .map(|index| {
            vec![SemanticAttribute {
                name: "data-index",
                value: index.to_string(),
            }]
        })
        .unwrap_or_default();
    fmt.semantic_with_attributes(&class, output, &semantic_attributes)
}

/// Render a single component using a specific output format and an existing renderer instance.
pub fn render_component_with_format_and_renderer<F: OutputFormat<Output = String>>(
    component: &ProcTemplateComponent,
    fmt: &F,
    show_semantics: bool,
) -> F::Output {
    // Get merged rendering (global config + local settings + overrides)
    let rendering = get_effective_rendering(component);

    // Check if suppressed
    if rendering.suppress == Some(true) {
        return fmt.text("");
    }

    let multilingual = component
        .config
        .as_ref()
        .and_then(|config| config.multilingual.as_ref());
    let (script, realization) = crate::values::punctuation_realization_context(
        component.item_language.as_deref(),
        multilingual,
        component.quote_marks.punctuation_realization.as_ref(),
    );
    let (prefix, suffix) = realized_component_affixes(&rendering, script, realization.as_deref());
    let inner_prefix = rendering
        .wrap
        .as_ref()
        .and_then(|w| w.inner_prefix.as_deref())
        .unwrap_or_default();
    let inner_suffix = rendering
        .wrap
        .as_ref()
        .and_then(|w| w.inner_suffix.as_deref())
        .unwrap_or_default();

    let mut output = if component.pre_formatted {
        // If already pre-formatted (e.g. from a List), don't escape again.
        // We just need to convert the String back to Output (which is String here).
        fmt.join(vec![component.value.clone()], "")
    } else {
        fmt.text(&component.value)
    };

    // Apply styles, links, inner affixes, wrap, outer affixes, then semantics.
    if rendering.emph == Some(true) {
        output = fmt.emph(output);
    }
    if rendering.strong == Some(true) {
        output = fmt.strong(output);
    }
    if rendering.small_caps == Some(true) {
        output = fmt.small_caps(output);
    }
    if rendering.vertical_align == Some(citum_schema::VerticalAlign::Superscript) {
        output = fmt.superscript(output);
    }
    // A `wrap: quotes` (applied below) already surrounds the value in quotation
    // marks; honoring the `quote` flag as well would double them (`““Title””`).
    // Only apply the flag when the wrap is not itself a quote wrap.
    let wrapped_in_quotes = rendering
        .wrap
        .as_ref()
        .is_some_and(|w| w.punctuation == citum_schema::template::WrapPunctuation::Quotes);
    if rendering.quote == Some(true) && !wrapped_in_quotes {
        output = fmt.quote(output, &component.quote_marks);
    }

    if let Some(url) = &component.url {
        output = fmt.link(url, output);
    }

    let total_inner_prefix = format!(
        "{}{}",
        inner_prefix,
        component.prefix.as_deref().unwrap_or_default()
    );
    let total_inner_suffix = format!(
        "{}{}",
        component.suffix.as_deref().unwrap_or_default(),
        inner_suffix
    );

    if !total_inner_prefix.is_empty() || !total_inner_suffix.is_empty() {
        output = fmt.inner_affix(&total_inner_prefix, output, &total_inner_suffix);
    }

    if let Some(wrap_config) = rendering.wrap.as_ref() {
        output = fmt.wrap_punctuation(
            &wrap_config.punctuation,
            output,
            &component.quote_marks,
            script,
            realization.as_deref(),
        );
    }

    if !prefix.is_empty() || !suffix.is_empty() {
        output = super::format::apply_punctuation_affixes(
            fmt,
            rendering
                .prefix
                .as_ref()
                .map(|punctuation| (punctuation, prefix.as_ref())),
            output,
            rendering
                .suffix
                .as_ref()
                .map(|punctuation| (punctuation, suffix.as_ref())),
        );
    }

    output = apply_component_semantics(component, fmt, show_semantics, output);

    // 7. Legacy literal-punctuation compatibility shim. Semantic punctuation
    // realizes before this point; this late remap remains only for external
    // bilingual styles authored with the original `punctuation: latin` option.
    if wants_latin_punctuation(component) {
        output = remap_to_latin_punctuation(output);
    }

    output
}

/// Whether this component opts into the legacy literal-punctuation remap.
///
/// New styles express punctuation with semantic marks. This fixed compatibility
/// shim remains crate-wide because external literal-authored styles may place
/// punctuation at component, citation-section, or citation-spec boundaries.
pub(crate) fn wants_latin_punctuation(component: &ProcTemplateComponent) -> bool {
    let configured = component
        .config
        .as_ref()
        .and_then(|cfg| cfg.multilingual.as_ref())
        .and_then(|ml| ml.scripts.get("latin"))
        .is_some_and(|script| {
            script.punctuation == Some(citum_schema::options::PunctuationStyle::Latin)
        });

    configured && crate::values::is_latin_script_language(component.item_language.as_deref())
}

/// Apply the legacy fixed CJK-to-Latin literal-punctuation remap.
///
/// `:`(U+FF1A) → `: `, `,`(U+FF0C) → `, `, `(`(U+FF08) → `(`, `)`(U+FF09) → `)`,
/// then any resulting doubled space is collapsed. Do not extend this table;
/// new punctuation behavior belongs in semantic realization.
pub(crate) fn remap_to_latin_punctuation(text: String) -> String {
    if !text.contains(['', '', '', '']) {
        return text;
    }

    let mut mapped = String::with_capacity(text.len());
    for ch in text.chars() {
        match ch {
            '' => mapped.push_str(": "),
            '' => mapped.push_str(", "),
            '' => mapped.push('('),
            '' => mapped.push(')'),
            _ => mapped.push(ch),
        }
    }

    while mapped.contains("  ") {
        mapped = mapped.replace("  ", " ");
    }
    mapped
}

/// Get effective rendering, applying global config, then local template settings, then type-specific overrides.
#[must_use]
pub fn get_effective_rendering(component: &ProcTemplateComponent) -> Rendering {
    let mut effective = Rendering::default();

    // 1. Layer global config
    if let Some(config) = &component.config {
        match &component.template_component {
            TemplateComponent::Title(t) => {
                if let Some(global_title) = get_title_category_rendering(
                    &t.title,
                    component.ref_type.as_deref(),
                    component.item_language.as_deref(),
                    config,
                ) {
                    effective.merge(&global_title);
                }
            }
            TemplateComponent::Contributor(c) => {
                if let Some(contributors_config) = &config.contributors
                    && let Some(role_config) = &contributors_config.role
                    && let Some(primary_role) = c.contributor.as_slice().first()
                    && let Some(role_rendering) = role_config.role_rendering(primary_role)
                {
                    effective.merge(&role_rendering.to_rendering());
                }
            }
            // Add other component types here as we expand Config
            _ => {}
        }
    }

    // 2. Layer local template rendering
    effective.merge(component.template_component.rendering());

    effective
}

/// Resolve title-category-specific rendering overrides for a title component.
///
/// The returned rendering reflects title type, mapped reference category, and
/// optional language-specific overrides from the style configuration.
#[must_use]
pub fn get_title_category_rendering(
    title_type: &TitleType,
    ref_type: Option<&str>,
    language: Option<&str>,
    config: &Config,
) -> Option<Rendering> {
    get_title_category_title_rendering(title_type, ref_type, language, config)
        .map(|rendering| rendering.to_rendering())
}

/// Resolve title-category-specific title rendering options for a title component.
///
/// The returned rendering reflects title type, mapped reference category, and
/// optional language-specific overrides from the style configuration.
#[must_use]
pub fn get_title_category_title_rendering(
    title_type: &TitleType,
    ref_type: Option<&str>,
    language: Option<&str>,
    config: &Config,
) -> Option<TitleRendering> {
    let titles_config = config.titles.as_ref()?;

    // Use type_mapping if available to resolve category
    let mapped_category = ref_type.and_then(|rt| {
        titles_config
            .type_mapping
            .as_ref()
            .and_then(|mapping| mapping.get(rt))
    });

    use crate::values::type_class::TitleCategory;

    let rendering = match title_type {
        TitleType::ContainerTitle => {
            if let Some(cat) = mapped_category {
                match cat.as_str() {
                    "periodical" => titles_config.periodical.as_ref(),
                    "serial" => titles_config.serial.as_ref(),
                    "monograph" | "collection" => titles_config
                        .container_monograph
                        .as_ref()
                        .or(titles_config.monograph.as_ref()),
                    _ => titles_config.default.as_ref(),
                }
            } else if let Some(rt) = ref_type {
                match crate::values::type_class::container_title_category(rt) {
                    TitleCategory::Periodical => titles_config.periodical.as_ref(),
                    TitleCategory::ContainerMonograph => titles_config
                        .container_monograph
                        .as_ref()
                        .or(titles_config.monograph.as_ref()),
                    _ => titles_config.default.as_ref(),
                }
            } else {
                titles_config.default.as_ref()
            }
        }
        TitleType::ParentSerial => {
            if let Some(cat) = mapped_category {
                match cat.as_str() {
                    "periodical" => titles_config.periodical.as_ref(),
                    "serial" => titles_config.serial.as_ref(),
                    _ => titles_config.periodical.as_ref(),
                }
            } else if let Some(rt) = ref_type {
                match crate::values::type_class::parent_serial_title_category(rt) {
                    TitleCategory::Periodical => titles_config.periodical.as_ref(),
                    _ => titles_config.serial.as_ref(),
                }
            } else {
                titles_config.periodical.as_ref()
            }
        }
        TitleType::ParentMonograph => titles_config
            .container_monograph
            .as_ref()
            .or(titles_config.monograph.as_ref()),
        TitleType::CollectionTitle => titles_config
            .container_monograph
            .as_ref()
            .or(titles_config.monograph.as_ref())
            .or(titles_config.default.as_ref()),
        TitleType::Primary => {
            if let Some(cat) = mapped_category {
                match cat.as_str() {
                    "component" => titles_config.component.as_ref(),
                    "monograph" => titles_config.monograph.as_ref(),
                    _ => titles_config.default.as_ref(),
                }
            } else if let Some(rt) = ref_type {
                match crate::values::type_class::title_category(rt) {
                    TitleCategory::Component => titles_config.component.as_ref(),
                    TitleCategory::Monograph => titles_config.monograph.as_ref(),
                    _ => titles_config.default.as_ref(),
                }
            } else {
                titles_config.default.as_ref()
            }
        }
        _ => None,
    };

    let selected = rendering.or(titles_config.default.as_ref())?;
    let mut effective = selected.clone();
    if let Some(override_rendering) = selected.locale_override(language) {
        effective.merge(override_rendering);
    }
    Some(effective)
}

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::indexing_slicing,
    clippy::todo,
    clippy::unimplemented,
    clippy::unreachable,
    clippy::get_unwrap,
    reason = "Panicking is acceptable and often desired in tests."
)]
mod tests {
    use super::*;
    use citum_schema::template::{Rendering, TemplateComponent, TemplateTitle, TitleType};

    #[test]
    fn test_render_with_emphasis() {
        let component = ProcTemplateComponent {
            template_component: TemplateComponent::Title(TemplateTitle {
                title: TitleType::Primary,
                rendering: Rendering {
                    emph: Some(true),
                    ..Default::default()
                },
                ..Default::default()
            }),
            value: "The Structure of Scientific Revolutions".to_string(),
            ..Default::default()
        };

        let result = render_component(&component);
        assert_eq!(result, "_The Structure of Scientific Revolutions_");
    }

    #[test]
    fn given_quote_flag_and_quote_wrap_when_render_then_single_pair_of_quotes() {
        use citum_schema::template::{WrapConfig, WrapPunctuation};

        // Migrated styles can carry both a global `titles.*.quote` flag and a
        // template `wrap: quotes`; applying both would double the quotes.
        let component = ProcTemplateComponent {
            template_component: TemplateComponent::Title(TemplateTitle {
                title: TitleType::Primary,
                rendering: Rendering {
                    quote: Some(true),
                    wrap: Some(WrapConfig {
                        punctuation: WrapPunctuation::Quotes,
                        inner_prefix: None,
                        inner_suffix: None,
                    }),
                    ..Default::default()
                },
                ..Default::default()
            }),
            value: "The Structure of Scientific Revolutions".to_string(),
            ..Default::default()
        };

        let result = render_component(&component);
        assert_eq!(
            result,
            "\u{201C}The Structure of Scientific Revolutions\u{201D}"
        );
    }

    #[test]
    fn given_quote_flag_and_non_quote_wrap_when_render_then_both_applied() {
        use citum_schema::template::{WrapConfig, WrapPunctuation};

        // A non-quote wrap (parentheses) does not subsume the quote flag, so
        // both must still apply.
        let component = ProcTemplateComponent {
            template_component: TemplateComponent::Title(TemplateTitle {
                title: TitleType::Primary,
                rendering: Rendering {
                    quote: Some(true),
                    wrap: Some(WrapConfig {
                        punctuation: WrapPunctuation::Parentheses,
                        inner_prefix: None,
                        inner_suffix: None,
                    }),
                    ..Default::default()
                },
                ..Default::default()
            }),
            value: "Title".to_string(),
            ..Default::default()
        };

        let result = render_component(&component);
        assert_eq!(result, "(\u{201C}Title\u{201D})");
    }
}