summaryrefslogtreecommitdiff
path: root/src/ast.rs
blob: 7efe83670268d4546131e4ef5d8b4bc2dc8ee196 (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
435
436
437
438
439
440
441
442
443
444
445
use lazy_static::lazy_static;
use log::trace;
use pandoc_ast::{Attr, Block, Inline, Map, MetaValue, Pandoc};
use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag};
use regex::Regex;
use serde::Deserialize;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

lazy_static! {
    // Pattern that recognises a YAML block at the beginning of a file.
    static ref LEADING_YAML_PATTERN: Regex = Regex::new(r"^(?:\S*\n)*(?P<yaml>-{3,}\n([^.].*\n)*\.{3,}\n)(?P<text>(.*\n)*)$").unwrap();


    // Pattern that recognises a YAML block at the end of a file.
    static ref TRAILING_YAML_PATTERN: Regex = Regex::new(r"(?P<text>(.*\n)*)\n*(?P<yaml>-{3,}\n([^.].*\n)*\.{3,}\n)(?:\S*\n)*$").unwrap();
}

/// An abstract syntax tree representation of a Markdown file.
///
/// This represents a Markdown file as an abstract syntax tree
/// compatible with Pandoc's AST. The document YAML metadata MUST be
/// at the top or bottom of the file, excluding leading or trailing
/// empty lines.
#[derive(Debug)]
pub struct AbstractSyntaxTree {
    blocks: Vec<Block>,
    meta: YamlMetadata,
}

impl AbstractSyntaxTree {
    /// Create a new AST.
    pub fn new(meta: YamlMetadata, markdown: &str) -> Self {
        let blocks = parse_blocks(markdown);
        Self { blocks, meta }
    }

    /// Return a Pandoc-compatible AST.
    pub fn to_pandoc(&self) -> Pandoc {
        Pandoc {
            meta: self.meta.to_map(),
            blocks: self.blocks.clone(),
            pandoc_api_version: vec![1, 20],
        }
    }
}

/// Extract YAML metadata from a Markdown document.
pub fn extract_metadata(markdown: &str) -> Result<(YamlMetadata, &str), Error> {
    trace!("Extracting YAML from Markdown");
    let (yaml, md) = if let Some((yaml, markdown)) = get_yaml(&LEADING_YAML_PATTERN, markdown) {
        trace!("Found leading YAML: {:?}", yaml);
        (yaml, markdown)
    } else if let Some((yaml, _markdown)) = get_yaml(&TRAILING_YAML_PATTERN, markdown) {
        trace!("Found trailing YAML: {:?}", yaml);
        (yaml, markdown)
    } else {
        trace!("No YAML to be found");
        return Err(Error::NoMetadata);
    };
    let meta = YamlMetadata::new(yaml)?;
    trace!("Parsing markdown: OK");
    Ok((meta, md))
}

// Extract a YAML metadata block using a given regex.
fn get_yaml<'a>(pat: &Regex, markdown: &'a str) -> Option<(&'a str, &'a str)> {
    trace!("Markdown: {:?}", markdown);
    if let Some(c) = pat.captures(markdown) {
        trace!("YAML regex matches: {:?}", c);
        let yaml = c.name("yaml");
        let text = c.name("text");
        trace!("YAML metadata: {:?}", yaml);
        trace!("markdown: {:?}", text);
        if yaml.is_some() && text.is_some() {
            trace!("YAML regex captures YAML and text");
            let yaml = yaml?;
            let text = text?;
            let yaml = &markdown[yaml.start()..yaml.end()];
            let text = &markdown[text.start()..text.end()];
            assert!(yaml.starts_with("---"));
            assert!(yaml.ends_with("...\n"));
            return Some((yaml, text));
        } else {
            trace!("YAML regex fails to capture YAML");
        }
    } else {
        trace!("YAML regex does not match");
    }
    None
}

