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

//! LaTeX output format.

use std::borrow::Cow;
use std::ops::Range;

use super::format::{OutputFormat, QuoteMarks, realize_wrap};
use super::visible_scan::{RunBuilder, skip_balanced};
use crate::values::ScriptClass;
use citum_schema::template::WrapPunctuation;

/// LaTeX renderer.
#[derive(Debug, Clone, Default)]
pub struct Latex;

impl Latex {
    /// Escapes characters that break a LaTeX `\href{...}` URL argument.
    ///
    /// Minimal set for the audit finding: a bare `%` starts a LaTeX comment
    /// and truncates the rest of the line, `#` breaks `{}`-grouping (macro
    /// parameter syntax), and `\` would otherwise be read as a control
    /// sequence. `\` is escaped first so the backslash-introducing escapes
    /// for `%`/`#` are not themselves re-escaped.
    fn escape_href_target(url: &str) -> String {
        url.replace('\\', r"\textbackslash{}")
            .replace('%', r"\%")
            .replace('#', r"\#")
    }
}

impl OutputFormat for Latex {
    type Output = String;

    fn text(&self, s: &str) -> Self::Output {
        let mut res = String::with_capacity(s.len() + 10);
        for c in s.chars() {
            match c {
                '\\' => res.push_str(r"\textbackslash{}"),
                '{' => res.push_str(r"\{"),
                '}' => res.push_str(r"\}"),
                '$' => res.push_str(r"\$"),
                '&' => res.push_str(r"\&"),
                '#' => res.push_str(r"\#"),
                '_' => res.push_str(r"\_"),
                '%' => res.push_str(r"\%"),
                '~' => res.push_str(r"\textasciitilde{}"),
                '^' => res.push_str(r"\textasciicircum{}"),
                _ => res.push(c),
            }
        }
        res
    }

    fn join(&self, items: Vec<Self::Output>, delimiter: &str) -> Self::Output {
        items.join(&self.text(delimiter))
    }

    fn finish(&self, output: Self::Output) -> String {
        // Escape any bare & not already preceded by backslash.
        // Locale terms (e.g. the & from AndOptions::Symbol) bypass text() and
        // arrive here unescaped; this final pass makes the output valid LaTeX.
        let mut result = String::with_capacity(output.len() + 4);
        let mut prev = '\0';
        for c in output.chars() {
            if c == '&' && prev != '\\' {
                result.push_str(r"\&");
            } else {
                result.push(c);
            }
            prev = c;
        }
        result
    }

    fn emph(&self, content: Self::Output) -> Self::Output {
        format!(r"\emph{{{content}}}")
    }

    fn strong(&self, content: Self::Output) -> Self::Output {
        format!(r"\textbf{{{content}}}")
    }

    fn small_caps(&self, content: Self::Output) -> Self::Output {
        format!(r"\textsc{{{content}}}")
    }

    fn superscript(&self, content: Self::Output) -> Self::Output {
        format!(r"\textsuperscript{{{content}}}")
    }

    fn quote(&self, content: Self::Output, marks: &QuoteMarks) -> Self::Output {
        let (open, close) = marks.for_depth(0);
        format!("{open}{content}{close}")
    }

    fn affix(&self, prefix: &str, content: Self::Output, suffix: &str) -> Self::Output {
        format!("{}{}{}", self.text(prefix), content, self.text(suffix))
    }

    fn inner_affix(&self, prefix: &str, content: Self::Output, suffix: &str) -> Self::Output {
        format!("{}{}{}", self.text(prefix), content, self.text(suffix))
    }

    fn wrap_punctuation(
        &self,
        wrap: &WrapPunctuation,
        content: Self::Output,
        marks: &QuoteMarks,
        script: ScriptClass,
        realization: Option<&citum_schema::options::PunctuationRealization>,
    ) -> Self::Output {
        match realize_wrap(wrap, script, realization) {
            Some((open, close)) => {
                format!("{}{}{}", self.text(&open), content, self.text(&close))
            }
            None => self.quote(content, marks),
        }
    }

    fn semantic(&self, _class: &str, content: Self::Output) -> Self::Output {
        // In LaTeX, we could use custom commands if we wanted semantic tagging
        // For now, just return content
        content
    }

    fn annotation(&self, content: Self::Output) -> Self::Output {
        if content.is_empty() {
            return content;
        }
        format!(
            "\n\\begin{{citumannotation}}\n{}\n\\end{{citumannotation}}",
            content
        )
    }

    fn link(&self, url: &str, content: Self::Output) -> Self::Output {
        let target = Self::escape_href_target(url);
        format!(r"\href{{{target}}}{{{content}}}")
    }

    // ── Block-level body markup methods ────────────────────────────────────

    fn paragraph(&self, content: Self::Output) -> Self::Output {
        if content.is_empty() {
            return content;
        }
        format!("{content}\n\n")
    }

    fn block_quote(&self, content: Self::Output) -> Self::Output {
        if content.is_empty() {
            return content;
        }
        let trimmed = content.trim_end();
        format!("\\begin{{quote}}\n{trimmed}\n\\end{{quote}}\n\n")
    }

