Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Handlebars with Tera #688

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
332 changes: 314 additions & 18 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Steve Klabnik <steve@steveklabnik.com>"]
edition = "2018"

[dependencies]
handlebars = { version = "3", features = ["dir_source"] }
tera = "1.16.0"
lazy_static = "1.4.0"
serde = "1.0"
serde_derive = "1.0"
Expand Down
71 changes: 41 additions & 30 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Blog>,
out_directory: PathBuf,
}
Expand All @@ -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<String, Value>) -> tera::Result<Value> {
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<Path>,
posts_directory: impl AsRef<Path>,
) -> Result<Self, Box<dyn Error>> {
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(),
})
Expand Down Expand Up @@ -241,12 +247,17 @@ impl<'a> Generator<'a> {
fn render_template(
&self,
name: impl AsRef<Path>,
template: &str,
template_name: &str,
data: serde_json::Value,
) -> Result<(), Box<dyn Error>> {
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(())
}
}
Expand Down
29 changes: 0 additions & 29 deletions templates/feed.hbs

This file was deleted.

29 changes: 29 additions & 0 deletions templates/feed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
<generator uri="https://blog.rust-lang.org/{{ blog.prefix }}" version="0.1.0">{{ blog.title }}</generator>
<link href="https://blog.rust-lang.org/{{ blog.prefix }}feed.xml" rel="self" type="application/atom+xml" />
<link href="https://blog.rust-lang.org/{{ blog.prefix }}" rel="alternate" type="text/html" />
<id>https://blog.rust-lang.org/{{ blog.prefix }}</id>
<title>{{ blog.title }}</title>
<subtitle>{{ blog.description }}</subtitle>
<author>
<name>Maintained by {{ blog.maintained_by }}.</name>
<uri>https://github.com/rust-lang/blog.rust-lang.org/</uri>
</author>
<updated>{{ feed_updated }}</updated>

{% for post in posts %}
<entry>
<title>{{ post.title }}</title>
<link rel="alternate" href="https://blog.rust-lang.org/{{ blog.prefix ~ post.url }}" type="text/html" title="{{ post.title }}" />
<published>{{ post.published }}</published>
<updated>{{ post.updated }}</updated>
<id>https://blog.rust-lang.org/{{ blog.prefix ~ post.url }}</id>
<content type="html" xml:base="https://blog.rust-lang.org/{{ blog.prefix ~ post.url }}">{{ post.contents }}</content>

<author>
<name>{{ post.author }}</name>
</author>
</entry>
{% endfor %}
</feed>
14 changes: 7 additions & 7 deletions templates/footer.hbs → templates/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ <h4>Terms and policies</h4>
<div class="four columns mt3 mt0-l">
<h4>Social</h4>
<div class="flex flex-row flex-wrap">
<a href="https://twitter.com/rustlang" target="_blank" rel="noopener" alt="twitter link"><img src="{{root}}images/twitter.svg" alt="twitter logo" title="Twitter"/></a>
<a href="https://www.youtube.com/channel/UCaYhcUwRBNscFNUKTjgPFiA" target="_blank" rel="noopener" alt="youtube link"><img style="padding-top: 6px; padding-bottom:6px" src="{{root}}images/youtube.svg" alt="youtube logo" title="YouTube"/></a>
<a href="https://discord.gg/rust-lang" target="_blank" rel="noopener" alt="discord link"><img src="{{root}}images/discord.svg" alt="discord logo" title="Discord"/></a>
<a href="https://github.com/rust-lang" target="_blank" rel="noopener" alt="github link"><img src="{{root}}images/github.svg" alt="github logo" title="GitHub"/></a>
<a href="https://twitter.com/rustlang" target="_blank" rel="noopener" alt="twitter link"><img src="{{ root }}images/twitter.svg" alt="twitter logo" title="Twitter"/></a>
<a href="https://www.youtube.com/channel/UCaYhcUwRBNscFNUKTjgPFiA" target="_blank" rel="noopener" alt="youtube link"><img style="padding-top: 6px; padding-bottom:6px" src="{{ root }}images/youtube.svg" alt="youtube logo" title="YouTube"/></a>
<a href="https://discord.gg/rust-lang" target="_blank" rel="noopener" alt="discord link"><img src="{{ root }}images/discord.svg" alt="discord logo" title="Discord"/></a>
<a href="https://github.com/rust-lang" target="_blank" rel="noopener" alt="github link"><img src="{{ root }}images/github.svg" alt="github logo" title="GitHub"/></a>
</div>
<h4 class="mt4 mb3">RSS</h4>
<ul>
<li><a href="{{root}}feed.xml">Main Blog</a></li>
<li><a href="{{root}}inside-rust/feed.xml">"Inside Rust" Blog</a></li>
<li><a href="{{ root }}feed.xml">Main Blog</a></li>
<li><a href="{{ root }}inside-rust/feed.xml">"Inside Rust" Blog</a></li>
</ul>
</div>

