summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: d471b09d5f99adde581ed3b9574d9d83550b7a6a (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
//! The compiler for the _Typst_ typesetting language.
//!
//! # Steps
//! - **Parsing:** The parsing step first transforms a plain string into an
//!   [iterator of tokens][tokens]. This token stream is [parsed] into a [syntax
//!   tree]. The structures describing the tree can be found in the [syntax]
//!   module.
//! - **Evaluation:** The next step is to [evaluate] the parsed "script" to a
//!   [document], a high-level, fully styled representation. The nodes of the
//!   document tree are fully self-contained and order-independent and thus much
//!   better suited for layouting than the syntax tree.
//! - **Layouting:** The next step is to [layout] the document into a portable
//!   version of the typeset document. The output of this is a vector of
//!   [`BoxLayout`]s (corresponding to pages), ready for exporting.
//! - **Exporting:** The finished layout can be exported into a supported
//!   format. Submodules for these formats are located in the [export] module.
//!   Currently, the only supported output format is [_PDF_].
//!
//! [tokens]: parse::Tokens
//! [parsed]: parse::parse
//! [syntax tree]: syntax::SynTree
//! [evaluate]: eval::eval
//! [document]: layout::Document
//! [layout]: layout::layout
//! [_PDF_]: export::pdf

#[macro_use]
pub mod diag;
#[macro_use]
pub mod eval;
pub mod color;
pub mod env;
pub mod export;
pub mod font;
pub mod geom;
pub mod layout;
pub mod library;
pub mod paper;
pub mod parse;
pub mod prelude;
pub mod shaping;
pub mod syntax;

use std::rc::Rc;

use crate::diag::{Feedback, Pass};
use crate::env::SharedEnv;
use crate::eval::State;
use crate::layout::BoxLayout;

/// Process _Typst_ source code directly into a collection of layouts.
pub fn typeset(src: &str, env: SharedEnv, state: State) -> Pass<Vec<BoxLayout>> {
    let Pass { output: tree, feedback: f1 } = parse::parse(src);
    let Pass { output: document, feedback: f2 } =
        eval::eval(&tree, Rc::clone(&env), state);
    let layouts = layout::layout(&document, env);
    Pass::new(layouts, Feedback::join(f1, f2))
}