Reuse savebox content through usebox
This commit is contained in:
1 parent
c57de1e03c
commit
6c771e0820
3 files changed
+127
-2
No files matched your search
+125
@@ -1987,6 +1987,13 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if trimmed.starts_with("\\newsavebox")
|
||||
|| trimmed.starts_with("\\sbox")
|
||||
|| trimmed.starts_with("\\savebox")
|
||||
{
|
||||
handle_savebox_line(trimmed, &mut geometry.macros);
|
||||
continue;
|
||||
}
|
||||
if let Some(rest) = trimmed.strip_prefix("\\newcount") {
|
||||
if let Some((command, _)) = control_sequence(rest.trim_start()) {
|
||||
let name = command.trim_start_matches('\\').to_string();
|
||||
@@ -3800,6 +3807,103 @@ fn dedupe_macros(macros: &mut Vec<MacroDefinition>) {
|
||||
*macros = result;
|
||||
}
|
||||
|
||||
/// Replace `\usebox{name}` with stored content and store `\sbox`/`\savebox` bodies.
|
||||
fn expand_saveboxes(body: &str, macros: &mut Vec<MacroDefinition>) -> String {
|
||||
let mut out = String::new();
|
||||
let mut index = 0;
|
||||
while index < body.len() {
|
||||
let rest = &body[index..];
|
||||
if let Some(after) = rest.strip_prefix("\\usebox") {
|
||||
let after = after.trim_start();
|
||||
if let Some((name, used)) = braced_group(after) {
|
||||
let name = format!("\\{}", name.trim().trim_start_matches('\\'));
|
||||
if let Some(definition) = macros.iter().find(|m| m.name == name) {
|
||||
out.push_str(&definition.body);
|
||||
}
|
||||
index += rest.len() - after.len() + used;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if rest.starts_with("\\sbox") || rest.starts_with("\\savebox") {
|
||||
let after = rest
|
||||
.strip_prefix("\\sbox")
|
||||
.or_else(|| rest.strip_prefix("\\savebox"))
|
||||
.unwrap_or("");
|
||||
let after = after.trim_start();
|
||||
if let Some((name, used)) = braced_group(after) {
|
||||
let body_rest = after[used..].trim_start();
|
||||
if let Some((content, used_body)) = braced_group(body_rest) {
|
||||
let name = format!("\\{}", name.trim().trim_start_matches('\\'));
|
||||
if let Some(existing) = macros.iter_mut().find(|m| m.name == name) {
|
||||
existing.body = content.to_string();
|
||||
existing.params = None;
|
||||
} else {
|
||||
macros.push(MacroDefinition {
|
||||
name,
|
||||
args: 0,
|
||||
default: None,
|
||||
params: None,
|
||||
body: content.to_string(),
|
||||
});
|
||||
}
|
||||
index += rest.len() - body_rest.len() + used_body;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
let character = rest.chars().next().expect("character");
|
||||
out.push(character);
|
||||
index += character.len_utf8();
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Register the content of a `\sbox`/`\savebox` or reserve a `\newsavebox` name.
|
||||
fn handle_savebox_line(line: &str, macros: &mut Vec<MacroDefinition>) {
|
||||
let rest = line.trim_start();
|
||||
if let Some(after) = rest.strip_prefix("\\newsavebox") {
|
||||
let after = after.trim_start();
|
||||
if let Some((name, _)) = braced_group(after) {
|
||||
let name = format!("\\{}", name.trim().trim_start_matches('\\'));
|
||||
if !macros.iter().any(|m| m.name == name) {
|
||||
macros.push(MacroDefinition {
|
||||
name,
|
||||
args: 0,
|
||||
default: None,
|
||||
params: None,
|
||||
body: String::new(),
|
||||
});
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
let after = rest
|
||||
.strip_prefix("\\sbox")
|
||||
.or_else(|| rest.strip_prefix("\\savebox"));
|
||||
let Some(after) = after else { return };
|
||||
let after = after.trim_start();
|
||||
let Some((name, used)) = braced_group(after) else {
|
||||
return;
|
||||
};
|
||||
let body_rest = after[used..].trim_start();
|
||||
let Some((body, _)) = braced_group(body_rest) else {
|
||||
return;
|
||||
};
|
||||
let name = format!("\\{}", name.trim().trim_start_matches('\\'));
|
||||
if let Some(existing) = macros.iter_mut().find(|m| m.name == name) {
|
||||
existing.body = body.to_string();
|
||||
existing.params = None;
|
||||
} else {
|
||||
macros.push(MacroDefinition {
|
||||
name,
|
||||
args: 0,
|
||||
default: None,
|
||||
params: None,
|
||||
body: body.to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn geometry_and_body(source: &str) -> (String, PageGeometry) {
|
||||
let (stripped, mut geometry) = strip_page_geometry(source);
|
||||
dedupe_macros(&mut geometry.macros);
|
||||
@@ -3808,6 +3912,7 @@ fn geometry_and_body(source: &str) -> (String, PageGeometry) {
|
||||
let mut body = latex_body(&stripped).trim_matches('\n').to_string();
|
||||
geometry.bibliography = extract_bibliography(&mut body);
|
||||
body = expand_environments(&body, &geometry.environments);
|
||||
body = expand_saveboxes(&body, &mut geometry.macros);
|
||||
let macros = geometry.macros.clone();
|
||||
body = expand_conditionals(
|
||||
&body,
|
||||
@@ -13198,6 +13303,26 @@ A definition.
|
||||
assert_eq!(runs[0].3, "Boxed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn saveboxes_reuse_their_content() {
|
||||
let source = "\\newsavebox{\\boxa}\n\\sbox{\\boxa}{Stored}\n\nFirst \\usebox{\\boxa}.";
|
||||
let (body, geometry) = geometry_and_body(source);
|
||||
assert_eq!(geometry.macros.len(), 1);
|
||||
let blocks = paragraphs(&body).expect("paragraphs");
|
||||
assert_eq!(blocks[0].text, "First Stored.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn saveboxes_defined_in_the_body_work() {
|
||||
let units =
|
||||
paragraphs("\\sbox{\\boxb}{In body}\n\nSee \\usebox{\\boxb}.").expect("paragraphs");
|
||||
let _ = units;
|
||||
let (body, _geometry) =
|
||||
geometry_and_body("\\sbox{\\boxb}{In body}\n\nSee \\usebox{\\boxb}.");
|
||||
let blocks = paragraphs(&body).expect("paragraphs");
|
||||
assert_eq!(blocks[0].text, "See In body.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn named_spaces_reach_the_layout() {
|
||||
let blocks = paragraphs("A\\enspace B").expect("paragraphs");
|
||||
|
||||
Reference in new issue
Block a user