Capture definitions written in the body
This commit is contained in:
1 parent
1d891b2c6a
commit
6efb210781
3 files changed
+37
-2
No files matched your search
@@ -4195,6 +4195,26 @@ fn handle_savebox_line(line: &str, macros: &mut Vec<MacroDefinition>) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Capture `\def` lines written in the document body, before macro expansion.
|
||||
fn capture_body_definitions(body: &mut String, macros: &mut Vec<MacroDefinition>) {
|
||||
let mut kept = String::new();
|
||||
let mut changed = false;
|
||||
for line in body.lines() {
|
||||
let trimmed = line.trim_start();
|
||||
let definition_line = strip_definition_prefixes(trimmed);
|
||||
if let Some(definition) = macro_definition(definition_line) {
|
||||
macros.push(definition);
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
kept.push_str(line);
|
||||
kept.push('\n');
|
||||
}
|
||||
if changed {
|
||||
*body = kept.trim_end_matches('\n').to_string();
|
||||
}
|
||||
}
|
||||
|
||||
fn geometry_and_body(source: &str) -> (String, PageGeometry) {
|
||||
let (stripped, mut geometry) = strip_page_geometry(source);
|
||||
dedupe_macros(&mut geometry.macros);
|
||||
@@ -4204,6 +4224,8 @@ fn geometry_and_body(source: &str) -> (String, PageGeometry) {
|
||||
geometry.bibliography = extract_bibliography(&mut body);
|
||||
body = expand_environments(&body, &geometry.environments);
|
||||
body = expand_saveboxes(&body, &mut geometry.macros);
|
||||
capture_body_definitions(&mut body, &mut geometry.macros);
|
||||
dedupe_macros(&mut geometry.macros);
|
||||
let macros = geometry.macros.clone();
|
||||
body = expand_conditionals(
|
||||
&body,
|
||||
@@ -12589,6 +12611,19 @@ See \ref{fig:a} and \ref{tab:b}.";
|
||||
assert_eq!(body.trim(), "loaded missing");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn body_definitions_expand_later_in_the_document() {
|
||||
let source = "\\def\\greet{Hello}\n\n\\greet world.";
|
||||
let (body, geometry) = geometry_and_body(source);
|
||||
assert_eq!(body.trim(), "Hello world.");
|
||||
assert!(
|
||||
geometry
|
||||
.macros
|
||||
.iter()
|
||||
.any(|definition| definition.name == "\\greet")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn newenvironment_optional_arguments_use_their_default() {
|
||||
let definition = environment_definition("\\newenvironment{note}[1][Default]{(#1)}{!}")
|
||||
|
||||
Reference in new issue
Block a user