Execute option declarations in local packages

This commit is contained in:
gloamxun committed 2026-10-24 23:18:28 +08:00
1 parent 9b44d68440
commit 00f95fdd09
5 files changed
+97 -54

No files matched your search

+34 -49
View File
@@ -435,11 +435,14 @@ fn expand_project_packages(source: &str, store: &projects::ProjectStore, id: &st
for line in source.lines() {
let trimmed = line.trim();
if trimmed.starts_with("\\usepackage") {
if let Some(names) = package_names(trimmed) {
if let Some((options, names)) = use_package_parts(trimmed) {
let mut expanded = String::new();
let mut replaced = false;
let requested = split_options(&options);
for name in names {
if let Some(text) = load_project_file(&name, "sty", store, id, &mut loaded, 1) {
if let Some(text) =
load_project_file(&name, "sty", &requested, store, id, &mut loaded, 1)
{
expanded.push_str(&text);
expanded.push('\n');
replaced = true;
@@ -479,6 +482,7 @@ fn expand_project_packages(source: &str, store: &projects::ProjectStore, id: &st
fn load_project_file(
name: &str,
extension: &str,
options: &[String],
store: &projects::ProjectStore,
id: &str,
loaded: &mut Vec<String>,
@@ -491,7 +495,13 @@ 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);
Some(sanitize_package(&expanded))
let processed = if extension == "sty" {
apply_class_options(&expanded, options)
} else {
let inherited = expand_class_inheritance(&expanded, store, id, loaded, depth, options);
apply_class_options(&inherited, options)
};
Some(sanitize_package(&processed))
}
fn expand_required_packages(
@@ -505,10 +515,11 @@ fn expand_required_packages(
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) {
if let Some((options, names)) = use_package_parts(trimmed) {
let requested = split_options(&options);
for name in names {
if let Some(text) =
load_project_file(&name, "sty", store, id, loaded, depth + 1)
load_project_file(&name, "sty", &requested, store, id, loaded, depth + 1)
{
output.push_str(&text);
output.push('\n');
@@ -523,24 +534,38 @@ fn expand_required_packages(
output
}
fn package_names(line: &str) -> Option<Vec<String>> {
fn split_options(options: &str) -> Vec<String> {
options
.trim_start_matches('[')
.trim_end_matches(']')
.split(',')
.map(str::trim)
.filter(|option| !option.is_empty())
.map(str::to_string)
.collect()
}
fn use_package_parts(line: &str) -> Option<(String, Vec<String>)> {
let mut rest = line
.strip_prefix("\\usepackage")
.or_else(|| line.strip_prefix("\\RequirePackage"))?
.trim_start();
let mut options = String::new();
if rest.starts_with('[') {
let close = rest.find(']')?;
options = rest[..=close].to_string();
rest = rest[close + 1..].trim_start();
}
let rest = rest.strip_prefix('{')?;
let close = rest.find('}')?;
Some(
Some((
options,
rest[..close]
.split(',')
.map(|name| name.trim().to_string())
.filter(|name| !name.is_empty())
.collect(),
)
))
}
fn documentclass_parts(line: &str) -> Option<(String, String)> {
@@ -756,12 +781,7 @@ fn load_project_class(
if depth > 8 || loaded.iter().any(|loaded| loaded == name) {
return None;
}
let bytes = store.read_file(id, &format!("{name}.cls")).ok()?;
let text = String::from_utf8(bytes).ok()?;
loaded.push(name.to_string());
let expanded = expand_required_packages(&text, store, id, loaded, depth);
let inherited = expand_class_inheritance(&expanded, store, id, loaded, depth, options);
Some(sanitize_package(&apply_class_options(&inherited, options)))
load_project_file(name, "cls", options, store, id, loaded, depth)
}
fn route_d_pdf_cache() -> &'static Mutex<Vec<((String, u64), Vec<u8>)>> {
@@ -965,41 +985,6 @@ fn handle_project_request(
if let Some(json) = route_d_cached_preview(&key) {
return respond_json(request, 200, &preview_without_changes(&json));
}
fn replace_number_field(json: &str, field: &str, value: u64) -> String {
let Some(start) = json.find(field) else {
return json.to_string();
};
let Some(colon) = json[start..].find(':') else {
return json.to_string();
};
let value_start = start + colon + 1;
let rest = &json[value_start..];
let Some(end) = rest.find([',', '}']) else {
return json.to_string();
};
format!("{}{value}{}", &json[..value_start], &rest[end..])
}
fn replace_array_field(json: &str, field: &str) -> String {
let Some(start) = json.find(field) else {
return json.to_string();
};
let Some(open_rel) = json[start..].find('[') else {
return json.to_string();
};
let open = start + open_rel;
let Some(close_rel) = json[open..].find(']') else {
return json.to_string();
};
let close = open + close_rel;
format!("{}[]{}", &json[..open], &json[close + 1..])
}
fn preview_without_changes(json: &str) -> String {
let json = replace_number_field(json, "\"recomputedPages\"", 0);
replace_array_field(&json, "\"changedPages\"")
}
return match route_d::project_preview_json(id, &expanded) {
Ok(json) => {
store_route_d_preview(key, json.clone());