Test declared macros with ifdefined
This commit is contained in:
1 parent
fa93b198bf
commit
206591c725
4 files changed
+40
-13
No files matched your search
+36
-9
@@ -1002,6 +1002,7 @@ fn conditional_at(
|
||||
text: &str,
|
||||
index: usize,
|
||||
flags: &[ConditionalDefinition],
|
||||
macros: &[MacroDefinition],
|
||||
) -> Option<(bool, usize)> {
|
||||
let (command, consumed) = control_sequence(text.get(index..)?)?;
|
||||
let word = command.strip_prefix('\\')?;
|
||||
@@ -1011,6 +1012,14 @@ fn conditional_at(
|
||||
if word == "iffalse" {
|
||||
return Some((false, consumed));
|
||||
}
|
||||
if word == "ifdefined" {
|
||||
let after = text.get(index + consumed..)?;
|
||||
let trimmed = after.trim_start();
|
||||
let skipped = after.len() - trimmed.len();
|
||||
let (command, command_consumed) = control_sequence(trimmed)?;
|
||||
let defined = macros.iter().any(|definition| definition.name == command);
|
||||
return Some((defined, consumed + skipped + command_consumed));
|
||||
}
|
||||
let name = word.strip_prefix("if")?;
|
||||
let flag = flags.iter().find(|flag| flag.name == name)?;
|
||||
Some((flag.value, consumed))
|
||||
@@ -1042,13 +1051,18 @@ fn conditional_end(text: &str, start: usize) -> Option<(Option<usize>, usize)> {
|
||||
None
|
||||
}
|
||||
|
||||
fn expand_conditionals(source: &str, flags: &mut [ConditionalDefinition]) -> String {
|
||||
expand_conditionals_at(source, flags, 0)
|
||||
fn expand_conditionals(
|
||||
source: &str,
|
||||
flags: &mut [ConditionalDefinition],
|
||||
macros: &[MacroDefinition],
|
||||
) -> String {
|
||||
expand_conditionals_at(source, flags, macros, 0)
|
||||
}
|
||||
|
||||
fn expand_conditionals_at(
|
||||
source: &str,
|
||||
flags: &mut [ConditionalDefinition],
|
||||
macros: &[MacroDefinition],
|
||||
depth: usize,
|
||||
) -> String {
|
||||
if depth > 8 {
|
||||
@@ -1066,7 +1080,7 @@ fn expand_conditionals_at(
|
||||
index = position + consumed;
|
||||
continue;
|
||||
}
|
||||
if let Some((value, consumed)) = conditional_at(source, position, flags) {
|
||||
if let Some((value, consumed)) = conditional_at(source, position, flags, macros) {
|
||||
let body_start = position + consumed;
|
||||
if let Some((else_at, fi_at)) = conditional_end(source, body_start) {
|
||||
let (chosen, after) = match else_at {
|
||||
@@ -1086,7 +1100,8 @@ fn expand_conditionals_at(
|
||||
(chosen, fi_at + 3)
|
||||
}
|
||||
};
|
||||
output.push_str(&expand_conditionals_at(chosen, flags, depth + 1));
|
||||
let chosen = chosen.trim_start();
|
||||
output.push_str(&expand_conditionals_at(chosen, flags, macros, depth + 1));
|
||||
index = after;
|
||||
continue;
|
||||
}
|
||||
@@ -1947,9 +1962,11 @@ 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_macros(&body, &geometry.macros);
|
||||
let macros = geometry.macros.clone();
|
||||
body = expand_conditionals(&body, &mut geometry.conditionals, ¯os);
|
||||
body = expand_macros(&body, ¯os);
|
||||
body = expand_links(&body);
|
||||
body = expand_conditionals(&body, &mut geometry.conditionals);
|
||||
body = expand_conditionals(&body, &mut geometry.conditionals, ¯os);
|
||||
let today = today_string();
|
||||
body = expand_today(&body, &today);
|
||||
(body, geometry)
|
||||
@@ -8115,6 +8132,16 @@ a & b
|
||||
assert_eq!(body, "12");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ifdefined_checks_declared_macros() {
|
||||
let (body, _) = geometry_and_body(
|
||||
"\\def\\known{}
|
||||
|
||||
\\ifdefined\\known yes\\else no\\fi and \\ifdefined\\missing yes\\else no\\fi",
|
||||
);
|
||||
assert_eq!(body, "yes and no");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn page_numbers_follow_the_page_counter() {
|
||||
let offset = preview_json("\\setcounter{page}{5}\n\nAlpha\n\n\\newpage\n\nBeta")
|
||||
@@ -8769,7 +8796,7 @@ See \ref{fig:a} and \ref{tab:b}.";
|
||||
let source =
|
||||
"\\newif\\ifdraft\n\nA\\ifdraft X\\else Y\\fi B\\drafttrue C\\ifdraft Z\\else W\\fi D";
|
||||
let (body, _) = geometry_and_body(source);
|
||||
assert_eq!(body.trim(), "A Y B C Z D");
|
||||
assert_eq!(body.trim(), "AY B CZ D");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -8777,7 +8804,7 @@ See \ref{fig:a} and \ref{tab:b}.";
|
||||
let source =
|
||||
"\\newif\\ifdraft\n\\drafttrue\n\n\\ifdraft A\\ifdraft B\\else C\\fi\\else D\\fi";
|
||||
let (body, _) = geometry_and_body(source);
|
||||
assert_eq!(body.trim(), "A B");
|
||||
assert_eq!(body.trim(), "AB");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -8799,7 +8826,7 @@ See \ref{fig:a} and \ref{tab:b}.";
|
||||
fn iftrue_and_iffalse_select_without_declarations() {
|
||||
let source = "\\iftrue Yes\\else No\\fi and\\iffalse A\\else B\\fi";
|
||||
let (body, _) = geometry_and_body(source);
|
||||
assert_eq!(body.trim(), "Yes and B");
|
||||
assert_eq!(body.trim(), "Yes andB");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user