about summary refs log tree commit diff stats
path: root/src/web/generate/templates.rs
blob: bc9c664309b2cd257c529c0bf18208e2f735890f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Back - An extremely simple git bug visualization system. Inspired by TVL's
// panettone.
//
// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This file is part of Back.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/agpl.txt>.

use vy::{DOCTYPE, IntoHtml, PreEscaped, body, div, head, html, link, meta, title};

pub(crate) fn make_page(
    content: impl IntoHtml,
    description: &str,
    title: Option<&str>,
) -> impl IntoHtml {
    (
        DOCTYPE,
        html!(
            lang = "en",
            head!(
                title!(title.unwrap_or("Back")),
                link!(rel = "icon", href = "/favicon.ico"),
                link!(rel = "stylesheet", "type" = "text/css", href = "/style.css"),
                meta!(charset = "UTF-8"),
                meta!(
                    name = "viewport",
                    content = "width=device-width,initial-scale=1"
                ),
                meta!(name = "description", content = description),
            ),
            body!(div!(class = "content", content))
        ),
    )
}

pub(super) fn to_markdown(input: &str, is_title: bool) -> PreEscaped<String> {
    let markdown = markdown::to_html(input.trim());

    // If the markdown contains only one line line, assuming that it is a title is okay.
    if input.lines().count() == 1 && markdown.starts_with("<p>") && is_title {
        PreEscaped(
            markdown
                .strip_prefix("<p>")
                .expect("We checked")
                .strip_suffix("</p>")
                .expect("markdown crate produces no invalid html")
                .to_owned(),
        )
    } else {
        PreEscaped(markdown)
    }
}