Echo Writes Code

templates.rs

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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use crate::errors::{ Result };
use crate::state::{ ApplicationState };

use serde::{ Serialize };

use chrono::{ DateTime };
use chrono::offset::{ Utc };

#[derive(Serialize)]
struct SiteContext {
	author: String,
	link: String,
	name: String,
	menu: Vec<LinkContext>,
	version: String,
}

#[derive(Serialize)]
struct ListContext {
	slug: String,
	items: Vec<ListItemContext>,
	link: String,
	name: String,
	updated: DateTime<Utc>,
	syndication_id: String,
}

#[derive(Serialize)]
struct ListItemContext {
	link: String,
	name: String,
	summary: String,
	published: DateTime<Utc>,
	updated: DateTime<Utc>,
	syndication_id: String,
	content: Option<String>,
}

#[derive(Serialize)]
struct LinkContext {
	link: String,
	name: String,
}

#[derive(Serialize)]
struct PageContext {
	content: String,
	name: String,
}

#[derive(Serialize)]
struct PostContext {
	content: String,
	name: String,
	published: String,
	published_timestamp: DateTime<Utc>,
	updated: String,
	updated_timestamp: DateTime<Utc>,
}

enum ListItemContentPolicy {
	WithContent,
	WithoutContent,
}

pub(crate) fn index_context(state: &ApplicationState) -> Result<tera::Context> {
	let mut context = tera::Context::new();
	add_site_context(&mut context, state)?;
	add_page_context(&mut context, state, "_index")?;

	Ok(context)
}

pub(crate) fn list_context(state: &ApplicationState, list_name: &str) -> Result<tera::Context> {
	// Do not load all the post contents, because we are just rendering an index
	let content_policy = ListItemContentPolicy::WithoutContent;

	let mut context = tera::Context::new();
	add_site_context(&mut context, state)?;
	add_list_context(&mut context, state, list_name, content_policy)?;

	Ok(context)
}

pub(crate) fn page_context(state: &ApplicationState, page_name: &str) -> Result<tera::Context> {
	let mut context = tera::Context::new();
	add_site_context(&mut context, state)?;
	add_page_context(&mut context, state, page_name)?;

	Ok(context)
}

pub(crate) fn post_context(state: &ApplicationState, post_name: &str) -> Result<tera::Context> {
	let mut context = tera::Context::new();
	add_site_context(&mut context, state)?;
	add_post_context(&mut context, state, post_name)?;

	Ok(context)
}

pub(crate) fn list_atom_feed_context(state: &ApplicationState, list_name: &str) -> Result<tera::Context> {
	// Load all the post contents, because we want to embed them in the feed
	let content_policy = ListItemContentPolicy::WithContent;

	let mut context = tera::Context::new();
	add_site_context(&mut context, state)?;
	add_list_context(&mut context, state, list_name, content_policy)?;

	Ok(context)
}

fn add_site_context(context: &mut tera::Context, state: &ApplicationState) -> Result<()> {
	let menu = state.content.site_menu()
		.iter()
		.map(|link| LinkContext {
			link: link.link.clone(),
			name: link.name.clone(),
		})
		.collect();

	let site_context = SiteContext {
		author: state.content.author(),
		link: state.content.site_link(),
		name: state.content.site_name(),
		menu,
		version: env!("CARGO_PKG_VERSION").to_string(),
	};

	context.insert("site", &site_context);
	Ok(())
}

fn add_list_context(context: &mut tera::Context, state: &ApplicationState, list_name: &str, content_policy: ListItemContentPolicy) -> Result<()> {
	let list = state.content.find_list(list_name)?;

	let items = state.content.find_list_items(&list)
		.iter()
		.map(|item| {
			let content = match content_policy {
				ListItemContentPolicy::WithContent => {
					let post = state.content.find_post(&item.slug)?;
					let content = state.content.render_post(&post)?;
					Some(content)
				},
				ListItemContentPolicy::WithoutContent => None,
			};

			Ok(ListItemContext {
				link: format!("/post/{}", item.slug),
				name: item.name.clone(),
				summary: item.summary.clone(),
				published: item.published.clone(),
				updated: item.updated.clone(),
				syndication_id: item.syndication_id.clone(),
				content,
			})
		}).collect::<Result<Vec<_>>>()?;

	let name = list.name;
	let slug = list.slug;
	let syndication_id = list.syndication_id;
	let link = format!("{}list/{}", state.content.site_link(), list_name);

	let updated = items
		.iter()
		.fold(DateTime::UNIX_EPOCH, |newest, item| {
			if item.updated > newest {
				item.updated.clone()
			} else {
				newest
			}
		});

	let list_context = ListContext {
		slug,
		items,
		link,
		name,
		updated,
		syndication_id,
	};

	context.insert("list", &list_context);
	Ok(())
}

fn add_page_context(context: &mut tera::Context, state: &ApplicationState, page_name: &str) -> Result<()> {
	let page = state.content.find_page(page_name)?;

	let content = state.content.render_page(&page)?;
	let name = page.name;

	let page_context = PageContext {
		content,
		name,
	};

	context.insert("page", &page_context);
	Ok(())
}

fn add_post_context(context: &mut tera::Context, state: &ApplicationState, post_name: &str) -> Result<()> {
	let post = state.content.find_post(post_name)?;

	let content = state.content.render_post(&post)?;
	let name = post.name;
	let published_timestamp = post.published;
	let updated_timestamp = post.updated;
	let published = published_timestamp.format("%Y-%m-%d").to_string();
	let updated = updated_timestamp.format("%Y-%m-%d").to_string();

	let post_context = PostContext {
		content,
		name,
		published,
		published_timestamp,
		updated,
		updated_timestamp,
	};

	context.insert("post", &post_context);
	Ok(())
}