diff --git a/Cargo.lock b/Cargo.lock index 2239f0ec8..44a54bbb9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -381,7 +381,7 @@ dependencies = [ "strum 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)", "systemstat 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tera 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "tera 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3079,7 +3079,7 @@ dependencies = [ [[package]] name = "tera" -version = "1.3.1" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "chrono 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", @@ -4032,7 +4032,7 @@ dependencies = [ "checksum tar 0.4.26 (registry+https://github.com/rust-lang/crates.io-index)" = "b3196bfbffbba3e57481b6ea32249fbaf590396a52505a2615adbb79d9d826d3" "checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" "checksum tendril 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "707feda9f2582d5d680d733e38755547a3e8fb471e7ba11452ecfd9ce93a5d3b" -"checksum tera 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "55df25c7768a0fb9f165931366eb0f21587c407061e1e69c1f5c2b495adfd9bb" +"checksum tera 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1381c83828bedd5ce4e59473110afa5381ffe523406d9ade4b77c9f7be70ff9a" "checksum termcolor 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f" "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" "checksum thin-slice 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8eaa81235c7058867fa8c0e7314f33dcce9c215f535d1913822a2b3f5e289f3c" diff --git a/Cargo.toml b/Cargo.toml index 9b224d88c..cf81f431c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,7 +60,7 @@ staticfile = { version = "0.4", features = ["cache"] } tempfile = "3.1.0" # Templating -tera = { version = "1.3.1", features = ["builtins"] } +tera = { version = "1.5.0", features = ["builtins"] } walkdir = "2" # Template hot-reloading diff --git a/src/lib.rs b/src/lib.rs index 63eaa9ac7..0d4ab7798 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,7 +35,7 @@ pub(crate) static GLOBAL_ALERT: Option = Some(GlobalAlert { url: "https://blog.rust-lang.org/2019/09/18/upcoming-docsrs-changes.html", text: "Upcoming docs.rs breaking changes!", css_class: "error", - fa_icon: "warning", + fa_icon: "exclamation-triangle", }); */ diff --git a/src/web/page/templates.rs b/src/web/page/templates.rs index c5eebac78..cdfd60bf4 100644 --- a/src/web/page/templates.rs +++ b/src/web/page/templates.rs @@ -8,6 +8,7 @@ use postgres::Client; use serde_json::Value; use std::{ collections::HashMap, + fmt, path::PathBuf, sync::{mpsc::channel, Arc}, thread, @@ -147,6 +148,9 @@ pub(super) fn load_templates(conn: &mut Client) -> Result { tera.register_filter("timeformat", timeformat); tera.register_filter("dbg", dbg); tera.register_filter("dedent", dedent); + tera.register_filter("fas", IconType::Strong); + tera.register_filter("far", IconType::Regular); + tera.register_filter("fab", IconType::Brand); Ok(tera) } @@ -250,6 +254,69 @@ fn dedent(value: &Value, _args: &HashMap) -> TeraResult { )) } +enum IconType { + Strong, + Regular, + Brand, +} + +impl fmt::Display for IconType { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let icon = match self { + Self::Strong => "fas", + Self::Regular => "far", + Self::Brand => "fab", + }; + + f.write_str(icon) + } +} + +impl tera::Filter for IconType { + fn filter(&self, value: &Value, args: &HashMap) -> TeraResult { + let mut aria_hidden = true; + let class = tera::escape_html(value.as_str().expect("Icons only take strings")); + let fixed_width = if args.get("fw").and_then(|fw| fw.as_bool()).unwrap_or(false) { + " fa-fw" + } else { + "" + }; + let aria_label = args + .get("aria-label") + .and_then(|l| l.as_str()) + .map(|label| { + aria_hidden = false; + format!(r#" aria_label="{}""#, tera::escape_html(label)) + }) + .unwrap_or_default(); + let id = args + .get("id") + .and_then(|l| l.as_str()) + .map(|id| format!(r#" id="{}""#, tera::escape_html(id))) + .unwrap_or_default(); + aria_hidden = args + .get("aria-hidden") + .and_then(|l| l.as_bool()) + .unwrap_or(aria_hidden); + + let icon = format!( + r#""#, + aria_hidden = aria_hidden, + icon_class = self, + fa_class = class, + fw = fixed_width, + aria_label = aria_label, + id = id, + ); + + Ok(Value::String(icon)) + } + + fn is_safe(&self) -> bool { + true + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/templates/about-base.html b/templates/about-base.html index 1ca789783..f70382bef 100644 --- a/templates/about-base.html +++ b/templates/about-base.html @@ -2,22 +2,31 @@ {% block header %}
-
-

Docs.rs documentation

-
-
    - {% set text = ' About' %} - {{ macros::active_link(expected="index", href="/about", text=text) }} - {% set text = ' Badges' %} - {{ macros::active_link(expected="badges", href="/about/badges", text=text) }} - {% set text = ' Builds' %} - {{ macros::active_link(expected="builds", href="/about/builds", text=text) }} - {% set text = ' Metadata' %} - {{ macros::active_link(expected="metadata", href="/about/metadata", text=text) }} - {% set text = ' Shorthand URLs' %} - {{ macros::active_link(expected="redirections", href="/about/redirections", text=text) }} -
-
-
+
+

Docs.rs documentation

+
+
    + {% set text = "info-circle" | fas(fw=true) %} + {% set text = text ~ ' About' %} + {{ macros::active_link(expected="index", href="/about", text=text) }} + + {% set text = "fonticons" | fab(fw=true) %} + {% set text = text ~ ' Badges' %} + {{ macros::active_link(expected="badges", href="/about/badges", text=text) }} + + {% set text = "cogs" | fas(fw=true) %} + {% set text = text ~ ' Builds' %} + {{ macros::active_link(expected="builds", href="/about/builds", text=text) }} + + {% set text = "table" | fas(fw=true) %} + {% set text = text ~ ' Metadata' %} + {{ macros::active_link(expected="metadata", href="/about/metadata", text=text) }} + + {% set text = "road" | fas(fw=true) %} + {% set text = text ~ ' Shorthand URLs' %} + {{ macros::active_link(expected="redirections", href="/about/redirections", text=text) }} +
+
+
{% endblock %} diff --git a/templates/base.html b/templates/base.html index 68ce0d944..1bf090f9d 100644 --- a/templates/base.html +++ b/templates/base.html @@ -9,16 +9,19 @@ {%- block meta -%}{%- endblock meta -%} + {# External styles #} - + + + {# Docs.rs styles #} + {%- block css -%}{%- endblock css -%} diff --git a/templates/core/home.html b/templates/core/home.html index c51b3e90d..77f1aa580 100644 --- a/templates/core/home.html +++ b/templates/core/home.html @@ -4,7 +4,7 @@ {%- block body -%}
-

Docs.rs

+

{{ "cubes" | fas(fw=true) }} Docs.rs

@@ -28,7 +28,7 @@

Docs.rs

Recent Releases - + {{ "rss-square" | fas }}
@@ -54,7 +54,7 @@

Docs.rs

{%- if varsb.show_stars -%}
{{ release.stars }} - + {{ "star" | fas }}
{%- else -%}
{%- if build.build_status -%} - {%- set build_icon = "check" -%} + {{ "check" | fas }} {%- else -%} - {%- set build_icon = "close" -%} + {{ "times" | fas }} {%- endif -%} -
{{ build.rustc_version }}
{{ build.docsrs_version }}
diff --git a/templates/crate/details.html b/templates/crate/details.html index 4868474a9..e3f435b60 100644 --- a/templates/crate/details.html +++ b/templates/crate/details.html @@ -39,7 +39,7 @@ {%- if details.homepage_url -%}
  • - Homepage + {{ "home" | fas(fw=true) }} Homepage
  • {%- endif -%} @@ -48,7 +48,7 @@ {%- if details.documentation_url -%}
  • - Documentation + {{ "file-alt" | far(fw=true) }} Documentation
  • {%- endif -%} @@ -60,14 +60,14 @@ {# If the repo link is for github, show some github stats #} {# TODO: add support for hosts besides github (#35) #} {%- if details.github -%} - - {{ details.github_stars }} - {{ details.github_forks }} - {{ details.github_issues }} + {{ "github" | fab(fw=true) }} + {{ "star" | fas(fw=true) }} {{ details.github_stars }} + {{ "code-branch" | fas(fw=true) }} {{ details.github_forks }} + {{ "exclamation-circle" | fas(fw=true) }} {{ details.github_issues }} {# If the repo link is unknown, just show a normal link #} {%- else -%} - Repository + {{ "code-branch" | fas(fw=true) }} Repository {%- endif -%} @@ -77,7 +77,7 @@
  • - Crates.io + {{ "cube" | fas(fw=true) }} Crates.io
  • diff --git a/templates/crate/source.html b/templates/crate/source.html index ea87a06d1..cf5c32bb0 100644 --- a/templates/crate/source.html +++ b/templates/crate/source.html @@ -19,7 +19,7 @@ {# If this isn't the root folder, show a 'back' button #} {%- if show_parent_link -%}
  • - .. + {{ "folder-open" | far(fw=true) }} ..
  • {%- endif -%} @@ -32,24 +32,25 @@ {# Directories #} {%- if file.mime == "dir" -%} - + {{ "folder-open" | far(fw=true) }} {# Rust files #} {%- elif file.mime == "text/rust" -%} - + {{ "rust" | fab(fw=true) }} {# Cargo.lock #} {%- elif file.mime == "text/plain" and file.name == "Cargo.lock" -%} - + {{ "lock" | fas(fw=true) }} - {# - TODO: Font awesome v4.6 doesn't support these, upgrade and enable them - {% elif file.mime == "text/markdown" %} - + {# Markdown files #} + {% elif file.mime == "text/markdown" %} + {{ "markdown" | fab(fw=true) }} - {% elif file.mime == "text/plain" and file.name == ".gitignore" %} - + {# .gitignore #} + {% elif file.mime == "text/plain" and file.name == ".gitignore" %} + {{ "git-alt" | fab(fw=true) }} + {# More ideas FontAwesome v5: ".application/x-bzip" @@ -69,11 +70,11 @@ {# Text files or files which mime starts with `text` #} {%- elif file.mime == "text/plain" or file.mime | split(pat="/") | first == "text" -%} - + {{ "file-alt" | far(fw=true) }} {# Binary files and any unrecognized types #} {% else -%} - + {{ "file-archive" | far(fw=true) }} {%- endif -%} {{ file.name }} diff --git a/templates/header/global_alert.html b/templates/header/global_alert.html index 02be56cdd..c1f1afdad 100644 --- a/templates/header/global_alert.html +++ b/templates/header/global_alert.html @@ -5,7 +5,7 @@ {%- if global_alert -%}
  • - + {{ global_alert.text }}
  • diff --git a/templates/header/package_navigation.html b/templates/header/package_navigation.html index 3645dd9d4..f4d8cf44d 100644 --- a/templates/header/package_navigation.html +++ b/templates/header/package_navigation.html @@ -21,7 +21,7 @@

    {{ title }} {%- else -%} {{ metadata.name }} {{ metadata.version }} - + {{ "copy" | far(id="clipboard", aria_label="Copy crate name and version information", fa=true) }} {%- endif -%}

    @@ -61,7 +61,7 @@

  • {# The docs tab redirects to the docs, so the tab will never be selected and seen #} - + {{ "book" | fas(fw=true) }} Documentation
  • @@ -70,7 +70,7 @@

    {# The crate information tab #}
  • - + {{ "cube" | fas(fw=true) }} Crate
  • @@ -79,7 +79,7 @@

  • - + {{ "folder-open" | far(fw=true) }} Source
  • @@ -88,7 +88,7 @@

  • - + {{ "cogs" | fas }} Builds
  • diff --git a/templates/header/topbar.html b/templates/header/topbar.html index e3cd5649f..e1ccd1522 100644 --- a/templates/header/topbar.html +++ b/templates/header/topbar.html @@ -10,7 +10,7 @@ {# The search bar #}
    {# If there is a search query, put it in the search bar #} @@ -20,7 +20,7 @@ {# The top-left logo and name #} - Docs.rs + {{ "cubes" | fas(fw=true) }} Docs.rs