1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
use std::fs;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use anyhow::{anyhow, bail, Context};
use same_file::is_same_file;
fn main() -> anyhow::Result<()> {
let args: Vec<_> = std::env::args().collect();
if args.len() < 2 || args.len() > 3 {
println!("usage: typst src.typ [out.pdf]");
return Ok(());
}
// Determine source and destination path.
let src_path = Path::new(&args[1]);
let dest_path = if let Some(arg) = args.get(2) {
PathBuf::from(arg)
} else {
let name = src_path
.file_name()
.ok_or_else(|| anyhow!("source path is not a file"))?;
Path::new(name).with_extension("pdf")
};
// Ensure that the source file is not overwritten.
if is_same_file(src_path, &dest_path).unwrap_or(false) {
bail!("source and destination files are the same");
}
// Create a loader for fonts and files.
let mut loader = typst::loading::FsLoader::new();
loader.search_path("fonts");
loader.search_system();
// Resolve the file id of the source file and read the file.
let src_id = loader.resolve_path(src_path).context("source file not found")?;
let src = fs::read_to_string(&src_path)
.map_err(|_| anyhow!("failed to read source file"))?;
// Typeset.
let mut ctx = typst::Context::new(Rc::new(loader));
let pass = ctx.typeset(src_id, &src);
// Print diagnostics.
let map = typst::parse::LineMap::new(&src);
for diag in pass.diags {
let start = map.location(diag.span.start).unwrap();
let end = map.location(diag.span.end).unwrap();
println!(
"{}: {}:{}-{}: {}",
diag.level,
src_path.display(),
start,
end,
diag.message,
);
}
// Export the PDF.
let buffer = typst::export::pdf(&ctx, &pass.output);
fs::write(&dest_path, buffer).context("failed to write PDF file")?;
Ok(())
}
|