diff options
| author | Federico Igne <git@federicoigne.com> | 2022-04-28 15:43:56 +0100 |
|---|---|---|
| committer | Federico Igne <git@federicoigne.com> | 2022-05-27 00:48:17 +0100 |
| commit | 8599b6196a6e6a6ded655b1475a022d8b306a158 (patch) | |
| tree | 9d278f57b744cf6459d1b38ab033122cbc14d7e5 | |
| parent | eecbd9ec0c2712c68d0b459e7fe5f7eb769b2d2c (diff) | |
| download | pangler-8599b6196a6e6a6ded655b1475a022d8b306a158.tar.gz pangler-8599b6196a6e6a6ded655b1475a022d8b306a158.zip | |
feat: generate files from entry macros
| -rw-r--r-- | src/main.rs | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs index 984c48c..cf2b551 100644 --- a/src/main.rs +++ b/src/main.rs | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | use std::io::Result; | 1 | use std::io::Result; |
| 2 | use std::fs; | ||
| 2 | use std::borrow::Cow; | 3 | use std::borrow::Cow; |
| 3 | use std::path::PathBuf; | 4 | use std::path::{Path,PathBuf}; |
| 4 | use std::collections::HashMap; | 5 | use std::collections::HashMap; |
| 5 | use lazy_static::lazy_static; | 6 | use lazy_static::lazy_static; |
| 6 | use regex::{Captures,Regex}; | 7 | use regex::{Captures,Regex}; |
| @@ -9,6 +10,18 @@ use pandoc_ast::Block; | |||
| 9 | 10 | ||
| 10 | type Blocks<'a> = HashMap<String,Cow<'a,str>>; | 11 | type Blocks<'a> = HashMap<String,Cow<'a,str>>; |
| 11 | 12 | ||
| 13 | /* Write code to target file */ | ||
| 14 | fn write_to_file<P: AsRef<Path>>(path: P, content: &str) -> std::io::Result<()> { | ||
| 15 | let base: PathBuf = PathBuf::from("code"); | ||
| 16 | if path.as_ref().is_relative() { | ||
| 17 | let path = base.join(path); | ||
| 18 | /* There is always *at least* the base directory as a parent */ | ||
| 19 | fs::create_dir_all(path.parent().unwrap())?; | ||
| 20 | fs::write(path, content)?; | ||
| 21 | } else { } | ||
| 22 | Ok(()) | ||
| 23 | } | ||
| 24 | |||
| 12 | /* | 25 | /* |
| 13 | * Here are some notes on the following function | 26 | * Here are some notes on the following function |
| 14 | * | 27 | * |
| @@ -68,7 +81,7 @@ fn build(blocks: &Blocks) { | |||
| 68 | ) { | 81 | ) { |
| 69 | code = Cow::from(step); | 82 | code = Cow::from(step); |
| 70 | } | 83 | } |
| 71 | println!("[[{}]]\n{}", k, code); | 84 | write_to_file(k, &code).expect("Unable to write to file"); |
| 72 | }) | 85 | }) |
| 73 | } | 86 | } |
| 74 | 87 | ||
| @@ -87,6 +100,7 @@ fn main() -> Result<()> { | |||
| 87 | if let Block::CodeBlock((id,classes,attrs), code) = block { | 100 | if let Block::CodeBlock((id,classes,attrs), code) = block { |
| 88 | // dbg!(block); | 101 | // dbg!(block); |
| 89 | if !id.is_empty() { | 102 | if !id.is_empty() { |
| 103 | // TODO: si puo' migliorare | ||
| 90 | let mut key = attrs.iter() | 104 | let mut key = attrs.iter() |
| 91 | .find_map(|(k,v)| if k == "path" { Some(v.clone()) } else { None }) | 105 | .find_map(|(k,v)| if k == "path" { Some(v.clone()) } else { None }) |
| 92 | .unwrap_or(String::from("")); | 106 | .unwrap_or(String::from("")); |
