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
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,
	syndicated: bool,
}

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

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

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_path: &str) -> Result<tera::Context> {
	let mut context = tera::Context::new();
	add_site_context(&mut context, state)?;
	add_list_context(&mut context, state, list_path)?;

	Ok(context)
}

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

	Ok(context)
}

pub(crate) fn atom_context(state: &ApplicationState, list_path: &str) -> Result<tera::Context> {
	// This is the same as list_context() for now, but it may be different later, so we have the API
	// in place

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

	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_path: &str) -> Result<()> {
	let list = state.content.find_list(list_path)?;

	let items = state.content.find_list_items(&list)
		.iter()
		.map(|item| ListItemContext {
			link: format!("/page/{}", item.slug),
			name: item.name.clone(),
			summary: item.summary.clone(),
			published: item.published.clone(),
			updated: item.updated.clone(),
			syndication_id: item.syndication_id.clone(),
			syndicated: item.syndicated,
		}).collect::<Vec<_>>();

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

	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_path: &str) -> Result<()> {
	let page = state.content.find_page(page_path)?;

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

	let page_context = PageContext {
		content,
		name,
		published,
		published_timestamp,
		updated,
		updated_timestamp,
		syndicated,
	};

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