From f600d2bfdcc30971f904a8f0dd3c099be4385c57 Mon Sep 17 00:00:00 2001 From: Aleksei Trifonov Date: Thu, 10 Sep 2020 20:30:28 +0300 Subject: [PATCH] Replace Handlebars with Tera --- Cargo.lock | 259 ++++++++++++++++++++++-- Cargo.toml | 2 +- src/main.rs | 71 ++++--- templates/feed.hbs | 29 --- templates/feed.html | 29 +++ templates/{footer.hbs => footer.html} | 8 +- templates/{headers.hbs => headers.html} | 24 +-- templates/index.hbs | 39 ---- templates/index.html | 42 ++++ templates/{layout.hbs => layout.html} | 8 +- templates/{nav.hbs => nav.html} | 4 +- templates/post.hbs | 19 -- templates/post.html | 24 +++ 13 files changed, 403 insertions(+), 155 deletions(-) delete mode 100644 templates/feed.hbs create mode 100644 templates/feed.html rename templates/{footer.hbs => footer.html} (92%) rename templates/{headers.hbs => headers.html} (61%) delete mode 100644 templates/index.hbs create mode 100644 templates/index.html rename templates/{layout.hbs => layout.html} (69%) rename templates/{nav.hbs => nav.html} (91%) delete mode 100644 templates/post.hbs create mode 100644 templates/post.html diff --git a/Cargo.lock b/Cargo.lock index 91e1853d9..f59277411 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -69,7 +69,6 @@ dependencies = [ "chrono", "comrak", "fs_extra", - "handlebars", "lazy_static", "regex", "sass-rs", @@ -77,6 +76,16 @@ dependencies = [ "serde_derive", "serde_json", "serde_yaml", + "tera", +] + +[[package]] +name = "bstr" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31accafdb70df7871592c058eca3985b71104e15ac32f64706022c58867da931" +dependencies = [ + "memchr", ] [[package]] @@ -114,6 +123,16 @@ dependencies = [ "time", ] +[[package]] +name = "chrono-tz" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2554a3155fec064362507487171dcc4edc3df60cb10f3a1fb10ed8094822b120" +dependencies = [ + "chrono", + "parse-zoneinfo", +] + [[package]] name = "clap" version = "2.33.0" @@ -148,6 +167,23 @@ dependencies = [ "xdg", ] +[[package]] +name = "crossbeam-utils" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" +dependencies = [ + "autocfg", + "cfg-if", + "lazy_static", +] + +[[package]] +name = "deunicode" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "850878694b7933ca4c9569d30a34b55031b9b139ee1fc7b94a527c4ef960d690" + [[package]] name = "digest" version = "0.8.1" @@ -175,6 +211,12 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "fs_extra" version = "1.1.0" @@ -191,17 +233,37 @@ dependencies = [ ] [[package]] -name = "handlebars" -version = "3.4.0" +name = "getrandom" +version = "0.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5deefd4816fb852b1ff3cb48f6c41da67be2d0e1d20b26a7a3b076da11f064b1" +checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6" dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "globset" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ad1da430bd7281dde2576f44c84cc3f0f7b475e7202cd503042dff01a8c8120" +dependencies = [ + "aho-corasick", + "bstr", + "fnv", "log", - "pest", - "pest_derive", - "quick-error", - "serde", - "serde_json", + "regex", +] + +[[package]] +name = "globwalk" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "178270263374052c40502e9f607134947de75302c1348d1a0e31db67c1691446" +dependencies = [ + "bitflags", + "ignore", "walkdir", ] @@ -214,6 +276,30 @@ dependencies = [ "libc", ] +[[package]] +name = "humansize" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e" + +[[package]] +name = "ignore" +version = "0.4.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22dcbf2a4a289528dbef21686354904e1c694ac642610a9bff9e7df730d9ec72" +dependencies = [ + "crossbeam-utils", + "globset", + "lazy_static", + "log", + "memchr", + "regex", + "same-file", + "thread_local", + "walkdir", + "winapi-util", +] + [[package]] name = "itoa" version = "0.4.5" @@ -294,6 +380,21 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" +[[package]] +name = "parse-zoneinfo" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c705f256449c60da65e11ff6626e0c16a0a0b96aaa348de61376b249bc340f41" +dependencies = [ + "regex", +] + +[[package]] +name = "percent-encoding" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" + [[package]] name = "pest" version = "2.1.3" @@ -343,6 +444,12 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677" +[[package]] +name = "ppv-lite86" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c36fa947111f5c62a733b652544dd0016a43ce89619538a8ef92724a6f501a20" + [[package]] name = "proc-macro2" version = "1.0.18" @@ -352,12 +459,6 @@ dependencies = [ "unicode-xid", ] -[[package]] -name = "quick-error" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ac73b1112776fc109b2e61909bc46c7e1bf0d7f690ffb1676553acce16d5cda" - [[package]] name = "quote" version = "1.0.3" @@ -367,6 +468,47 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom", + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core", +] + [[package]] name = "regex" version = "1.3.9" @@ -480,6 +622,15 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6fa3938c99da4914afedd13bf3d79bcb6c277d1b2c398d23257a304d9e1b074" +[[package]] +name = "slug" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3bc762e6a4b6c6fcaade73e77f9ebc6991b676f88bb2358bddb56560f073373" +dependencies = [ + "deunicode", +] + [[package]] name = "strsim" version = "0.8.0" @@ -497,6 +648,28 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "tera" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1381c83828bedd5ce4e59473110afa5381ffe523406d9ade4b77c9f7be70ff9a" +dependencies = [ + "chrono", + "chrono-tz", + "globwalk", + "humansize", + "lazy_static", + "percent-encoding", + "pest", + "pest_derive", + "rand", + "regex", + "serde", + "serde_json", + "slug", + "unic-segment", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -559,6 +732,56 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eeba86d422ce181a719445e51872fa30f1f7413b62becb52e95ec91aa262d85c" +[[package]] +name = "unic-char-property" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8c57a407d9b6fa02b4795eb81c5b6652060a15a7903ea981f3d723e6c0be221" +dependencies = [ + "unic-char-range", +] + +[[package]] +name = "unic-char-range" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0398022d5f700414f6b899e10b8348231abf9173fa93144cbc1a43b9793c1fbc" + +[[package]] +name = "unic-common" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80d7ff825a6a654ee85a63e80f92f054f904f21e7d12da4e22f9834a4aaa35bc" + +[[package]] +name = "unic-segment" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4ed5d26be57f84f176157270c112ef57b86debac9cd21daaabbe56db0f88f23" +dependencies = [ + "unic-ucd-segment", +] + +[[package]] +name = "unic-ucd-segment" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2079c122a62205b421f499da10f3ee0f7697f012f55b675e002483c73ea34700" +dependencies = [ + "unic-char-property", + "unic-char-range", + "unic-ucd-version", +] + +[[package]] +name = "unic-ucd-version" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96bd2f2237fe450fcd0a1d2f5f4e91711124f7857ba2e964247776ebeeb7b0c4" +dependencies = [ + "unic-common", +] + [[package]] name = "unicode-width" version = "0.1.7" @@ -594,6 +817,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + [[package]] name = "winapi" version = "0.3.8" diff --git a/Cargo.toml b/Cargo.toml index a75b4be19..d2b9755f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Steve Klabnik "] edition = "2018" [dependencies] -handlebars = { version = "3", features = ["dir_source"] } +tera = "1.5.0" lazy_static = "1.4.0" serde = "1.0" serde_derive = "1.0" diff --git a/src/main.rs b/src/main.rs index 51f89292b..35c822132 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,18 +4,19 @@ mod posts; use crate::blogs::Blog; use crate::posts::Post; use chrono::Timelike; -use handlebars::{handlebars_helper, Handlebars}; +use tera::{Tera, try_get_value, Context}; use sass_rs::{compile_file, Options}; use serde_derive::Serialize; -use serde_json::json; +use serde_json::{json, Value, to_value}; use std::convert::AsRef; +use std::collections::HashMap; use std::error::Error; use std::fs::{self, File}; use std::io::Write; use std::path::{Path, PathBuf}; -struct Generator<'a> { - handlebars: Handlebars<'a>, +struct Generator { + tera: Tera, blogs: Vec, out_directory: PathBuf, } @@ -31,34 +32,39 @@ struct ReleasePost { title: String, url: String, } -handlebars_helper!(hb_month_name_helper: |month_num: u64| match month_num { - 1 => "Jan.", - 2 => "Feb.", - 3 => "Mar.", - 4 => "Apr.", - 5 => "May", - 6 => "June", - 7 => "July", - 8 => "Aug.", - 9 => "Sept.", - 10 => "Oct.", - 11 => "Nov.", - 12 => "Dec.", - _ => "Error!", -}); - -impl<'a> Generator<'a> { + +fn month_name_filter(value: &Value, _: &HashMap) -> tera::Result { + let month_num = try_get_value!("month_name", "value", u64, value); + + let month_name = match month_num { + 1 => "Jan.", + 2 => "Feb.", + 3 => "Mar.", + 4 => "Apr.", + 5 => "May", + 6 => "June", + 7 => "July", + 8 => "Aug.", + 9 => "Sept.", + 10 => "Oct.", + 11 => "Nov.", + 12 => "Dec.", + _ => "Error!", + }; + + Ok(to_value(month_name).unwrap()) +} + +impl Generator { fn new( out_directory: impl AsRef, posts_directory: impl AsRef, ) -> Result> { - let mut handlebars = Handlebars::new(); - handlebars.set_strict_mode(true); - handlebars.register_templates_directory(".hbs", "templates")?; - handlebars.register_helper("month_name", Box::new(hb_month_name_helper)); + let mut tera = Tera::new("templates/**/*.html")?; + tera.register_filter("month_name", month_name_filter); Ok(Generator { - handlebars, + tera, blogs: crate::blogs::load(posts_directory.as_ref())?, out_directory: out_directory.as_ref().into(), }) @@ -213,12 +219,17 @@ impl<'a> Generator<'a> { fn render_template( &self, name: impl AsRef, - template: &str, + template_name: &str, data: serde_json::Value, ) -> Result<(), Box> { - let out_file = self.out_directory.join(name.as_ref()); - let file = File::create(out_file)?; - self.handlebars.render_to_write(template, &data, file)?; + let out_path = self.out_directory.join(name.as_ref()); + let mut out_file = File::create(out_path)?; + let context = Context::from_value(data)?; + let template_file_name = template_name.to_string() + ".html"; + + let rendered = self.tera.render(&template_file_name, &context)?; + out_file.write(rendered.as_bytes())?; + Ok(()) } } diff --git a/templates/feed.hbs b/templates/feed.hbs deleted file mode 100644 index 2511a8d0d..000000000 --- a/templates/feed.hbs +++ /dev/null @@ -1,29 +0,0 @@ - - - {{blog.title}} - - - https://blog.rust-lang.org/{{blog.prefix}} - {{blog.title}} - {{blog.description}} - - Maintained by {{blog.maintained_by}}. - https://github.com/rust-lang/blog.rust-lang.org/ - - {{feed_updated}} - - {{#each posts}} - - {{title}} - - {{published}} - {{updated}} - https://blog.rust-lang.org/{{../blog.prefix}}{{url}} - {{contents}} - - - {{author}} - - - {{/each}} - diff --git a/templates/feed.html b/templates/feed.html new file mode 100644 index 000000000..b6308ced2 --- /dev/null +++ b/templates/feed.html @@ -0,0 +1,29 @@ + + + {{ blog.title }} + + + https://blog.rust-lang.org/{{ blog.prefix }} + {{ blog.title }} + {{ blog.description }} + + Maintained by {{ blog.maintained_by }}. + https://github.com/rust-lang/blog.rust-lang.org/ + + {{ feed_updated }} + + {% for post in posts %} + + {{ post.title }} + + {{ post.published }} + {{ post.updated }} + https://blog.rust-lang.org/{{ blog.prefix ~ post.url }} + {{ post.contents }} + + + {{ post.author }} + + + {% endfor %} + diff --git a/templates/footer.hbs b/templates/footer.html similarity index 92% rename from templates/footer.hbs rename to templates/footer.html index 9b3535b62..4ae30bb2e 100644 --- a/templates/footer.hbs +++ b/templates/footer.html @@ -22,10 +22,10 @@

