1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
use crate::config::Config; use serde_derive::{Deserialize, Serialize}; use std::collections::HashMap; use tera::{Context, Tera}; fn template_one(text: &str, context: &Context, auto_escape: bool) -> String { Tera::one_off(text, context, auto_escape).expect("could not render this template") } #[derive(Serialize, Debug, Clone)] pub struct MetaData { file_name: String, file_id: String, } #[derive(Serialize, Debug, Clone)] pub struct Post { config: Config, body: String, header: HashMap<String, String>, metadata: MetaData, } #[derive(Serialize, Debug)] pub struct Index<'a> { config: &'a Config, posts: &'a Vec<Post>, } #[derive(Serialize, Debug)] pub struct Feed<'a> { config: &'a Config, posts: &'a Vec<Post>, } #[derive(Serialize, Deserialize, Debug)] pub struct InitConfig { config: Config, } impl MetaData { pub fn new(file_name: String, file_id: String) -> MetaData { MetaData { file_name: file_name, file_id: file_id, } } } impl Post { pub fn new( config: Config, body: String, header: HashMap<String, String>, metadata: MetaData, ) -> Post { Post { config: config, body: body, header: header, metadata: metadata, } } pub fn template_text(&self, file_content: &str) -> String { let mut context = Context::new(); context.insert("config", &self.config); context.insert("post", &(&self.body, &self.header)); template_one(file_content, &context, false) } } impl Index<'_> { pub fn new<'a>(config: &'a Config, posts: &'a Vec<Post>) -> Index<'a> { Index { config: config, posts: posts, } } pub fn template_text(&self, file_content: &str) -> String { let mut context = Context::new(); context.insert("config", &self.config); context.insert("posts", &self.posts); template_one(file_content, &context, false) } } impl Feed<'_> { pub fn new<'a>(config: &'a Config, posts: &'a Vec<Post>) -> Feed<'a> { Feed { config: config, posts: posts, } } pub fn template_text(&self, file_content: &str) -> String { let mut context = Context::new(); context.insert("config", &self.config); context.insert("posts", &self.posts); template_one(file_content, &context, true) } } impl InitConfig { pub fn new(config: Config) -> InitConfig { InitConfig { config: config } } pub fn template_text(&self, file_content: &str) -> String { let mut context = Context::new(); context.insert("config", &self.config); template_one(file_content, &context, false) } }