    fn bullet_list(&self, items: Vec<Self::Output>) -> Self::Output {
        if items.is_empty() {
            return String::new();
        }
        let body = items
            .iter()
            .map(|item| format!("  \\item {}", item.trim()))
            .collect::<Vec<_>>()
            .join("\n");
        format!("\\begin{{itemize}}\n{body}\n\\end{{itemize}}\n\n")
    }

    fn ordered_list(&self, items: Vec<Self::Output>) -> Self::Output {
        if items.is_empty() {
            return String::new();
        }
        let body = items
            .iter()
            .map(|item| format!("  \\item {}", item.trim()))
            .collect::<Vec<_>>()
            .join("\n");
        format!("\\begin{{enumerate}}\n{body}\n\\end{{enumerate}}\n\n")
    }

    fn heading(&self, level: u8, content: Self::Output) -> Self::Output {
        let cmd = match level {
            1 => "\\section",
            2 => "\\subsection",
            3 => "\\subsubsection",
            _ => "\\paragraph",
        };
        format!("{cmd}{{{content}}}\n\n")
    }

    fn unnumbered_heading(&self, level: u8, content: Self::Output) -> Self::Output {
        let cmd = match level {
            1 => "\\section*",
            2 => "\\subsection*",
            3 => "\\subsubsection*",
            _ => "\\paragraph*",
        };
        format!("{cmd}{{{content}}}\n\n")
    }

    fn code_block(&self, _lang: Option<&str>, content: Self::Output) -> Self::Output {
        format!("\\begin{{verbatim}}\n{content}\\end{{verbatim}}\n\n")
    }

    fn inline_code(&self, content: Self::Output) -> Self::Output {
        // \texttt is not verbatim; escape LaTeX specials in the raw code content.
        format!("\\texttt{{{}}}", self.text(&content))
    }

    fn strikeout(&self, content: Self::Output) -> Self::Output {
        if content.is_empty() {
            return content;
        }
        format!("\\sout{{{content}}}")
    }

    fn hard_break(&self) -> Self::Output {
        "\\\\\n".to_string()
    }

    fn bibliography(&self, entries: Vec<Self::Output>) -> Self::Output {
        entries.join("\\par\\vspace{0.5em}")
    }

    fn entry(
        &self,
        _id: &str,
        content: Self::Output,
        _url: Option<&str>,
        _metadata: &super::format::ProcEntryMetadata,
    ) -> Self::Output {
        format!("\\noindent\\hangindent=2em\\hangafter=1 {content}")
    }

    /// Strip LaTeX commands (`\emph`, `\textbf`, ...) and their brace
    /// delimiters, keeping brace *contents* visible. A backslash-escaped
    /// punctuation mark (`\&`, `\_`, `\%`, `\#`, `\$`, `\{`, `\}`) is visible
    /// as the escaped character itself. `\href{target}{content}` is
    /// special-cased so the URL target stays invisible — it must not
    /// participate in separator/dedup decisions — while `content` does.
    ///
    /// Named commands that stand in for a single visible character
    /// (`\textbackslash{}`, `\textasciitilde{}`, `\textasciicircum{}`) have
    /// no backing byte range here — see [`Self::visible_text`], which
    /// synthesizes them for boundary decisions.
    fn visible_runs(&self, fragment: &str) -> Vec<Range<usize>> {
        let mut runs = RunBuilder::default();
        let chars: Vec<(usize, char)> = fragment.char_indices().collect();
        let mut i = 0;
        while let Some(&(pos, ch)) = chars.get(i) {
            if ch == '\\' {
                let (escape, next_i) = scan_backslash_escape(&chars, i);
                if let LatexEscape::Punct(_) = escape
                    && let Some(&(epos, echar)) = chars.get(i + 1)
                {
                    runs.push_visible(epos, epos + echar.len_utf8());
                }
                i = next_i;
                continue;
            }
            if ch == '{' || ch == '}' {
                i += 1;
                continue;
            }
            runs.push_visible(pos, pos + ch.len_utf8());
            i += 1;
        }
        runs.finish()
    }

    /// As [`Self::visible_runs`], but additionally synthesizes the single
    /// visible character that `\textbackslash{}`/`\textasciitilde{}`/
    /// `\textasciicircum{}` stand in for (`\`, `~`, `^`), since those have no
    /// backing byte range in the raw fragment. Used for read-only boundary
    /// decisions (first/last visible char); `visible_runs` stays raw-byte
    /// accurate for `cleanup_dangling_punctuation`'s in-place raw edits.
    fn visible_text<'a>(&self, fragment: &'a str) -> Cow<'a, str> {
        let chars: Vec<(usize, char)> = fragment.char_indices().collect();
        let mut i = 0;
        let mut owned = String::with_capacity(fragment.len());
        let mut any_markup = false;
        while let Some(&(_, ch)) = chars.get(i) {
            if ch == '\\' {
                any_markup = true;
                let (escape, next_i) = scan_backslash_escape(&chars, i);
                match escape {
                    LatexEscape::Punct(c) => owned.push(c),
                    LatexEscape::Command { synth: Some(c) } => owned.push(c),
                    LatexEscape::Command { synth: None } | LatexEscape::Bare => {}
                }
                i = next_i;
                continue;
            }
            if ch == '{' || ch == '}' {
                any_markup = true;
                i += 1;
                continue;
            }
            owned.push(ch);
            i += 1;
        }
        if any_markup {
            Cow::Owned(owned)
        } else {
            Cow::Borrowed(fragment)
        }
    }
}

