Render the keywords environment

A keywords environment was unsupported, so classes that print their keyword
list as an environment leaked the wrapper. A body pass now finds the block,
replaces \sep with ", ", and emits a bold "Keywords:" lead-in followed by the
entries; the environment name counts as supported.

A preview test renders "alpha\sep beta" as "Keywords: alpha, beta" with no
warnings.
This commit is contained in:
gloamxun committed 2026-10-27 03:50:14 +08:00
1 parent 58c35c85b9
commit 59ba5ac247
4 files changed
+63 -6

No files matched your search

+57
View File
@@ -6421,6 +6421,7 @@ fn geometry_and_body_with_images(
body = expand_environments(&body, &geometry.environments);
body = expand_keywords(&body);
body = expand_acks(&body);
body = expand_keywords_environment(&body);
body = body
.replace("\\begin{appendices}", "\n\n\\appendix\n\n")
.replace("\\end{appendices}", "\n\n");
@@ -10325,6 +10326,35 @@ fn optional_group(text: &str) -> Option<(String, usize)> {
Some((text[..end].to_string(), end + 1))
}
/// Turn a `keywords` environment into a bold "Keywords:" lead-in, joining its
/// `\sep`-separated entries with commas.
fn expand_keywords_environment(body: &str) -> String {
let mut output = String::new();
let mut rest = body;
loop {
let Some(start) = rest.find("\\begin{keywords}") else {
output.push_str(rest);
break;
};
let begin_end = start + "\\begin{keywords}".len();
let Some(relative) = rest[begin_end..].find("\\end{keywords}") else {
output.push_str(rest);
break;
};
let inner_end = begin_end + relative;
output.push_str(&rest[..start]);
let inner = rest[begin_end..inner_end]
.replace("\\sep", ", ")
.trim()
.to_string();
output.push_str("\n\n\\textbf{Keywords:} ");
output.push_str(&inner);
output.push_str("\n\n");
rest = &rest[inner_end + "\\end{keywords}".len()..];
}
output
}
/// Consume `\markboth`/`\markright` that appear inside running text (the
/// line-start forms are handled while the page geometry is read) and record the
/// most recent marks for the running heads.
@@ -13990,6 +14020,7 @@ const SUPPORTED_ENVIRONMENTS: &[&str] = &[
"flushright",
"minipage",
"acks",
"keywords",
"appendices",
"acknowledgments",
"acknowledgements",
@@ -17412,6 +17443,32 @@ A definition.
assert!(text.contains("D"), "{text}");
}
#[test]
fn keywords_environment_renders_a_lead_in() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\begin{keywords}\nalpha\\sep beta\n\\end{keywords}\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("Keywords:alpha,beta"), "{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";