A static site generator written in Rust
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

entry.rs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. use chrono::NaiveDate;
  2. use pulldown_cmark::{html, Parser};
  3. use regex::Regex;
  4. use std::fs;
  5. use std::path;
  6. use config::Config;
  7. #[derive(Debug, PartialEq)]
  8. pub enum EntryKind {
  9. Page,
  10. Post,
  11. }
  12. #[derive(Debug)]
  13. pub struct Entry {
  14. pub kind: EntryKind,
  15. pub title: String,
  16. pub slug: String,
  17. pub body: String,
  18. pub date: Option<NaiveDate>,
  19. }
  20. impl Entry {
  21. fn render(&self, template: &str, config: &Config) -> String {
  22. let parser = Parser::new(&self.body);
  23. let mut html_buf = String::new();
  24. html::push_html(&mut html_buf, parser);
  25. let formatted_date = match self.date {
  26. Some(date) => date.format("%Y-%m-%d").to_string(),
  27. None => String::from(""),
  28. };
  29. template
  30. .replace(
  31. "{{ page_title }}",
  32. &format!("{} | {}", &self.title, &config.site_name),
  33. )
  34. .replace("{{ title }}", &self.title)
  35. .replace("{{ slug }}", &self.slug)
  36. .replace("{{ body }}", &html_buf.to_string())
  37. .replace("{{ date }}", &formatted_date)
  38. }
  39. }
  40. pub fn read_entry_dir(cwd: &path::Path) -> Vec<fs::DirEntry> {
  41. match fs::read_dir(cwd) {
  42. Ok(entries) => entries.into_iter().map(|entry| entry.unwrap()).collect(),
  43. Err(err) => panic!(err),
  44. }
  45. }
  46. pub fn parse_entry(contents: &str, path: &path::Path) -> Entry {
  47. lazy_static! {
  48. static ref re_with_date: Regex =
  49. Regex::new(r"^# (?P<title>.*) \| (?P<date>\d{4}-\d{2}-\d{2})\n\n(?s)(?P<body>.*)")
  50. .unwrap();
  51. static ref re_without_date: Regex =
  52. Regex::new(r"^# (?P<title>.*)\n\n(?s)(?P<body>.*)").unwrap();
  53. static ref slug_re: Regex = Regex::new(r"(?P<slug>\S+).md").unwrap();
  54. }
  55. let filename = &path.file_name().unwrap().to_str().unwrap();
  56. let slug = &slug_re
  57. .captures(filename)
  58. .expect("Couldn't parse slug from filename")["slug"];
  59. if let Some(date_string) = &re_with_date.captures(&contents) {
  60. let title = &re_with_date
  61. .captures(&contents)
  62. .expect("Couldn't parse title")["title"];
  63. let body = &re_with_date
  64. .captures(&contents)
  65. .expect("Couldn't parse title")["body"];
  66. let date = Some(
  67. NaiveDate::parse_from_str(&date_string["date"], "%Y-%m-%d")
  68. .expect("Couldn't parse date"),
  69. );
  70. Entry {
  71. kind: EntryKind::Post,
  72. title: String::from(title),
  73. body: String::from(body),
  74. slug: String::from(slug),
  75. date: date,
  76. }
  77. } else {
  78. let title = &re_without_date
  79. .captures(&contents)
  80. .expect("Couldn't parse title")["title"];
  81. let body = &re_without_date
  82. .captures(&contents)
  83. .expect("Couldn't parse title")["body"];
  84. Entry {
  85. kind: EntryKind::Page,
  86. title: String::from(title),
  87. body: String::from(body),
  88. slug: String::from(slug),
  89. date: None,
  90. }
  91. }
  92. }
  93. pub fn write_entry(
  94. cwd: &path::Path,
  95. layout: &str,
  96. post_template: &str,
  97. entry: &Entry,
  98. config: &Config,
  99. ) {
  100. let root_path = match entry.kind {
  101. EntryKind::Post => cwd.join("public").join("posts").join(&entry.slug),
  102. EntryKind::Page => cwd.join("public").join(&entry.slug),
  103. };
  104. match fs::create_dir(&root_path) {
  105. Ok(_) => {}
  106. Err(err) => match err.kind() {
  107. std::io::ErrorKind::AlreadyExists => {}
  108. _ => panic!(err),
  109. },
  110. }
  111. let template = layout.replace("{{ contents }}", &post_template);
  112. fs::write(
  113. &root_path.join("index.html"),
  114. entry.render(&template, &config),
  115. )
  116. .expect("Unable to write file");
  117. }
  118. pub fn write_entry_listing(
  119. cwd: &path::Path,
  120. layout: &str,
  121. post_listing_template: &str,
  122. post_item_template: &str,
  123. posts: &Vec<Entry>,
  124. config: &Config,
  125. ) {
  126. fs::write(
  127. cwd.join("public").join("index.html"),
  128. render_post_listing(
  129. layout,
  130. post_listing_template,
  131. post_item_template,
  132. posts,
  133. config,
  134. ),
  135. )
  136. .expect("Unable to write file");
  137. }
  138. pub fn render_post_listing(
  139. layout: &str,
  140. post_listing_template: &str,
  141. post_item_template: &str,
  142. posts: &Vec<Entry>,
  143. config: &Config,
  144. ) -> String {
  145. layout
  146. .replace("{{ page_title }}", &format!("{}", config.site_name))
  147. .replace(
  148. "{{ contents }}",
  149. &post_listing_template.replace(
  150. "{{ post_listing }}",
  151. &posts
  152. .iter()
  153. .map(|post| post.render(&post_item_template, &config))
  154. .collect::<Vec<String>>()
  155. .join("\n"),
  156. ),
  157. )
  158. }
  159. #[cfg(test)]
  160. mod tests {
  161. use chrono::NaiveDate;
  162. use uuid::Uuid;
  163. use std::{env, fs, path};
  164. use super::*;
  165. #[test]
  166. fn test_render_post() {
  167. let config = Config {
  168. site_name: String::from("Test Site"),
  169. };
  170. let post = Entry {
  171. title: String::from("hello world"),
  172. body: String::from("lorem ipsum dolor sit amet"),
  173. slug: String::from("hello-world"),
  174. date: Some(NaiveDate::from_ymd(2019, 1, 1)),
  175. kind: EntryKind::Post,
  176. };
  177. let output = post.render(
  178. "<html><head><title>{{ page_title }}</title></head><body><article><h1>{{ title }}</h1><div>{{ body }}</div></article></body></html>", &config
  179. )
  180. .replace("\n", "");
  181. assert_eq!(
  182. "<html><head><title>hello world | Test Site</title></head><body><article><h1>hello world</h1><div><p>lorem ipsum dolor sit amet</p></div></article></body></html>",
  183. &output,
  184. );
  185. }
  186. #[test]
  187. fn test_render_post_listing() {
  188. let config = Config {
  189. site_name: String::from("Test Site"),
  190. };
  191. let posts = vec![
  192. Entry {
  193. title: String::from("First post"),
  194. body: String::from("lorem ipsum dolor sit amet"),
  195. slug: String::from("first-post"),
  196. date: Some(NaiveDate::from_ymd(2019, 1, 1)),
  197. kind: EntryKind::Post,
  198. },
  199. Entry {
  200. title: String::from("Second post"),
  201. body: String::from("lorem ipsum dolor sit amet"),
  202. slug: String::from("second-post"),
  203. date: Some(NaiveDate::from_ymd(2019, 1, 1)),
  204. kind: EntryKind::Post,
  205. },
  206. Entry {
  207. title: String::from("Third post"),
  208. body: String::from("lorem ipsum dolor sit amet"),
  209. slug: String::from("third-post"),
  210. date: Some(NaiveDate::from_ymd(2019, 1, 1)),
  211. kind: EntryKind::Post,
  212. },
  213. ];
  214. let output = render_post_listing(
  215. "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>",
  216. "<ul>{{ post_listing }}</ul>",
  217. "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>",
  218. &posts,
  219. &config,
  220. )
  221. .replace("\n", "");
  222. assert_eq!(
  223. "<html><head><title>Test Site</title></head><body><ul><li><a href=\"/first-post\">First post</a></li><li><a href=\"/second-post\">Second post</a></li><li><a href=\"/third-post\">Third post</a></li></ul></body></html>",
  224. &output,
  225. );
  226. }
  227. #[test]
  228. fn test_write_entry() {
  229. let temp_dir = env::temp_dir();
  230. let project_dir = temp_dir.join(&Uuid::new_v4().to_string());
  231. fs::create_dir(&project_dir).unwrap();
  232. fs::create_dir(project_dir.join("public")).unwrap();
  233. fs::create_dir(project_dir.join("public").join("posts")).unwrap();
  234. let layout =
  235. "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>";
  236. let post_template = "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>";
  237. let post = Entry {
  238. title: String::from("Hello world"),
  239. body: String::from("Lorem ipsum dolor sit amet"),
  240. slug: String::from("hello-world"),
  241. date: Some(NaiveDate::from_ymd(2019, 1, 1)),
  242. kind: EntryKind::Post,
  243. };
  244. let config = Config {
  245. site_name: "Test Site".to_string(),
  246. };
  247. write_entry(&project_dir, &layout, &post_template, &post, &config);
  248. let content = fs::read_to_string(
  249. project_dir
  250. .join("public")
  251. .join("posts")
  252. .join("hello-world")
  253. .join("index.html"),
  254. )
  255. .unwrap();
  256. assert_eq!(
  257. "<html><head><title>Hello world | Test Site</title></head><body><article><h1>Hello world</h1><div><p>Lorem ipsum dolor sit amet</p></div></article></body></html>",
  258. content.replace("\n", "")
  259. );
  260. fs::remove_dir_all(temp_dir.join(&project_dir)).unwrap();
  261. }
  262. #[test]
  263. fn test_write_post_listing() {
  264. let temp_dir = env::temp_dir();
  265. let project_dir = temp_dir.join(&Uuid::new_v4().to_string());
  266. fs::create_dir(&project_dir).unwrap();
  267. env::set_current_dir(&project_dir).unwrap();
  268. let cwd = env::current_dir().unwrap();
  269. fs::create_dir(cwd.join("public")).unwrap();
  270. let layout =
  271. "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>";
  272. let post_listing_template = "<ul>{{ post_listing }}</ul>";
  273. let post_item_template = "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>";
  274. let posts = vec![
  275. Entry {
  276. title: String::from("First post"),
  277. body: String::from("lorem ipsum dolor sit amet"),
  278. slug: String::from("first-post"),
  279. date: Some(NaiveDate::from_ymd(2019, 1, 1)),
  280. kind: EntryKind::Post,
  281. },
  282. Entry {
  283. title: String::from("Second post"),
  284. body: String::from("lorem ipsum dolor sit amet"),
  285. slug: String::from("second-post"),
  286. date: Some(NaiveDate::from_ymd(2019, 1, 1)),
  287. kind: EntryKind::Post,
  288. },
  289. Entry {
  290. title: String::from("Third post"),
  291. body: String::from("lorem ipsum dolor sit amet"),
  292. slug: String::from("third-post"),
  293. date: Some(NaiveDate::from_ymd(2019, 1, 1)),
  294. kind: EntryKind::Post,
  295. },
  296. ];
  297. let config = Config {
  298. site_name: "Test Site".to_string(),
  299. };
  300. write_entry_listing(
  301. &cwd,
  302. &layout,
  303. &post_listing_template,
  304. &post_item_template,
  305. &posts,
  306. &config,
  307. );
  308. assert_eq!(
  309. "<html><head><title>Test Site</title></head><body><ul><li><a href\
  310. =\"/first-post\">First post</a></li><li><a href=\"/second-post\">\
  311. Second post</a></li><li><a href=\"/third-post\">Third post</a></li\
  312. ></ul></body></html>",
  313. fs::read_to_string(&cwd.join("public").join("index.html"))
  314. .unwrap()
  315. .replace("\n", ""),
  316. );
  317. fs::remove_dir_all(temp_dir.join(&project_dir)).unwrap();
  318. }
  319. #[test]
  320. fn test_parse_post_entry() {
  321. let post = parse_entry(
  322. "# Test Title | 2000-01-01\n\nThis is the body",
  323. path::PathBuf::from("posts/one.md").as_path(),
  324. );
  325. assert_eq!(post.kind, EntryKind::Post);
  326. assert_eq!(post.slug, "one");
  327. assert_eq!(post.title, "Test Title");
  328. assert_eq!(post.body, "This is the body");
  329. assert_eq!(post.date, Some(NaiveDate::from_ymd(2000, 1, 1)));
  330. }
  331. #[test]
  332. fn test_parse_page_entry() {
  333. let post = parse_entry(
  334. "# Test Title\n\nThis is the body",
  335. path::PathBuf::from("pages/one.md").as_path(),
  336. );
  337. assert_eq!(post.kind, EntryKind::Page);
  338. assert_eq!(post.slug, "one");
  339. assert_eq!(post.title, "Test Title");
  340. assert_eq!(post.body, "This is the body");
  341. assert_eq!(post.date, None);
  342. }
  343. #[test]
  344. fn test_display_entry_date() {
  345. let post = Entry {
  346. kind: EntryKind::Post,
  347. title: String::from("Test Title"),
  348. slug: String::from("one"),
  349. body: String::from("This is the body"),
  350. date: Some(NaiveDate::from_ymd(2000, 1, 1)),
  351. };
  352. let config = Config {
  353. site_name: String::from("Test Site"),
  354. };
  355. assert_eq!("2000-01-01", post.render("{{ date }}", &config));
  356. }
  357. }