typlite/
library.rs

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//! # Typlite Library

use crate::{scopes::Scopes, tinymist_std::typst::diag::EcoString, worker::TypliteWorker};

use super::*;
use ecow::eco_format;
use typst_syntax::{ast, SyntaxKind, SyntaxNode};
use value::*;

mod docstring;
pub use docstring::docstring_lib;

pub fn library() -> Scopes<Value> {
    let mut scopes = Scopes::new();
    scopes.define("link", link as RawFunc);
    scopes.define("kbd", kbd as RawFunc);
    scopes.define("md-alter", md_alter as RawFunc);
    scopes.define("image", image as RawFunc);
    scopes.define("figure", figure as RawFunc);
    scopes.define("raw", raw as RawFunc);
    scopes.define("strike", strike as RawFunc);
    scopes.define("pad", pad as RawFunc);
    scopes.define("note-box", note as RawFunc);
    scopes.define("tip-box", tip as RawFunc);
    scopes.define("important-box", important_box as RawFunc);
    scopes.define("warning-box", warning_box as RawFunc);
    scopes.define("caution-box", caution_box as RawFunc);
    scopes.define("table", table as RawFunc);
    scopes.define("grid", grid as RawFunc);
    scopes
}

/// Evaluates a link.
pub fn link(mut args: Args) -> Result<Value> {
    let dest = get_pos_named!(args, dest: EcoString);
    let body = get_pos_named!(args, body: Content);

    Ok(Value::Content(eco_format!("[{body}]({dest})")))
}

/// Evaluates an image.
pub fn image(mut args: Args) -> Result<Value> {
    let path = get_pos_named!(args, path: EcoString);
    let alt = get_named!(args, alt: EcoString := "");

    Ok(Value::Image { path, alt })
}

/// Evaluates a figure.
pub fn figure(mut args: Args) -> Result<Value> {
    let body = get_pos_named!(args, path: Value);
    let caption = get_named!(args, caption: Option<Value>).map(TypliteWorker::value);

    match (body, caption) {
        (Value::Image { path, alt }, None) => Ok(Value::Content(eco_format!("![{alt}]({path})"))),
        (Value::Image { path, alt }, Some(caption)) if args.vm.feat.gfm => Ok(Value::Content(
            eco_format!("![{caption}, {alt}]({path} {caption:?})"),
        )),
        (Value::Image { path, alt }, Some(caption)) => {
            Ok(Value::Content(eco_format!("![{caption}, {alt}]({path})")))
        }
        _ => Err("figure only accepts image as body".into()),
    }
}

/// Evaluates a raw.
pub fn raw(mut args: Args) -> Result<Value> {
    let content = get_pos_named!(args, content: EcoString);

    let max_consecutive_backticks = content
        .chars()
        .fold((0, 0), |(max, count), c| {
            if c == '`' {
                (max, count + 1)
            } else {
                (max.max(count), 0)
            }
        })
        .0;

    Ok(Value::Content(eco_format!(
        "{backticks}\n{content}\n{backticks}",
        backticks = "`".repeat((max_consecutive_backticks + 1).max(3)),
    )))
}

/// Evaluates a strike.
pub fn strike(mut args: Args) -> Result<Value> {
    let body = get_pos_named!(args, body: Content);

    Ok(Value::Content(eco_format!("~~{body}~~")))
}

/// Evaluates a padded content.
pub fn pad(mut args: Args) -> Result<Value> {
    Ok(get_pos_named!(args, path: Value))
}

/// Evaluates a `kbd` element.
pub fn kbd(mut args: Args) -> Result<Value> {
    let key = get_pos_named!(args, key: EcoString);

    Ok(Value::Content(eco_format!("<kbd>{key}</kbd>")))
}

/// Evaluates a markdown alteration.
pub fn md_alter(mut args: Args) -> Result<Value> {
    let _: () = get_pos_named!(args, left: ());
    let right = get_pos_named!(args, right: LazyContent);

    Ok(Value::Content(right.0))
}

/// Evaluates a note.
pub fn note(mut args: Args) -> Result<Value> {
    let body = get_pos_named!(args, body: Content);

    Ok(note_box("NOTE", body))
}

/// Evaluates a tip note box.
pub fn tip(mut args: Args) -> Result<Value> {
    let body = get_pos_named!(args, body: Content);

    Ok(note_box("TIP", body))
}

