summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: d0762b3e7cee1b2458b0acdd6486ca47e545147d (plain) (blame)
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
67
68
69
70
71
72
73
74
75
76
use std::fs;
use std::path::{Path, PathBuf};

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(());
    }

    // Create a loader for fonts and files.
    let mut loader = typst::loading::FsLoader::new();
    loader.search_path("fonts");
    loader.search_system();

    // Resolve the canonical path because the compiler needs it for module
    // loading.
    let src_path = Path::new(&args[1]);

    // Find out the file name to create the output file.
    let name = src_path
        .file_name()
        .ok_or_else(|| anyhow!("source path is not a file"))?;

    let dest_path = if args.len() <= 2 {
        Path::new(name).with_extension("pdf")
    } else {
        PathBuf::from(&args[2])
    };

    // 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");
    }

    // Read the source.
    let src = fs::read_to_string(&src_path)
        .map_err(|_| anyhow!("failed to read source file"))?;

    // Compile.
    let mut cache = typst::cache::Cache::new(&loader);
    let scope = typst::library::new();
    let state = typst::exec::State::default();
    let pass = typst::typeset(
        &mut loader,
        &mut cache,
        Some(&src_path),
        &src,
        &scope,
        state,
    );

    // 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(&cache, &pass.output);
    fs::write(&dest_path, buffer).context("failed to write PDF file")?;

    Ok(())
}