about summary refs log tree commit diff stats
path: root/src/web/generate/templates.rs
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-06-06 15:45:11 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-06-06 15:45:11 +0200
commita6baea06697f6c76c695dc4198099deb8ba916e0 (patch)
tree476a3865f6b4bef04751ba20534813a58892811b /src/web/generate/templates.rs
parentchore: Initial commit (diff)
downloadback-a6baea06697f6c76c695dc4198099deb8ba916e0.zip
feat(treewide): Prepare for first release
This commit contains many changes, as they were developed alongside
`git-bug-rs` and unfortunately not separately committed.

A toplevel summary would include:
- Appropriate redirects,
- The templating moved to `vy` (as this works with rustfmt formatting),
- Search support (via `git-bug-rs`),
- And better layout in the link section.
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)
+    }
+}