aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.toml13
-rw-r--r--src/main.rs50
2 files changed, 63 insertions, 0 deletions
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..ccf3662
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,13 @@
1[package]
2name = "pangler"
3version = "0.1.0"
4edition = "2021"
5
6# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7
8[dependencies]
9lazy_static = "1.4"
10regex = "1.5"
11pandoc = "0.8"
12pandoc_ast = "0.8"
13
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..13824df
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,50 @@
1use std::io::Result;
2use std::path::PathBuf;
3use std::collections::HashMap;
4use lazy_static::lazy_static;
5use regex::{Captures,Regex};
6use pandoc::{InputFormat,InputKind,OutputFormat,OutputKind,Pandoc};
7use pandoc_ast::Block;
8
9type Blocks<'a> = HashMap<&'a str,&'a str>;
10
11fn build(blocks: &Blocks, entry: &str) -> String {
12 lazy_static! {
13 static ref RE: Regex = Regex::new(r"<<([^>\s]+)>>").unwrap();
14 }
15 if let Some(entry) = blocks.get(entry).clone() {
16 RE.replace_all(entry, |caps: &Captures| blocks.get(&caps[1]).expect("Block not present")).to_string()
17 } else {
18 String::from("")
19 }
20}
21
22fn main() -> Result<()> {
23 let mut pandoc = Pandoc::new();
24 /* Pandoc input setup */
25 pandoc.set_input(InputKind::Files(vec![PathBuf::from("test.md")]));
26 pandoc.set_input_format(InputFormat::Markdown, vec![]);
27 /* Pandoc output setup */
28 pandoc.set_output(OutputKind::Pipe);
29 pandoc.set_output_format(OutputFormat::Json, vec![]);
30 /* Process literate program */
31 pandoc.add_filter(|json| pandoc_ast::filter(json, |pandoc| {
32 let mut blocks: Blocks = HashMap::new();
33 pandoc.blocks.iter().for_each(|block|
34 if let Block::CodeBlock(attr, code) = block {
35 if attr.0.len() > 0 {
36 blocks.insert(&attr.0, &code);
37 } /*else {
38 println!("The following code has no ID:");
39 code.lines().for_each(|l| println!(" {}", l));
40 }*/
41 }
42 );
43 let program = build(&blocks, "main.rs");
44 println!("{}", program);
45 pandoc
46 }));
47 pandoc.execute().unwrap();
48 Ok(())
49}
50