diff options
| author | Federico Igne <git@federicoigne.com> | 2022-04-22 22:13:50 +0100 |
|---|---|---|
| committer | Federico Igne <git@federicoigne.com> | 2022-04-22 22:13:50 +0100 |
| commit | f5df9e6e301e5dd76b1b93ee01ad4035c9db90b0 (patch) | |
| tree | b4371b3f0a10534f1e5f4ac1c1932ef149f33d73 | |
| parent | 69cd74abbfaa3fa75b8d438b38e67db2f085dd55 (diff) | |
| download | pangler-f5df9e6e301e5dd76b1b93ee01ad4035c9db90b0.tar.gz pangler-f5df9e6e301e5dd76b1b93ee01ad4035c9db90b0.zip | |
Add first attempt at tangler
| -rw-r--r-- | Cargo.toml | 13 | ||||
| -rw-r--r-- | src/main.rs | 50 |
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] | ||
| 2 | name = "pangler" | ||
| 3 | version = "0.1.0" | ||
| 4 | edition = "2021" | ||
| 5 | |||
| 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
| 7 | |||
| 8 | [dependencies] | ||
| 9 | lazy_static = "1.4" | ||
| 10 | regex = "1.5" | ||
| 11 | pandoc = "0.8" | ||
| 12 | pandoc_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 @@ | |||
| 1 | use std::io::Result; | ||
| 2 | use std::path::PathBuf; | ||
| 3 | use std::collections::HashMap; | ||
| 4 | use lazy_static::lazy_static; | ||
| 5 | use regex::{Captures,Regex}; | ||
| 6 | use pandoc::{InputFormat,InputKind,OutputFormat,OutputKind,Pandoc}; | ||
| 7 | use pandoc_ast::Block; | ||
| 8 | |||
| 9 | type Blocks<'a> = HashMap<&'a str,&'a str>; | ||
| 10 | |||
| 11 | fn 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 | |||
| 22 | fn 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 | |||
