Accept long global and protected definition prefixes
This commit is contained in:
1 parent
c95cfa5967
commit
cb00e21127
4 files changed
+65
-11
No files matched your search
+59
-7
@@ -409,10 +409,37 @@ fn expand_csnames(text: &str) -> String {
|
||||
result
|
||||
}
|
||||
|
||||
fn strip_definition_prefixes(line: &str) -> &str {
|
||||
let mut rest = line.trim_start();
|
||||
if rest.starts_with(r"\protected@") {
|
||||
return rest;
|
||||
}
|
||||
loop {
|
||||
let mut changed = false;
|
||||
for prefix in [r"\long", r"\global", r"\outer", r"\protected"] {
|
||||
if let Some(after) = rest.strip_prefix(prefix)
|
||||
&& !after
|
||||
.chars()
|
||||
.next()
|
||||
.is_some_and(|character| character.is_alphabetic())
|
||||
{
|
||||
rest = after.trim_start();
|
||||
changed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if !changed {
|
||||
return rest;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn edef_definition(line: &str, macros: &[MacroDefinition]) -> Option<MacroDefinition> {
|
||||
let rest = line
|
||||
.strip_prefix(r"\edef")
|
||||
.or_else(|| line.strip_prefix(r"\xdef"))?
|
||||
.or_else(|| line.strip_prefix(r"\xdef"))
|
||||
.or_else(|| line.strip_prefix(r"\protected@edef"))
|
||||
.or_else(|| line.strip_prefix(r"\protected@xdef"))?
|
||||
.trim_start();
|
||||
let (name, consumed) = control_sequence(rest)?;
|
||||
let rest = &rest[consumed..];
|
||||
@@ -441,7 +468,10 @@ fn macro_alias(line: &str) -> Option<(String, String)> {
|
||||
}
|
||||
|
||||
fn macro_definition(line: &str) -> Option<MacroDefinition> {
|
||||
if let Some(rest) = line.strip_prefix("\\def") {
|
||||
if let Some(rest) = line
|
||||
.strip_prefix("\\def")
|
||||
.or_else(|| line.strip_prefix("\\gdef"))
|
||||
{
|
||||
let rest = rest.trim_start();
|
||||
let (name, consumed) = control_sequence(rest)?;
|
||||
let rest = &rest[consumed..];
|
||||
@@ -1848,8 +1878,13 @@ 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) {
|
||||
let definition_line = strip_definition_prefixes(trimmed);
|
||||
if definition_line.starts_with(r"\edef")
|
||||
|| definition_line.starts_with(r"\xdef")
|
||||
|| definition_line.starts_with(r"\protected@edef")
|
||||
|| definition_line.starts_with(r"\protected@xdef")
|
||||
{
|
||||
if let Some(definition) = edef_definition(definition_line, &geometry.macros) {
|
||||
geometry.macros.push(definition);
|
||||
}
|
||||
continue;
|
||||
@@ -1871,7 +1906,7 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
}
|
||||
}
|
||||
let is_macro_command = |command: &str| {
|
||||
trimmed.strip_prefix(command).is_some_and(|rest| {
|
||||
definition_line.strip_prefix(command).is_some_and(|rest| {
|
||||
rest.starts_with('{')
|
||||
|| rest.starts_with('\\')
|
||||
|| rest.chars().next().is_some_and(char::is_whitespace)
|
||||
@@ -1881,8 +1916,9 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
|| is_macro_command("\\renewcommand")
|
||||
|| is_macro_command("\\providecommand")
|
||||
|| is_macro_command("\\def")
|
||||
|| is_macro_command("\\gdef")
|
||||
{
|
||||
if let Some(definition) = macro_definition(trimmed) {
|
||||
if let Some(definition) = macro_definition(definition_line) {
|
||||
geometry.macros.push(definition);
|
||||
}
|
||||
continue;
|
||||
@@ -1897,7 +1933,7 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if let Some((name, target)) = macro_alias(trimmed) {
|
||||
if let Some((name, target)) = macro_alias(definition_line) {
|
||||
let definition = geometry
|
||||
.macros
|
||||
.iter()
|
||||
@@ -9688,6 +9724,22 @@ a & b
|
||||
assert!(body.contains("[[x] and no y."), "{body}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn definition_prefixes_are_transparent() {
|
||||
let (body, geometry) = geometry_and_body(
|
||||
"\\long\\def\\a{A}\n\\global\\def\\b{B}\n\\gdef\\c{C}\n\\protected@edef\\d{\\a\\b}\n\n\\a \\b \\c \\d",
|
||||
);
|
||||
assert_eq!(body, "A B C AB");
|
||||
assert_eq!(geometry.macros.len(), 4);
|
||||
assert!(!body.contains("long"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn global_let_copies_a_definition() {
|
||||
let (body, _) = geometry_and_body("\\def\\x{X}\n\\global\\let\\y=\\x\n\n\\y");
|
||||
assert_eq!(body, "X");
|
||||
}
|
||||
|
||||
#[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