summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWriterV2 <primary-v2@protonmail.com>2026-05-11 00:16:43 +0200
committerWriterV2 <primary-v2@protonmail.com>2026-05-11 00:16:43 +0200
commitfa0f57d719dee5f3a34a03f660e9db0084e03f74 (patch)
treea92e212ecd1e705b2f241b3632583d660530c55f
parent5a819e715b6ae9e81417b38c7147bd7cd0999350 (diff)
Add home page
-rw-r--r--src/main.rs84
-rw-r--r--src/page.rs18
2 files changed, 95 insertions, 7 deletions
diff --git a/src/main.rs b/src/main.rs
index 7ad56c2..5882335 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -12,12 +12,89 @@ struct Data {
stories: Vec<Story>,
}
+impl page::Page for Data {
+ fn page_title() -> String {
+ String::from("Home")
+ }
+
+ fn alternative_htmlfilename() -> Option<String> {
+ Some(String::from("index"))
+ }
+
+ fn render_content(&self) -> Markup {
+ let last_story = self.stories.first();
+ html!(
+ @if let Some(story) = last_story {
+ div class="latest-story" {
+ h2 { "Latest Story" }
+ h3 {
+ (story.name)
+ }
+ p { (story.description) }
+ ul {
+ li { "Language: " (story.language) }
+ li {
+ a href=(&story.format_pdf_path()) download
+ {
+ "PDF"
+ }
+ }
+ }
+
+ }
+ }
+ )
+ }
+
+ fn content_style() -> Markup {
+ html!(
+ """
+ h2 {
+ font-size: 2.5rem;
+ color: #2F3C57;
+ text-decoration: 0.1rem solid underline black;
+ }
+
+ h3 {
+ font-size: 1.8rem;
+ color: #2F3C57;
+ padding: 2rem 0;
+ }
+
+ .latest-story {
+ padding: 3rem;
+ margin: 3rem;
+ text-align: left;
+ background-color: #FFD1A3;
+ * {
+ margin: 1rem 0;
+ }
+
+ p {
+ font-size: 1.2rem;
+ }
+ }
+
+ a {
+ text-decoration: underline;
+ color: #3D1D49;
+ }
+
+ ul {
+ list-style: none;
+ }
+
+ """
+ )
+ }
+}
+
impl page::Page for Vec<Story> {
fn page_title() -> String {
String::from("Stories")
}
- fn render_content(self) -> Markup {
+ fn render_content(&self) -> Markup {
html!(
@for story in self {
(story.render_as_card())
@@ -111,7 +188,7 @@ struct Story {
}
impl Story {
- fn render_as_card(self) -> Markup {
+ fn render_as_card(&self) -> Markup {
html!(div class="story-card" {
card {
h2 { (self.name) }
@@ -147,7 +224,8 @@ impl Story {
fn main() -> Result<()> {
let data_file_content =
std::fs::read_to_string("data.toml").with_context(|| "Failed to read data.toml")?;
- let data: Data = toml::from_str(&data_file_content)?;
+ let mut data: Data = toml::from_str(&data_file_content)?;
data.stories.write_page()?;
+ data.write_page()?;
Ok(())
}
diff --git a/src/page.rs b/src/page.rs
index ed2e0a5..6f3331f 100644
--- a/src/page.rs
+++ b/src/page.rs
@@ -3,7 +3,7 @@ use maud::{html, Markup, DOCTYPE};
use minify_html::{minify, Cfg};
pub trait Page: Sized {
- fn render_content(self) -> Markup;
+ fn render_content(&self) -> Markup;
fn page_title() -> String;
fn render_head() -> Markup {
html!(
@@ -83,6 +83,8 @@ pub trait Page: Sized {
}
}
+
+
"""
)
}
@@ -100,19 +102,22 @@ pub trait Page: Sized {
)
}
- fn render_page(self) -> Markup {
+ fn render_page(&self) -> Markup {
html!((Self::render_head())(Self::style())(
Self::render_navigation()
)(Self::render_content(self)))
}
- fn write_page(mut self) -> Result<()> {
+ fn write_page(&mut self) -> Result<()> {
self.prepare_content()?;
let page_html = self.render_page().into_string();
let mut cfg = Cfg::new();
cfg.minify_css = true;
let minified_html = minify(page_html.as_bytes(), &cfg);
- let html_path = format!("dist/{}.html", Self::page_title().to_lowercase());
+ let html_path = format!(
+ "dist/{}.html",
+ Self::alternative_htmlfilename().unwrap_or(Self::page_title().to_lowercase())
+ );
std::fs::write(&html_path, minified_html)
.with_context(|| format!("Failed to write {}", html_path))?;
@@ -123,4 +128,9 @@ pub trait Page: Sized {
fn prepare_content(&mut self) -> Result<()> {
Ok(())
}
+
+ // without ".html"
+ fn alternative_htmlfilename() -> Option<String> {
+ None
+ }
}