Print tokens literally with string
\string<token> was unsupported, so documenting a control sequence expanded it
instead of printing it. The expand loop now reads the following token and emits
its characters without expansion; backslash and braces are replaced by
private-use placeholders that the renderer maps back to the real characters, so
the printed text cannot be re-tokenised and expanded by a later pass.
A preview test defines \foo, prints \string\foo, \string{ and \string}, and
checks the literal "\foo and { and }." with no expansion of \foo and no
warnings.
This commit is contained in:
1 parent
57424015de
commit
47dcdfec47
4 files changed
+54
-3
No files matched your search
@@ -794,6 +794,24 @@ fn brace_depth_delta(line: &str) -> i32 {
|
||||
depth
|
||||
}
|
||||
|
||||
/// `\string<token>` prints the token's characters without expanding it.
|
||||
fn string_token(text: &str) -> Option<(String, usize)> {
|
||||
let after = text.strip_prefix("\\string")?;
|
||||
if after
|
||||
.chars()
|
||||
.next()
|
||||
.is_some_and(|character| character.is_ascii_alphabetic())
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let (token, used) = next_token(after)?;
|
||||
let escaped = token
|
||||
.replace('\\', "\u{E023}")
|
||||
.replace('{', "\u{E024}")
|
||||
.replace('}', "\u{E025}");
|
||||
Some((escaped, "\\string".len() + used))
|
||||
}
|
||||
|
||||
fn toks_register(text: &str) -> Option<(String, usize)> {
|
||||
let after = text.strip_prefix("\\toks")?;
|
||||
if after
|
||||
@@ -1674,6 +1692,12 @@ fn expand_macros_mut(
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
if let Some((text, used)) = string_token(rest) {
|
||||
output.push_str(&text);
|
||||
index += used;
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
if let Some((content, used)) = newtoks_declaration(rest) {
|
||||
if !tokens.iter().any(|(key, _)| key == &content) {
|
||||
tokens.push((content, String::new()));
|
||||
@@ -16003,6 +16027,28 @@ A definition.
|
||||
assert!(!text.contains("makeatletter"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn string_prints_tokens_literally() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\n\\def\\foo{bar}\n\n\\string\\foo and \\string{ and \\string}.\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 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("\\fooand{and}."), "{text}");
|
||||
assert!(!text.contains("bar"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn image_saveboxes_measure_the_image() {
|
||||
let mut images = HashMap::new();
|
||||
|
||||
Reference in new issue
Block a user