diff options
| author | Federico Igne <git@federicoigne.com> | 2022-12-30 11:23:59 +0000 |
|---|---|---|
| committer | Federico Igne <git@federicoigne.com> | 2022-12-30 12:35:18 +0000 |
| commit | 909876999eb1eb968f71e6f8b199689cfc68f711 (patch) | |
| tree | d8f7080ebec3c7f1723c0ceb68ca39c63481533f /src | |
| parent | 6f8fa69e1ad8ad68c8d1afa455b414cd459bb498 (diff) | |
| download | pangler-909876999eb1eb968f71e6f8b199689cfc68f711.tar.gz pangler-909876999eb1eb968f71e6f8b199689cfc68f711.zip | |
fix: allow more flexible entrypoint definition
Entrypoints can now be forced by using the class `entry` or by
specifying an attribute `path`.
Compatibility with previous versions is maintained and any identifier
that actually looks like a file is considered as such.
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 45 |
1 files changed, 33 insertions, 12 deletions
diff --git a/src/main.rs b/src/main.rs index 02fbcd4..7d2f786 100644 --- a/src/main.rs +++ b/src/main.rs | |||
| @@ -27,7 +27,22 @@ struct Config { | |||
| 27 | input: Vec<PathBuf>, | 27 | input: Vec<PathBuf>, |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | type Blocks<'a> = HashMap<String,Cow<'a,str>>; | 30 | #[derive(Eq, Hash, PartialEq)] |
| 31 | enum Key { | ||
| 32 | Macro(String), | ||
| 33 | Entry(PathBuf) | ||
| 34 | } | ||
| 35 | |||
| 36 | impl Key { | ||
| 37 | fn get_path(&self) -> Option<&PathBuf> { | ||
| 38 | match self { | ||
| 39 | Self::Entry(s) => Some(&s), | ||
| 40 | Self::Macro(_) => None | ||
| 41 | } | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | type Blocks<'a> = HashMap<Key,Cow<'a,str>>; | ||
| 31 | 46 | ||
| 32 | fn build( | 47 | fn build( |
| 33 | base: &Option<PathBuf>, | 48 | base: &Option<PathBuf>, |
| @@ -35,10 +50,6 @@ fn build( | |||
| 35 | max_depth: u32 | 50 | max_depth: u32 |
| 36 | ) { | 51 | ) { |
| 37 | lazy_static! { | 52 | lazy_static! { |
| 38 | static ref PATH: Regex = | ||
| 39 | Regex::new( | ||
| 40 | r"^(?:[[:word:]\.-]+/)*[[:word:]\.-]+\.[[:alpha:]]+$" | ||
| 41 | ).unwrap(); | ||
| 42 | static ref MACRO: Regex = | 53 | static ref MACRO: Regex = |
| 43 | Regex::new( | 54 | Regex::new( |
| 44 | r"(?m)^([[:blank:]]*)<<([^>\s]+)>>" | 55 | r"(?m)^([[:blank:]]*)<<([^>\s]+)>>" |
| @@ -46,7 +57,8 @@ fn build( | |||
| 46 | } | 57 | } |
| 47 | blocks | 58 | blocks |
| 48 | .iter() | 59 | .iter() |
| 49 | .for_each(|(path,code)| if PATH.is_match(path) { | 60 | .filter_map(|(key,code)| { key.get_path().map(|k| (k,code)) }) |
| 61 | .for_each(|(path,code)| { | ||
| 50 | let mut current_depth = 0; | 62 | let mut current_depth = 0; |
| 51 | let mut code = code.clone(); | 63 | let mut code = code.clone(); |
| 52 | while let Cow::Owned(new_code) = MACRO.replace_all( | 64 | while let Cow::Owned(new_code) = MACRO.replace_all( |
| @@ -54,7 +66,7 @@ fn build( | |||
| 54 | |caps: &Captures| { | 66 | |caps: &Captures| { |
| 55 | if current_depth < max_depth { | 67 | if current_depth < max_depth { |
| 56 | let block = blocks | 68 | let block = blocks |
| 57 | .get(&caps[2]) | 69 | .get(&Key::Macro(caps[2].to_string())) |
| 58 | .expect("Block not present") | 70 | .expect("Block not present") |
| 59 | .clone(); | 71 | .clone(); |
| 60 | indent(block, caps[1].len()) | 72 | indent(block, caps[1].len()) |
| @@ -132,11 +144,20 @@ fn main() -> Result<()> { | |||
| 132 | if let Block::CodeBlock((id,clss,attrs), code) = block { | 144 | if let Block::CodeBlock((id,clss,attrs), code) = block { |
| 133 | if !id.is_empty() { | 145 | if !id.is_empty() { |
| 134 | let key = { | 146 | let key = { |
| 135 | let path = attrs.iter().find(|(k,_)| k == "path"); | 147 | lazy_static! { |
| 136 | if let Some(path) = path { | 148 | static ref PATH: Regex = |
| 137 | format!("{}{}", path.1, id) | 149 | Regex::new( |
| 150 | r"^(?:[[:word:]\.-]+/)*[[:word:]\.-]+\.[[:alpha:]]+$" | ||
| 151 | ).unwrap(); | ||
| 152 | } | ||
| 153 | let entry = clss.contains(&String::from("entry")); | ||
| 154 | let path = attrs | ||
| 155 | .into_iter() | ||
| 156 | .find_map(|(k,p)| if k == "path" { Some(p.clone()) } else { None }); | ||
| 157 | if entry || path.is_some() || PATH.is_match(id) { | ||
| 158 | Key::Entry(PathBuf::from(path.unwrap_or_default()).join(id)) | ||
| 138 | } else { | 159 | } else { |
| 139 | id.to_string() | 160 | Key::Macro(id.to_string()) |
| 140 | } | 161 | } |
| 141 | }; | 162 | }; |
| 142 | if clss.iter().any(|c| c == "override") { | 163 | if clss.iter().any(|c| c == "override") { |
| @@ -162,4 +183,4 @@ fn main() -> Result<()> { | |||
| 162 | ); | 183 | ); |
| 163 | pandoc.execute().unwrap(); | 184 | pandoc.execute().unwrap(); |
| 164 | Ok(()) | 185 | Ok(()) |
| 165 | } \ No newline at end of file | 186 | } |
