Skip to content

Commit 1ad3d4e

Browse files
committed
Use tera safe filters
1 parent 6e25e2c commit 1ad3d4e

File tree

18 files changed

+112
-89
lines changed

18 files changed

+112
-89
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ staticfile = { version = "0.4", features = ["cache"] }
6060
tempfile = "3.1.0"
6161

6262
# Templating
63-
tera = { version = "1.3.1", features = ["builtins"] }
63+
tera = { version = "1.5.0", features = ["builtins"] }
6464
walkdir = "2"
6565

6666
# Template hot-reloading

src/web/page/templates.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ pub(super) fn load_templates(conn: &mut Client) -> Result<Tera> {
149149
tera.register_filter("timeformat", timeformat);
150150
tera.register_filter("dbg", dbg);
151151
tera.register_filter("dedent", dedent);
152+
tera.register_filter("fas", IconType::Strong);
153+
tera.register_filter("far", IconType::Regular);
154+
tera.register_filter("fab", IconType::Brand);
152155

153156
Ok(tera)
154157
}
@@ -252,6 +255,43 @@ fn dedent(value: &Value, _args: &HashMap<String, Value>) -> TeraResult<Value> {
252255
))
253256
}
254257

258+
enum IconType {
259+
Strong,
260+
Regular,
261+
Brand,
262+
}
263+
264+
impl fmt::Display for IconType {
265+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
266+
let icon = match self {
267+
Self::Strong => "fas",
268+
Self::Regular => "far",
269+
Self::Brand => "fab",
270+
};
271+
272+
f.write_str(icon)
273+
}
274+
}
275+
276+
impl tera::Filter for IconType {
277+
fn filter(&self, value: &Value, args: &HashMap<String, Value>) -> TeraResult<Value> {
278+
let icon = format!(
279+
r#"<span class="{} fa-{}" aria-hidden="true" {}></span>"#,
280+
self,
281+
value.as_str().expect("Icons only take strings"),
282+
args.get("extra")
283+
.and_then(Value::as_str)
284+
.unwrap_or_default(),
285+
);
286+
287+
Ok(Value::String(icon))
288+
}
289+
290+
fn is_safe(&self) -> bool {
291+
true
292+
}
293+
}
294+
255295
#[cfg(test)]
256296
mod tests {
257297
use super::*;

templates/about-base.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66
<h3 id="crate-title">Docs.rs documentation</h3>
77
<div class="pure-menu pure-menu-horizontal">
88
<ul class="pure-menu-list">
9-
{% set text = "info-circle" | fas | safe %}
9+
{% set text = "info-circle" | fas %}
1010
{% set text = text ~ ' <span class="title">About</span>' %}
1111
{{ macros::active_link(expected="index", href="/about", text=text) }}
1212

13-
{% set text = "fonticons" | fab | safe %}
13+
{% set text = "fonticons" | fab%}
1414
{% set text = text ~ ' <span class="title">Badges</span>' %}
1515
{{ macros::active_link(expected="badges", href="/about/badges", text=text) }}
1616

17-
{% set text = "cogs" | fas | safe %}
17+
{% set text = "cogs" | fas %}
1818
{% set text = text ~ ' <span class="title">Builds</span>' %}
1919
{{ macros::active_link(expected="builds", href="/about/builds", text=text) }}
2020

21-
{% set text = "table" | fas | safe %}
21+
{% set text = "table" | fas %}
2222
{% set text = text ~ ' <span class="title">Metadata</span>' %}
2323
{{ macros::active_link(expected="metadata", href="/about/metadata", text=text) }}
2424

25-
{% set text = "road" | fas | safe %}
25+
{% set text = "road" | fas %}
2626
{% set text = text ~ ' <span class="title">Shorthand URLs</span>' %}
2727
{{ macros::active_link(expected="redirections", href="/about/redirections", text=text) }}
2828
</ul>

templates/base.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{%- import "macros.html" as macros -%}
2-
{%- import "icons.html" as fa -%}
32

43
<!DOCTYPE html>
54
<html lang="en">

templates/core/home.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
{%- block body -%}
66
<div class="container landing">
7-
<h1 class="brand">{{ "cubes" | fas | safe }} Docs.rs</h1>
7+
<h1 class="brand">{{ "cubes" | fas }} Docs.rs</h1>
88

99
<form action="/releases/search" method="GET" class="landing-search-form">
1010
<div>
@@ -28,7 +28,7 @@ <h1 class="brand">{{ "cubes" | fas | safe }} Docs.rs</h1>
2828
<strong>Recent Releases</strong>
2929
</a>
3030
<a href="/releases/feed" title="Atom feed">
31-
{{ "rss-square" | fas | safe }}
31+
{{ "rss-square" | fas }}
3232
</a>
3333
</div>
3434

@@ -54,7 +54,7 @@ <h1 class="brand">{{ "cubes" | fas | safe }} Docs.rs</h1>
5454
{%- if varsb.show_stars -%}
5555
<div class="pure-u-1 pure-u-sm-4-24 pure-u-md-3-24 date">
5656
{{ release.stars }}
57-
{{ "star" | fas | safe }}
57+
{{ "star" | fas }}
5858
</div>
5959
{%- else -%}
6060
<div class="pure-u-1 pure-u-sm-4-24 pure-u-md-3-24 date"

templates/crate/builds.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@
4343
<div class="pure-g">
4444
<div class="pure-u-1 pure-u-sm-1-24 build">
4545
{%- if build.build_status -%}
46-
{{ "check" | fas | safe }}
46+
{{ "check" | fas }}
4747
{%- else -%}
48-
{{ "times" | fas | safe }}
48+
{{ "times" | fas }}
4949
{%- endif -%}
5050
</div>
5151
<div class="pure-u-1 pure-u-sm-10-24">{{ build.rustc_version }}</div>

templates/crate/details.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
{%- if details.homepage_url -%}
3333
<li class="pure-menu-item">
3434
<a href="{{ details.homepage_url }}" class="pure-menu-link">
35-
{{ "home" | fas | safe }} Homepage
35+
{{ "home" | fas }} Homepage
3636
</a>
3737
</li>
3838
{%- endif -%}
@@ -41,7 +41,7 @@
4141
{%- if details.documentation_url -%}
4242
<li class="pure-menu-item">
4343
<a href="{{ details.documentation_url }}" title="Canonical documentation" class="pure-menu-link">
44-
{{ "file-alt" | far | safe }} Documentation
44+
{{ "file-alt" | far }} Documentation
4545
</a>
4646
</li>
4747
{%- endif -%}
@@ -53,14 +53,14 @@
5353
{# If the repo link is for github, show some github stats #}
5454
{# TODO: add support for hosts besides github (#35) #}
5555
{%- if details.github -%}
56-
{{ "github" | fab| safe }}
57-
{{ "star" | fas| safe }} {{ details.github_stars }}
58-
{{ fa::s(icon="code-branch") }} {{ details.github_forks }}
59-
{{ "exclamation-circle" | fas | safe }} {{ details.github_issues }}
56+
{{ "github" | fab }}
57+
{{ "star" | fas }} {{ details.github_stars }}
58+
{{ "code-branch" | fas }} {{ details.github_forks }}
59+
{{ "exclamation-circle" | fas }} {{ details.github_issues }}
6060

6161
{# If the repo link is unknown, just show a normal link #}
6262
{%- else -%}
63-
{{ fa::s(icon="code-branch") }} Repository
63+
{{ "code-branch" | fas }} Repository
6464
{%- endif -%}
6565
</a>
6666
</li>
@@ -70,7 +70,7 @@
7070
<li class="pure-menu-item">
7171
<a href="https://crates.io/crates/{{ details.name }}" class="pure-menu-link"
7272
title="See {{ details.name }} on crates.io">
73-
{{ "cube" | fas | safe }} Crates.io
73+
{{ "cube" | fas }} Crates.io
7474
</a>
7575
</li>
7676

templates/crate/source.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
{# If this isn't the root folder, show a 'back' button #}
2020
{%- if show_parent_link -%}
2121
<li class="pure-menu-item">
22-
<a href="../" class="pure-menu-link">{{ "folder-open" | far | safe }} ..</a>
22+
<a href="../" class="pure-menu-link">{{ "folder-open" | far }} ..</a>
2323
</li>
2424
{%- endif -%}
2525

@@ -32,23 +32,23 @@
3232
<a href="./{{ file.name }}{% if file.mime == 'dir' %}/{% endif %}" class="pure-menu-link">
3333
{# Directories #}
3434
{%- if file.mime == "dir" -%}
35-
{{ "folder-open" | far | safe }}
35+
{{ "folder-open" | far }}
3636

3737
{# Rust files #}
3838
{%- elif file.mime == "text/rust" -%}
39-
{{ "rust" | fab | safe }}
39+
{{ "rust" | fab }}
4040

4141
{# Cargo.lock #}
4242
{%- elif file.mime == "text/plain" and file.name == "Cargo.lock" -%}
43-
{{ "lock" | fas | safe }}
43+
{{ "lock" | fas }}
4444

4545
{# Markdown files #}
4646
{% elif file.mime == "text/markdown" %}
47-
{{ "markdown" | fas | safe }}
47+
{{ "markdown" | fab }}
4848

4949
{# .gitignore #}
5050
{% elif file.mime == "text/plain" and file.name == ".gitignore" %}
51-
{{ "git-alt" | fab | safe }}
51+
{{ "git-alt" | fab }}
5252

5353
{#
5454
More ideas
@@ -70,11 +70,11 @@
7070

7171
{# Text files or files which mime starts with `text` #}
7272
{%- elif file.mime == "text/plain" or file.mime | split(pat="/") | first == "text" -%}
73-
{{ "file-alt" | far | safe }}
73+
{{ "file-alt" | far }}
7474

7575
{# Binary files and any unrecognized types #}
7676
{% else -%}
77-
{{ "file-archive" | far | safe }}
77+
{{ "file-archive" | far }}
7878
{%- endif -%}
7979

8080
{{ file.name }}

templates/header/global_alert.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{%- if global_alert -%}
66
<li class="pure-menu-item">
77
<a href="{{ global_alert.url | safe }}" class="pure-menu-link {{ global_alert.css_class }}">
8-
<i class="{{ global_alert.fa_icon | fas | safe }}"></i>
8+
<i class="{{ global_alert.fa_icon | fas }}"></i>
99
{{ global_alert.text }}
1010
</a>
1111
</li>

0 commit comments

Comments
 (0)