Run package document hooks and strip makeat

This commit is contained in:
gloamxun committed 2026-10-25 00:41:24 +08:00
1 parent e3e230042a
commit 3723036710
5 files changed
+105 -6

No files matched your search

+64 -2
View File
@@ -530,7 +530,45 @@ fn load_project_file(
let inherited = expand_class_inheritance(&expanded, store, id, loaded, depth, options);
apply_class_options(&inherited, options)
};
Some(sanitize_package(&processed))
let processed = expand_document_hooks(&processed);
Some(sanitize_package(&strip_makeat(&processed)))
}
fn expand_document_hooks(source: &str) -> String {
let mut output = String::new();
let mut rest = source;
while let Some(position) = rest
.find("\\AtBeginDocument")
.or_else(|| rest.find("\\AtEndDocument"))
{
output.push_str(&rest[..position]);
let command_end = position
+ if rest[position..].starts_with("\\AtBeginDocument") {
"\\AtBeginDocument".len()
} else {
"\\AtEndDocument".len()
};
let after = &rest[command_end..];
let trimmed = after.trim_start();
let Some(start) = after.len().checked_sub(trimmed.len()) else {
break;
};
match balanced_group_at(after, start) {
Some((body, consumed)) => {
output.push_str(&body);
rest = &after[consumed..];
}
None => rest = after,
}
}
output.push_str(rest);
output
}
fn strip_makeat(source: &str) -> String {
source
.replace("\\makeatletter", "")
.replace("\\makeatother", "")
}
fn expand_file_inputs(
@@ -1459,7 +1497,7 @@ fn ascii_file_name(name: &str) -> String {
#[cfg(test)]
mod class_option_tests {
use super::apply_class_options;
use super::{apply_class_options, expand_document_hooks, strip_makeat};
#[test]
fn declared_options_run_only_when_requested() {
@@ -1475,6 +1513,30 @@ mod class_option_tests {
);
}
#[test]
fn at_begin_document_runs_its_body() {
let source = "\\AtBeginDocument{\\newcommand{\\greet}{QZ}} tail";
let expanded = expand_document_hooks(source);
assert!(expanded.contains("\\newcommand{\\greet}{QZ}"));
assert!(!expanded.contains("AtBeginDocument"));
}
#[test]
fn at_end_document_is_dropped() {
assert_eq!(
expand_document_hooks("\\AtEndDocument{DONE}").trim(),
"DONE"
);
}
#[test]
fn makeat_commands_are_removed() {
assert_eq!(
strip_makeat("\\makeatletter\\def\\x{A}\\makeatother"),
"\\def\\x{A}"
);
}
#[test]
fn execute_options_supply_defaults() {
let source = "\\DeclareOption{plain}{PLAIN}\n\\DeclareOption{boxed}{BOX}\n\\ExecuteOptions{plain}\n\\ProcessOptions";