about summary refs log tree commit diff stats
path: root/src/web/generate/templates.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/web/generate/templates.rs')
-rw-r--r--src/web/generate/templates.rs44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/web/generate/templates.rs b/src/web/generate/templates.rs
new file mode 100644
index 0000000..693b862
--- /dev/null
+++ b/src/web/generate/templates.rs
@@ -0,0 +1,44 @@
+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)
+    }
+}