summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: aef0f573d6aa8bbe439dc235b7aaa126bdeca2a3 (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
use std::fs;
use std::path::{Path, PathBuf};

use anyhow::{anyhow, bail, Context};

use typst::cache::Cache;
use typst::diag::Pass;
use typst::env::{Env, FsLoader};
use typst::exec::State;
use typst::library;
use typst::parse::LineMap;
use typst::pdf;
use typst::typeset;

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

    let src_path = Path::new(&args[1]);
    let dest_path = if args.len() <= 2 {
        let name = src_path
            .file_name()
            .ok_or_else(|| anyhow!("Source path is not a file."))?;
        Path::new(name).with_extension("pdf")
    } else {
        PathBuf::from(&args[2])
    };

    if src_path == dest_path {
        bail!("Source and destination path are the same.");
    }

    let src = fs::read_to_string(src_path).context("Failed to read from source file.")?;

    let mut loader = FsLoader::new();
    loader.search_path("fonts");
    loader.search_system();

    let mut env = Env::new(loader);
    let mut cache = Cache::new();
    let scope = library::new();
    let state = State::default();

    let Pass { output: frames, diags } =
        typeset(&mut env, &mut cache, &src, &scope, state);
    if !diags.is_empty() {
        let map = LineMap::new(&src);
        for diag in 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,
            );
        }
    }

    let pdf_data = pdf::export(&env, &frames);
    fs::write(&dest_path, pdf_data).context("Failed to write PDF file.")?;

    Ok(())
}