Skip to content

Commit d0ae70b

Browse files
committed
Replace tera filters with macros
Zola doesn't support adding custom filters, but macros will work.
1 parent 5b83cba commit d0ae70b

File tree

7 files changed

+55
-58
lines changed

7 files changed

+55
-58
lines changed

src/lib.rs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rayon::prelude::*;
99
use sass_rs::{Options, compile_file};
1010
use serde::Serialize;
1111
use serde_json::{Value, json};
12-
use std::collections::HashMap;
1312
use std::fs::{self, File};
1413
use std::io::{self, Write};
1514
use std::path::{Path, PathBuf};
@@ -33,53 +32,12 @@ struct ReleasePost {
3332
url: String,
3433
}
3534

36-
fn month_name(month_num: &Value, _args: &HashMap<String, Value>) -> tera::Result<Value> {
37-
let month_num = month_num
38-
.as_u64()
39-
.expect("month_num should be an unsigned integer");
40-
let name = match month_num {
41-
1 => "Jan.",
42-
2 => "Feb.",
43-
3 => "Mar.",
44-
4 => "Apr.",
45-
5 => "May",
46-
6 => "June",
47-
7 => "July",
48-
8 => "Aug.",
49-
9 => "Sept.",
50-
10 => "Oct.",
51-
11 => "Nov.",
52-
12 => "Dec.",
53-
_ => panic!("invalid month! ({month_num})"),
54-
};
55-
Ok(name.into())
56-
}
57-
58-
// Tera and Handlebars escape HTML differently by default.
59-
// Tera: &<>"'/
60-
// Handlebars: &<>"'`=
61-
// To make the transition testable, this function escapes just like Handlebars.
62-
fn escape_hbs(input: &Value, _args: &HashMap<String, Value>) -> tera::Result<Value> {
63-
let input = input.as_str().expect("input should be a string");
64-
Ok(input
65-
.replace("&", "&amp;")
66-
.replace("<", "&lt;")
67-
.replace(">", "&gt;")
68-
.replace("\"", "&quot;")
69-
.replace("'", "&#x27;")
70-
.replace("`", "&#x60;")
71-
.replace("=", "&#x3D;")
72-
.into())
73-
}
74-
7535
impl Generator {
7636
fn new(
7737
out_directory: impl AsRef<Path>,
7838
posts_directory: impl AsRef<Path>,
7939
) -> eyre::Result<Self> {
8040
let mut tera = Tera::new("templates/*")?;
81-
tera.register_filter("month_name", month_name);
82-
tera.register_filter("escape_hbs", escape_hbs);
8341
tera.autoescape_on(vec![]); // disable auto-escape for .html templates
8442
Ok(Generator {
8543
tera,

templates/feed.xml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{% import "macros.html" as macros %}
12
<?xml version="1.0" encoding="utf-8"?>
23
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
34
<generator uri="https://blog.rust-lang.org/{{blog.prefix}}" version="0.1.0">{{blog.title}}</generator>
@@ -14,15 +15,15 @@
1415

1516
{% for post in posts %}
1617
<entry>
17-
<title>{{post.title | escape_hbs}}</title>
18-
<link rel="alternate" href="https://blog.rust-lang.org/{{blog.prefix}}{{post.url | escape_hbs}}" type="text/html" title="{{post.title | escape_hbs}}" />
19-
<published>{{post.published | escape_hbs}}</published>
20-
<updated>{{post.updated | escape_hbs}}</updated>
21-
<id>https://blog.rust-lang.org/{{blog.prefix}}{{post.url | escape_hbs}}</id>
22-
<content type="html" xml:base="https://blog.rust-lang.org/{{blog.prefix}}{{post.url | escape_hbs}}">{{post.contents | escape_hbs}}</content>
18+
<title>{{ macros::escape_hbs(input=post.title) }}</title>
19+
<link rel="alternate" href="https://blog.rust-lang.org/{{blog.prefix}}{{ macros::escape_hbs(input=post.url) }}" type="text/html" title="{{ macros::escape_hbs(input=post.title) }}" />
20+
<published>{{ macros::escape_hbs(input=post.published) }}</published>
21+
<updated>{{ macros::escape_hbs(input=post.updated) }}</updated>
22+
<id>https://blog.rust-lang.org/{{blog.prefix}}{{ macros::escape_hbs(input=post.url) }}</id>
23+
<content type="html" xml:base="https://blog.rust-lang.org/{{blog.prefix}}{{ macros::escape_hbs(input=post.url) }}">{{ macros::escape_hbs(input=post.contents) }}</content>
2324

2425
<author>
25-
<name>{{post.author | escape_hbs}}</name>
26+
<name>{{ macros::escape_hbs(input=post.author) }}</name>
2627
</author>
2728
</entry>
2829
{%- endfor %}

templates/headers.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
<meta name="twitter:card" content="summary">
44
<meta name="twitter:site" content="@rustlang">
55
<meta name="twitter:creator" content="@rustlang">
6-
<meta name="twitter:title" content="{{title | escape_hbs}}">
6+
<meta name="twitter:title" content="{{ macros::escape_hbs(input=title) }}">
77
<meta name="twitter:description" content="{{blog.description}}">
88
<meta name="twitter:image" content="https://www.rust-lang.org/static/images/rust-social.jpg">
99

1010
<!-- Facebook OpenGraph -->
11-
<meta property="og:title" content="{{title | escape_hbs}}" />
11+
<meta property="og:title" content="{{ macros::escape_hbs(input=title) }}" />
1212
<meta property="og:description" content="{{blog.description}}">
1313
<meta property="og:image" content="https://www.rust-lang.org/static/images/rust-social-wide.jpg" />
1414
<meta property="og:type" content="website" />

templates/index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{% import "macros.html" as macros %}
12
{% extends "layout.html" %}
23
{% block page %}
34
<header class="mt3 mt0-ns mb4-ns">
@@ -11,7 +12,7 @@
1112
<p>
1213
<b>See also:</b>
1314
{%- for other in other_blogs %}
14-
<a href="/{{other.url}}">{{other.link_text | escape_hbs}}</a>
15+
<a href="/{{other.url}}">{{ macros::escape_hbs(input=other.link_text) }}</a>
1516
{%- endfor %}
1617
</p>
1718
</div>
@@ -28,8 +29,8 @@
2829
<td class="bn"><h3 class="f0-l f1-m f2-s mt4 mb0">Posts in {{post.year}}</h3></td>
2930
</tr>{% endif %}
3031
<tr>
31-
<td class="tr o-60 pr4 pr5-l bn">{{post.month | month_name}}&nbsp;{{post.day}}</td>
32-
<td class="bn"><a href="{{post.url}}">{{post.title | escape_hbs}}</a></td>
32+
<td class="tr o-60 pr4 pr5-l bn">{{ macros::month_name(num=post.month) }}&nbsp;{{post.day}}</td>
33+
<td class="bn"><a href="{{post.url}}">{{ macros::escape_hbs(input=post.title) }}</a></td>
3334
</tr>
3435
{%- endfor %}
3536
</table>

templates/layout.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
{% import "macros.html" as macros %}
12
{% import "headers.html" as headers %}
23
{% import "nav.html" as nav %}
34
{% import "footer.html" as footer %}
45
<!DOCTYPE html>
56
<html lang="en">
67
<head>
78
<meta charset="utf-8">
8-
<title>{{ title | escape_hbs }}</title>
9+
<title>{{ macros::escape_hbs(input=title) }}</title>
910
<meta name="viewport" content="width=device-width,initial-scale=1.0">
1011
<meta name="description" content="Empowering everyone to build reliable and efficient software.">
1112
{{ headers::headers(title=title, blog=blog) | indent(prefix=" ", blank=true) }}

templates/macros.html

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{% macro month_name(num) %}
2+
{%- if num == 1 %}Jan.
3+
{%- elif num == 2 %}Feb.
4+
{%- elif num == 3 %}Mar.
5+
{%- elif num == 4 %}Apr.
6+
{%- elif num == 5 %}May
7+
{%- elif num == 6 %}June
8+
{%- elif num == 7 %}July
9+
{%- elif num == 8 %}Aug.
10+
{%- elif num == 9 %}Sept.
11+
{%- elif num == 10 %}Oct.
12+
{%- elif num == 11 %}Nov.
13+
{%- elif num == 12 %}Dec.
14+
{%- else %}{{ throw(message="invalid month! " ~ num) }}
15+
{%- endif %}
16+
{% endmacro month_name %}
17+
18+
{#
19+
The blog templates used to be written in Handlebars, but Tera and Handlebars
20+
escape HTML differently by default:
21+
Tera: &<>"'/
22+
Handlebars: &<>"'`=
23+
To keep the output identical, this macro matches the behavior of Handlebars.
24+
#}
25+
{% macro escape_hbs(input) %}
26+
{{ input
27+
| replace(from="&", to="&amp;")
28+
| replace(from="<", to="&lt;")
29+
| replace(from=">", to="&gt;")
30+
| replace(from='"', to="&quot;")
31+
| replace(from="'", to="&#x27;")
32+
| replace(from="`", to="&#x60;")
33+
| replace(from="=", to="&#x3D;")
34+
}}
35+
{% endmacro escape_hbs %}

templates/post.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
{% import "macros.html" as macros %}
12
{% extends "layout.html" %}
23
{% block page %}
3-
<section id="{{post.title | escape_hbs}}" class="white">
4+
<section id="{{ macros::escape_hbs(input=post.title) }}" class="white">
45
<div class="w-100 mw-none ph3 mw8-m mw8-l center f3">
56
<header>
6-
<h2>{{ post.title | escape_hbs }}</h2>
7+
<h2>{{ macros::escape_hbs(input=post.title) }}</h2>
78
<div class="highlight mt2 mb3"></div>
89
</header>
910

10-
<div class="publish-date-author">{{post.month | month_name}} {{post.day}}, {{post.year}} &middot; {{post.author | escape_hbs}}
11+
<div class="publish-date-author">{{ macros::month_name(num=post.month) }} {{post.day}}, {{post.year}} &middot; {{ macros::escape_hbs(input=post.author) }}
1112
{% if post.has_team %} on behalf of <a href="{{post.team_url}}">{{post.team}}</a> {% endif %}
1213
</div>
1314

0 commit comments

Comments
 (0)