Expand All @@ -42,4 +42,4 @@ <h4 class="mt4 mb3">RSS</h4>
</footer>

<!-- scripts -->
<script src="{{root}}scripts/highlight.js"></script>
<script src="{{ root }}scripts/highlight.js"></script>
33 changes: 0 additions & 33 deletions templates/headers.hbs

This file was deleted.

33 changes: 33 additions & 0 deletions templates/headers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!-- Twitter card -->
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@rustlang">
<meta name="twitter:creator" content="@rustlang">
<meta name="twitter:title" content="{{ title }}">
<meta name="twitter:description" content="{{ blog.description }}">
<meta name="twitter:image" content="https://www.rust-lang.org/static/images/rust-social.jpg">

<!-- Facebook OpenGraph -->
<meta property="og:title" content="{{ title }}">
<meta property="og:description" content="{{ blog.description }}">
<meta property="og:image" content="https://www.rust-lang.org/static/images/rust-social-wide.jpg">
<meta property="og:type" content="website">
<meta property="og:locale" content="en_US">

<!-- styles -->
<link rel="stylesheet" href="{{ root }}styles/vendor.css"/>
<link rel="stylesheet" href="{{ root }}styles/fonts.css"/>
<link rel="stylesheet" href="{{ root }}styles/app.css"/>
<link rel="stylesheet" href="{{ root }}styles/highlight.css"/>

<!-- favicon -->
<link rel="apple-touch-icon" sizes="180x180" href="{{ root }}images/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="16x16" href="{{ root }}images/favicon-16x16.png">
<link rel="icon" type="image/png" sizes="32x32" href="{{ root }}images/favicon-32x32.png">
<link rel="icon" type="image/svg+xml" href="{{ root }}images/favicon.svg">
<link rel="manifest" href="{{ root }}images/site.webmanifest">
<link rel="mask-icon" href="{{ root }}images/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#00aba9">
<meta name="theme-color" content="#ffffff">

<!-- atom -->
<link type="application/atom+xml" rel="alternate" href="https://blog.rust-lang.org/{{ blog.prefix }}feed.xml" title="{{ blog.title }}">
39 changes: 0 additions & 39 deletions templates/index.hbs

This file was deleted.

42 changes: 42 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{% extends "layout.html" %}

{% block page %}
<header class="mt3 mt0-ns mb4-ns">
<div class="container flex flex-column flex-row-l justify-between-l">
<div class="mw6-l">
<p>{{ blog.index_html | safe }}</p>
</div>
</div>
<div class="container flex flex-column flex-row-l justify-between-l">
<div class="mw8-l">
<p>
<b>See also:</b>
{% for blog in other_blogs %}
<a href="{{ blog.url }}">{{ blog.link_text }}</a>
{% endfor %}
</p>
</div>
</div>
</header>

<section id="posts" class="posts">
<div class="w-100 mw-none ph3 mw8-m mw9-l center f3">

<table class="post-list collapse w-100 f2-l f2-m f3-s">
{% for post in blog.posts %}
{% if post.show_year %}
<tr>
<td class="bn"></td>
<td class="bn"><h3 class="f0-l f1-m f2-s mt4 mb0">Posts in {{ post.year }}</h3></td>
</tr>
{% endif %}
<tr>
<td class="tr o-60 pr4 pr5-l bn">{{ post.month | month_name }}&nbsp;{{ post.day }}</td>
<td class="bn"><a href="{{ blog.prefix ~ post.url }}">{{ post.title }}</a></td>
</tr>
{% endfor %}
</table>

</div>
</section>
{% endblock page %}
8 changes: 4 additions & 4 deletions templates/layout.hbs → templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<title>{{ title }}</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<meta name="description" content="Empowering everyone to build reliable and efficient software.">
{{> headers }}
{% include "headers.html" %}
</head>
<body>
{{> nav }}
{{~> page}}
{{> footer }}
{% include "nav.html" %}
{% block page %}{% endblock page%}
{% include "footer.html" %}
</body>
</html>
6 changes: 3 additions & 3 deletions templates/nav.hbs → templates/nav.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<nav class="flex flex-row justify-center justify-end-l items-center flex-wrap ph2 pl3-ns pr4-ns">
<div class="brand flex-auto w-100 w-auto-l self-start tc tl-l">
<a href="{{root}}{{blog.prefix}}">
<img class="v-mid ml0-l" alt="Rust Logo" src="{{root}}images/rust-logo-blk.svg">
<span class="dib ml1 ml0-l">{{blog.title}}</span>
<a href="{{ root }}{{ blog.prefix }}">
<img class="v-mid ml0-l" alt="Rust Logo" src="{{ root }}images/rust-logo-blk.svg">
<span class="dib ml1 ml0-l">{{ blog.title }}</span>
</a>
</div>

Expand Down