aboutsummaryrefslogtreecommitdiff
path: root/src/post.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/post.rs')
-rw-r--r--src/post.rs63
1 files changed, 22 insertions, 41 deletions
diff --git a/src/post.rs b/src/post.rs
index afe6b77..6f2120e 100644
--- a/src/post.rs
+++ b/src/post.rs
@@ -1,5 +1,5 @@
use crate::metadata::Metadata;
-use crate::template::{TemplateContext, TemplateValue};
+use crate::template::{Context, Value};
use std::collections::HashMap;
pub struct Post {
@@ -10,82 +10,63 @@ pub struct Post {
}
impl Post {
- pub fn to_template_context(posts: &Vec<Post>) -> TemplateContext {
+ pub fn to_template_context(posts: &[Post]) -> Context {
let mut context = HashMap::new();
- let posts_collection = posts.iter().map(|post| post.to_template_value()).collect();
- context.insert(
- "has_posts".to_string(),
- TemplateValue::Bool(posts.len() > 0),
- );
+ let posts_collection = posts.iter().map(Post::to_template_value).collect();
+ context.insert("has_posts".to_string(), Value::Bool(!posts.is_empty()));
context.insert(
"posts_length".to_string(),
- TemplateValue::Unsigned(posts.len().try_into().unwrap()),
- );
- context.insert(
- "posts".to_string(),
- TemplateValue::Collection(posts_collection),
+ Value::Unsigned(posts.len().try_into().unwrap()),
);
+ context.insert("posts".to_string(), Value::Collection(posts_collection));
context
}
- pub fn to_template_value(&self) -> TemplateContext {
+ pub fn to_template_value(&self) -> Context {
let mut context = HashMap::new();
context.insert(
"id".to_string(),
- TemplateValue::String(format!("{}", self.metadata.id)),
+ Value::String(self.metadata.id.to_string()),
);
context.insert(
"created_on".to_string(),
- TemplateValue::Unsigned(self.metadata.created_on),
+ Value::Unsigned(self.metadata.created_on),
);
if let Some(created_on_utc) = self.metadata.created_on_utc() {
- context.insert(
- "created_on_utc".to_string(),
- TemplateValue::String(created_on_utc),
- );
+ context.insert("created_on_utc".to_string(), Value::String(created_on_utc));
}
- context.insert("title".to_string(), TemplateValue::String(self.title()));
- context.insert(
- "index".to_string(),
- TemplateValue::Unsigned(self.index.into()),
- );
- context.insert(
- "html".to_string(),
- TemplateValue::String(format!("{}", self.html)),
- );
+ context.insert("title".to_string(), Value::String(self.title()));
+ context.insert("index".to_string(), Value::Unsigned(self.index.into()));
+ context.insert("html".to_string(), Value::String(self.html.to_string()));
context.insert(
"escaped_html".to_string(),
- TemplateValue::String(format!("{}", self.escaped_html())),
- );
- context.insert(
- "raw".to_string(),
- TemplateValue::String(format!("{}", self.raw)),
+ Value::String(self.escaped_html().to_string()),
);
+ context.insert("raw".to_string(), Value::String(self.raw.to_string()));
context
}
fn title(&self) -> String {
self.raw
- .trim()
- .split('\n')
+ .lines()
.next()
.unwrap()
.replace('#', "")
- .replace("&", "&amp;")
+ .replace('&', "&amp;")
.trim()
.to_string()
}
fn escaped_html(&self) -> String {
self.html
- .replace("&", "&amp;")
- .replace("<", "&lt;")
- .replace(">", "&gt;")
- .replace("\"", "&quot;")
- .replace("'", "&apos;")
+ .replace('&', "&amp;")
+ .replace('<', "&lt;")
+ .replace('>', "&gt;")
+ .replace('"', "&quot;")
+ .replace('\'', "&apos;")
}
}