diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2020-12-05 11:20:35 +0000 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2020-12-10 09:57:05 +0000 |
commit | 8da8a9693b2f6404a174d000840bf40c5826abdb (patch) | |
tree | af1e18da8d542207370f0904b80ab1cb042e1e7c /src/codegen.rs | |
parent | 0a953dc1a5112a3df4f407717ef3f9992ae77fb2 (diff) | |
download | subplot-8da8a9693b2f6404a174d000840bf40c5826abdb.tar.gz |
codegen: Add a nameslug filter
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'src/codegen.rs')
-rw-r--r-- | src/codegen.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/codegen.rs b/src/codegen.rs index 0cb63fe..f798b25 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -64,6 +64,7 @@ fn tera(tmplspec: &TemplateSpec) -> Result<Tera> { // only, so we use a glob that doesn't match anything. let mut tera = Tera::new(&"*notexist").expect("new"); tera.register_filter("base64", base64); + tera.register_filter("nameslug", nameslug); tera.add_template_file(tmplspec.template_filename(), Some("template"))?; Ok(tera) } @@ -90,6 +91,25 @@ fn base64(v: &Value, _: &HashMap<String, Value>) -> tera::Result<Value> { } } +fn nameslug(name: &Value, _: &HashMap<String, Value>) -> tera::Result<Value> { + match name { + Value::String(s) => { + let newname = s + .chars() + .map(|c| match c { + 'a'..='z' => c, + 'A'..='Z' => c.to_ascii_lowercase(), + _ => '_', + }) + .collect(); + Ok(Value::String(newname)) + } + _ => Err(tera::Error::msg( + "can only create nameslugs from strings".to_string(), + )), + } +} + #[derive(Debug, Serialize)] pub struct Func { pub source: PathBuf, |