use std::collections::BTreeMap;
use std::path::PathBuf;
use std::sync::{Mutex, OnceLock};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Request {
pub name: String,
pub extension: &'static str,
pub options: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Outcome {
Loaded(PathBuf),
Refused { path: PathBuf, reason: String },
NotFound,
}
pub fn requests(src: &str) -> Vec<Request> {
let mut out = Vec::new();
for (command, extension) in [
("\\documentclass", "cls"),
("\\LoadClass", "cls"),
("\\usepackage", "sty"),
("\\RequirePackage", "sty"),
] {
for (options, names) in calls(src, command) {
for name in names.split(',') {
let name = name.trim();
if name.is_empty() {
continue;
}
out.push(Request {
name: name.to_string(),
extension,
options: options.clone(),
});
}
}
}
out
}
fn calls(src: &str, command: &str) -> Vec<(String, String)> {
let mut out = Vec::new();
for line in lines_naming(src, command) {
let mut rest = line;
while let Some(at) = rest.find(command) {
let after = &rest[at + command.len()..];
rest = after;
if after.starts_with(|c: char| c.is_ascii_alphabetic()) {
continue;
}
let after = after.trim_start();
let (options, after) = match after.strip_prefix('[') {
Some(body) => match body.find(']') {
Some(end) => (body[..end].to_string(), body[end + 1..].trim_start()),
None => continue,
},
None => (String::new(), after),
};
let Some(body) = after.strip_prefix('{') else {
continue;
};
let Some(end) = body.find('}') else { continue };
out.push((options, body[..end].to_string()));
}
}
out
}
fn lines_naming<'a>(src: &'a str, command: &'a str) -> impl Iterator<Item = &'a str> + 'a {
let mut from = 0;
std::iter::from_fn(move || {
let at = from + src[from..].find(command)?;
let start = src[from..at].rfind('\n').map_or(from, |n| from + n + 1);
let line = match src[at..].find('\n') {
Some(n) => {
from = at + n + 1;
let line = &src[start..at + n];
line.strip_suffix('\r').unwrap_or(line)
}
None => {
from = src.len();
&src[start..]
}
};
Some(uncommented(line))
})
}
fn uncommented(line: &str) -> &str {
let mut escaped = false;
for (at, byte) in line.bytes().enumerate() {
match (escaped, byte) {
(true, _) => escaped = false,
(false, b'\\') => escaped = true,
(false, b'%') => return &line[..at],
(false, _) => {}
}
}
line
}
pub fn resolve(name: &str, extension: &str) -> Option<PathBuf> {
locate(&format!("{name}.{extension}"))
}
pub fn locate(file: &str) -> Option<PathBuf> {
crate::kpse::locate(file)
}
pub fn warm(names: &[String]) {
crate::kpse::warm(names);
}
pub fn existence(text: &str) -> String {
let mut out = String::new();
let mut seen = std::collections::BTreeSet::new();
for command in ["\\IfFileExists", "\\InputIfFileExists"] {
for name in braced_names(text, command) {
if !seen.insert(name.clone()) {
continue;
}
if !exists(&name) {
continue;
}
out.push_str(&format!(
"\\expandafter\\let\\csname @fe@{name}\\endcsname\\@fe@yes\n"
));
}
}
match out.is_empty() {
true => out,
false => format!("% The files this document asks for -- see src/latex/load.rs.\n{out}"),
}
}
fn exists(name: &str) -> bool {
if std::path::Path::new(name).is_file() {
return true;
}
if let Ok(paths) = std::env::var("TEXINPUTS") {
if std::env::split_paths(&paths).any(|d| d.join(name).is_file()) {
return true;
}
}
locate(name).is_some()
}
fn braced_names(text: &str, command: &str) -> Vec<String> {
let mut out = Vec::new();
for line in lines_naming(text, command) {
let mut rest = line;
while let Some(at) = rest.find(command) {
let after = &rest[at + command.len()..];
rest = after;
if after.starts_with(|c: char| c.is_ascii_alphabetic()) {
continue;
}
let after = after.trim_start_matches([' ', '\t']);
let Some(body) = after.strip_prefix('{') else {
continue;
};
let Some(end) = body.find('}') else { continue };
let name = body[..end].trim();
if !name.is_empty() && !name.contains('\\') {
out.push(name.to_string());
}
}
}
out
}
pub fn without_trailing_endinput(text: String) -> String {
let Some(at) = text.rfind("\\endinput") else {
return text;
};
let line_start = text[..at].rfind('\n').map_or(0, |n| n + 1);
if !text[line_start..at].trim().is_empty() {
return text;
}
let after = &text[at + "\\endinput".len()..];
let (first, rest) = match after.find('\n') {
Some(n) => (&after[..n], &after[n + 1..]),
None => (after, ""),
};
let commentary = |line: &str| {
let line = line.trim();
line.is_empty() || line.starts_with('%')
};
if !commentary(first) || !rest.lines().all(commentary) {
return text;
}
format!("{}\\relax{}", &text[..at], after)
}
pub fn attempt(request: &Request) -> Outcome {
static CACHE: OnceLock<Mutex<BTreeMap<String, Outcome>>> = OnceLock::new();
let cache = CACHE.get_or_init(|| Mutex::new(BTreeMap::new()));
let key = format!("{}.{}", request.name, request.extension);
if let Some(hit) = cache.lock().ok().and_then(|c| c.get(&key).cloned()) {
return hit;
}
let outcome = match resolve(&request.name, request.extension) {
None => Outcome::NotFound,
Some(_) => read_through(&chain(request)),
};
if let Ok(mut c) = cache.lock() {
c.insert(key, outcome.clone());
}
outcome
}
fn attempt_all(requests: &[Request]) {
let mut distinct: Vec<&Request> = Vec::new();
for request in requests {
let named =
|seen: &&Request| seen.name == request.name && seen.extension == request.extension;
if !distinct.iter().any(named) {
distinct.push(request);
}
}
let workers = crate::parallel::default_threads().min(distinct.len());
if workers < 2 {
return;
}
let next = std::sync::atomic::AtomicUsize::new(0);
let work = || {
while let Some(request) =
distinct.get(next.fetch_add(1, std::sync::atomic::Ordering::Relaxed))
{
attempt(request);
}
};
std::thread::scope(|s| {
let handles: Vec<_> = (0..workers)
.map(|_| {
std::thread::Builder::new()
.name("texrs-load".into())
.stack_size(ATTEMPT_STACK)
.spawn_scoped(s, work)
.expect("spawn a package attempt")
})
.collect();
for handle in handles {
if let Err(panic) = handle.join() {
std::panic::resume_unwind(panic);
}
}
});
}
const ATTEMPT_STACK: usize = 64 << 20;
const MAX_REQUIRES: usize = 8;
pub fn chain(request: &Request) -> Vec<(Request, PathBuf)> {
let mut seen = std::collections::BTreeSet::new();
let mut out = Vec::new();
walk(request, 0, &mut seen, &mut out);
out
}
fn walk(
request: &Request,
depth: usize,
seen: &mut std::collections::BTreeSet<PathBuf>,
out: &mut Vec<(Request, PathBuf)>,
) {
let Some(path) = resolve(&request.name, request.extension) else {
return;
};
if !seen.insert(path.clone()) {
return;
}
let text = std::fs::read_to_string(&path).unwrap_or_default();
if depth < MAX_REQUIRES {
let required = requests(&text);
warm(
&required
.iter()
.map(|r| format!("{}.{}", r.name, r.extension))
.collect::<Vec<_>>(),
);
for inner in required {
walk(&inner, depth + 1, seen, out);
}
}
out.push((request.clone(), path));
}
fn read_through(chain: &[(Request, PathBuf)]) -> Outcome {
let Some((_, last)) = chain.last() else {
return Outcome::NotFound;
};
let mut text = String::new();
let mut source = String::new();
for (request, path) in chain {
text.push_str(&std::fs::read_to_string(path).unwrap_or_default());
text.push('\n');
source.push_str(&preamble_for(request, path));
}
let mut lowerer = crate::lower::Lowerer::new();
if let Err(e) = lowerer.preload(&super::support(&text)) {
return Outcome::Refused {
path: last.clone(),
reason: e.0,
};
}
if let Err(e) = lowerer.preload(&source) {
return Outcome::Refused {
path: last.clone(),
reason: e.0,
};
}
match lowerer.preload(AFTERWARDS) {
Ok(()) => Outcome::Loaded(last.clone()),
Err(e) => Outcome::Refused {
path: last.clone(),
reason: e.0,
},
}
}
const AFTERWARDS: &str = "\\makeatletter\n\
\\setlength{\\parskip}{6pt plus 2pt minus 1pt}\n\
\\setlength{\\emergencystretch}{3em}\n\
\\addtolength{\\parskip}{1pt}\n\
\\makeatother\n";
fn preamble_for(request: &Request, path: &std::path::Path) -> String {
let text = std::fs::read_to_string(path).unwrap_or_default();
format!(
"\\def\\@currname{{{}}}\\def\\@currext{{{}}}\\def\\@curroptions{{{}}}\n\
\\makeatletter\n{}\\input {}\\makeatother\n",
request.name,
request.extension,
request.options,
crate::latex::switches::definitions(&text),
path.display(),
)
}
pub fn preamble(src: &str) -> (String, Vec<String>, String) {
let mut tex = String::new();
let mut said = Vec::new();
let mut loaded = String::new();
let requests = requests(src);
warm(
&requests
.iter()
.map(|r| format!("{}.{}", r.name, r.extension))
.collect::<Vec<_>>(),
);
attempt_all(&requests);
let mut read = std::collections::BTreeSet::new();
for request in requests {
match attempt(&request) {
Outcome::Loaded(_) => {
for (inner, path) in chain(&request) {
if !read.insert(path.clone()) {
continue;
}
loaded.push_str(&std::fs::read_to_string(&path).unwrap_or_default());
loaded.push('\n');
tex.push_str(&preamble_for(&inner, &path));
}
}
Outcome::Refused { reason, .. } => {
said.push(format!("package {} {}", request.name, needs(&reason)));
}
Outcome::NotFound => {
said.push(format!(
"package {} was not found by kpsewhich; it is consumed rather than loaded",
request.name
));
}
}
}
(tex, said, loaded)
}
fn needs(reason: &str) -> String {
let trimmed = reason.trim().trim_start_matches("! ").trim_end_matches('.');
match trimmed.strip_prefix("Undefined control sequence ") {
Some(cs) => format!("needs {cs}"),
None => format!("is not loadable: {trimmed}"),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_docstrip_endinput_is_replaced_and_anything_else_is_left_alone() {
let generated = "\\def\\a{1}\n\\endinput\n%%\n%% End of file `x.sty'.\n";
let got = without_trailing_endinput(generated.to_string());
assert!(!got.contains("\\endinput"), "{got}");
assert!(got.contains("\\relax"), "{got}");
assert!(got.contains("\\def\\a{1}"), "the code is kept: {got}");
let early = "\\ifx\\x\\y\\endinput\\fi\n\\def\\b{2}\n";
assert_eq!(without_trailing_endinput(early.to_string()), early);
let inline = "\\def\\stop{\\endinput}\n%%\n";
assert_eq!(without_trailing_endinput(inline.to_string()), inline);
}
#[test]
fn only_a_literal_file_name_is_answered_by_the_existence_table() {
assert_eq!(
braced_names(
"\\InputIfFileExists{textcomp.cfg}{}{}\n",
"\\InputIfFileExists"
),
["textcomp.cfg"]
);
assert!(braced_names(
"\\IfFileExists{\\f@encoding\\f@family.fd}{}{}\n",
"\\IfFileExists"
)
.is_empty());
assert!(braced_names("% \\IfFileExists{a.tex}{}{}\n", "\\IfFileExists").is_empty());
}
#[test]
fn a_package_is_read_after_what_it_requires() {
if resolve("graphicx", "sty").is_none() {
eprintln!("skipping: kpsewhich cannot find graphicx.sty");
return;
}
let names: Vec<String> = chain(&Request {
name: "graphicx".into(),
extension: "sty",
options: String::new(),
})
.into_iter()
.map(|(r, _)| r.name)
.collect();
assert_eq!(
names.last().map(String::as_str),
Some("graphicx"),
"{names:?}"
);
assert!(names.contains(&"keyval".to_string()), "{names:?}");
assert!(
names.iter().position(|n| n == "keyval") < names.iter().position(|n| n == "graphicx"),
"what is required is read first: {names:?}"
);
}
#[test]
fn a_class_and_a_comma_list_of_packages_are_all_requests() {
let src = "\\documentclass[11pt]{extreport}\n\\usepackage{amsmath,amssymb}\n";
let got = requests(src);
assert_eq!(got[0].name, "extreport");
assert_eq!(got[0].extension, "cls");
assert_eq!(got[0].options, "11pt");
assert_eq!(got[1].name, "amsmath");
assert_eq!(got[2].name, "amssymb");
assert_eq!(got[1].extension, "sty");
}
#[test]
fn a_commented_out_usepackage_is_not_a_request() {
assert!(requests("% \\usepackage{tikz}\n").is_empty());
assert_eq!(requests("\\usepackage{a} % \\usepackage{b}\n").len(), 1);
}
#[test]
fn the_comment_rules_are_the_ones_the_line_copying_scan_applied() {
fn copied(src: &str) -> Vec<String> {
let mut out = Vec::new();
for line in src.lines() {
let mut kept = String::new();
let mut escaped = false;
for ch in line.chars() {
if escaped {
kept.push(ch);
escaped = false;
continue;
}
match ch {
'\\' => {
kept.push(ch);
escaped = true;
}
'%' => break,
_ => kept.push(ch),
}
}
out.push(kept);
}
out
}
let src = "\\usepackage{a}\\usepackage{b}\n\
\\% \\usepackage{escaped}\n\
\\\\% \\usepackage{after-an-escaped-backslash}\n\
\\verb|%| \\usepackage{after-verb}\n\
\\usepackage{é}\\é% \\usepackage{x}\r\n\
\r\n\
%\\usepackage{c}\n\
\\usepackage[opt]{d}\r";
let command = "\\usepackage";
let sliced: Vec<&str> = lines_naming(src, command)
.filter(|l| l.contains(command))
.collect();
let expected: Vec<String> = copied(src)
.into_iter()
.filter(|l| l.contains(command))
.collect();
assert_eq!(sliced, expected);
let names: Vec<String> = requests(src).into_iter().map(|r| r.name).collect();
assert_eq!(names, ["a", "b", "escaped", "é", "d"]);
}
#[test]
fn an_undefined_control_sequence_is_reported_as_what_the_package_needs() {
assert_eq!(
needs("! Undefined control sequence \\DeclareRelease."),
"needs \\DeclareRelease"
);
assert_eq!(
needs("! Missing number, found \\catcode."),
"is not loadable: Missing number, found \\catcode"
);
}
}