Merge passed package and class options
Preamble \PassOptionsToPackage and \PassOptionsToClass declarations are collected and merged into the next \usepackage or \documentclass request, so the shared option lists in article-class projects reach the local class and package option protocol.
This commit is contained in:
1 parent
6aa9d70a49
commit
9195d090de
5 files changed
+122
-13
No files matched your search
+73
-9
@@ -446,14 +446,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();
|
||||
let mut passed_packages: Vec<(String, Vec<String>)> = Vec::new();
|
||||
let mut passed_classes: Vec<(String, Vec<String>)> = Vec::new();
|
||||
for line in source.lines() {
|
||||
let trimmed = line.trim();
|
||||
if trimmed.starts_with("\\PassOptionsToPackage") {
|
||||
if let Some((options, names)) = pass_options_parts(trimmed, "\\PassOptionsToPackage") {
|
||||
for name in names {
|
||||
record_passed_options(&mut passed_packages, &name, &options);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
} else if trimmed.starts_with("\\PassOptionsToClass") {
|
||||
if let Some((options, names)) = pass_options_parts(trimmed, "\\PassOptionsToClass") {
|
||||
for name in names {
|
||||
record_passed_options(&mut passed_classes, &name, &options);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if trimmed.starts_with("\\usepackage") {
|
||||
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 {
|
||||
let mut requested = split_options(&options);
|
||||
requested.extend(take_passed_options(&mut passed_packages, &name));
|
||||
dedupe_options(&mut requested);
|
||||
if let Some(text) =
|
||||
load_project_file(&name, "sty", &requested, store, id, &mut loaded, 1)
|
||||
{
|
||||
@@ -469,14 +488,9 @@ fn expand_project_packages(source: &str, store: &projects::ProjectStore, id: &st
|
||||
}
|
||||
} else if trimmed.starts_with("\\documentclass") {
|
||||
if let Some((options, name)) = documentclass_parts(trimmed) {
|
||||
let requested: Vec<String> = options
|
||||
.trim_start_matches('[')
|
||||
.trim_end_matches(']')
|
||||
.split(',')
|
||||
.map(str::trim)
|
||||
.filter(|option| !option.is_empty())
|
||||
.map(str::to_string)
|
||||
.collect();
|
||||
let mut requested = split_options(&options);
|
||||
requested.extend(take_passed_options(&mut passed_classes, &name));
|
||||
dedupe_options(&mut requested);
|
||||
if let Some(text) = load_project_class(&name, &requested, store, id, &mut loaded, 1)
|
||||
{
|
||||
output.push_str(&format!("\\documentclass{}{{{}}}", options, "article"));
|
||||
@@ -616,6 +630,56 @@ fn split_options(options: &str) -> Vec<String> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn pass_options_parts(line: &str, command: &str) -> Option<(Vec<String>, Vec<String>)> {
|
||||
let rest = line.strip_prefix(command)?.trim_start();
|
||||
let rest = rest.strip_prefix('{')?;
|
||||
let close = rest.find('}')?;
|
||||
let options = split_options(&rest[..close]);
|
||||
let rest = rest[close + 1..].trim_start();
|
||||
let rest = rest.strip_prefix('{')?;
|
||||
let close = rest.find('}')?;
|
||||
let names = rest[..close]
|
||||
.split(',')
|
||||
.map(str::trim)
|
||||
.filter(|name| !name.is_empty())
|
||||
.map(str::to_string)
|
||||
.collect();
|
||||
Some((options, names))
|
||||
}
|
||||
|
||||
fn record_passed_options(passed: &mut Vec<(String, Vec<String>)>, name: &str, options: &[String]) {
|
||||
if let Some(entry) = passed.iter_mut().find(|(key, _)| key == name) {
|
||||
entry.1.extend(options.iter().cloned());
|
||||
} else {
|
||||
passed.push((name.to_string(), options.to_vec()));
|
||||
}
|
||||
}
|
||||
|
||||
fn take_passed_options(passed: &mut Vec<(String, Vec<String>)>, name: &str) -> Vec<String> {
|
||||
let mut options = Vec::new();
|
||||
passed.retain(|(key, values)| {
|
||||
if key == name {
|
||||
options.extend(values.iter().cloned());
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
});
|
||||
options
|
||||
}
|
||||
|
||||
fn dedupe_options(options: &mut Vec<String>) {
|
||||
let mut seen = Vec::new();
|
||||
options.retain(|option| {
|
||||
if seen.contains(option) {
|
||||
false
|
||||
} else {
|
||||
seen.push(option.clone());
|
||||
true
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn use_package_parts(line: &str) -> Option<(String, Vec<String>)> {
|
||||
let mut rest = line
|
||||
.strip_prefix("\\usepackage")
|
||||
|
||||
Reference in new issue
Block a user