Apply the percent catcode when stripping comments

Comments were stripped in a separate pass before canonicalisation, so
\catcode`\%=12 happened too late to make a later percent literal. Move comment
stripping into the canonicalisation pass: it now tracks whether % is a comment
char (default yes), skips to the end of line when it is, and \catcode`\%=12
(or 13) turns that off so later percents reach the text. The source order in
geometry_and_body becomes canonicalise (comments + catcodes) then collapse
definition bodies, and the separate strip_tex_comments call is gone.

A test previews "100% sure" under \catcode`\%=12 and the default commented form
("Hello% hidden" + "world") as "Helloworld" with no warnings.
This commit is contained in:
gloamxun committed 2026-10-27 01:21:10 +08:00
1 parent d791e3a0f2
commit 05e1bd096b
3 files changed
+51 -8

No files matched your search

+44 -2
View File
@@ -5947,6 +5947,7 @@ fn canonicalize_at_catcodes(source: &str) -> String {
let mut out = String::with_capacity(source.len());
let mut index = 0;
let mut at_letter = false;
let mut percent_comments = true;
let mut saved = Vec::new();
while index < characters.len() {
let character = characters[index];
@@ -5962,6 +5963,12 @@ fn canonicalize_at_catcodes(source: &str) -> String {
index += 1;
continue;
}
if character == '%' && percent_comments {
while index < characters.len() && characters[index] != '\n' {
index += 1;
}
continue;
}
if character != '\\' {
out.push(character);
index += 1;
@@ -6051,6 +6058,8 @@ fn canonicalize_at_catcodes(source: &str) -> String {
// assignments are consumed so they do not leak as text.
if target == '@' {
at_letter = value == 11;
} else if target == '%' {
percent_comments = value != 12 && value != 13;
}
index = next;
continue;
@@ -6149,9 +6158,8 @@ fn geometry_and_body_with_images(
// body before `\begin{document}` so its commands are consumed as setup
// instead of being typeset as text at the top of page one.
let source = lift_at_begin_document(source);
let source = collapse_definition_bodies(&source);
let source = strip_tex_comments(&source);
let source = canonicalize_at_catcodes(&source);
let source = collapse_definition_bodies(&source);
let source = collapse_braced_command(&source, "\\newgeometry");
// Class files write the title metadata across lines, especially author
// blocks with forced line breaks and thanks notes.
@@ -16073,6 +16081,40 @@ A definition.
assert!(!text.contains("makeatletter"), "{text}");
}
#[test]
fn percent_catcode_turns_comments_literal() {
let literal = "\\documentclass{article}\n\\catcode`\\%=12\n\\begin{document}\n\n100% sure\n\n\\end{document}\n";
let json = preview_json(literal).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 svg = value["pages"][0]["svg"].as_str().expect("svg");
let text: String = svg
.split('>')
.filter_map(|piece| piece.split('<').next())
.collect::<String>()
.split_whitespace()
.collect();
assert!(text.contains("100%sure"), "{text}");
let commented = "\\documentclass{article}\n\\begin{document}\n\nHello% hidden\nworld\n\n\\end{document}\n";
let json = preview_json(commented).expect("preview");
let value: serde_json::Value = serde_json::from_str(&json).expect("JSON");
let svg = value["pages"][0]["svg"].as_str().expect("svg");
let text: String = svg
.split('>')
.filter_map(|piece| piece.split('<').next())
.collect::<String>()
.split_whitespace()
.collect();
assert!(text.contains("Helloworld"), "{text}");
assert!(!text.contains("hidden"), "{text}");
}
#[test]
fn meaning_prints_macro_bodies() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\def\\greet#1{Hello #1}\n\n\\meaning\\greet and \\meaning\\missing.\n\n\\end{document}\n";