Inline local class files in project previews
This commit is contained in:
1 parent
a7f3680a13
commit
64325d4b87
5 files changed
+67
-5
No files matched your search
+29
-1
@@ -430,6 +430,18 @@ fn expand_project_packages(source: &str, store: &projects::ProjectStore, id: &st
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
output.push_str(line);
|
||||
output.push('\n');
|
||||
@@ -454,8 +466,21 @@ fn package_names(line: &str) -> Option<Vec<String>> {
|
||||
)
|
||||
}
|
||||
|
||||
fn documentclass_parts(line: &str) -> Option<(String, String)> {
|
||||
let mut rest = line.strip_prefix("\\documentclass")?.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((options, rest[..close].trim().to_string()))
|
||||
}
|
||||
|
||||
fn sanitize_package(source: &str) -> String {
|
||||
const BOILERPLATE: [&str; 9] = [
|
||||
const BOILERPLATE: [&str; 12] = [
|
||||
"\\ProvidesPackage",
|
||||
"\\ProvidesClass",
|
||||
"\\NeedsTeXFormat",
|
||||
@@ -464,6 +489,9 @@ fn sanitize_package(source: &str) -> String {
|
||||
"\\ProcessOptions",
|
||||
"\\ExecuteOptions",
|
||||
"\\PassOptionsToPackage",
|
||||
"\\PassOptionsToClass",
|
||||
"\\LoadClass",
|
||||
"\\LoadClassWithOptions",
|
||||
"\\endinput",
|
||||
];
|
||||
source
|
||||
|
||||
Reference in new issue
Block a user