/// Outcome of classifying the backslash escape at `chars[i]`.
enum LatexEscape {
    /// An escaped punctuation mark (`\&`, `\_`, ...); visible as the escaped char.
    Punct(char),
    /// A named ascii-alpha command. `synth` holds the single character it
    /// stands in for (`\textbackslash{}` → `\`, etc.), when applicable.
    Command { synth: Option<char> },
    /// A lone backslash with no recognized continuation.
    Bare,
}

/// Classify the `\` at `chars[i]`, returning the outcome and the index just
/// past it — past the command name (and past `\href`'s target brace group),
/// or past the escaped char for punctuation escapes.
fn scan_backslash_escape(chars: &[(usize, char)], i: usize) -> (LatexEscape, usize) {
    match chars.get(i + 1).map(|&(_, c)| c) {
        Some(c @ ('{' | '}' | '$' | '&' | '#' | '_' | '%')) => (LatexEscape::Punct(c), i + 2),
        Some(c) if c.is_ascii_alphabetic() => {
            let mut j = i + 1;
            let mut command = String::new();
            while let Some(&(_, cc)) = chars.get(j) {
                if !cc.is_ascii_alphabetic() {
                    break;
                }
                command.push(cc);
                j += 1;
            }
            let synth = match command.as_str() {
                "textbackslash" => Some('\\'),
                "textasciitilde" => Some('~'),
                "textasciicircum" => Some('^'),
                _ => None,
            };
            let end = if command == "href" {
                skip_balanced(chars, j, '{', '}', false)
            } else {
                j
            };
            (LatexEscape::Command { synth }, end)
        }
        _ => (LatexEscape::Bare, i + 1),
    }
}

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::indexing_slicing,
    reason = "Panicking is acceptable and often desired in tests."
)]
mod tests {
    use super::*;

    #[test]
    fn visible_text_strips_emph_command_and_braces() {
        let fmt = Latex;
        assert_eq!(fmt.visible_text(r"\emph{Title.}"), "Title.");
    }

    #[test]
    fn visible_text_keeps_escaped_punctuation() {
        let fmt = Latex;
        assert_eq!(fmt.visible_text(r"Smith \& Jones"), "Smith & Jones");
    }

    #[test]
    fn visible_text_hides_href_target_keeps_content() {
        let fmt = Latex;
        assert_eq!(
            fmt.visible_text(r"\href{https://example.com/a.b}{Example}"),
            "Example"
        );
    }

    #[test]
    fn visible_text_handles_nested_commands() {
        let fmt = Latex;
        assert_eq!(fmt.visible_text(r"\textbf{\emph{Title.}}"), "Title.");
    }

    #[test]
    fn visible_text_is_borrowed_when_no_markup() {
        let fmt = Latex;
        assert_eq!(fmt.visible_text("Plain text."), "Plain text.");
    }

    #[test]
    fn visible_text_synthesizes_escaped_backslash() {
        let fmt = Latex;
        assert_eq!(fmt.visible_text(r"C:\textbackslash{}Users"), r"C:\Users");
    }

    #[test]
    fn visible_text_synthesizes_escaped_tilde() {
        let fmt = Latex;
        assert_eq!(
            fmt.visible_text(r"Title\textasciitilde{}Subtitle"),
            "Title~Subtitle"
        );
    }

    #[test]
    fn visible_text_synthesizes_escaped_caret() {
        let fmt = Latex;
        assert_eq!(fmt.visible_text(r"x\textasciicircum{}2"), "x^2");
    }

    #[test]
    fn visible_text_synthesized_char_is_seen_as_the_trailing_char() {
        // A field ending in an escaped tilde must expose that tilde as the
        // last visible char, not disappear (the gap this fixes: the raw
        // fragment's last char is `}`, not `~`).
        let fmt = Latex;
        let rendered = fmt.emph(r"Title\textasciitilde{}".to_string());
        assert_eq!(fmt.visible_text(&rendered).chars().last(), Some('~'));
    }

    #[test]
    fn visible_runs_does_not_claim_a_byte_range_for_synthesized_chars() {
        // visible_runs stays raw-byte accurate (used for in-place raw edits
        // in cleanup_dangling_punctuation) — the synthesized char has no
        // backing byte, so it must not appear as a run.
        let fmt = Latex;
        let runs = fmt.visible_runs(r"\textasciitilde{}");
        assert!(runs.is_empty(), "expected no visible runs, got {runs:?}");
    }
}