gazetta_render_ext/
head.rs

1//  Copyright (C) 2015 Steven Allen
2//
3//  This file is part of gazetta.
4//
5//  This program is free software: you can redistribute it and/or modify it under the terms of the
6//  GNU General Public License as published by the Free Software Foundation version 3 of the
7//  License.
8//
9//  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10//  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
11//  the GNU General Public License for more details.
12//
13//  You should have received a copy of the GNU General Public License along with this program.  If
14//  not, see <http://www.gnu.org/licenses/>.
15//
16use gazetta_core::render::Gazetta;
17use gazetta_core::view::Context;
18use horrorshow::prelude::*;
19use horrorshow::{Concat, html};
20
21/// Renders common head tags for a site and page.
22pub struct Head<'a, G>(pub &'a Context<'a, G>)
23where
24    G: Gazetta + 'a,
25    G::SiteMeta: 'a,
26    G::PageMeta: 'a;
27
28impl<'a, G> RenderOnce for Head<'a, G>
29where
30    G: Gazetta + 'a,
31    G::SiteMeta: 'a,
32    G::PageMeta: 'a,
33{
34    fn render_once(self, tmpl: &mut TemplateBuffer) {
35        self.render(tmpl)
36    }
37}
38
39impl<'a, G> RenderMut for Head<'a, G>
40where
41    G: Gazetta + 'a,
42    G::SiteMeta: 'a,
43    G::PageMeta: 'a,
44{
45    fn render_mut(&mut self, tmpl: &mut TemplateBuffer) {
46        self.render(tmpl)
47    }
48}
49
50impl<'a, G> Render for Head<'a, G>
51where
52    G: Gazetta + 'a,
53    G::SiteMeta: 'a,
54    G::PageMeta: 'a,
55{
56    fn render(&self, tmpl: &mut TemplateBuffer) {
57        tmpl << html! {
58            meta(charset="utf-8");
59            base(href=Concat(
60                self.0.page.href
61                    .split('/')
62                    .filter(|s| !s.is_empty())
63                    .map(|_| "../")));
64            @ if let Some(css) = self.0.site.stylesheets {
65                link(rel="stylesheet", href=css);
66            }
67            @ if let Some(js) = self.0.site.javascript {
68                script(async, src=js) {}
69            }
70            @ if let Some(icon) = self.0.site.icon {
71                link(rel="shortcut icon", href=icon);
72            }
73            @ if let Some(feed) = self.0.page.index.and_then(|idx| idx.feed) {
74                link(rel="alternate", type="application/atom+xml", title=self.0.page.title, href=feed);
75            }
76            @ if let Some(desc) = self.0.page.description {
77                meta(name="description", content = desc);
78            }
79        };
80    }
81}