1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
use std::borrow::Cow; use std::collections::HashMap; use abc::*; pub use types::abc::{Args, Type, BType, ex}; pub use types::context::{GlobalContext, Context}; pub use container::Template; use loader::GroupLoader; #[derive(Debug)] pub struct Incrust { pub loaders: GroupLoader, filters: HashMap<String, Box<Filter>>, top_context: HashMap<String, BType>, } impl Default for Incrust { fn default() -> Self { #![cfg_attr(feature = "clippy", allow(used_underscore_binding))] use ::renderer::filter::{Escape, Unescape}; let mut filters: HashMap<String, Box<Filter>> = HashMap::new(); filters.insert("e".into(), box Escape); filters.insert("escape".into(), box Escape); filters.insert("unescape".into(), box Unescape); let env = hashmap!{ "True".into() => ex(true), "true".into() => ex(true), "False".into() => ex(false), "false".into() => ex(false), }; Incrust { loaders: Vec::new(), filters: filters, top_context: env, } } } impl Incrust { pub fn new() -> Self { Incrust::default() } pub fn no_default() -> Self { Incrust { loaders: Vec::new(), filters: HashMap::new(), top_context: HashMap::new(), } } pub fn top_context(&self) -> &HashMap<String, BType> { &self.top_context } pub fn load(&self, name: &str) -> LoadResult { for loader in &self.loaders { if let Ok(template) = loader.load(name) { return Ok(template) } } Err(LoadError::NotFound) } pub fn filter<'a, 's: 'a>(&'s self, id: &str, context: &'a Context, value: Option<Cow<'a, BType>>) -> FilterResult<Cow<'a, BType>> { match self.filters.get(id) { Some(ref filter) => filter.filter(context, value), None => Err(FilterError::UnknownFormatter(id.into())) } } pub fn parse(&self, template: &str) -> TemplateParseResult<Template> { Template::parse(template) } pub fn render<'a, C:Into<Args<'a>>>(&'a self, name: &str, args: C) -> RenderResult<String> { self.render_text(&self.load(name)?, args) } pub fn render_text<'a, C:Into<Args<'a>>>(&'a self, text: &str, args: C) -> RenderResult<String> { self.render_parsed(&self.parse(text)?, args) } pub fn render_parsed<'a, C:Into<Args<'a>>>(&'a self, template: &Template, args: C) -> RenderResult<String> { self.render_prepared(&self.create_global_context(template, &args.into())?.top_scope()) } pub fn render_prepared(&self, context: &Context) -> RenderResult<String> { ::renderer::text(context) } pub fn create_global_context<'a>(&'a self, template: &'a Template, args: &'a Args<'a>) -> RenderResult<GlobalContext<'a>> { GlobalContext::new(self, template, args) } } #[cfg(test)] mod tests { #![cfg_attr(feature = "clippy", allow(used_underscore_binding))] use super::*; #[test] fn text() { let templ = "Hello, World!"; let expected = "Hello, World!"; let incrust = Incrust::new(); let result = incrust.render_text(templ, hashmap!{}).unwrap(); assert_eq!(result, expected); } #[test] fn comments() { let incrust = Incrust::new(); let templ = incrust.parse("<p>Visible {# partially #} paragraph</p>").unwrap(); let expected = "<p>Visible paragraph</p>"; let result = incrust.render_parsed(&templ, hashmap!{}).unwrap(); assert_eq!(result, expected); } #[test] fn mustache() { let templ = "Hello, {{name}}!"; let expected = "Hello, World!"; let incrust = Incrust::new(); let result = incrust.render_text(templ, hashmap!{ "name".into() => ex("World") }).unwrap(); assert_eq!(result, expected); } #[test] fn filter() { let templ = "<textarea>{{ html | e }}</textarea>"; let args: Args = hashmap!{ "html".into() => ex("<h1>Hello, World!</h1>") }; let expected = "<textarea><h1>Hello, World!</h1></textarea>"; let incrust = Incrust::new(); let result = incrust.render_text(templ, args).unwrap(); assert_eq!(result, expected); } #[test] fn literal() { let incrust = Incrust::new(); assert_eq!("Braces: {{", incrust.render_text(r#"Braces: {{ "{{" }}"#, hashmap!{}).unwrap()); assert_eq!("The answer: 42", incrust.render_text(r#"The answer: {{ 42 }}"#, hashmap!{}).unwrap()); assert_eq!("Pi: 3.1415926", incrust.render_text(r#"Pi: {{ 3.1415926 }}"#, hashmap!{}).unwrap()); } #[test] fn expression() { let incrust = Incrust::new(); let args = hashmap!{ "what".into() => ex("Hello"), "who".into() => ex("World") }; assert_eq!(r#"Say: "Hello, World!""#, incrust.render_text(r#"Say: "{{ what + ", " + who }}!""#, args).unwrap()); let args = hashmap!{ "alpha".into() => ex(6_i64), "omega".into() => ex(7_f64) }; assert_eq!("The answer is 42", incrust.render_text(r#"The answer is {{ alpha * omega }}"#, args).unwrap()); let args = hashmap!{ "amount".into() => ex(6_i64) }; assert_eq!("1 + 1 = 2", incrust.render_text(r#"1 + 1 = {{ 1 + 1 }}"#, args).unwrap()); let args = hashmap!{ "amount".into() => ex(6_i64) }; assert_eq!("Amount: 6 pcs", incrust.render_text(r#"Amount: {{ amount and ("" + amount + " pcs") or "-" }}"#, args).unwrap()); let args = hashmap!{ "amount".into() => ex(0_i64) }; assert_eq!("Amount: -", incrust.render_text(r#"Amount: {{ amount and ("" + amount + " pcs") or "-" }}"#, args).unwrap()); } #[test] fn if_statement() { let incrust = Incrust::new(); let test = |expected, template| assert_eq!(expected, incrust.render_text(template, hashmap!{}).unwrap()); test("Mode: on", r#"Mode: {% if True %}on{% endif %}"#); test("String is empty", r#"String {% if "" %}has chars{% else %}is empty{% endif %}"#); test("String is true", r#"String {% if "" %}has chars{% elif True %}is true{% else %}is empty{% endif %}"#); } #[test] fn for_statement() { let incrust = Incrust::new(); let args = hashmap!{ "fruits".into() => ex(vec![ex("Orange"), ex("Apple"), ex("Banana")]) }; let tpl = r#" <ul> {%- for fruit in fruits %} <li>{{ index }}. {{ fruit | e }}</li> {%- endfor %} </ul> "#; let expected = r#" <ul> <li>1. Orange</li> <li>2. Apple</li> <li>3. Banana</li> </ul> "#; assert_eq!(expected, incrust.render_text(tpl, args).unwrap()); } #[test] fn block_statement() { let incrust = Incrust::new(); let args = hashmap!{ "fruits".into() => ex(vec![ex("Orange"), ex("Apple"), ex("Banana")]) }; let tpl = r#" <body> <h1>{% block title %}Default title{% endblock %}</h1> </body> "#; let expected = r#" <body> <h1>Default title</h1> </body> "#; assert_eq!(expected, incrust.render_text(tpl, args).unwrap()); } #[test] fn inheritance() { let base = r#" <body> <h1>{% block title %}Default title{% endblock %}</h1> <main> {%- block body %} <p>Default body<p> {%- endblock %} </main> </body> "#; let tpl = r#" {% extends parent_layout %} {% block title -%} New title {%- endblock %} "#; let expected = r#" <body> <h1>New title</h1> <main> <p>Default body<p> </main> </body> "#; let mut incrust = Incrust::new(); incrust.loaders.push(box hashmap!{ "base".into() => base.into(), "tpl".into() => tpl.into(), }); let args = hashmap!{ "parent_layout".into() => ex("base") }; assert_eq!(expected, incrust.render("tpl", args).unwrap()); } #[test] fn include() { let default_menu = r#" <ul> <li><a href="/">Home</a></li> <li><a href="/about">About Us</a></li> </ul> "#; let tpl = r#" <nav> {%- include menu -%} </nav> <h1>Body</h1> "#; let expected = r#" <nav> <ul> <li><a href="/">Home</a></li> <li><a href="/about">About Us</a></li> </ul> </nav> <h1>Body</h1> "#; let mut incrust = Incrust::new(); incrust.loaders.push(box hashmap!{ "default_menu".into() => default_menu.into(), "tpl".into() => tpl.into(), }); let args = hashmap!{ "menu".into() => ex("default_menu") }; assert_eq!(expected, incrust.render("tpl", args).unwrap()); } }