Resolve cleveref cref lists
\cref and \Cref were unsupported, so documents using cleveref leaked the command. The reference pass now scans both commands, splits the comma-separated keys, looks each up in the existing label and kind tables, and joins them as "Section 1 and Figure 1"; \Cref capitalises each kind and unknown keys become "??". A preview test labels a section and a figure and checks both forms with no warnings.
This commit is contained in:
1 parent
25c7659afe
commit
a451df620c
4 files changed
+79
-6
No files matched your search
@@ -12397,6 +12397,52 @@ fn resolve_section_refs(
|
||||
.unwrap_or(value);
|
||||
block.text = block.text.replace(&format!("\\nameref{{{label}}}"), name);
|
||||
}
|
||||
for command in ["\\Cref", "\\cref"] {
|
||||
loop {
|
||||
let Some(start) = block.text.find(command) else {
|
||||
break;
|
||||
};
|
||||
let after = &block.text[start + command.len()..];
|
||||
let Some(rest) = after.strip_prefix('{') else {
|
||||
break;
|
||||
};
|
||||
let Some(end) = rest.find('}') else {
|
||||
break;
|
||||
};
|
||||
let mut parts = Vec::new();
|
||||
for key in rest[..end].split(',') {
|
||||
let key = key.trim();
|
||||
let value = labels
|
||||
.iter()
|
||||
.find(|(label, _)| label == key)
|
||||
.map(|(_, value)| value.as_str());
|
||||
let kind = kinds
|
||||
.iter()
|
||||
.find(|(label, _)| label == key)
|
||||
.map(|(_, kind)| kind.as_str())
|
||||
.unwrap_or("Section");
|
||||
let kind = if command == "\\Cref" {
|
||||
let mut chars = kind.chars();
|
||||
match chars.next() {
|
||||
Some(first) => {
|
||||
first.to_uppercase().collect::<String>() + chars.as_str()
|
||||
}
|
||||
None => String::new(),
|
||||
}
|
||||
} else {
|
||||
kind.to_string()
|
||||
};
|
||||
parts.push(match value {
|
||||
Some(value) => format!("{kind} {value}"),
|
||||
None => "??".to_string(),
|
||||
});
|
||||
}
|
||||
let joined = parts.join(" and ");
|
||||
block
|
||||
.text
|
||||
.replace_range(start..start + command.len() + 1 + end + 1, &joined);
|
||||
}
|
||||
}
|
||||
for command in ["\\ref", "\\eqref", "\\autoref", "\\nameref"] {
|
||||
loop {
|
||||
let prefix = format!("{command}{{");
|
||||
@@ -17360,6 +17406,33 @@ A definition.
|
||||
assert!(text.contains("D"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cref_resolves_multiple_labels() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\n\\section{Intro\\label{sec:a}}\n\n\\begin{figure}[h]\n\\rule{10pt}{10pt}\n\\caption{Plot}\\label{fig:b}\n\\end{figure}\n\n\\cref{sec:a,fig:b} and \\Cref{fig:b,sec:a}.\n\n\\end{document}\n";
|
||||
let json = preview_json(source).expect("preview");
|
||||
let value: serde_json::Value = serde_json::from_str(&json).expect("JSON");
|
||||
assert_eq!(
|
||||
value["warnings"].as_array().map(Vec::len),
|
||||
Some(0),
|
||||
"{}",
|
||||
value["warnings"]
|
||||
);
|
||||
let pages = value["pages"].as_array().expect("pages");
|
||||
let mut text = String::new();
|
||||
for page in pages {
|
||||
if let Some(svg) = page["svg"].as_str() {
|
||||
text.push_str(
|
||||
&svg.split('>')
|
||||
.filter_map(|piece| piece.split('<').next())
|
||||
.collect::<String>(),
|
||||
);
|
||||
}
|
||||
}
|
||||
let text: String = text.split_whitespace().collect();
|
||||
assert!(text.contains("Section1andFigure1"), "{text}");
|
||||
assert!(text.contains("Figure1andSection1"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn siunitx_commands_expand() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\n\\SI{3.5}{m}, \\SI{2}{\\kilo\\metre\\per\\second}, \\num{1\\,000}, \\si{\\metre}.\n\n\\end{document}\n";
|
||||
|
||||
Reference in new issue
Block a user