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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
use anyhow::{bail, Result};
use chrono::{DateTime, Datelike, Utc};
use serde::{Deserialize, Serialize};
use slug::slugify;
use std::collections::HashMap;
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
use crate::markdown::markdown_to_html;
#[derive(Debug)]
pub struct Blog {
pub conf: BlogConf,
pub posts: HashMap<String, Post>,
pub ordered_posts: Vec<String>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct BlogConf {
title: String,
root: String,
page_size: Option<usize>,
enable_drafts: Option<bool>,
posts_dir: Option<String>,
github: Option<String>,
twitter: Option<String>,
disqus: Option<String>,
giscus: Option<Giscus>,
google_analytics: Option<GoogleAnalytics>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Giscus {
pub script_src: String,
pub repo: String,
pub repo_id: String,
pub category: String,
pub category_id: String,
pub mapping: String,
pub reactions_enabled: u32,
pub emit_metadata: u32,
pub theme: String,
pub lang: String,
pub crossorigin: String,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct GoogleAnalytics {
pub ga_measurement_id: String,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Post {
metadata: PostMetadata,
content: String,
html_content: String,
attachments: HashMap<String, Attachment>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct PostMetadata {
title: String,
subtitle: Option<String>,
slug: Option<String>,
date: Option<DateTime<Utc>>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Attachment {
path: PathBuf,
}
impl Blog {
pub fn from_conf(conf: BlogConf) -> Result<Self> {
let posts = Post::read_all_from_dir(Path::new(conf.get_post_dir().as_ref().unwrap()))?;
let mut values: Vec<&Post> = posts
.values()
.filter(|p| p.get_metadata().get_date().is_some())
.collect();
values.sort_by(|a, b| {
b.get_metadata()
.get_date()
.as_ref()
.unwrap()
.cmp(a.get_metadata().get_date().as_ref().unwrap())
});
let ordered_posts: Vec<String> = values
.into_iter()
.map(|p| {
let slug = &p.get_metadata().get_slug().as_ref().unwrap().to_owned();
slug.to_string()
})
.collect();
Ok(Blog {
conf,
posts,
ordered_posts,
})
}
pub fn get_paged_posts(&self, page: usize) -> impl Iterator<Item = &str> {
let page_size = self.conf.get_page_size();
self.get_all_posts()
.skip((page - 1) * page_size)
.take(page_size)
}
pub fn get_all_posts(&self) -> impl Iterator<Item = &str> {
self.ordered_posts.iter().map(|s| s.as_ref())
}
pub fn get_post(&self, key: &str) -> Option<&Post> {
self.posts.get(key)
}
pub fn get_blog_conf(&self) -> &BlogConf {
&self.conf
}
pub fn get_current_year(&self) -> i32 {
Utc::now().year()
}
}
impl BlogConf {
pub fn new_from_file(conf_path: &Path) -> Result<Self> {
if !conf_path.exists() {
bail!("File not found - {:?}", &conf_path);
}
let conf_contents = fs::read_to_string(&conf_path)?;
let mut conf: BlogConf = serde_yaml::from_str(&conf_contents)?;
if conf.posts_dir.is_none() {
conf.posts_dir = Some(String::from("./posts"));
}
let mut posts_dir_path = PathBuf::from(conf.posts_dir.as_ref().unwrap());
if posts_dir_path.is_relative() {
let conf_dir = conf_path.parent().unwrap();
posts_dir_path = conf_dir.join(posts_dir_path);
conf.posts_dir = Some(posts_dir_path.to_str().unwrap().to_owned());
}
if !posts_dir_path.exists() {
bail!(
"Directory not found - {:?}",
conf.posts_dir.as_ref().unwrap()
);
}
Ok(conf)
}
pub fn get_post_dir(&self) -> Option<&str> {
self.posts_dir.as_deref()
}
pub fn get_page_size(&self) -> usize {
self.page_size.unwrap_or(5)
}
pub fn get_title(&self) -> &str {
&self.title
}
pub fn get_root(&self) -> &str {
&self.root
}
pub fn get_github(&self) -> Option<&str> {
self.github.as_deref()
}
pub fn get_twitter(&self) -> Option<&str> {
self.twitter.as_deref()
}
pub fn get_disqus(&self) -> Option<&str> {
self.disqus.as_deref()
}
pub fn get_giscus(&self) -> Option<&Giscus> {
self.giscus.as_ref()
}
pub fn get_google_analytics(&self) -> Option<&GoogleAnalytics> {
self.google_analytics.as_ref()
}
}
impl Post {
pub fn read_all_from_dir(path: &Path) -> Result<HashMap<String, Post>> {
let mut map = HashMap::new();
for path in fs::read_dir(path)? {
let path = path?.path();
let metadata = fs::metadata(&path)?;
let post = if metadata.is_file() {
if path.extension().and_then(OsStr::to_str).unwrap() != "md" {
continue;
}
Post::new_from_file(&path)?
} else {
Post::new_from_dir(&path)?
};
let key = post.metadata.slug.as_ref().unwrap();
if map.contains_key(key) {
bail!("Post {:?} already exists", &path);
} else {
map.insert(String::from(key), post);
}
}
Ok(map)
}
pub fn new_from_str(raw: &str) -> Result<Post> {
let header_start = raw.find("---");
if header_start.is_none() {
bail!("--- header not found");
}
let header_start = header_start.unwrap();
let content_start = &raw[header_start + 3..].find("---");
if content_start.is_none() {
bail!("--- content not found");
}
let content_start = content_start.unwrap();
let header = &raw[header_start..content_start + 3];
let content = &raw[header.len() + 3..];
let mut metadata: PostMetadata = serde_yaml::from_str(header)?;
metadata.slug = match &metadata.slug {
Some(slug) => Some(slug.trim().to_lowercase()),
None => Some(slugify(&metadata.title)),
};
let content = content.trim();
let post = Post {
metadata,
content: content.into(),
html_content: markdown_to_html(content),
attachments: HashMap::new(),
};
Ok(post)
}
pub fn new_from_file(path: &Path) -> Result<Post> {
let raw = fs::read_to_string(path)?;
Post::new_from_str(&raw)
}
pub fn new_from_dir(path: &Path) -> Result<Post> {
let mut all_md_files: Vec<fs::DirEntry> = fs::read_dir(path)?
.filter_map(Result::ok)
.filter_map(|d| {
if d.path().extension()? == "md" {
Some(d)
} else {
None
}
})
.collect();
all_md_files.sort_by_key(|p| p.path());
let post_file = &all_md_files.first().unwrap();
let mut post = Post::new_from_file(&post_file.path())?;
for path in fs::read_dir(path)? {
let path = path?.path();
let metadata = fs::metadata(&path)?;
if metadata.is_file() && path.to_str() != post_file.path().to_str() {
let attachement = Attachment {
path: PathBuf::from(&path),
};
post.attachments.insert(
path.file_name()
.unwrap()
.to_os_string()
.into_string()
.unwrap(),
attachement,
);
}
}
Ok(post)
}
pub fn get_content(&self) -> &str {
&self.content
}
pub fn get_html_content(&self) -> &str {
&self.html_content
}
pub fn get_metadata(&self) -> &PostMetadata {
&self.metadata
}
pub fn get_url(&self) -> String {
format!(
"/posts/{}/",
&self.get_metadata().get_slug().as_ref().unwrap()
)
}
pub fn get_attachment(&self, name: &str) -> Option<&Attachment> {
self.attachments.get(name)
}
}
impl PostMetadata {
pub fn get_title(&self) -> &str {
&self.title
}
pub fn get_subtitle(&self) -> Option<&str> {
self.subtitle.as_deref()
}
pub fn get_slug(&self) -> Option<&str> {
self.slug.as_deref()
}
pub fn get_date(&self) -> &Option<DateTime<Utc>> {
&self.date
}
pub fn get_iso8601_datetime(&self) -> Option<String> {
self.get_date().as_ref().map(|date| date.to_rfc3339())
}
pub fn get_rfc2822_datetime(&self) -> Option<String> {
self.get_date().as_ref().map(|date| date.to_rfc2822())
}
pub fn get_friendly_date(&self) -> Option<String> {
self.get_date()
.as_ref()
.map(|date| date.format("%v").to_string())
}
}
impl Attachment {
pub fn get_path(&self) -> &Path {
&self.path
}
}