Skip to content

Commit b3a702a

Browse files
committed
feat(templates): add glob pattern support to exclude_templates (#66)
1 parent c34aed3 commit b3a702a

1 file changed

Lines changed: 38 additions & 8 deletions

File tree

src/components/templates.rs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::LUA;
22
use minijinja::ErrorKind::UndefinedError;
33
use mlua::{ExternalError, FromLua, LuaSerdeExt, UserData};
4+
use regex;
45
use std::sync::Arc;
56

67
/// Will include the name, path, and source
@@ -145,14 +146,43 @@ impl UserData for TemplatingEngine<'_> {
145146
.collect::<Vec<_>>())
146147
});
147148
methods.add_method_mut("exclude_templates", |_, this, names: Vec<String>| {
148-
for i in names {
149-
this.templates = this
150-
.templates
151-
.iter()
152-
.filter(|template| template.name != i)
153-
.cloned()
154-
.collect::<Vec<_>>();
155-
this.exclusions.push(i.into());
149+
for pattern in names {
150+
if pattern.contains('*') || pattern.contains('?') || pattern.contains('[') {
151+
let re_pattern = pattern
152+
.replace(".", "\\.")
153+
.replace("**", "###DOUBLESTAR###")
154+
.replace("*", "[^/]*")
155+
.replace("###DOUBLESTAR###", ".*")
156+
.replace("?", ".");
157+
let re = regex::Regex::new(&re_pattern)
158+
.unwrap_or_else(|_| regex::Regex::new(".*").unwrap());
159+
this.templates = this
160+
.templates
161+
.iter()
162+
.filter(|template| {
163+
let name_match = re.is_match(&template.name);
164+
let path_match = template
165+
.path
166+
.as_ref()
167+
.map(|p| {
168+
let path_str = p.replace("\\", "/");
169+
re.is_match(&path_str)
170+
})
171+
.unwrap_or(false);
172+
!name_match && !path_match
173+
})
174+
.cloned()
175+
.collect::<Vec<_>>();
176+
this.exclusions.push(pattern.into());
177+
} else {
178+
this.templates = this
179+
.templates
180+
.iter()
181+
.filter(|template| template.name != pattern)
182+
.cloned()
183+
.collect::<Vec<_>>();
184+
this.exclusions.push(pattern.into());
185+
}
156186
}
157187

158188
Ok(())

0 commit comments

Comments
 (0)