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
|
use std::io::Result;
use std::path::PathBuf;
use std::collections::HashMap;
use lazy_static::lazy_static;
use regex::{Captures,Regex};
use pandoc::{InputFormat,InputKind,OutputFormat,OutputKind,Pandoc};
use pandoc_ast::Block;
type Blocks<'a> = HashMap<&'a str,&'a str>;
fn build(blocks: &Blocks, entry: &str) -> String {
lazy_static! {
static ref RE: Regex = Regex::new(r"<<([^>\s]+)>>").unwrap();
}
if let Some(entry) = blocks.get(entry).clone() {
RE.replace_all(entry, |caps: &Captures| blocks.get(&caps[1]).expect("Block not present")).to_string()
} else {
String::from("")
}
}
fn main() -> Result<()> {
let mut pandoc = Pandoc::new();
/* Pandoc input setup */
pandoc.set_input(InputKind::Files(vec![PathBuf::from("test.md")]));
pandoc.set_input_format(InputFormat::Markdown, vec![]);
/* Pandoc output setup */
pandoc.set_output(OutputKind::Pipe);
pandoc.set_output_format(OutputFormat::Json, vec![]);
/* Process literate program */
pandoc.add_filter(|json| pandoc_ast::filter(json, |pandoc| {
let mut blocks: Blocks = HashMap::new();
pandoc.blocks.iter().for_each(|block|
if let Block::CodeBlock(attr, code) = block {
if attr.0.len() > 0 {
blocks.insert(&attr.0, &code);
} /*else {
println!("The following code has no ID:");
code.lines().for_each(|l| println!(" {}", l));
}*/
}
);
let program = build(&blocks, "main.rs");
println!("{}", program);
pandoc
}));
pandoc.execute().unwrap();
Ok(())
}
|