/// Creates a important note box.
pub fn important_box(mut args: Args) -> Result<Value> {
    let body = get_pos_named!(args, body: Content);

    Ok(note_box("IMPORTANT", body))
}

/// Creates a warning note box.
pub fn warning_box(mut args: Args) -> Result<Value> {
    let body = get_pos_named!(args, body: Content);

    Ok(note_box("WARNING", body))
}

/// Creates a caution note box.
pub fn caution_box(mut args: Args) -> Result<Value> {
    let body = get_pos_named!(args, body: Content);

    Ok(note_box("CAUTION", body))
}

fn note_box(title: &str, body: Content) -> Value {
    let mut res = EcoString::new();
    res.push_str("> [!");
    res.push_str(title);
    res.push_str("]\n");
    let body = body.0;
    for line in body.lines() {
        res.push_str("> ");
        res.push_str(line);
        res.push('\n');
    }

    Value::Content(res)
}

/// Evaluates a table.
pub fn table(args: Args) -> Result<Value> {
    table_eval(args, &EcoString::from("table"))
}

/// Evaluates a grid.
pub fn grid(args: Args) -> Result<Value> {
    table_eval(args, &EcoString::from("grid"))
}

fn table_eval(mut args: Args, kind: &EcoString) -> Result<Value> {
    let columns = if let Some(columns) = args.get_named_("columns") {
        match columns.kind() {
            SyntaxKind::Array => {
                let array: ast::Array = args.get_named_("columns").unwrap().cast().unwrap();
                array.items().count()
            }
            SyntaxKind::Int => {
                let int_val: ast::Int = args.get_named_("columns").unwrap().cast().unwrap();
                int_val.get().try_into().unwrap()
            }
            other => return Err(format!("invalid columns argument of type {:?}", other).into()),
        }
    } else {
        1
    };

    let header_field = SyntaxNode::inner(
        SyntaxKind::FieldAccess,
        vec![
            SyntaxNode::leaf(SyntaxKind::Ident, kind),
            SyntaxNode::leaf(SyntaxKind::Dot, "."),
            SyntaxNode::leaf(SyntaxKind::Ident, "header"),
        ],
    );
    let footer_field = SyntaxNode::inner(
        SyntaxKind::FieldAccess,
        vec![
            SyntaxNode::leaf(SyntaxKind::Ident, kind),
            SyntaxNode::leaf(SyntaxKind::Dot, "."),
            SyntaxNode::leaf(SyntaxKind::Ident, "footer"),
        ],
    );

    let mut header: Vec<EcoString> = Vec::new();
    let mut cells: Vec<EcoString> = Vec::new();

    while let Some(pos_arg) = args.pos.pop() {
        if pos_arg.kind() != SyntaxKind::FuncCall {
            let evaluated = args.vm.eval(pos_arg)?;
            cells.push(TypliteWorker::value(evaluated));
        } else {
            let func_call: ast::FuncCall = pos_arg.cast().unwrap();
            let first_child = pos_arg.children().next().unwrap();

            if header_field.spanless_eq(first_child) {
                let mut header_args = Args::new(args.vm, func_call.args());
                while let Some(arg) = header_args.pos.pop() {
                    let evaluated = header_args.vm.eval(arg)?;
                    header.push(TypliteWorker::value(evaluated));
                }
            } else {
                let evaluated = args.vm.eval(pos_arg)?;
                cells.push(TypliteWorker::value(evaluated));
            }
            if footer_field.spanless_eq(first_child) {
                let mut footer_args = Args::new(args.vm, func_call.args());
                while let Some(arg) = footer_args.pos.pop() {
                    let evaluated = footer_args.vm.eval(arg)?;
                    cells.push(TypliteWorker::value(evaluated));
                }
            }
        }
    }

    let mut res = EcoString::from("<table>\n");
    if !header.is_empty() {
        res.push_str("  <thead>\n    <tr>\n");
        for cell in &header {
            res.push_str(&eco_format!("      <th>{}</th>\n", cell));
        }
        res.push_str("    </tr>\n  </thead>\n");
    }
    res.push_str("  <tbody>\n");
    for row in cells.chunks(columns) {
        res.push_str("    <tr>\n");
        for cell in row {
            res.push_str(&eco_format!("      <td>{}</td>\n", cell));
        }
        res.push_str("    </tr>\n");
    }
    res.push_str("  </tbody>\n</table>");

    Ok(Value::Content(res))
}