Inline local base classes from LoadClass

This commit is contained in:
gloamxun committed 2026-10-24 23:14:01 +08:00
1 parent 4b0e4eba0c
commit fe9e93f796
5 files changed
+101 -6

No files matched your search

+54 -1
View File
@@ -693,6 +693,58 @@ fn apply_class_options(source: &str, requested: &[String]) -> String {
output
}
/// Inlines local base classes requested by `\LoadClass` /
/// `\LoadClassWithOptions` inside a class file. Requests for classes that
/// are not part of the project keep LaTeX's article fallback, so the line is
/// dropped rather than leaking into the document body.
fn expand_class_inheritance(
source: &str,
store: &projects::ProjectStore,
id: &str,
loaded: &mut Vec<String>,
depth: usize,
inherited_options: &[String],
) -> String {
let mut output = String::new();
for line in source.lines() {
let trimmed = line.trim();
let load = trimmed
.strip_prefix("\\LoadClassWithOptions")
.map(|rest| (rest, inherited_options.to_vec()))
.or_else(|| {
trimmed
.strip_prefix("\\LoadClass")
.map(|rest| (rest, Vec::new()))
});
if let Some((mut rest, mut options)) = load {
rest = rest.trim_start();
if rest.starts_with('[') {
if let Some(close) = rest.find(']') {
options = rest[1..close]
.split(',')
.map(str::trim)
.filter(|option| !option.is_empty())
.map(str::to_string)
.collect();
rest = rest[close + 1..].trim_start();
}
}
if let Some((name, _)) = balanced_group_at(rest, 0) {
if let Some(text) =
load_project_class(name.trim(), &options, store, id, loaded, depth + 1)
{
output.push_str(&text);
output.push('\n');
}
continue;
}
}
output.push_str(line);
output.push('\n');
}
output
}
fn load_project_class(
name: &str,
options: &[String],
@@ -708,7 +760,8 @@ fn load_project_class(
let text = String::from_utf8(bytes).ok()?;
loaded.push(name.to_string());
let expanded = expand_required_packages(&text, store, id, loaded, depth);
Some(sanitize_package(&apply_class_options(&expanded, options)))
let inherited = expand_class_inheritance(&expanded, store, id, loaded, depth, options);
Some(sanitize_package(&apply_class_options(&inherited, options)))
}
fn route_d_pdf_cache() -> &'static Mutex<Vec<((String, u64), Vec<u8>)>> {