citum-engine 0.61.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
/*
SPDX-License-Identifier: MIT OR Apache-2.0
SPDX-FileCopyrightText: © 2023-2026 Bruce D'Arcus and Citum contributors
*/

//! High-level document-processing orchestration.

use super::output::{
    HtmlPlaceholderRegistry, RenderedDocumentBody, append_document_bibliography,
    bibliography_block_placeholder, render_document_bibliography_block_replacement,
    rewrite_document_markup_for_typst, rewrite_group_headings_for_document,
    stage_document_bibliography_blocks,
};
use super::{BibliographyBlock, CitationParser, DocumentFormat, ParsedDocument};
use crate::processor::Processor;

impl Processor {
    /// Process citations in a document and append a bibliography.
    ///
    /// This is the primary document-level entry point. It:
    /// 1. Parses the source document using the provided adapter.
    /// 2. Resolves frontmatter overrides (integral-name policy, bibliography options).
    /// 3. Chooses a bibliography orchestration path based on frontmatter and document blocks.
    #[allow(
        clippy::string_slice,
        reason = "parser-guaranteed boundaries and indices"
    )]
    pub fn process_document<P, F>(
        &self,
        content: &str,
        parser: &P,
        format: DocumentFormat,
    ) -> String
    where
        P: CitationParser,
        F: crate::render::format::OutputFormat<Output = String>,
    {
        let mut parsed = parser.parse_document(content, &self.locale);

        if let Some(err) = &parsed.frontmatter_error {
            eprintln!("citum: error: frontmatter parse error: {err}");
            std::process::exit(1);
        }

        // `options.*` fields take precedence over the legacy top-level fields.
        let effective_integral_override = parsed
            .frontmatter_options
            .as_ref()
            .and_then(|o| o.integral_name_memory.as_ref())
            .or(parsed.frontmatter_integral_name_memory.as_ref());
        let owned_integral =
            self.processor_with_document_integral_name_override(effective_integral_override);

        // `options.org-abbreviation-memory` takes precedence over the legacy top-level field.
        let effective_org_override = parsed
            .frontmatter_options
            .as_ref()
            .and_then(|o| o.org_abbreviation_memory.as_ref())
            .or(parsed.frontmatter_org_abbreviation_memory.as_ref());
        let owned_org = {
            let base = owned_integral.as_ref().unwrap_or(self);
            base.processor_with_document_org_abbreviation_override(effective_org_override)
        };

        // Apply bibliography overrides from the options block.
        let owned_bib = parsed
            .frontmatter_options
            .as_ref()
            .filter(|o| o.bibliography.is_some())
            .map(|options| {
                let base = owned_org
                    .as_ref()
                    .or(owned_integral.as_ref())
                    .unwrap_or(self);
                base.processor_with_bibliography_override(options)
            });

        let processor = owned_bib
            .as_ref()
            .or(owned_org.as_ref())
            .or(owned_integral.as_ref())
            .unwrap_or(self);
        let body = &content[parsed.body_start..];
        if let Some(groups) = parsed.frontmatter_groups.take() {
            return processor.process_document_with_frontmatter_groups::<P, F>(
                body, parsed, groups, parser, format,
            );
        }

        if !parsed.bibliography_blocks.is_empty() {
            return processor.process_document_with_bibliography_blocks::<P, F>(
                body,
                std::mem::take(&mut parsed.bibliography_blocks),
                parser,
                format,
            );
        }

        processor.process_document_with_default_bibliography::<P, F>(body, parsed, parser, format)
    }

    /// Orchestrate document processing with custom frontmatter bibliography groups.
    fn process_document_with_frontmatter_groups<P, F>(
        &self,
        body: &str,
        parsed: ParsedDocument,
        groups: Vec<citum_schema::grouping::BibliographyGroup>,
        parser: &P,
        format: DocumentFormat,
    ) -> String
    where
        P: CitationParser,
        F: crate::render::format::OutputFormat<Output = String>,
    {
        self.render_document_with_trailing_bibliography::<P, F, _>(
            body,
            parsed,
            parser,
            format,
            |processor| {
                rewrite_group_headings_for_document(
                    processor.render_document_bibliography_groups::<F>(&groups),
                    format,
                )
            },
        )
    }

    /// Orchestrate document processing with explicit bibliography blocks.
    fn process_document_with_bibliography_blocks<P, F>(
        &self,
        body: &str,
        blocks: Vec<BibliographyBlock>,
        parser: &P,
        format: DocumentFormat,
    ) -> String
    where
        P: CitationParser,
        F: crate::render::format::OutputFormat<Output = String>,
    {
        let staged = stage_document_bibliography_blocks(body, &blocks);
        let parsed_staged = parser.parse_document(&staged, &self.locale);
        let mut rendered = self.render_document_body::<F>(&staged, parsed_staged, format);
        self.replace_document_bibliography_blocks::<F>(&mut rendered, &blocks, format);
        self.finalize_document_output::<P, F>(parser, format, rendered)
    }

    /// Orchestrate document processing with the default trailing bibliography.
    fn process_document_with_default_bibliography<P, F>(
        &self,
        body: &str,
        parsed: ParsedDocument,
        parser: &P,
        format: DocumentFormat,
    ) -> String
    where
        P: CitationParser,
        F: crate::render::format::OutputFormat<Output = String>,
    {
        self.render_document_with_trailing_bibliography::<P, F, _>(
            body,
            parsed,
            parser,
            format,
            super::super::Processor::render_grouped_bibliography_with_format::<F>,
        )
    }

    /// Generic helper for rendering document body + trailing bibliography.
    fn render_document_with_trailing_bibliography<P, F, B>(
        &self,
        body: &str,
        parsed: ParsedDocument,
        parser: &P,
        format: DocumentFormat,
        render_bibliography: B,
    ) -> String
    where
        P: CitationParser,
        F: crate::render::format::OutputFormat<Output = String>,
        B: FnOnce(&Self) -> String,
    {
        let mut rendered = self.render_document_body::<F>(body, parsed, format);
        let bibliography = render_bibliography(self);
        append_document_bibliography(&mut rendered, format, bibliography);
        self.finalize_document_output::<P, F>(parser, format, rendered)
    }

    /// Render the citation-annotated document body.
    ///
    /// Governs the choice between note-style and inline-style processing,
    /// and handles placeholder registration for format finalization.
    /// HTML and terminal formats (Typst, LaTeX) both use the placeholder path
    /// so that body markup can be converted after citations are spliced in.
    fn render_document_body<F>(
        &self,
        content: &str,
        parsed: ParsedDocument,
        format: DocumentFormat,
    ) -> RenderedDocumentBody
    where
        F: crate::render::format::OutputFormat<Output = String>,
    {
        if matches!(format, DocumentFormat::Html) {
            let mut placeholders = HtmlPlaceholderRegistry::default();
            let content = if self.is_note_style() {
                self.process_note_document_html(content, parsed, &mut placeholders)
            } else {
                self.process_inline_document_html(content, parsed, &mut placeholders)
            };
            return RenderedDocumentBody {
                content,
                placeholders: Some(placeholders),
                trailing: None,
            };
        }

        // Terminal formats (Typst, LaTeX) need the same placeholder flow so
        // the body markup can be converted to the target format after citations
        // are replaced with NUL-token placeholders.
        if matches!(format, DocumentFormat::Typst | DocumentFormat::Latex) {
            let mut placeholders = HtmlPlaceholderRegistry::default();
            // Note styles emit raw footnote syntax that the body markup
            // converter doesn't understand; fall back to the passthrough path
            // and let the caller handle conversion separately if needed.
            let content = if self.is_note_style() {
                self.process_note_document::<F>(content, parsed)
            } else {
                self.process_inline_document_with_placeholders::<F>(
                    content,
                    parsed,
                    &mut placeholders,
                )
            };
            return RenderedDocumentBody {
                content,
                placeholders: if self.is_note_style() {
                    None
                } else {
                    Some(placeholders)
                },
                trailing: None,
            };
        }

        let content = if self.is_note_style() {
            self.process_note_document::<F>(content, parsed)
        } else {
            self.process_inline_document::<F>(content, parsed)
        };

        RenderedDocumentBody {
            content,
            placeholders: None,
            trailing: None,
        }
    }

    /// Splice `F`-rendered citations into document markup using NUL placeholders.
    ///
    /// Mirrors `process_inline_document_html` but renders citations using the
    /// generic format `F` (e.g. Typst or LaTeX) instead of HTML. The
    /// surrounding body markup still contains the source syntax at this point;
    /// `finalize_document_output` converts it after placeholder substitution.
    #[allow(
        clippy::string_slice,
        reason = "parser-guaranteed boundaries and indices"
    )]
    fn process_inline_document_with_placeholders<F>(
        &self,
        content: &str,
        parsed: ParsedDocument,
        placeholders: &mut HtmlPlaceholderRegistry,
    ) -> String
    where
        F: crate::render::format::OutputFormat<Output = String>,
    {
        let mut result = String::new();
        let mut last_idx = 0;
        let normalized = self.normalize_integral_name_citations(&parsed);

        for (parsed, citation) in parsed.citations.iter().zip(normalized) {
            result.push_str(&content[last_idx..parsed.start]);
            match self.process_citation_with_format::<F>(&citation) {
                Ok(rendered) => result.push_str(&placeholders.push_inline(rendered)),
                Err(_) => result.push_str(&content[parsed.start..parsed.end]),
            }
            last_idx = parsed.end;
        }

        result.push_str(&content[last_idx..]);
        result
    }

    /// Splice rendered citations into document markup for non-note styles.
    #[allow(
        clippy::string_slice,
        reason = "parser-guaranteed boundaries and indices"
    )]
    fn process_inline_document<F>(&self, content: &str, parsed: ParsedDocument) -> String
    where
        F: crate::render::format::OutputFormat<Output = String>,
    {
        let mut result = String::new();
        let mut last_idx = 0;
        let normalized = self.normalize_integral_name_citations(&parsed);

        for (parsed, citation) in parsed.citations.iter().zip(normalized) {
            result.push_str(&content[last_idx..parsed.start]);
            match self.process_citation_with_format::<F>(&citation) {
                Ok(rendered) => result.push_str(&rendered),
                Err(_) => result.push_str(&content[parsed.start..parsed.end]),
            }
            last_idx = parsed.end;
        }

        result.push_str(&content[last_idx..]);
        result
    }

    /// Splice HTML-rendered citations into document markup using placeholders.
    #[allow(
        clippy::string_slice,
        reason = "parser-guaranteed boundaries and indices"
    )]
    fn process_inline_document_html(
        &self,
        content: &str,
        parsed: ParsedDocument,
        placeholders: &mut HtmlPlaceholderRegistry,
    ) -> String {
        let mut result = String::new();
        let mut last_idx = 0;
        let normalized = self.normalize_integral_name_citations(&parsed);

        for (parsed, citation) in parsed.citations.iter().zip(normalized) {
            result.push_str(&content[last_idx..parsed.start]);
            match self.process_citation_with_format::<crate::render::html::Html>(&citation) {
                Ok(rendered) => result.push_str(&placeholders.push_inline(rendered)),
                Err(_) => result.push_str(&content[parsed.start..parsed.end]),
            }
            last_idx = parsed.end;
        }

        result.push_str(&content[last_idx..]);
        result
    }

    /// Replace bibliography block placeholders with rendered content.
    fn replace_document_bibliography_blocks<F>(
        &self,
        rendered: &mut RenderedDocumentBody,
        blocks: &[BibliographyBlock],
        format: DocumentFormat,
    ) where
        F: crate::render::format::OutputFormat<Output = String>,
    {
        let mut assigned = std::collections::HashSet::<String>::new();
        for (index, block) in blocks.iter().enumerate() {
            let placeholder = bibliography_block_placeholder(index);
            let rendered_group =
                self.render_document_bibliography_block::<F>(&block.group, &mut assigned);
            let replacement = render_document_bibliography_block_replacement(
                rendered.placeholders.as_mut(),
                format,
                rendered_group.heading,
                rendered_group.body,
            );
            rendered.content = rendered.content.replace(&placeholder, &replacement);
        }
    }

    /// Perform final document rewrites and resolve placeholders.
    ///
    /// For HTML: converts body markup via `finalize_html_output` then
    /// substitutes citation placeholder tokens.
    /// For Typst/LaTeX: converts body markup via `render_body_markup::<F>`
    /// then substitutes citation placeholder tokens.
    /// For other formats: returns the spliced content as-is.
    fn finalize_document_output<P, F>(
        &self,
        parser: &P,
        format: DocumentFormat,
        rendered: RenderedDocumentBody,
    ) -> String
    where
        P: CitationParser,
        F: crate::render::format::OutputFormat<Output = String>,
    {
        let mut result = if let Some(placeholders) = rendered.placeholders {
            let fmt = F::default();
            let converted = match format {
                DocumentFormat::Html => parser.finalize_html_output(&rendered.content),
                DocumentFormat::Typst | DocumentFormat::Latex => {
                    parser.render_body_markup(&rendered.content, &fmt)
                }
                _ => rendered.content,
            };
            placeholders.apply(converted)
        } else {
            // Passthrough path (Plain, Djot, Markdown, note-style Typst/LaTeX).
            // Keep the heading-rewrite for Typst in case headings came from
            // bibliography group labels rather than body markup.
            let content = rewrite_document_markup_for_typst(rendered.content, format);
            match format {
                DocumentFormat::Html => parser.finalize_html_output(&content),
                _ => content,
            }
        };
        // Append any trailing content (e.g. Typst/LaTeX bibliography) that was
        // deferred so it would not pass through the body markup converter.
        // Trim the body's trailing whitespace first: the markup renderer may
        // have added paragraph-separator newlines that would otherwise double
        // the leading newlines of the bibliography heading.
        if let Some(tail) = rendered.trailing {
            let trimmed = result.trim_end_matches('\n');
            result = format!("{trimmed}{tail}");
        }
        result
    }
}