summaryrefslogtreecommitdiff
path: root/src/typeset.rs
blob: 3196efd3dfa43b1d7caf25874d09ab3f1e133656 (plain)
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Typeset various data types into HTML.

use std::cmp::Ordering;

use base64::prelude::{Engine as _, BASE64_STANDARD};

use crate::{
    bindings::Bindings,
    diagrams::{DiagramMarkup, DotMarkup, PikchrMarkup, PlantumlMarkup, Svg},
    doc::Document,
    error::SubplotError,
    html::{Attribute, Content, Element, ElementTag, HtmlPage, Location},
    matches::{MatchedStep, PartialStep},
    resource,
};

// Name of standard Subplot CSS file.
const CSS: &str = "subplot.css";

// List of attributes that we don't want a typeset element to have.
const UNWANTED_ATTRS: &[&str] = &["add-newline"];

/// Render a matched step as HTML.
pub fn typeset_matched_step(step: &MatchedStep) -> Element {
    let mut e = Element::new(ElementTag::Span);
    e.push_attribute(Attribute::new("class", "scenario_step"));
    e.push_child(Content::Text(step.text().into()));
    e
}

/// Return Document as an HTML page serialized into HTML text
pub fn typeset_doc(doc: &Document) -> Result<String, SubplotError> {
    let css_file = resource::read_as_string(CSS, None)
        .map_err(|e| SubplotError::CssFileNotFound(CSS.into(), e))?;

    let mut head = Element::new(crate::html::ElementTag::Head);
    let mut title = Element::new(crate::html::ElementTag::Title);
    title.push_child(crate::html::Content::Text(doc.meta().title().into()));
    head.push_child(crate::html::Content::Elt(title));

    let mut css = Element::new(ElementTag::Style);
    css.push_child(Content::Text(css_file));
    for css_file in doc.meta().css_embed() {
        css.push_child(Content::Text(css_file.into()));
    }
    head.push_child(Content::Elt(css));

    for css_url in doc.meta().css_urls() {
        let mut link = Element::new(ElementTag::Link);
        link.push_attribute(Attribute::new("rel", "stylesheet"));
        link.push_attribute(Attribute::new("type", "text/css"));
        link.push_attribute(Attribute::new("href", css_url));
        head.push_child(Content::Elt(link));
    }

    let mut body_content = Element::new(crate::html::ElementTag::Div);
    body_content.push_attribute(Attribute::new("class", "content"));
    for md in doc.markdowns().iter() {
        body_content.push_child(Content::Elt(md.root_element().clone()));
    }

    let mut body = Element::new(crate::html::ElementTag::Div);
    body.push_child(Content::Elt(meta(doc)));
    body.push_child(Content::Elt(toc(&body_content)));
    body.push_child(Content::Elt(body_content));

    let page = HtmlPage::new(head, body);
    page.serialize().map_err(SubplotError::ParseMarkdown)
}

fn meta(doc: &Document) -> Element {
    let mut div = Element::new(ElementTag::Div);
    div.push_attribute(Attribute::new("class", "meta"));

    div.push_child(Content::Elt(title(doc.meta().title())));

    if let Some(names) = doc.meta().authors() {
        div.push_child(Content::Elt(authors(names)));
    }

    if let Some(d) = doc.meta().date() {
        div.push_child(Content::Elt(date(d)));
    }

    div
}

fn title(title: &str) -> Element {
    let mut e = Element::new(ElementTag::H1);
    e.push_attribute(Attribute::new("class", "title"));
    e.push_child(Content::Text(title.into()));
    e
}

fn authors(authors: &[String]) -> Element {
    let mut list = Element::new(ElementTag::P);
    list.push_attribute(Attribute::new("class", "authors"));
    list.push_child(Content::Text("By: ".into()));
    let mut first = true;
    for a in authors {
        if !first {
            list.push_child(Content::Text(", ".into()));
        }
        list.push_child(Content::Text(a.into()));
        first = false;
    }
    list
}

fn date(date: &str) -> Element {
    let mut e = Element::new(ElementTag::P);
    e.push_attribute(Attribute::new("class", "date"));
    e.push_child(Content::Text(date.into()));
    e
}

