Skip to main content

flm_core/stdlib/features/xnotes/
labels.rs

1//! The optional argument of an xnote, which holds the `\label{…}` commands
2//! that label the xnote and nothing else: [`labels_of`] reads it, and
3//! [`Rejected`] records what it refuses.
4
5use techy::core::node::NodeRef;
6
7use crate::lang::text::{is_blank, is_space};
8use crate::lang::{Flm, FlmSpan, structural};
9use crate::stdlib::features::refs::{self, LabelUse};
10
11use super::LABELS_ARG;
12
13/// Something written in the optional argument of an xnote that is neither a
14/// `\label{…}`, whitespace nor a comment (text, a group, `\textbf{…}`, a
15/// command that no feature defines there), as [`labels_of`] returns it: what
16/// was written, and where.
17///
18/// Such a thing is ignored. The lowering rule of the xnote reports the first
19/// one of each xnote as the Error
20/// [`BadLabelArgument`](super::BadLabelArgument).
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct Rejected {
23    /// The node as written in the source, trimmed, and cut after forty
24    /// characters with `…`.
25    pub found: String,
26    /// The node's span.
27    pub span: FlmSpan,
28}
29
30/// The longest text a [`Rejected`] quotes, in characters.
31const QUOTED_CHARS: usize = 40;
32
33/// Reads the optional argument [`LABELS_ARG`] of the xnote node `node`: the
34/// `\label{…}` commands written there, in order, and every other thing
35/// written there that is not whitespace or a comment.
36///
37/// The labels are read by the references feature's own reader,
38/// [`refs::label_uses_in`], so a `\label` here is read exactly as one written
39/// after a heading; [`refs::point_labels`] splits them into what a reference
40/// point declares. The labels count regardless of what else the argument
41/// holds: `\footnote[\textbf{x}\label{fn:e}]{…}` is labeled `fn:e`, and its
42/// `\textbf{x}` is rejected.
43///
44/// A `\label` counts only where the references feature declares it, in its
45/// scoped package [`refs::LABEL_PACKAGE`]: in a registry without that
46/// feature, a `\label` in the argument is an unknown command and is rejected
47/// like any other. For a node with no such argument, or with the argument not
48/// written, the function returns two empty lists.
49pub fn labels_of(node: &NodeRef<'_, Flm>) -> (Vec<LabelUse>, Vec<Rejected>) {
50    let Ok(Some(run)) = node.argument_content_nodes_named(LABELS_ARG) else {
51        return (Vec::new(), Vec::new());
52    };
53    let uses = refs::label_uses_in(run.iter());
54    let rejected = run
55        .iter()
56        .filter(|node| refs::label_use(node).is_none() && !is_layout(node))
57        .map(|node| Rejected { found: quoted(node.span_content()), span: node.span().clone() })
58        .collect();
59    (uses, rejected)
60}
61
62/// Whether `node` is nothing but layout: whitespace ([`is_blank`]: text made
63/// of whitespace alone, or a node that is whitespace, such as `~` and `\ `), a
64/// comment, or the blank line that the block-structure pass reports in an
65/// inline argument.
66fn is_layout(node: &NodeRef<'_, Flm>) -> bool {
67    node.is_comment() || is_blank(node) || node.ext().element.name.as_str() == structural::PARBREAK
68}
69
70/// `text` without the whitespace ([`is_space`]) at either end, cut after
71/// [`QUOTED_CHARS`] characters with `…`.
72fn quoted(text: &str) -> String {
73    let text = text.trim_matches(is_space);
74    match text.char_indices().nth(QUOTED_CHARS) {
75        Some((at, _)) => format!("{}\u{2026}", &text[..at]),
76        None => text.to_string(),
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn a_long_text_is_cut() {
86        assert_eq!(quoted("  \\textbf{x}  "), "\\textbf{x}");
87        let long = "a".repeat(50);
88        assert_eq!(quoted(&long), format!("{}\u{2026}", "a".repeat(40)));
89        assert_eq!(quoted(&"é".repeat(40)), "é".repeat(40));
90    }
91}