Inline input files inside project classes and packages

This commit is contained in:
gloamxun committed 2026-10-24 23:47:57 +08:00
1 parent cca108dc15
commit 04446e673f
5 files changed
+101 -4

No files matched your search

+57
View File
@@ -509,6 +509,7 @@ fn load_project_file(
let text = String::from_utf8(bytes).ok()?;
loaded.push(name.to_string());
let expanded = expand_required_packages(&text, store, id, loaded, depth);
let expanded = expand_file_inputs(&expanded, store, id, depth);
let processed = if extension == "sty" {
apply_class_options(&expanded, options)
} else {
@@ -518,6 +519,62 @@ fn load_project_file(
Some(sanitize_package(&processed))
}
fn expand_file_inputs(
source: &str,
store: &projects::ProjectStore,
id: &str,
depth: usize,
) -> String {
if depth > 8 {
return String::new();
}
let mut output = String::new();
let mut rest = source;
loop {
let input = rest.find("\\input{");
let include = rest.find("\\include{");
let found = match (input, include) {
(Some(left), Some(right)) => Some(left.min(right)),
(Some(position), None) | (None, Some(position)) => Some(position),
(None, None) => None,
};
let Some(position) = found else {
output.push_str(rest);
break;
};
output.push_str(&rest[..position]);
let after = &rest[position..];
let open = after.find('{').expect("input brace");
let Some(close) = after[open + 1..].find('}') else {
output.push_str(after);
break;
};
let name = after[open + 1..open + 1 + close].trim();
if let Some(text) = read_project_tex(store, id, name) {
output.push_str(&expand_file_inputs(&text, store, id, depth + 1));
output.push('\n');
}
rest = &after[open + 1 + close + 1..];
}
output
}
fn read_project_tex(store: &projects::ProjectStore, id: &str, name: &str) -> Option<String> {
for candidate in [
name.to_string(),
format!("{name}.tex"),
format!("{name}.clo"),
format!("{name}.sty"),
] {
if let Ok(bytes) = store.read_file(id, &candidate) {
if let Ok(text) = String::from_utf8(bytes) {
return Some(text);
}
}
}
None
}
fn expand_required_packages(
source: &str,
store: &projects::ProjectStore,