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
#![crate_name = "liquid"]
#![doc(html_root_url = "https://cobalt-org.github.io/liquid-rust/")]
#![cfg_attr(feature="clippy", feature(plugin))]
#![cfg_attr(feature="clippy", plugin(clippy))]
#![deny(warnings)]
#![cfg_attr(feature="dev", warn(warnings))]
#![cfg_attr(feature="clippy", allow(clippy))]
#![cfg_attr(feature="dev", warn(clippy))]
#![cfg_attr(feature="clippy", deny(
explicit_iter_loop,
clone_on_copy,
len_zero,
map_clone,
map_entry,
match_bool,
match_same_arms,
new_ret_no_self,
new_without_default,
needless_lifetimes,
needless_range_loop,
no_effect,
ok_expect,
out_of_bounds_indexing,
ptr_arg,
redundant_closure,
single_char_pattern,
unused_collect,
useless_vec,
))]
#[macro_use]
extern crate lazy_static;
extern crate regex;
use std::collections::HashMap;
use lexer::Element;
use tags::{assign_tag, include_tag, break_tag, continue_tag,
comment_block, raw_block, for_block, if_block, unless_block, capture_block};
use std::default::Default;
use std::path::PathBuf;
use error::Result;
pub use value::Value;
pub use context::Context;
pub use template::Template;
pub use error::Error;
pub use filters::{FilterResult, FilterError};
pub use token::Token;
pub mod lexer;
pub mod parser;
mod token;
mod error;
mod template;
mod output;
mod text;
mod tags;
mod filters;
mod value;
mod variable;
mod context;
#[derive(Clone, Copy)]
pub enum ErrorMode {
Strict,
Warn,
Lax,
}
impl Default for ErrorMode {
fn default() -> ErrorMode {
ErrorMode::Warn
}
}
pub type Tag = Fn(&str, &[Token], &LiquidOptions) -> Result<Box<Renderable>>;
pub type Block = Fn(&str, &[Token], Vec<Element>, &LiquidOptions) -> Result<Box<Renderable>>;
pub trait Renderable {
fn render(&self, context: &mut Context) -> Result<Option<String>>;
}
#[derive(Default)]
pub struct LiquidOptions {
pub blocks: HashMap<String, Box<Block>>,
pub tags: HashMap<String, Box<Tag>>,
pub file_system: Option<PathBuf>,
pub error_mode: ErrorMode,
}
impl LiquidOptions {
pub fn with_known_blocks() -> LiquidOptions {
let mut options = LiquidOptions::default();
options.register_known_blocks();
options
}
pub fn register_known_blocks(&mut self) {
self.register_tag("assign", Box::new(assign_tag));
self.register_tag("break", Box::new(break_tag));
self.register_tag("continue", Box::new(continue_tag));
self.register_tag("include", Box::new(include_tag));
self.register_block("raw", Box::new(raw_block));
self.register_block("if", Box::new(if_block));
self.register_block("unless", Box::new(unless_block));
self.register_block("for", Box::new(for_block));
self.register_block("comment", Box::new(comment_block));
self.register_block("capture", Box::new(capture_block));
}
pub fn register_block(&mut self, name: &str, block: Box<Block>) {
self.blocks.insert(name.to_owned(), block);
}
pub fn register_tag(&mut self, name: &str, tag: Box<Tag>) {
self.tags.insert(name.to_owned(), tag);
}
}
pub fn parse(text: &str, options: LiquidOptions) -> Result<Template> {
let mut options = options;
options.register_known_blocks();
let tokens = try!(lexer::tokenize(&text));
parser::parse(&tokens, &options).map(Template::new)
}