Skip to content

Commit 1b8462f

Browse files
committed
Use tera safe filters
1 parent 31549f1 commit 1b8462f

File tree

18 files changed

+112
-88
lines changed

18 files changed

+112
-88
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
@@ -148,6 +148,9 @@ pub(super) fn load_templates(conn: &mut Client) -> Result<Tera> {
148148
tera.register_filter("timeformat", timeformat);
149149
tera.register_filter("dbg", dbg);
150150
tera.register_filter("dedent", dedent);
151+
tera.register_filter("fas", IconType::Strong);
152+
tera.register_filter("far", IconType::Regular);
153+
tera.register_filter("fab", IconType::Brand);
151154

152155
Ok(tera)
153156
}
@@ -251,6 +254,43 @@ fn dedent(value: &Value, _args: &HashMap<String, Value>) -> TeraResult<Value> {
251254
))
252255
}
253256

257+
enum IconType {
258+
Strong,
259+
Regular,
260+
Brand,
261+
}
262+
263+
impl fmt::Display for IconType {
264+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
265+
let icon = match self {
266+
Self::Strong => "fas",
267+
Self::Regular => "far",
268+
Self::Brand => "fab",
269+
};
270+
271+
f.write_str(icon)
272+
}
273+
}
274+
275+
impl tera::Filter for IconType {
276+
fn filter(&self, value: &Value, args: &HashMap<String, Value>) -> TeraResult<Value> {
277+
let icon = format!(
278+
r#"<span class="{} fa-{}" aria-hidden="true" {}></span>"#,
279+
self,
280+
value.as_str().expect("Icons only take strings"),
281+
args.get("extra")
282+
.and_then(Value::as_str)
283+
.unwrap_or_default(),
284+
);
285+
286+
Ok(Value::String(icon))
287+
}
288+
289+
fn is_safe(&self) -> bool {
290+
true
291+
}
292+
}
293+
254294
#[cfg(test)]
255295
mod tests {
256296
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
@@ -39,7 +39,7 @@
3939
{%- if details.homepage_url -%}
4040
<li class="pure-menu-item">
4141
<a href="{{ details.homepage_url }}" class="pure-menu-link">
42-
{{ "home" | fas | safe }} Homepage
42+
{{ "home" | fas }} Homepage
4343
</a>
4444
</li>
4545
{%- endif -%}
@@ -48,7 +48,7 @@
4848
{%- if details.documentation_url -%}
4949
<li class="pure-menu-item">
5050
<a href="{{ details.documentation_url }}" title="Canonical documentation" class="pure-menu-link">
51-
{{ "file-alt" | far | safe }} Documentation
51+
{{ "file-alt" | far }} Documentation
5252
</a>
5353
</li>
5454
{%- endif -%}
@@ -60,14 +60,14 @@
6060
{# If the repo link is for github, show some github stats #}
6161
{# TODO: add support for hosts besides github (#35) #}
6262
{%- if details.github -%}
63-
{{ "github" | fab| safe }}
64-
{{ "star" | fas| safe }} {{ details.github_stars }}
65-
{{ fa::s(icon="code-branch") }} {{ details.github_forks }}
66-
{{ "exclamation-circle" | fas | safe }} {{ details.github_issues }}
63+
{{ "github" | fab }}
64+
{{ "star" | fas }} {{ details.github_stars }}
65+
{{ "code-branch" | fas }} {{ details.github_forks }}
66+
{{ "exclamation-circle" | fas }} {{ details.github_issues }}
6767

6868
{# If the repo link is unknown, just show a normal link #}
6969
{%- else -%}
70-
{{ fa::s(icon="code-branch") }} Repository
70+
{{ "code-branch" | fas }} Repository
7171
{%- endif -%}
7272
</a>
7373
</li>
@@ -77,7 +77,7 @@
7777
<li class="pure-menu-item">
7878
<a href="https://crates.io/crates/{{ details.name }}" class="pure-menu-link"
7979
title="See {{ details.name }} on crates.io">
80-
{{ "cube" | fas | safe }} Crates.io
80+
{{ "cube" | fas }} Crates.io
8181
</a>
8282
</li>
8383

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>

templates/header/package_navigation.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ <h1 id="crate-title">
6161
<li class="pure-menu-item">
6262
{# The docs tab redirects to the docs, so the tab will never be selected and seen #}
6363
<a href="/{{ crate_path | safe }}/{{ metadata.target_name }}/" class="pure-menu-link">
64-
{{ "book" | fas | safe }}
64+
{{ "book" | fas }}
6565
<span class="title"> Documentation</span>
6666
</a>
6767
</li>
@@ -70,7 +70,7 @@ <h1 id="crate-title">
7070
{# The crate information tab #}
7171
<li class="pure-menu-item"><a href="/crate/{{ crate_path | safe }}"
7272
class="pure-menu-link{% if active_tab == 'crate' %} pure-menu-active{% endif %}">
73-
{{ "cube" | fas | safe }}
73+
{{ "cube" | fas }}
7474
<span class="title"> Crate</span>
7575
</a>
7676
</li>
@@ -79,7 +79,7 @@ <h1 id="crate-title">
7979
<li class="pure-menu-item">
8080
<a href="/crate/{{ crate_path | safe }}/source/"
8181
class="pure-menu-link{% if active_tab == 'source' %} pure-menu-active{% endif %}">
82-
{{ "folder-open" | far | safe }}
82+
{{ "folder-open" | far }}
8383
<span class="title"> Source</span>
8484
</a>
8585
</li>
@@ -88,7 +88,7 @@ <h1 id="crate-title">
8888
<li class="pure-menu-item">
8989
<a href="/crate/{{ crate_path | safe }}/builds"
9090
class="pure-menu-link{% if active_tab == 'builds' %} pure-menu-active{% endif %}">
91-
{{ "cogs" | fas | safe }}
91+
{{ "cogs" | fas }}
9292
<span class="title"> Builds</span>
9393
</a>
9494
</li>

templates/header/topbar.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
{# The search bar #}
1111
<div id="search-input-nav">
1212
<label for="nav-search">
13-
{{ "search" | fas | safe }}
13+
{{ "search" | fas }}
1414
</label>
1515

1616
{# If there is a search query, put it in the search bar #}
@@ -20,7 +20,7 @@
2020

2121
{# The top-left logo and name #}
2222
<a href="/" class="pure-menu-heading pure-menu-link" aria-label="Docs.rs">
23-
{{ "book" | fas | safe }} Docs.rs
23+
{{ "cubes" | fas }} Docs.rs
2424
</a>
2525

2626
<ul class="pure-menu-list">

templates/icons.html

Lines changed: 0 additions & 16 deletions
This file was deleted.

templates/macros.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,26 +126,26 @@
126126
{# If the release isn't a library, then display that warning #}
127127
{% if not release.is_library -%}
128128
<a href="{{ release_url | safe }}" class="pure-menu-link warn" title="{{ release_name }} is not a library">
129-
<i class="fa fa-fw fa-warning"></i> {{ release.version }}
129+
{{ "exclamation-triangle" | fas }} {{ release.version }}
130130
</a>
131131

132132
{# If the release has been yanked and failed to build, display a warning #}
133133
{%- elif release.yanked and release.build_status -%}
134134
<a href="{{ release_url | safe }}" class="pure-menu-link warn" title="{{ release_name }} is yanked">
135-
{{ fa::s(icon="exclamation-triangle") }} {{ release.version }}
135+
{{ "exclamation-triangle" | fas }} {{ release.version }}
136136
</a>
137137

138138
{# If the release has been yanked and failed to build, display a warning #}
139139
{%- elif release.yanked and not release.build_status -%}
140140
<a href="{{ release_url | safe }}" class="pure-menu-link warn"
141141
title="{{ release_name }} is yanked and docs.rs failed to build it">
142-
<i class="fa fa-fw fa-warning"></i> {{ release.version }}
142+
{{ "exclamation-triangle" | fas }} {{ release.version }}
143143
</a>
144144

145145
{# If the release failed to build, display a warning #}
146146
{%- elif not release.build_status -%}
147147
<a href="{{ release_url | safe }}" class="pure-menu-link warn" title="docs.rs failed to build {{ release_name }}">
148-
<i class="fa fa-fw fa-warning"></i> {{ release.version }}
148+
{{ "exclamation-triangle" | fas }} {{ release.version }}
149149
</a>
150150

151151
{# Otherwise just display the version #}

0 commit comments

Comments
 (0)