84 lines
2.1 KiB
Rust
84 lines
2.1 KiB
Rust
use super::{
|
|
pagination::{PaginatableTemplate, PaginationInfo},
|
|
posts::{parse::Tag, HtmlContent, Post},
|
|
util::templates::filters,
|
|
util::templates::TemplateCommon,
|
|
};
|
|
use crate::generator::pagination::render_paginated;
|
|
use askama::Template;
|
|
use std::collections::HashMap;
|
|
|
|
pub fn generate(posts: &[Post<HtmlContent>]) -> HashMap<&Tag, Vec<&Post<HtmlContent>>> {
|
|
let mut tags: HashMap<&Tag, Vec<&Post<HtmlContent>>> = HashMap::new();
|
|
for p in posts.iter() {
|
|
if let Some(ref ts) = p.metadata.tags {
|
|
for t in ts.iter() {
|
|
if tags.contains_key(t) {
|
|
let res = tags.get_mut(t).unwrap();
|
|
res.push(&p);
|
|
} else {
|
|
tags.insert(t, vec![&p]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// for (k, v) in tags.iter() {
|
|
// print!("{}: [", k.slug);
|
|
// let mut first = true;
|
|
// for p in v.iter() {
|
|
// if !first {
|
|
// print!(", ");
|
|
// } else {
|
|
// first = false;
|
|
// }
|
|
// print!("'{}'", p.metadata.title);
|
|
// }
|
|
// println!("]");
|
|
// }
|
|
|
|
for (&tag, posts) in &tags {
|
|
render_paginated(
|
|
&TagTemplate {
|
|
tag,
|
|
posts: &[],
|
|
pagination_info: Default::default(),
|
|
},
|
|
posts,
|
|
&tag.slug,
|
|
)
|
|
.unwrap();
|
|
}
|
|
|
|
return tags;
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "tag.html")]
|
|
struct TagTemplate<'a> {
|
|
tag: &'a Tag,
|
|
posts: &'a [&'a Post<HtmlContent>],
|
|
pagination_info: PaginationInfo,
|
|
}
|
|
|
|
impl<'a> TemplateCommon for TagTemplate<'a> {}
|
|
|
|
impl<'a> TagTemplate<'a> {
|
|
fn permalink(&self) -> String {
|
|
format!("/{}/", self.tag.slug)
|
|
}
|
|
}
|
|
|
|
impl<'a> PaginatableTemplate<'a> for TagTemplate<'a> {
|
|
fn with_posts(
|
|
&'a self,
|
|
posts: &'a [&'a Post<HtmlContent>],
|
|
pagination_info: PaginationInfo,
|
|
) -> Self {
|
|
Self {
|
|
posts,
|
|
pagination_info,
|
|
..*self
|
|
}
|
|
}
|
|
}
|