gazetta_render_ext/content.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 horrorshow::{html, prelude::*};
17
18/// Renders content:
19///
20/// - Raw text will be rendered as escaped paragraphs (separated by double newlines).
21/// - HTML will be injected as-is with no processing.
22/// - Markdown documents will be rendered as one might expect EXCEPT that:
23/// - "./" at the beginning of links, image references, etc. will be replaced with `path`.
24/// - If `root` is specified, all links will be made absolute (prefixed with `root`).
25pub struct Content<'a> {
26 /// The content to render.
27 pub content: &'a gazetta_core::view::Content<'a>,
28 /// The website's "root" (origin). If specified, links and references will be absolute (markdown
29 /// only).
30 pub root: Option<&'a str>,
31 /// The current page, relative to the root (markdown only).
32 pub path: &'a str,
33 /// Whether or not to syntax highlight code blocks (markdown only).
34 pub syntax_highlight: bool,
35}
36
37impl RenderOnce for Content<'_> {
38 fn render_once(self, tmpl: &mut TemplateBuffer) {
39 self.render(tmpl);
40 }
41}
42
43impl RenderMut for Content<'_> {
44 fn render_mut(&mut self, tmpl: &mut TemplateBuffer) {
45 self.render(tmpl);
46 }
47}
48
49impl Render for Content<'_> {
50 fn render(&self, tmpl: &mut TemplateBuffer) {
51 match self.content.format {
52 "mkd" | "md" | "markdown" => {
53 tmpl << crate::Markdown::new(
54 self.content.data,
55 self.root,
56 self.path,
57 self.syntax_highlight,
58 )
59 }
60 "html" => tmpl << Raw(self.content.data),
61 "" | "text" | "txt" => {
62 tmpl << html! {
63 @ for p in self.content.data.split("\n\n") {
64 p : p;
65 }
66 }
67 }
68 format => tmpl.record_error(format!("unknown format '{format}'")),
69 }
70 }
71}