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
//! Extract code files from Markdown files.
//!
//! Write guides in Markdown with code blocks that belong in several files, and
//! let _waltz_ extract the code for you so you can build/run/test it easily.
//!
//! Meant as a companion to [tango].
//!
//! [tango]: https://github.com/pnkfelix/tango

#![deny(missing_docs)]

#[macro_use]
extern crate failure;

#[macro_use]
extern crate log;
#[macro_use]
extern crate lazy_static;
extern crate pulldown_cmark;
extern crate regex;

use pulldown_cmark::{Event, Tag};

mod code_block;
pub use code_block::CodeBlock;

mod code_flags;
use code_flags::CodeFlags;

#[derive(Debug, Clone, Copy)]
enum Location {
    SomewhereUnimportant,
    InCodeBlock,
}

/// Extract code blocks from Markdown events
///
/// The input needs to be an Iterator as returned by [pulldown-cmark]'s parser.
///
/// [pulldown-cmark]: https://github.com/google/pulldown-cmark
///
/// # Examples
///
/// ```rust
/// extern crate waltz;
/// extern crate pulldown_cmark;
///
/// let example =
///     r#"```rust,file=examples/demo.rs
///     pub const: &'static str = "Yeah!";
///     ```"#;
/// let markdown = pulldown_cmark::Parser::new(example);
/// let code_blocks = waltz::extract_code_blocks(markdown).unwrap();
/// assert_eq!(code_blocks[0].filename(), Some("examples/demo.rs".to_string()));
/// ```
pub fn extract_code_blocks<'md, I: Iterator<Item = Event<'md>>>(
    md_events: I,
) -> Result<Vec<CodeBlock>, failure::Error> {
    let mut code_blocks = Vec::new();
    let mut location = Location::SomewhereUnimportant;
    let mut current_code_block = CodeBlock::default();

    for event in md_events {
        match (event, location) {
            (Event::Start(Tag::CodeBlock(flags)), Location::SomewhereUnimportant) => {
                location = Location::InCodeBlock;
                let flags = flags.parse::<CodeFlags>()?;

                trace!(
                    "found code block{}",
                    if let Some(f) = flags.filename() {
                        format!(" with file name `{}`", f)
                    } else {
                        " without a file name".to_string()
                    }
                );

                current_code_block.set_flags(flags);
            }
            (Event::Text(code), Location::InCodeBlock) => {
                current_code_block.push_content(&code);
            }
            (Event::End(Tag::CodeBlock(_lang)), Location::InCodeBlock) => {
                location = Location::SomewhereUnimportant;
                trace!(
                    "end of code block for file `{}`",
                    current_code_block
                        .filename()
                        .unwrap_or_else(|| "<unnamed>".to_string())
                );
                code_blocks.push(current_code_block.clone());
                current_code_block = CodeBlock::default();
            }
            _ => {}
        }
    }

    Ok(code_blocks)
}