Skip to content
Snippets Groups Projects
Commit 1a41ded7 authored by Stephen D's avatar Stephen D
Browse files

page support

parent c8bf7385
Branches
No related tags found
No related merge requests found
......@@ -54,3 +54,7 @@ img, video {
th, td {
padding-left: 1em;
}
.page-link {
padding-left: 1em;
}
......@@ -7,7 +7,7 @@ use crate::{
};
pub struct Blog {
//pages: Vec<Page>,
pages: Vec<Post>,
posts: Vec<Post>,
imgs: Vec<MyImage>,
misc: Vec<Misc>,
......@@ -16,19 +16,14 @@ pub struct Blog {
impl Blog {
pub fn new() -> Result<Self, Error> {
/*let pages = fs::read_dir("pages")?
.map(|path| Page::new(path?)?)
.collect();*/
let pages = load_from_path("pages", "pages")?;
let posts = load_from_path("posts", "posts")?;
let imgs = load_from_path("assets/img", "")?;
let misc = load_from_path("assets/misc", "misc")?;
let custom_style = Style::load_if_exists("assets/style.css")?;
Ok(Self {
pages,
posts,
imgs,
misc,
......@@ -50,7 +45,7 @@ impl Blog {
content.push_str("</table>");
Ok(dress_page("Stephen's Site", &content))
Ok(dress_page("Stephen's Site", &content, &self.pages))
}
}
......@@ -75,7 +70,13 @@ impl TryFrom<Blog> for RenderedBlog {
let mut pages = HashMap::new();
for p in &b.posts {
let body = dress_page(&p.title, &p.html()?);
let body = dress_page(&p.title, &p.html()?, &b.pages);
pages.insert(p.url.clone(), Response::html(body));
}
for p in &b.pages {
let body = dress_page(&p.title, &p.html()?, &b.pages);
pages.insert(p.url.clone(), Response::html(body));
}
......@@ -103,6 +104,7 @@ impl TryFrom<Blog> for RenderedBlog {
let not_found = Response::html(dress_page(
"Page not found",
include_str!("assets/404.html"),
&b.pages,
));
dbg!(pages.keys());
......@@ -111,8 +113,13 @@ impl TryFrom<Blog> for RenderedBlog {
}
}
fn dress_page(title: &str, content: &str) -> String {
fn dress_page(title: &str, content: &str, pages: &[Post]) -> String {
let mut page_links = String::new();
for p in pages {
page_links.push_str(&p.link_short());
}
format!(
r#"<!DOCTYPE html><html><head><link rel="stylesheet" href="/style.css" /><title>{title}</title></head><body><a href="/">Home</a><hr>{content}</body></html>"#
r#"<!DOCTYPE html><html><head><link rel="stylesheet" href="/style.css" /><title>{title}</title></head><body><a href="/">Home</a>{page_links}<hr>{content}</body></html>"#
)
}
......@@ -14,7 +14,7 @@ use crate::{
#[derive(Eq)]
pub struct Post {
pub title: String,
pub date: Date,
pub date: Option<Date>,
pub content: String,
pub url: String,
}
......@@ -24,28 +24,50 @@ impl Post {
let title = &self.title;
let content = &self.content;
let date = &self.date_f()?;
Ok(format!(
r#"<div class="header-container"><h1>{title}</h1><div class="header-date">{date}</div></div>{content}"#
))
let mut out = format!(r#"<div class="header-container"><h1>{title}</h1>"#);
if let Some(date) = date {
out.push_str(r#"<div class="header-date">"#);
out.push_str(date);
out.push_str("</div>");
}
out.push_str("</div>");
out.push_str(content);
Ok(out)
}
pub fn link(&self) -> anyhow::Result<String> {
let link = format!(
let link = match self.date_f()? {
Some(date_f) => format!(
r#"<tr><td><a href="{}">{}</a></td><td>[{}]</td></tr>"#,
self.url,
self.title,
self.date_f()?
);
self.url, self.title, date_f
),
None => format!(
r#"<tr><td><a href="{}">{}</a></td><td></td></tr>"#,
self.url, self.title
),
};
Ok(link)
}
fn date_f(&self) -> anyhow::Result<String> {
let date_f = self
.date
.format(format_description!("[day] [month repr:short] [year]"))?;
pub fn link_short(&self) -> String {
format!(
r#"<a href="{}" class="page-link">{}</a>"#,
self.url, self.title
)
}
fn date_f(&self) -> anyhow::Result<Option<String>> {
let date_f = match self.date {
Some(date) => date.format(format_description!("[day] [month repr:short] [year]"))?,
None => return Ok(None),
};
Ok(date_f)
Ok(Some(date_f))
}
}
......@@ -137,7 +159,7 @@ impl Parser<Post> for PostParser {
fn render(self) -> anyhow::Result<Post> {
let title = self.title.context("Could not find post title")?;
let date = self.date.context("Could not find post date")?;
let date = self.date;
let content = String::from_utf8(self.content)?;
let url = self.url;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment