1
0
mirror of https://github.com/dpc/tagwiki synced 2024-06-17 10:39:54 +00:00

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", "libc",
] ]
[[package]]
name = "itertools"
version = "0.4.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4a9b56eb56058f43dc66e58f40a214b2ccbc9f3df51861b63d51dec7b65bc3f"
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "0.4.5" version = "0.4.5"
@ -1232,6 +1238,7 @@ dependencies = [
"env_logger", "env_logger",
"hex", "hex",
"horrorshow", "horrorshow",
"itertools",
"lazy_static", "lazy_static",
"log 0.4.8", "log 0.4.8",
"pulldown-cmark", "pulldown-cmark",

View File

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

View File

@ -14,7 +14,7 @@ use std::collections::{HashMap, HashSet};
pub struct Index<T> { pub struct Index<T> {
page_ids_by_tag: HashMap<String, HashSet<Id>>, page_ids_by_tag: HashMap<String, HashSet<Id>>,
tags_by_page_id: HashMap<Id, HashSet<Tag>>, 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, store: T,
} }
@ -23,6 +23,7 @@ pub struct Index<T> {
pub struct PageInfo { pub struct PageInfo {
pub id: Id, pub id: Id,
pub title: String, pub title: String,
pub headers: page::Headers,
} }
/// Results of tag query lookup /// Results of tag query lookup
@ -54,7 +55,7 @@ where
let mut s = Index { let mut s = Index {
page_ids_by_tag: Default::default(), page_ids_by_tag: Default::default(),
tags_by_page_id: Default::default(), tags_by_page_id: Default::default(),
title_by_page_id: Default::default(), page_info_by_page_id: Default::default(),
store, store,
}; };
@ -126,10 +127,7 @@ impl<T> Index<T> {
.tags_by_page_id .tags_by_page_id
.keys() .keys()
.cloned() .cloned()
.map(|id| PageInfo { .map(|id| self.page_info_by_page_id[&id].clone())
id: id.clone(),
title: self.title_by_page_id[&id].clone(),
})
.collect(); .collect();
} }
@ -142,10 +140,7 @@ impl<T> Index<T> {
if let Some(ids) = &self.page_ids_by_tag.get(*tag) { if let Some(ids) = &self.page_ids_by_tag.get(*tag) {
matching_pages = ids matching_pages = ids
.iter() .iter()
.map(|id| PageInfo { .map(|id| self.page_info_by_page_id[id].clone())
id: id.to_owned(),
title: self.title_by_page_id[id].clone(),
})
.collect(); .collect();
matching_tags.push(tag.to_string()) matching_tags.push(tag.to_string())
} else { } else {
@ -175,6 +170,8 @@ impl<T> Index<T> {
} }
} }
} }
matching_pages.sort_unstable_by_key(|info| std::cmp::Reverse(info.headers.creation_time));
FindResults { FindResults {
matching_pages, matching_pages,
matching_tags, matching_tags,
@ -190,8 +187,14 @@ impl<T> Index<T> {
} }
self.tags_by_page_id self.tags_by_page_id
.insert(page.id().to_owned(), page.tags.clone()); .insert(page.id().to_owned(), page.tags.clone());
self.title_by_page_id self.page_info_by_page_id.insert(
.insert(page.id().to_owned(), page.title.clone()); 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) { fn clean_data_for_page(&mut self, id: Id) {
@ -206,7 +209,7 @@ impl<T> Index<T> {
.map(|set| set.remove(&id)); .map(|set| set.remove(&id));
} }
self.tags_by_page_id.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( return Ok(warp_reply_from_render(render::html_page(
render::post_list( render::post_list(
page_state, page_state,
compact_results.tags.into_iter(), compact_results.tags,
results.matching_pages.into_iter(), results.matching_pages.into_iter(),
), ),
))); )));

View File

@ -2,6 +2,8 @@ use horrorshow::helper::doctype;
use horrorshow::prelude::*; use horrorshow::prelude::*;
use horrorshow::{box_html, owned_html}; use horrorshow::{box_html, owned_html};
use itertools::Itertools;
use crate::index; use crate::index;
use crate::page::{Parsed, Tag}; 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( pub fn post_list(
page_state: PageState, page_state: PageState,
unmatched_tags: impl Iterator<Item = (Tag, usize)>, unmatched_tags: Vec<(Tag, usize)>,
posts: impl Iterator<Item = index::PageInfo> + 'static, posts: impl Iterator<Item = index::PageInfo> + 'static,
) -> impl RenderOnce { ) -> impl RenderOnce {
let menu = menu(page_state.clone(), None); let menu = menu(page_state.clone(), None);
@ -186,18 +188,23 @@ pub fn post_list(
: menu; : menu;
div(id="page-content") { div(id="page-content") {
h1 { : "Pages" } h1 { : "Pages" }
ul(id="index") { @ for (date, group) in posts.group_by(|info| info.headers.creation_time.date()) {
@ for post in posts { h3 { : date.format("%A, %Y-%m-%d").to_string() }
li { ul {
a(href=format!("./?id={}", post.id)) : post.title @ for post in group {
li {
a(href=format!("./?id={}", post.id)) : post.title
}
} }
} }
} }
h1 { : "Subtags" } @ if !unmatched_tags.is_empty() {
ul(id="index") { h1 { : "Subtags" }
@ for tag in unmatched_tags { ul(id="index") {
li { @ for tag in unmatched_tags {
a(href=format!("./{}/", tag.0)) : format!("{} ({})", tag.0, tag.1) li {
a(href=format!("./{}/", tag.0)) : format!("{} ({})", tag.0, tag.1)
}
} }
} }
} }