// Parse Markdown into a sequence of Blocks.
fn parse_blocks(markdown: &str) -> Vec<Block> {
    trace!("Parsing blocks");

    // Define the Markdown parser.
    let mut options = Options::empty();
    options.insert(Options::ENABLE_TABLES);
    options.insert(Options::ENABLE_FOOTNOTES);
    options.insert(Options::ENABLE_STRIKETHROUGH);
    options.insert(Options::ENABLE_TASKLISTS);
    options.insert(Options::ENABLE_SMART_PUNCTUATION);
    let parser = Parser::new_ext(markdown, options);

    // The sequence of blocks that represents the parsed document.
    let mut blocks = vec![];

    // The current set of inline elements we've collected. This gets
    // emptied whenever we finish a block.
    let mut inlines: Vec<Inline> = vec![];

    for event in parser {
        trace!("Parsing event: {:?}", event);
        match event {
            // We ignore these for now. They're not needed for codegen.
            Event::Html(_)
            | Event::FootnoteReference(_)
            | Event::SoftBreak
            | Event::HardBreak
            | Event::Rule
            | Event::TaskListMarker(_) => (),

            // Inline text of various kinds.
            Event::Text(text) => inlines.push(inline_text(&text)),
            Event::Code(text) => inlines.push(inline_code(&text)),

            // We only handle the end events.
            Event::Start(_) => (),

            // End of a block or inline.
            Event::End(tag) => match tag {
                // Collect inline elements for later inclusion in a block.
                Tag::Emphasis | Tag::Strong | Tag::Strikethrough => {
                    inline_from_inlines(&tag, &mut inlines)
                }
                Tag::Paragraph => blocks.push(paragraph(&mut inlines)),
                Tag::Heading(level, _fragment, _classes) => {
                    blocks.push(heading(level as i64, &mut inlines))
                }
                Tag::CodeBlock(kind) => blocks.push(code_block(&kind, &mut inlines)),
                Tag::Image(_link, dest, title) => blocks.push(image_block(&dest, &title)),
                // We don't handle anything else yet.
                _ => (),
            },
        }
    }

    // We MUST have emptied all inline elements.
    // assert!(inlines.is_empty());

    trace!("Parsing blocks: OK");
    blocks
}

fn inline_text(text: &str) -> Inline {
    Inline::Str(text.to_string())
}

fn inline_code(text: &str) -> Inline {
    let attr = ("".to_string(), vec![], vec![]);
    Inline::Code(attr, text.to_string())
}

fn paragraph(inlines: &mut Vec<Inline>) -> Block {
    Block::Para(std::mem::take(inlines))
}

fn heading(level: i64, inlines: &mut Vec<Inline>) -> Block {
    let attr = ("".to_string(), vec![], vec![]);
    Block::Header(level, attr, std::mem::take(inlines))
}

fn image_block(dest: &str, title: &str) -> Block {
    let attr = ("".to_string(), vec![], vec![]);
    Block::Para(vec![Inline::Image(
        attr,
        vec![],
        (dest.to_string(), title.to_string()),
    )])
}

fn code_block(kind: &CodeBlockKind, inlines: &mut Vec<Inline>) -> Block {
    trace!("code block: {:?}", kind);
    let attr = if let CodeBlockKind::Fenced(lang) = kind {
        trace!("fenced code block, lang={:?}", lang);
        parse_code_block_attrs(lang)
    } else {
        trace!("indented code block");
        parse_code_block_attrs("")
    };
    trace!("code block attrs: {:?}", attr);
    let mut code = String::new();
    for inline in inlines.drain(0..) {
        let text = plain_text_inline(inline);
        code.push_str(&text);
    }
    // pulldown_cmark and pandoc differ in their codeblock handling,
    // pulldown_cmark has an extra newline which we trim for now to be
    // compatible with pandoc's parsing
    if !code.is_empty() {
        assert_eq!(code.pop(), Some('\n'));
    }
    Block::CodeBlock(attr, code)
}

fn plain_text_inline(inline: Inline) -> String {
    match inline {
        Inline::Str(text) => text,
        Inline::Code(_, text) => text,
        Inline::Emph(inlines) => {
            let mut text = String::new();
            for inline in inlines {
                text.push_str(&plain_text_inline(inline));
            }
            text
        }
        _ => panic!("not text in code block: {:?}", inline),
    }
}

fn parse_code_block_attrs(attrs: &str) -> Attr {
    trace!("parsing code block attrs: {:?}", attrs);
    let mut id = "".to_string();
    let mut classes = vec![];
    let mut keyvalues = vec![];
    if attrs.starts_with('{') && attrs.ends_with('}') {
        let attrs = &attrs[1..attrs.len() - 1];
        for word in attrs.split_ascii_whitespace() {
            if let Some(x) = word.strip_prefix('#') {
                id = x.to_string();
            } else if let Some(x) = word.strip_prefix('.') {
                classes.push(x.to_string());
            } else if let Some(i) = word.find('=') {
                let k = &word[..i];
                let v = &word[i + 1..];
                keyvalues.push((k.to_string(), v.to_string()));
            }
        }
    } else if !attrs.is_empty() {
        classes.push(attrs.to_string());
    }
    (id, classes, keyvalues)
}

fn inline_from_inlines(tag: &Tag, inlines: &mut Vec<Inline>) {
    let new_inlines = inlines.clone();
    inlines.clear();

    let inline = match tag {
        Tag::Emphasis => Inline::Emph(new_inlines),
        Tag::Strong => Inline::Strong(new_inlines),
        Tag::Strikethrough => Inline::Strikeout(new_inlines),
        _ => unreachable!(),
    };

    inlines.push(inline);
}

