Inline required local packages recursively

This commit is contained in:
gloamxun committed 2026-10-24 22:32:41 +08:00
1 parent 64325d4b87
commit f48b10340a
5 files changed
+121 -25

No files matched your search

+69 -21
View File
@@ -411,35 +411,33 @@ const MAX_UPLOAD_BYTES: usize = 16 * 1024 * 1024;
fn expand_project_packages(source: &str, store: &projects::ProjectStore, id: &str) -> String {
let mut output = String::new();
let mut loaded = Vec::new();
for line in source.lines() {
if line.trim_start().starts_with("\\usepackage") {
if let Some(names) = package_names(line.trim()) {
let trimmed = line.trim();
if trimmed.starts_with("\\usepackage") {
if let Some(names) = package_names(trimmed) {
let mut expanded = String::new();
let mut replaced = false;
let mut packages = String::new();
for name in names {
if let Ok(bytes) = store.read_file(id, &format!("{name}.sty")) {
if let Ok(text) = String::from_utf8(bytes) {
packages.push_str(&sanitize_package(&text));
packages.push('\n');
replaced = true;
}
if let Some(text) = load_project_file(&name, "sty", store, id, &mut loaded, 1) {
expanded.push_str(&text);
expanded.push('\n');
replaced = true;
}
}
if replaced {
output.push_str(&packages);
output.push_str(&expanded);
continue;
}
}
} else if line.trim_start().starts_with("\\documentclass") {
if let Some((options, name)) = documentclass_parts(line.trim()) {
if let Ok(bytes) = store.read_file(id, &format!("{name}.cls")) {
if let Ok(text) = String::from_utf8(bytes) {
output.push_str(&format!("\\documentclass{}{{{}}}", options, "article"));
output.push('\n');
output.push_str(&sanitize_package(&text));
output.push('\n');
continue;
}
} else if trimmed.starts_with("\\documentclass") {
if let Some((options, name)) = documentclass_parts(trimmed) {
if let Some(text) = load_project_file(&name, "cls", store, id, &mut loaded, 1) {
output.push_str(&format!("\\documentclass{}{{{}}}", options, "article"));
output.push('\n');
output.push_str(&text);
output.push('\n');
continue;
}
}
}
@@ -449,8 +447,58 @@ fn expand_project_packages(source: &str, store: &projects::ProjectStore, id: &st
output
}
fn load_project_file(
name: &str,
extension: &str,
store: &projects::ProjectStore,
id: &str,
loaded: &mut Vec<String>,
depth: usize,
) -> Option<String> {
if depth > 8 || loaded.iter().any(|loaded| loaded == name) {
return None;
}
let bytes = store.read_file(id, &format!("{name}.{extension}")).ok()?;
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(&expanded))
}
fn expand_required_packages(
source: &str,
store: &projects::ProjectStore,
id: &str,
loaded: &mut Vec<String>,
depth: usize,
) -> String {
let mut output = String::new();
for line in source.lines() {
let trimmed = line.trim();
if trimmed.starts_with("\\RequirePackage") || trimmed.starts_with("\\usepackage") {
if let Some(names) = package_names(trimmed) {
for name in names {
if let Some(text) =
load_project_file(&name, "sty", store, id, loaded, depth + 1)
{
output.push_str(&text);
output.push('\n');
}
}
}
continue;
}
output.push_str(line);
output.push('\n');
}
output
}
fn package_names(line: &str) -> Option<Vec<String>> {
let mut rest = line.strip_prefix("\\usepackage")?.trim_start();
let mut rest = line
.strip_prefix("\\usepackage")
.or_else(|| line.strip_prefix("\\RequirePackage"))?
.trim_start();
if rest.starts_with('[') {
let close = rest.find(']')?;
rest = rest[close + 1..].trim_start();