Group pages by date

This commit is contained in:
Dawid Ciężarkiewicz 2020-05-15 23:34:53 -07:00
parent 2e564195be
commit eb98caab39
5 changed files with 42 additions and 24 deletions

7
Cargo.lock generated
View File

@ -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",

View File

@ -34,3 +34,4 @@ serde = "*"
serde_derive = "*"
horrorshow = "*"
chrono = "*"
itertools = "0.4"

View File

@ -14,7 +14,7 @@ use std::collections::{HashMap, HashSet};
pub struct Index<T> {
page_ids_by_tag: HashMap<String, HashSet<Id>>,
tags_by_page_id: HashMap<Id, HashSet<Tag>>,
title_by_page_id: HashMap<Id, String>,
page_info_by_page_id: HashMap<Id, PageInfo>,
store: T,
}
@ -23,6 +23,7 @@ pub struct Index<T> {
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<T> Index<T> {
.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<T> Index<T> {
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<T> Index<T> {
}
}
}
matching_pages.sort_unstable_by_key(|info| std::cmp::Reverse(info.headers.creation_time));
FindResults {
matching_pages,
matching_tags,
@ -190,8 +187,14 @@ impl<T> Index<T> {
}
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<T> Index<T> {
.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);
}
}

View File

@ -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(),
),
)));

View File

@ -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<Item = (Tag, usize)>,
unmatched_tags: Vec<(Tag, usize)>,
posts: impl Iterator<Item = index::PageInfo> + '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)
}
}
}
}