Terms and policies

Social

- twitter logo - youtube logo - discord logo - github logo + twitter logo + youtube logo + discord logo + github logo

RSS

    diff --git a/templates/headers.hbs b/templates/headers.html similarity index 61% rename from templates/headers.hbs rename to templates/headers.html index bdc90040c..d176832e7 100644 --- a/templates/headers.hbs +++ b/templates/headers.html @@ -2,22 +2,22 @@ - - + + - - - - - + + + + + - - - - + + + + @@ -30,4 +30,4 @@ - + diff --git a/templates/index.hbs b/templates/index.hbs deleted file mode 100644 index f0574185f..000000000 --- a/templates/index.hbs +++ /dev/null @@ -1,39 +0,0 @@ -{{#*inline "page"}} -
    -
    -
    -

    {{{blog.index_html}}}

    -
    -
    -
    -
    -

    - See also: - {{#each other_blogs}} - {{link_text}} - {{/each}} -

    -
    -
    -
    - -
    -
    - - - {{#each blog.posts}} - {{#if show_year}} - - - {{/if}} - - - - - {{/each}} -

    Posts in {{year}}

    {{month_name month}} {{day}}{{title}}
    - -
    -
    -{{/inline}} -{{~> (parent)~}} diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 000000000..f0796b568 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,42 @@ +{% extends "layout.html" %} + +{% block page %} +
    +
    +
    +

    {{ blog.index_html | safe }}

    +
    +
    +
    +
    +

    + See also: + {% for blog in other_blogs %} + {{ blog.link_text }} + {% endfor %} +

    +
    +
    +
    + +
    +
    + + + {% for post in blog.posts %} + {% if post.show_year %} + + + + + {% endif %} + + + + + {% endfor %} +

    Posts in {{ post.year }}

    {{ post.month | month_name }} {{ post.day }}{{ post.title }}
    + +
    +
    +{% endblock page %} \ No newline at end of file diff --git a/templates/layout.hbs b/templates/layout.html similarity index 69% rename from templates/layout.hbs rename to templates/layout.html index 7a79cfbeb..6e4804f7e 100644 --- a/templates/layout.hbs +++ b/templates/layout.html @@ -5,11 +5,11 @@ {{ title }} - {{> headers }} + {% include "headers.html" %} - {{> nav }} - {{~> page}} - {{> footer }} + {% include "nav.html" %} + {% block page %}{% endblock page%} + {% include "footer.html" %} diff --git a/templates/nav.hbs b/templates/nav.html similarity index 91% rename from templates/nav.hbs rename to templates/nav.html index 8b60ae001..511528daa 100644 --- a/templates/nav.hbs +++ b/templates/nav.html @@ -1,8 +1,8 @@