Inline local style packages in project previews
This commit is contained in:
1 parent
491d4e528a
commit
a7f3680a13
5 files changed
+115
-6
No files matched your search
+78
-2
@@ -409,6 +409,82 @@ fn respond_pdf(request: Request, pdf: Vec<u8>) -> Result<(), String> {
|
||||
const MAX_ZIP_BYTES: usize = 32 * 1024 * 1024;
|
||||
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();
|
||||
for line in source.lines() {
|
||||
if line.trim_start().starts_with("\\usepackage") {
|
||||
if let Some(names) = package_names(line.trim()) {
|
||||
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 replaced {
|
||||
output.push_str(&packages);
|
||||
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();
|
||||
if rest.starts_with('[') {
|
||||
let close = rest.find(']')?;
|
||||
rest = rest[close + 1..].trim_start();
|
||||
}
|
||||
let rest = rest.strip_prefix('{')?;
|
||||
let close = rest.find('}')?;
|
||||
Some(
|
||||
rest[..close]
|
||||
.split(',')
|
||||
.map(|name| name.trim().to_string())
|
||||
.filter(|name| !name.is_empty())
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
fn sanitize_package(source: &str) -> String {
|
||||
const BOILERPLATE: [&str; 9] = [
|
||||
"\\ProvidesPackage",
|
||||
"\\ProvidesClass",
|
||||
"\\NeedsTeXFormat",
|
||||
"\\RequirePackage",
|
||||
"\\DeclareOption",
|
||||
"\\ProcessOptions",
|
||||
"\\ExecuteOptions",
|
||||
"\\PassOptionsToPackage",
|
||||
"\\endinput",
|
||||
];
|
||||
source
|
||||
.lines()
|
||||
.filter(|line| {
|
||||
let trimmed = line.trim();
|
||||
!trimmed.starts_with('%')
|
||||
&& !BOILERPLATE
|
||||
.iter()
|
||||
.any(|command| trimmed.starts_with(command))
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
fn expand_project_source(source: &str, store: &projects::ProjectStore, id: &str) -> String {
|
||||
let expanded = expand_project_inputs(source, store, id);
|
||||
let expanded = expand_project_packages(&expanded, store, id);
|
||||
expand_project_inputs(&expanded, store, id)
|
||||
}
|
||||
|
||||
fn expand_project_inputs(source: &str, store: &projects::ProjectStore, id: &str) -> String {
|
||||
let read = |path: &str| {
|
||||
let bytes = store
|
||||
@@ -511,7 +587,7 @@ fn handle_project_request(
|
||||
Err(error) => return respond_api_error(request, 422, &error),
|
||||
};
|
||||
if query_param(url, "engine").as_deref() == Some("route-d") {
|
||||
let expanded = expand_project_inputs(&source, &store, id);
|
||||
let expanded = expand_project_source(&source, &store, id);
|
||||
return match route_d::project_preview_json(id, &expanded) {
|
||||
Ok(json) => respond_json(request, 200, &json),
|
||||
Err(error) => respond_api_error(request, 422, &error),
|
||||
@@ -533,7 +609,7 @@ fn handle_project_request(
|
||||
Err(error) => return respond_api_error(request, 422, &error),
|
||||
};
|
||||
if query_param(url, "engine").as_deref() == Some("route-d") {
|
||||
let expanded = expand_project_inputs(&source, &store, id);
|
||||
let expanded = expand_project_source(&source, &store, id);
|
||||
return match route_d::pdf_bytes(&expanded) {
|
||||
Ok(pdf) => respond_pdf(request, pdf),
|
||||
Err(error) => respond(request, 422, "text/plain; charset=utf-8", &error),
|
||||
|
||||
Reference in new issue
Block a user