summaryrefslogtreecommitdiff
path: root/src/library/mod.rs
blob: 0f61b901ca538efe72627e6c6c9cb5d75bb50630 (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
//! The standard library.

mod font;
mod layout;
mod page;
mod spacing;

pub use font::*;
pub use layout::*;
pub use page::*;
pub use spacing::*;

use crate::func::prelude::*;
use crate::syntax::scope::Scope;

/// Create a scope with all standard library functions.
pub fn std() -> Scope {
    let mut std = Scope::new::<ValFunc>();

    std.add::<ValFunc>("val");
    std.add::<FontFunc>("font");
    std.add::<PageFunc>("page");
    std.add::<AlignFunc>("align");
    std.add::<BoxFunc>("box");
    std.add::<ParBreakFunc>("parbreak");
    std.add::<PageBreakFunc>("pagebreak");
    std.add_with_meta::<SpacingFunc>("h", Horizontal);
    std.add_with_meta::<SpacingFunc>("v", Vertical);

    std
}

function! {
    /// `val`: Ignores all arguments and layouts the body flatly.
    ///
    /// This is also the fallback function, which is used when a function name
    /// could not be resolved.
    #[derive(Debug, Clone, PartialEq)]
    pub struct ValFunc {
        body: Option<SyntaxTree>,
    }

    parse(header, body, state, f) {
        header.args.pos.0.clear();
        header.args.key.0.clear();
        Self { body: parse_maybe_body(body, state, f), }
    }

    layout(self, ctx, f) {
        match &self.body {
            Some(tree) => vec![LayoutSyntaxTree(tree)],
            None => vec![],
        }
    }
}