Capture body-level newif definitions
This commit is contained in:
1 parent
dc49b92a47
commit
13de893c46
3 files changed
+20
-4
No files matched your search
+18
-2
@@ -4196,12 +4196,21 @@ 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>) {
|
||||
fn capture_body_definitions(
|
||||
body: &mut String,
|
||||
macros: &mut Vec<MacroDefinition>,
|
||||
conditionals: &mut Vec<ConditionalDefinition>,
|
||||
) {
|
||||
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(name) = newif_definition(definition_line) {
|
||||
conditionals.push(ConditionalDefinition { name, value: false });
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
if let Some(definition) = macro_definition(definition_line) {
|
||||
macros.push(definition);
|
||||
changed = true;
|
||||
@@ -4251,7 +4260,7 @@ 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);
|
||||
capture_body_definitions(&mut body, &mut geometry.macros, &mut geometry.conditionals);
|
||||
dedupe_macros(&mut geometry.macros);
|
||||
let macros = geometry.macros.clone();
|
||||
body = expand_conditionals(
|
||||
@@ -12658,6 +12667,13 @@ See \ref{fig:a} and \ref{tab:b}.";
|
||||
assert_eq!(body.trim(), "A b.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn body_newif_defines_a_conditional() {
|
||||
let source = "\\newif\\ifdraft\n\\ifdraft Draft\\else Final\\fi";
|
||||
let (body, _geometry) = geometry_and_body(source);
|
||||
assert_eq!(body.trim(), "Final");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn body_definitions_expand_later_in_the_document() {
|
||||
let source = "\\def\\greet{Hello}\n\n\\greet world.";
|
||||
|
||||
Reference in new issue
Block a user