-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathfilters.rs
361 lines (324 loc) · 13.1 KB
/
filters.rs
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
use std::borrow::Cow;
use std::collections::HashMap;
use std::hash::BuildHasher;
use std::sync::{Arc, Mutex};
use config::Config;
use libs::base64::engine::{general_purpose::STANDARD as standard_b64, Engine};
use libs::regex::Regex;
use libs::tera::{
to_value, try_get_value, Error as TeraError, Filter as TeraFilter, Result as TeraResult, Tera,
Value,
};
use markdown::{render_content, RenderContext};
#[derive(Debug)]
pub struct MarkdownFilter {
config: Config,
permalinks: HashMap<String, String>,
tera: Tera,
}
impl MarkdownFilter {
pub fn new(config: Config, permalinks: HashMap<String, String>, tera: Tera) -> Self {
Self { config, permalinks, tera }
}
}
impl TeraFilter for MarkdownFilter {
fn filter(&self, value: &Value, args: &HashMap<String, Value>) -> TeraResult<Value> {
// NOTE: RenderContext below is not aware of the current language
// However, it should not be a problem because the surrounding tera
// template has language context, and will most likely call a piece of
// markdown respecting language preferences.
let mut context = RenderContext::from_config(&self.config);
context.permalinks = Cow::Borrowed(&self.permalinks);
context.tera = Cow::Borrowed(&self.tera);
let def = utils::templates::get_shortcodes(&self.tera);
context.set_shortcode_definitions(&def);
let s = try_get_value!("markdown", "value", String, value);
let inline = match args.get("inline") {
Some(val) => try_get_value!("markdown", "inline", bool, val),
None => false,
};
let mut html = match render_content(&s, &context) {
Ok(res) => res.body,
Err(e) => return Err(format!("Failed to render markdown filter: {:?}", e).into()),
};
if inline {
html = html
.trim_start_matches("<p>")
// pulldown_cmark finishes a paragraph with `</p>\n`
.trim_end_matches("</p>\n")
.to_string();
}
Ok(to_value(&html).unwrap())
}
}
pub fn base64_encode<S: BuildHasher>(
value: &Value,
_: &HashMap<String, Value, S>,
) -> TeraResult<Value> {
let s = try_get_value!("base64_encode", "value", String, value);
Ok(to_value(standard_b64.encode(s.as_bytes())).unwrap())
}
pub fn base64_decode<S: BuildHasher>(
value: &Value,
_: &HashMap<String, Value, S>,
) -> TeraResult<Value> {
let s = try_get_value!("base64_decode", "value", String, value);
let decoded = standard_b64
.decode(s.as_bytes())
.map_err(|e| format!("`base64_decode`: failed to decode: {}", e))?;
let as_str =
String::from_utf8(decoded).map_err(|e| format!("`base64_decode`: invalid utf-8: {}", e))?;
Ok(to_value(as_str).unwrap())
}
#[derive(Debug)]
pub struct RegexReplaceFilter {
re_cache: Arc<Mutex<HashMap<String, Regex>>>,
}
impl RegexReplaceFilter {
pub fn new() -> Self {
return Self { re_cache: Arc::new(Mutex::new(HashMap::new())) };
}
}
impl TeraFilter for RegexReplaceFilter {
fn filter(&self, value: &Value, args: &HashMap<String, Value>) -> TeraResult<Value> {
let text = try_get_value!("regex_replace", "value", String, value);
let pattern = match args.get("pattern") {
Some(val) => try_get_value!("regex_replace", "pattern", String, val),
None => {
return Err(TeraError::msg(
"Filter `regex_replace` expected an arg called `pattern`",
))
}
};
let rep = match args.get("rep") {
Some(val) => try_get_value!("regex_replace", "rep", String, val),
None => {
return Err(TeraError::msg("Filter `regex_replace` expected an arg called `rep`"))
}
};
let mut cache = self.re_cache.lock().expect("re_cache lock");
let replaced = {
match cache.get(&pattern) {
Some(pat) => pat.replace_all(&text, &rep),
None => {
let pat = Regex::new(&pattern)
.map_err(|e| format!("`regex_replace`: failed to compile regex: {}", e))?;
let replaced = pat.replace_all(&text, &rep);
cache.insert(pattern, pat);
replaced
}
}
};
Ok(to_value(replaced).unwrap())
}
}
#[derive(Debug)]
pub struct NumFormatFilter {
default_language: String,
}
impl NumFormatFilter {
pub fn new<S: Into<String>>(default_language: S) -> Self {
Self { default_language: default_language.into() }
}
}
impl TeraFilter for NumFormatFilter {
fn filter(&self, value: &Value, args: &HashMap<String, Value>) -> TeraResult<Value> {
use libs::num_format::{Locale, ToFormattedString};
let num = try_get_value!("num_format", "value", i64, value);
let locale = match args.get("locale") {
Some(locale) => try_get_value!("num_format", "locale", String, locale),
None => self.default_language.clone(),
};
let locale = Locale::from_name(&locale).map_err(|_| {
TeraError::msg(format!(
"Filter `num_format` was called with an invalid `locale` argument: `{}`.",
locale
))
})?;
Ok(to_value(num.to_formatted_string(&locale)).unwrap())
}
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use libs::tera::{to_value, Filter, Tera};
use super::{
base64_decode, base64_encode, MarkdownFilter, NumFormatFilter, RegexReplaceFilter,
};
use config::Config;
#[test]
fn markdown_filter() {
let result = MarkdownFilter::new(Config::default(), HashMap::new(), Tera::default())
.filter(&to_value(&"# Hey").unwrap(), &HashMap::new());
assert!(result.is_ok());
assert_eq!(result.unwrap(), to_value(&"<h1 id=\"hey\">Hey</h1>\n").unwrap());
}
#[test]
fn markdown_filter_override_lang() {
// We're checking that we can use a workaround to explicitly provide `lang` in markdown filter from tera,
// because otherwise markdown filter shortcodes are not aware of the current language
// NOTE: This should also work for `nth` although i don't see a reason to do that
let args = HashMap::new();
let config = Config::default();
let permalinks = HashMap::new();
let mut tera = Tera::default();
tera.add_raw_template("shortcodes/explicitlang.html", "a{{ lang }}a").unwrap();
let filter = MarkdownFilter { config, permalinks, tera };
let result = filter.filter(&to_value(&"{{ explicitlang(lang='jp') }}").unwrap(), &args);
println!("{:?}", result);
assert!(result.is_ok());
assert_eq!(result.unwrap(), to_value(&"ajpa").unwrap());
}
#[test]
fn markdown_filter_inline() {
let mut args = HashMap::new();
args.insert("inline".to_string(), to_value(true).unwrap());
let result = MarkdownFilter::new(Config::default(), HashMap::new(), Tera::default())
.filter(
&to_value(&"Using `map`, `filter`, and `fold` instead of `for`").unwrap(),
&args,
);
assert!(result.is_ok());
assert_eq!(result.unwrap(), to_value(&"Using <code>map</code>, <code>filter</code>, and <code>fold</code> instead of <code>for</code>").unwrap());
}
// https://github.com/Keats/gutenberg/issues/417
#[test]
fn markdown_filter_inline_tables() {
let mut args = HashMap::new();
args.insert("inline".to_string(), to_value(true).unwrap());
let result = MarkdownFilter::new(Config::default(), HashMap::new(), Tera::default())
.filter(
&to_value(
&r#"
|id|author_id| timestamp_created|title |content |
|-:|--------:|-----------------------:|:---------------------|:-----------------|
| 1| 1|2018-09-05 08:03:43.141Z|How to train your ORM |Badly written blog|
| 2| 1|2018-08-22 13:11:50.050Z|How to bake a nice pie|Badly written blog|
"#,
)
.unwrap(),
&args,
);
assert!(result.is_ok());
assert!(result.unwrap().as_str().unwrap().contains("<table>"));
}
#[test]
fn markdown_filter_use_config_options() {
let mut config = Config::default();
config.markdown.highlight_code = true;
config.markdown.smart_punctuation = true;
config.markdown.render_emoji = true;
config.markdown.external_links_target_blank = true;
let md = "Hello <https://google.com> :smile: ...";
let result = MarkdownFilter::new(config.clone(), HashMap::new(), Tera::default())
.filter(&to_value(&md).unwrap(), &HashMap::new());
assert!(result.is_ok());
assert_eq!(result.unwrap(), to_value(&"<p>Hello <a rel=\"noopener\" target=\"_blank\" href=\"https://google.com\">https://google.com</a> 😄 …</p>\n").unwrap());
let md = "```py\ni=0\n```";
let result = MarkdownFilter::new(config, HashMap::new(), Tera::default())
.filter(&to_value(&md).unwrap(), &HashMap::new());
assert!(result.is_ok());
assert!(result.unwrap().as_str().unwrap().contains("style"));
}
#[test]
fn mardown_filter_can_use_internal_links() {
let mut permalinks = HashMap::new();
permalinks.insert("blog/_index.md".to_string(), "/foo/blog".to_string());
let md = "Hello. Check out [my blog](@/blog/_index.md)!";
let result = MarkdownFilter::new(Config::default(), permalinks, Tera::default())
.filter(&to_value(&md).unwrap(), &HashMap::new());
assert!(result.is_ok());
assert_eq!(
result.unwrap(),
to_value(&"<p>Hello. Check out <a href=\"/foo/blog\">my blog</a>!</p>\n").unwrap()
);
}
#[test]
fn base64_encode_filter() {
// from https://tools.ietf.org/html/rfc4648#section-10
let tests = vec![
("", ""),
("f", "Zg=="),
("fo", "Zm8="),
("foo", "Zm9v"),
("foob", "Zm9vYg=="),
("fooba", "Zm9vYmE="),
("foobar", "Zm9vYmFy"),
];
for (input, expected) in tests {
let args = HashMap::new();
let result = base64_encode(&to_value(input).unwrap(), &args);
assert!(result.is_ok());
assert_eq!(result.unwrap(), to_value(expected).unwrap());
}
}
#[test]
fn base64_decode_filter() {
let tests = vec![
("", ""),
("Zg==", "f"),
("Zm8=", "fo"),
("Zm9v", "foo"),
("Zm9vYg==", "foob"),
("Zm9vYmE=", "fooba"),
("Zm9vYmFy", "foobar"),
];
for (input, expected) in tests {
let args = HashMap::new();
let result = base64_decode(&to_value(input).unwrap(), &args);
assert!(result.is_ok());
assert_eq!(result.unwrap(), to_value(expected).unwrap());
}
}
#[test]
fn regex_replace_filter() {
let value = "Springsteen, Bruce";
let expected = "Bruce Springsteen";
let pattern = r"(?P<last>[^,\s]+),\s+(?P<first>\S+)";
let rep = "$first $last";
let mut args = HashMap::new();
args.insert("pattern".to_string(), to_value(pattern).unwrap());
args.insert("rep".to_string(), to_value(rep).unwrap());
let regex_replace = RegexReplaceFilter::new();
let result = regex_replace.filter(&to_value(value).unwrap(), &args);
assert!(result.is_ok());
assert_eq!(result.unwrap(), to_value(expected).unwrap());
assert!(regex_replace.re_cache.lock().unwrap().contains_key(pattern));
}
#[test]
fn num_format_filter() {
let tests = vec![
(100, "100"),
(1_000, "1,000"),
(10_000, "10,000"),
(100_000, "100,000"),
(1_000_000, "1,000,000"),
];
for (input, expected) in tests {
let args = HashMap::new();
let result = NumFormatFilter::new("en").filter(&to_value(input).unwrap(), &args);
let result = dbg!(result);
assert!(result.is_ok());
assert_eq!(result.unwrap(), to_value(expected).unwrap());
}
}
#[test]
fn num_format_filter_with_locale() {
let tests = vec![
("en", 1_000_000, "1,000,000"),
("en-IN", 1_000_000, "10,00,000"),
// Note:
// U+202F is the "NARROW NO-BREAK SPACE" code point.
// When displayed to the screen, it looks like a space.
("fr", 1_000_000, "1\u{202f}000\u{202f}000"),
];
for (locale, input, expected) in tests {
let mut args = HashMap::new();
args.insert("locale".to_string(), to_value(locale).unwrap());
let result = NumFormatFilter::new("en").filter(&to_value(input).unwrap(), &args);
let result = dbg!(result);
assert!(result.is_ok());
assert_eq!(result.unwrap(), to_value(expected).unwrap());
}
}
}