1 use std::collections::HashMap;
2 use crate::metadata::Metadata;
3 use crate::template::{TemplateContext, TemplateValue};
6 pub metadata: Metadata,
13 pub fn to_template_context(posts: &Vec<Post>) -> TemplateContext {
14 let mut context = HashMap::new();
16 let posts_collection = posts
18 .map(|post| post.to_template_value())
21 "has_posts".to_string(),
22 TemplateValue::Bool(posts.len() > 0)
25 "posts_length".to_string(),
26 TemplateValue::Unsigned(
27 posts.len().try_into().unwrap()
32 TemplateValue::Collection(posts_collection)
37 pub fn to_template_value(&self) -> TemplateContext {
38 let mut context = HashMap::new();
42 TemplateValue::String(format!("{}", self.metadata.id))
45 "created_on".to_string(),
46 TemplateValue::Unsigned(self.metadata.created_on)
49 if let Some(created_on_utc) = self.metadata.created_on_utc() {
51 "created_on_utc".to_string(),
52 TemplateValue::String(created_on_utc)
58 TemplateValue::String(self.title())
62 TemplateValue::Unsigned(self.index.into())
66 TemplateValue::String(format!("{}", self.html))
69 "escaped_html".to_string(),
70 TemplateValue::String(format!("{}", self.escaped_html()))
74 TemplateValue::String(format!("{}", self.raw))
79 fn title(&self) -> String {
81 .split('\n').next().unwrap()
83 .replace("&", "&")
88 fn escaped_html(&self) -> String {
89 self.html.replace("&", "&")
92 .replace("\"", """)
93 .replace("'", "'")