From eb98caab39f29096199cbf5b89407c5d3344d5e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Ci=C4=99=C5=BCarkiewicz?= Date: Fri, 15 May 2020 23:34:53 -0700 Subject: [PATCH] Group pages by date --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/index.rs | 29 ++++++++++++++++------------- src/main.rs | 2 +- src/render.rs | 27 +++++++++++++++++---------- 5 files changed, 42 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8dcaa9e..fd08606 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -539,6 +539,12 @@ dependencies = [ "libc", ] +[[package]] +name = "itertools" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4a9b56eb56058f43dc66e58f40a214b2ccbc9f3df51861b63d51dec7b65bc3f" + [[package]] name = "itoa" version = "0.4.5" @@ -1232,6 +1238,7 @@ dependencies = [ "env_logger", "hex", "horrorshow", + "itertools", "lazy_static", "log 0.4.8", "pulldown-cmark", diff --git a/Cargo.toml b/Cargo.toml index f393786..51444a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,3 +34,4 @@ serde = "*" serde_derive = "*" horrorshow = "*" chrono = "*" +itertools = "0.4" diff --git a/src/index.rs b/src/index.rs index 2d17879..65fde66 100644 --- a/src/index.rs +++ b/src/index.rs @@ -14,7 +14,7 @@ use std::collections::{HashMap, HashSet}; pub struct Index { page_ids_by_tag: HashMap>, tags_by_page_id: HashMap>, - title_by_page_id: HashMap, + page_info_by_page_id: HashMap, store: T, } @@ -23,6 +23,7 @@ pub struct Index { pub struct PageInfo { pub id: Id, pub title: String, + pub headers: page::Headers, } /// Results of tag query lookup @@ -54,7 +55,7 @@ where let mut s = Index { page_ids_by_tag: Default::default(), tags_by_page_id: Default::default(), - title_by_page_id: Default::default(), + page_info_by_page_id: Default::default(), store, }; @@ -126,10 +127,7 @@ impl Index { .tags_by_page_id .keys() .cloned() - .map(|id| PageInfo { - id: id.clone(), - title: self.title_by_page_id[&id].clone(), - }) + .map(|id| self.page_info_by_page_id[&id].clone()) .collect(); } @@ -142,10 +140,7 @@ impl Index { if let Some(ids) = &self.page_ids_by_tag.get(*tag) { matching_pages = ids .iter() - .map(|id| PageInfo { - id: id.to_owned(), - title: self.title_by_page_id[id].clone(), - }) + .map(|id| self.page_info_by_page_id[id].clone()) .collect(); matching_tags.push(tag.to_string()) } else { @@ -175,6 +170,8 @@ impl Index { } } } + + matching_pages.sort_unstable_by_key(|info| std::cmp::Reverse(info.headers.creation_time)); FindResults { matching_pages, matching_tags, @@ -190,8 +187,14 @@ impl Index { } self.tags_by_page_id .insert(page.id().to_owned(), page.tags.clone()); - self.title_by_page_id - .insert(page.id().to_owned(), page.title.clone()); + self.page_info_by_page_id.insert( + page.id().to_owned(), + PageInfo { + id: page.id().to_owned(), + title: page.title.clone(), + headers: page.headers.clone(), + }, + ); } fn clean_data_for_page(&mut self, id: Id) { @@ -206,7 +209,7 @@ impl Index { .map(|set| set.remove(&id)); } self.tags_by_page_id.remove(&id); - self.title_by_page_id.remove(&id); + self.page_info_by_page_id.remove(&id); } } diff --git a/src/main.rs b/src/main.rs index f5465ef..b3d4d9b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -304,7 +304,7 @@ async fn handle_get( return Ok(warp_reply_from_render(render::html_page( render::post_list( page_state, - compact_results.tags.into_iter(), + compact_results.tags, results.matching_pages.into_iter(), ), ))); diff --git a/src/render.rs b/src/render.rs index f9365f5..fdf7d2d 100644 --- a/src/render.rs +++ b/src/render.rs @@ -2,6 +2,8 @@ use horrorshow::helper::doctype; use horrorshow::prelude::*; use horrorshow::{box_html, owned_html}; +use itertools::Itertools; + use crate::index; use crate::page::{Parsed, Tag}; @@ -178,7 +180,7 @@ pub fn page_view(page_state: PageState, sub_pages: impl RenderOnce) -> impl Rend pub fn post_list( page_state: PageState, - unmatched_tags: impl Iterator, + unmatched_tags: Vec<(Tag, usize)>, posts: impl Iterator + 'static, ) -> impl RenderOnce { let menu = menu(page_state.clone(), None); @@ -186,18 +188,23 @@ pub fn post_list( : menu; div(id="page-content") { h1 { : "Pages" } - ul(id="index") { - @ for post in posts { - li { - a(href=format!("./?id={}", post.id)) : post.title + @ for (date, group) in posts.group_by(|info| info.headers.creation_time.date()) { + h3 { : date.format("%A, %Y-%m-%d").to_string() } + ul { + @ for post in group { + li { + a(href=format!("./?id={}", post.id)) : post.title + } } } } - h1 { : "Subtags" } - ul(id="index") { - @ for tag in unmatched_tags { - li { - a(href=format!("./{}/", tag.0)) : format!("{} ({})", tag.0, tag.1) + @ if !unmatched_tags.is_empty() { + h1 { : "Subtags" } + ul(id="index") { + @ for tag in unmatched_tags { + li { + a(href=format!("./{}/", tag.0)) : format!("{} ({})", tag.0, tag.1) + } } } }