fn toc(body: &Element) -> Element {
    let mut toc = Element::new(ElementTag::Div);
    toc.push_attribute(Attribute::new("class", "toc"));

    let mut heading = Element::new(ElementTag::H1);
    heading.push_child(Content::Text("Table of Contents".into()));
    toc.push_child(Content::Elt(heading));

    let heading_elements: Vec<&Element> = crate::md::Markdown::visit(body)
        .iter()
        .filter(|e| {
            matches!(
                e.tag(),
                ElementTag::H1
                    | ElementTag::H2
                    | ElementTag::H3
                    | ElementTag::H4
                    | ElementTag::H5
                    | ElementTag::H6
            )
        })
        .cloned()
        .collect();

    let mut headings = vec![];
    for e in heading_elements {
        let id = e
            .attr("id")
            .expect("heading has id")
            .value()
            .expect("id attribute has value");
        match e.tag() {
            ElementTag::H1 => headings.push((1, e.content(), id)),
            ElementTag::H2 => headings.push((2, e.content(), id)),
            ElementTag::H3 => headings.push((3, e.content(), id)),
            ElementTag::H4 => headings.push((4, e.content(), id)),
            ElementTag::H5 => headings.push((5, e.content(), id)),
            ElementTag::H6 => headings.push((6, e.content(), id)),
            _ => (),
        }
    }

    let mut stack = vec![];
    let mut numberer = HeadingNumberer::default();
    for (level, text, id) in headings {
        assert!(level >= 1);
        assert!(level <= 6);

        let mut number = Element::new(ElementTag::Span);
        number.push_attribute(Attribute::new("class", "heading-number"));
        number.push_child(Content::Text(numberer.number(level)));

        let mut htext = Element::new(ElementTag::Span);
        htext.push_attribute(Attribute::new("class", "heading-text"));
        htext.push_child(Content::Text(text));

        let mut a = Element::new(ElementTag::A);
        a.push_attribute(crate::html::Attribute::new("href", &format!("#{}", id)));
        a.push_attribute(Attribute::new("class", "toc-link"));
        a.push_child(Content::Elt(number));
        a.push_child(Content::Text(" ".into()));
        a.push_child(Content::Elt(htext));

        let mut li = Element::new(ElementTag::Li);
        li.push_child(Content::Elt(a));

        match level.cmp(&stack.len()) {
            Ordering::Equal => (),
            Ordering::Greater => stack.push(Element::new(ElementTag::Ol)),
            Ordering::Less => {
                assert!(!stack.is_empty());
                let child = stack.pop().unwrap();
                assert!(child.tag() == ElementTag::Ol);
                let mut li = Element::new(ElementTag::Li);
                li.push_child(Content::Elt(child));
                assert!(!stack.is_empty());
                let mut parent = stack.pop().unwrap();
                parent.push_child(Content::Elt(li));
                stack.push(parent);
            }
        }

        assert!(!stack.is_empty());
        let mut ol = stack.pop().unwrap();
        ol.push_child(Content::Elt(li));
        stack.push(ol);
    }

    while stack.len() > 1 {
        let child = stack.pop().unwrap();
        assert!(child.tag() == ElementTag::Ol);
        let mut li = Element::new(ElementTag::Li);
        li.push_child(Content::Elt(child));

        let mut parent = stack.pop().unwrap();
        parent.push_child(Content::Elt(li));
        stack.push(parent);
    }

    assert!(stack.len() <= 1);
    if let Some(ol) = stack.pop() {
        toc.push_child(Content::Elt(ol));
    }

    toc
}

/// Type set an HTML element.
pub fn typeset_element(
    e: &Element,
    template: Option<&str>,
    bindings: &Bindings,
) -> Result<Element, SubplotError> {
    let new = match e.tag() {
        ElementTag::Pre if e.has_attr("class", "scenario") => {
            typeset_scenario(e, template, bindings)
        }
        ElementTag::Pre if e.has_attr("class", "file") => file(e),
        ElementTag::Pre if e.has_attr("class", "example") => example(e),
        ElementTag::Pre if e.has_attr("class", "dot") => dot(e),
        ElementTag::Pre if e.has_attr("class", "plantuml") => plantuml(e),
        ElementTag::Pre if e.has_attr("class", "roadmap") => roadmap(e),
        ElementTag::Pre if e.has_attr("class", "pikchr") => pikchr(e),
        _ => {
            let mut new = Element::new(e.tag());
            for attr in e.all_attrs() {
                new.push_attribute(attr.clone());
            }
            for child in e.children() {
                if let Content::Elt(ce) = child {
                    new.push_child(Content::Elt(typeset_element(ce, template, bindings)?));
                } else {
                    new.push_child(child.clone());
                }
            }
            Ok(new)
        }
    };
    let mut new = new?;
    new.drop_attributes(UNWANTED_ATTRS);
    Ok(new)
}

fn typeset_scenario(
    e: &Element,
    template: Option<&str>,
    bindings: &Bindings,
) -> Result<Element, SubplotError> {
    let template = template.unwrap_or("python"); // FIXME

    let text = e.content();
    let steps = crate::steps::parse_scenario_snippet(&text, &Location::Unknown)?;

    let mut scenario = Element::new(ElementTag::Div);
    scenario.push_attribute(Attribute::new("class", "scenario"));

    for st in steps {
        if let Ok(matched) = bindings.find(template, &st) {
            scenario.push_child(Content::Elt(step(&matched)));
        } else {
            scenario.push_child(Content::Text(st.text().into()));
        }
    }

    Ok(scenario)
}

