diff options
author | Federico Igne <git@federicoigne.com> | 2022-12-30 13:43:32 +0000 |
---|---|---|
committer | Federico Igne <git@federicoigne.com> | 2022-12-30 13:43:32 +0000 |
commit | a39e120e3fa6723c7ad8c4d27afca79233947935 (patch) | |
tree | dd86083453db4260e016457bdfd3b6ee4b57157b | |
parent | 909876999eb1eb968f71e6f8b199689cfc68f711 (diff) | |
download | pangler-a39e120e3fa6723c7ad8c4d27afca79233947935.tar.gz pangler-a39e120e3fa6723c7ad8c4d27afca79233947935.zip |
feat(target): allow limiting the generated entrypoints
Introduces the `-t/--target` flag that can be used to provide a path.
Only entry points that have this path as a prefix will be generated.
-rw-r--r-- | README.md | 38 | ||||
-rw-r--r-- | src/main.rs | 35 |
2 files changed, 58 insertions, 15 deletions
@@ -124,12 +124,19 @@ if !id.is_empty() { | |||
124 | .into_iter() | 124 | .into_iter() |
125 | .find_map(|(k,p)| if k == "path" { Some(p.clone()) } else { None }); | 125 | .find_map(|(k,p)| if k == "path" { Some(p.clone()) } else { None }); |
126 | if entry || path.is_some() || PATH.is_match(id) { | 126 | if entry || path.is_some() || PATH.is_match(id) { |
127 | Key::Entry(PathBuf::from(path.unwrap_or_default()).join(id)) | 127 | let path = PathBuf::from(path.unwrap_or_default()).join(id); |
128 | if path.starts_with(&target) { | ||
129 | Some(Key::Entry(path)) | ||
130 | } else { | ||
131 | None | ||
132 | } | ||
128 | } else { | 133 | } else { |
129 | Key::Macro(id.to_string()) | 134 | Some(Key::Macro(id.to_string())) |
130 | } | 135 | } |
131 | }; | 136 | }; |
132 | <<code_block>> | 137 | if let Some(key) = key { |
138 | <<code_block>> | ||
139 | } | ||
133 | } else { | 140 | } else { |
134 | eprintln!("Ignoring code block without ID:"); | 141 | eprintln!("Ignoring code block without ID:"); |
135 | eprintln!("{}", indent(Cow::from(code),4)); | 142 | eprintln!("{}", indent(Cow::from(code),4)); |
@@ -231,6 +238,7 @@ The `struct` holding the CLI information is defined as follow | |||
231 | struct Config { | 238 | struct Config { |
232 | <<config_depth>> | 239 | <<config_depth>> |
233 | <<config_output>> | 240 | <<config_output>> |
241 | <<config_target>> | ||
234 | <<config_input>> | 242 | <<config_input>> |
235 | } | 243 | } |
236 | ``` | 244 | ``` |
@@ -241,6 +249,8 @@ and the arguments are parsed as | |||
241 | let config = Config::parse(); | 249 | let config = Config::parse(); |
242 | ``` | 250 | ``` |
243 | 251 | ||
252 | ## Input files | ||
253 | |||
244 | `pangler` accepts a sequence of files that will be parsed, code will be collected and used to build the final program. | 254 | `pangler` accepts a sequence of files that will be parsed, code will be collected and used to build the final program. |
245 | Note that the order of the file provided on the CLI is important when using the [overriding functionality](#writing-programs). | 255 | Note that the order of the file provided on the CLI is important when using the [overriding functionality](#writing-programs). |
246 | 256 | ||
@@ -249,6 +259,25 @@ Note that the order of the file provided on the CLI is important when using the | |||
249 | input: Vec<PathBuf>, | 259 | input: Vec<PathBuf>, |
250 | ``` | 260 | ``` |
251 | 261 | ||
262 | ## Specifying target entry points | ||
263 | |||
264 | By default `pangler` will generate all entry points gathered from the input file(s). | ||
265 | This behaviour can be overridden with the `-t/--target` flag. | ||
266 | |||
267 | ```{#config_target .rust} | ||
268 | /// Target files prefix | ||
269 | #[clap(short, long)] | ||
270 | target: Option<PathBuf>, | ||
271 | ``` | ||
272 | |||
273 | ```{#config_parse .rust} | ||
274 | let target = config.target.unwrap_or_default(); | ||
275 | ``` | ||
276 | |||
277 | Any entry point that does not have the provided target *as a prefix* will be ignored. | ||
278 | |||
279 | ## Custom output folder | ||
280 | |||
252 | By default, files are generated in the current working directory. | 281 | By default, files are generated in the current working directory. |
253 | 282 | ||
254 | ```{#constants .rust} | 283 | ```{#constants .rust} |
@@ -256,6 +285,7 @@ const BASE: &str = "./"; | |||
256 | ``` | 285 | ``` |
257 | 286 | ||
258 | This behaviour can be overridden using the `-o`/`--output` flag. | 287 | This behaviour can be overridden using the `-o`/`--output` flag. |
288 | If the output folder does not exists, *it will be created*. | ||
259 | 289 | ||
260 | ```{#config_output .rust} | 290 | ```{#config_output .rust} |
261 | /// Base output directory [default: './'] | 291 | /// Base output directory [default: './'] |
@@ -263,6 +293,8 @@ This behaviour can be overridden using the `-o`/`--output` flag. | |||
263 | output: Option<PathBuf>, | 293 | output: Option<PathBuf>, |
264 | ``` | 294 | ``` |
265 | 295 | ||
296 | ## Limiting recursion depth | ||
297 | |||
266 | Finally, recursive substitution of blocks can lead to an infinite loop. | 298 | Finally, recursive substitution of blocks can lead to an infinite loop. |
267 | By default, `pangler` will stop after 10 substitution iterations, but this parameter can be changed with the `-d`/`--depth` flag. | 299 | By default, `pangler` will stop after 10 substitution iterations, but this parameter can be changed with the `-d`/`--depth` flag. |
268 | 300 | ||
diff --git a/src/main.rs b/src/main.rs index 7d2f786..8168ad8 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -23,6 +23,9 @@ struct Config { | |||
23 | /// Base output directory [default: './'] | 23 | /// Base output directory [default: './'] |
24 | #[clap(short, long)] | 24 | #[clap(short, long)] |
25 | output: Option<PathBuf>, | 25 | output: Option<PathBuf>, |
26 | /// Target files prefix | ||
27 | #[clap(short, long)] | ||
28 | target: Option<PathBuf>, | ||
26 | /// Input files | 29 | /// Input files |
27 | input: Vec<PathBuf>, | 30 | input: Vec<PathBuf>, |
28 | } | 31 | } |
@@ -131,6 +134,7 @@ fn write_to_file( | |||
131 | 134 | ||
132 | fn main() -> Result<()> { | 135 | fn main() -> Result<()> { |
133 | let config = Config::parse(); | 136 | let config = Config::parse(); |
137 | let target = config.target.unwrap_or_default(); | ||
134 | let mut pandoc = Pandoc::new(); | 138 | let mut pandoc = Pandoc::new(); |
135 | pandoc.set_input(InputKind::Files(config.input)); | 139 | pandoc.set_input(InputKind::Files(config.input)); |
136 | pandoc.set_input_format(InputFormat::Markdown, vec![]); | 140 | pandoc.set_input_format(InputFormat::Markdown, vec![]); |
@@ -155,20 +159,27 @@ fn main() -> Result<()> { | |||
155 | .into_iter() | 159 | .into_iter() |
156 | .find_map(|(k,p)| if k == "path" { Some(p.clone()) } else { None }); | 160 | .find_map(|(k,p)| if k == "path" { Some(p.clone()) } else { None }); |
157 | if entry || path.is_some() || PATH.is_match(id) { | 161 | if entry || path.is_some() || PATH.is_match(id) { |
158 | Key::Entry(PathBuf::from(path.unwrap_or_default()).join(id)) | 162 | let path = PathBuf::from(path.unwrap_or_default()).join(id); |
163 | if path.starts_with(&target) { | ||
164 | Some(Key::Entry(path)) | ||
165 | } else { | ||
166 | None | ||
167 | } | ||
159 | } else { | 168 | } else { |
160 | Key::Macro(id.to_string()) | 169 | Some(Key::Macro(id.to_string())) |
161 | } | 170 | } |
162 | }; | 171 | }; |
163 | if clss.iter().any(|c| c == "override") { | 172 | if let Some(key) = key { |
164 | blocks.insert(key, Cow::from(code)); | 173 | if clss.iter().any(|c| c == "override") { |
165 | } else { | 174 | blocks.insert(key, Cow::from(code)); |
166 | blocks.entry(key) | 175 | } else { |
167 | .and_modify(|s| { | 176 | blocks.entry(key) |
168 | *s += "\n"; | 177 | .and_modify(|s| { |
169 | *s += Cow::from(code) | 178 | *s += "\n"; |
170 | }) | 179 | *s += Cow::from(code) |
171 | .or_insert(Cow::from(code)); | 180 | }) |
181 | .or_insert(Cow::from(code)); | ||
182 | } | ||
172 | } | 183 | } |
173 | } else { | 184 | } else { |
174 | eprintln!("Ignoring code block without ID:"); | 185 | eprintln!("Ignoring code block without ID:"); |
@@ -183,4 +194,4 @@ fn main() -> Result<()> { | |||
183 | ); | 194 | ); |
184 | pandoc.execute().unwrap(); | 195 | pandoc.execute().unwrap(); |
185 | Ok(()) | 196 | Ok(()) |
186 | } | 197 | } \ No newline at end of file |