/// Errors from Markdown parsing.
#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error(transparent)]
    Regex(#[from] regex::Error),

    #[error("Markdown doesn't contain a YAML block for document metadata")]
    NoMetadata,

    #[error(transparent)]
    Yaml(#[from] serde_yaml::Error),
}

/// Document metadata.
///
/// This is expressed in the Markdown input file as an embedded YAML
/// block.
///
/// Note that this structure needs to be able to capture any metadata
/// block we can work with, in any input file. By being strict here we
/// make it easier to tell the user when a metadata block has, say, a
/// misspelled field.
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct YamlMetadata {
    title: String,
    subtitle: Option<String>,
    author: Option<String>,
    date: Option<String>,
    classes: Option<Vec<String>>,
    bibliography: Option<Vec<PathBuf>>,
    markdowns: Vec<PathBuf>,
    bindings: Option<Vec<PathBuf>>,
    documentclass: Option<String>,
    #[serde(default)]
    impls: BTreeMap<String, Vec<PathBuf>>,
}

impl YamlMetadata {
    fn new(yaml_text: &str) -> Result<Self, Error> {
        trace!("Parsing YAML");
        let meta: Self = serde_yaml::from_str(yaml_text)?;
        Ok(meta)
    }

    /// Name of file with the Markdown for the subplot document.
    pub fn markdown(&self) -> &Path {
        &self.markdowns[0]
    }

    /// Convert into a pandoc_ast::Map.
    pub fn to_map(&self) -> Map<String, MetaValue> {
        trace!("Creating metadata map from parsed YAML");
        let mut map: Map<String, MetaValue> = Map::new();

        map.insert("title".into(), meta_string(&self.title));

        if let Some(v) = &self.subtitle {
            map.insert("subtitle".into(), meta_string(v));
        }

        if let Some(v) = &self.author {
            map.insert("author".into(), meta_string(v));
        }

        if let Some(v) = &self.date {
            map.insert("date".into(), meta_string(v));
        }

        if let Some(v) = &self.classes {
            map.insert("classes".into(), meta_strings(v));
        }

        if !self.impls.is_empty() {
            let impls = self
                .impls
                .iter()
                .map(|(k, v)| (k.to_owned(), Box::new(meta_path_bufs(v))))
                .collect();
            map.insert("impls".into(), MetaValue::MetaMap(impls));
        }

        if let Some(v) = &self.bibliography {
            map.insert("bibliography".into(), meta_path_bufs(v));
        }

        if let Some(v) = &self.bindings {
            map.insert("bindings".into(), meta_path_bufs(v));
        }

        if let Some(v) = &self.documentclass {
            map.insert("documentclass".into(), meta_string(v));
        }

        trace!("Created metadata map from parsed YAML");
        map
    }
}

fn meta_string(s: &str) -> MetaValue {
    MetaValue::MetaString(s.to_string())
}

fn meta_strings(v: &[String]) -> MetaValue {
    MetaValue::MetaList(v.iter().map(|s| meta_string(s)).collect())
}

fn meta_path_buf(p: &Path) -> MetaValue {
    meta_string(&p.display().to_string())
}

fn meta_path_bufs(v: &[PathBuf]) -> MetaValue {
    MetaValue::MetaList(v.iter().map(|p| meta_path_buf(p)).collect())
}

#[cfg(test)]
mod test {
    use super::{parse_code_block_attrs, YamlMetadata};
    use std::path::{Path, PathBuf};

    #[test]
    fn code_block_attrs() {
        assert_eq!(parse_code_block_attrs(""), ("".to_string(), vec![], vec![]));
        assert_eq!(
            parse_code_block_attrs("foo"),
            ("".to_string(), vec!["foo".to_string()], vec![])
        );
        assert_eq!(
            parse_code_block_attrs("{#foo}"),
            ("foo".to_string(), vec![], vec![])
        );
        assert_eq!(
            parse_code_block_attrs("{#foo .file bar=yo}"),
            (
                "foo".to_string(),
                vec!["file".to_string()],
                vec![("bar".to_string(), "yo".to_string())]
            )
        );
    }

    #[test]
    fn full_meta() {
        let meta = YamlMetadata::new(
            "\
title: Foo Bar
date: today
classes: [json, text]
impls:
  python:
   - foo.py
   - bar.py
bibliography:
- foo.bib
- bar.bib
markdowns:
- test.md
bindings:
- foo.yaml
- bar.yaml
",
        )
        .unwrap();
        assert_eq!(meta.title, "Foo Bar");
        assert_eq!(meta.date.unwrap(), "today");
        assert_eq!(meta.classes.unwrap(), &["json", "text"]);
        assert_eq!(
            meta.bibliography.unwrap(),
            &[path("foo.bib"), path("bar.bib")]
        );
        assert_eq!(meta.markdowns, vec![Path::new("test.md")]);
        assert_eq!(
            meta.bindings.unwrap(),
            &[path("foo.yaml"), path("bar.yaml")]
        );
        assert!(!meta.impls.is_empty());
        for (k, v) in meta.impls.iter() {
            assert_eq!(k, "python");
            assert_eq!(v, &[path("foo.py"), path("bar.py")]);
        }
    }

    fn path(s: &str) -> PathBuf {
        PathBuf::from(s)
    }
}