fn step(matched: &MatchedStep) -> Element {
    let mut e = Element::new(ElementTag::Div);
    let mut keyword = Element::new(ElementTag::Span);
    keyword.push_attribute(Attribute::new("class", "keyword"));
    keyword.push_child(Content::Text(matched.kind().to_string()));
    keyword.push_child(Content::Text(" ".into()));
    e.push_child(Content::Elt(keyword));
    for part in matched.parts() {
        match part {
            PartialStep::UncapturedText(snippet) => {
                let text = snippet.text();
                if !text.trim().is_empty() {
                    let mut estep = Element::new(ElementTag::Span);
                    estep.push_attribute(Attribute::new("class", "uncaptured"));
                    estep.push_child(Content::Text(text.into()));
                    e.push_child(Content::Elt(estep));
                }
            }
            PartialStep::CapturedText {
                name: _,
                text,
                kind,
            } => {
                if !text.trim().is_empty() {
                    let mut estep = Element::new(ElementTag::Span);
                    let class = format!("capture-{}", kind.as_str());
                    estep.push_attribute(Attribute::new("class", &class));
                    estep.push_child(Content::Text(text.into()));
                    e.push_child(Content::Elt(estep));
                }
            }
        }
    }
    e
}

fn file(e: &Element) -> Result<Element, SubplotError> {
    Ok(e.clone()) // FIXME
}

fn example(e: &Element) -> Result<Element, SubplotError> {
    Ok(e.clone()) // FIXME
}

fn dot(e: &Element) -> Result<Element, SubplotError> {
    let dot = e.content();
    let svg = DotMarkup::new(&dot).as_svg()?;
    Ok(svg_to_element(svg, "Dot diagram"))
}

fn plantuml(e: &Element) -> Result<Element, SubplotError> {
    let markup = e.content();
    let svg = PlantumlMarkup::new(&markup).as_svg()?;
    Ok(svg_to_element(svg, "UML diagram"))
}

fn pikchr(e: &Element) -> Result<Element, SubplotError> {
    let markup = e.content();
    let svg = PikchrMarkup::new(&markup, None).as_svg()?;
    Ok(svg_to_element(svg, "Pikchr diagram"))
}

fn roadmap(e: &Element) -> Result<Element, SubplotError> {
    const WIDTH: usize = 50;

    let yaml = e.content();
    let roadmap = roadmap::from_yaml(&yaml)?;
    let dot = roadmap.format_as_dot(WIDTH)?;
    let svg = DotMarkup::new(&dot).as_svg()?;
    Ok(svg_to_element(svg, "Road map"))
}

fn svg_to_element(svg: Svg, alt: &str) -> Element {
    let url = svg_as_data_url(svg);
    let img = html_img(&url, alt);
    html_p(vec![Content::Elt(img)])
}

fn svg_as_data_url(svg: Svg) -> String {
    let svg = BASE64_STANDARD.encode(svg.data());
    format!("data:image/svg+xml;base64,{svg}")
}

fn html_p(children: Vec<Content>) -> Element {
    let mut new = Element::new(ElementTag::P);
    for child in children {
        new.push_child(child);
    }
    new
}

fn html_img(src: &str, alt: &str) -> Element {
    let mut new = Element::new(ElementTag::Img);
    new.push_attribute(Attribute::new("src", src));
    new.push_attribute(Attribute::new("alt", alt));
    new
}

#[derive(Debug, Default)]
struct HeadingNumberer {
    prev: Vec<usize>,
}

impl HeadingNumberer {
    fn number(&mut self, level: usize) -> String {
        match level.cmp(&self.prev.len()) {
            Ordering::Equal => {
                if let Some(n) = self.prev.pop() {
                    self.prev.push(n + 1);
                } else {
                    self.prev.push(1);
                }
            }
            Ordering::Greater => {
                self.prev.push(1);
            }
            Ordering::Less => {
                assert!(!self.prev.is_empty());
                self.prev.pop();
                if let Some(n) = self.prev.pop() {
                    self.prev.push(n + 1);
                } else {
                    self.prev.push(1);
                }
            }
        }

        let mut s = String::new();
        for i in self.prev.iter() {
            if !s.is_empty() {
                s.push('.');
            }
            s.push_str(&i.to_string());
        }
        s
    }
}

#[cfg(test)]
mod test_numberer {
    use super::HeadingNumberer;

    #[test]
    fn numbering() {
        let mut n = HeadingNumberer::default();
        assert_eq!(n.number(1), "1");
        assert_eq!(n.number(2), "1.1");
        assert_eq!(n.number(1), "2");
        assert_eq!(n.number(2), "2.1");
    }
}