Expand edef bodies and csname sequences at definition time
This commit is contained in:
1 parent
028137a9ad
commit
863f3e1b80
4 files changed
+77
-5
No files matched your search
@@ -369,6 +369,51 @@ fn definition_argument_count(params: &str) -> Option<u8> {
|
||||
Some(count)
|
||||
}
|
||||
|
||||
fn expand_csnames(text: &str) -> String {
|
||||
let mut result = String::with_capacity(text.len());
|
||||
let mut rest = text;
|
||||
while let Some(index) = rest.find(r"\csname") {
|
||||
result.push_str(&rest[..index]);
|
||||
let after = &rest[index + r"\csname".len()..];
|
||||
let Some(end) = after.find(r"\endcsname") else {
|
||||
result.push_str(&rest[index..]);
|
||||
return result;
|
||||
};
|
||||
let name = after[..end].trim();
|
||||
if name.is_empty() {
|
||||
result.push_str(r"\relax");
|
||||
} else {
|
||||
result.push('\\');
|
||||
result.push_str(name);
|
||||
}
|
||||
rest = &after[end + r"\endcsname".len()..];
|
||||
}
|
||||
result.push_str(rest);
|
||||
result
|
||||
}
|
||||
|
||||
fn edef_definition(line: &str, macros: &[MacroDefinition]) -> Option<MacroDefinition> {
|
||||
let rest = line
|
||||
.strip_prefix(r"\edef")
|
||||
.or_else(|| line.strip_prefix(r"\xdef"))?
|
||||
.trim_start();
|
||||
let (name, consumed) = control_sequence(rest)?;
|
||||
let rest = &rest[consumed..];
|
||||
let open = rest.find('{')?;
|
||||
let args = definition_argument_count(&rest[..open])?;
|
||||
let (body, _) = take_group_after_open(&rest[open + 1..])?;
|
||||
let mut body = expand_csnames(&body);
|
||||
if args == 0 {
|
||||
body = expand_macros(&body, macros);
|
||||
}
|
||||
Some(MacroDefinition {
|
||||
name,
|
||||
args,
|
||||
default: None,
|
||||
body,
|
||||
})
|
||||
}
|
||||
|
||||
fn macro_alias(line: &str) -> Option<(String, String)> {
|
||||
let rest = line.strip_prefix("\\let")?.trim_start();
|
||||
let (name, consumed) = control_sequence(rest)?;
|
||||
@@ -1306,6 +1351,12 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if trimmed.starts_with(r"\edef") || trimmed.starts_with(r"\xdef") {
|
||||
if let Some(definition) = edef_definition(trimmed, &geometry.macros) {
|
||||
geometry.macros.push(definition);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if trimmed.starts_with("\\newcommand")
|
||||
|| trimmed.starts_with("\\renewcommand")
|
||||
|| trimmed.starts_with("\\providecommand")
|
||||
@@ -7990,6 +8041,27 @@ a & b
|
||||
assert!(!json.contains("pageref"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn edef_expands_at_definition_time() {
|
||||
let (body, geometry) =
|
||||
geometry_and_body("\\def\\a{A}\n\\edef\\b{\\a B}\n\\def\\a{C}\n\nBody \\b.");
|
||||
assert_eq!(body, "Body A B.");
|
||||
assert_eq!(geometry.macros.len(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn csname_resolves_at_expansion_time() {
|
||||
let (body, _) =
|
||||
geometry_and_body("\\def\\foo{Z}\n\\edef\\cs{\\csname foo\\endcsname}\n\n\\cs.");
|
||||
assert_eq!(body, "Z.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xdef_is_an_edef_alias() {
|
||||
let (body, _) = geometry_and_body("\\def\\x{1}\n\\xdef\\y{\\x}\n\\def\\x{2}\n\n\\y");
|
||||
assert_eq!(body, "1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn page_numbers_follow_the_page_counter() {
|
||||
let offset = preview_json("\\setcounter{page}{5}\n\nAlpha\n\n\\newpage\n\nBeta")
|
||||
|
||||
Reference in new issue
Block a user