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 12KB

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(kind: EntryKind, 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. .to_string();
  60. if kind == EntryKind::Post {
  61. let captures = &RE_WITH_DATE
  62. .captures(&contents)
  63. .expect("Couldn't parse post");
  64. let title = captures["title"].to_string();
  65. let body = captures["body"].to_string();
  66. let date = Some(
  67. NaiveDate::parse_from_str(&captures["date"], "%Y-%m-%d").expect("Couldn't parse date"),
  68. );
  69. Entry {
  70. kind,
  71. title,
  72. body,
  73. slug,
  74. date,
  75. }
  76. } else {
  77. let captures = &RE_WITHOUT_DATE
  78. .captures(&contents)
  79. .expect("Couldn't parse page");
  80. let title = captures["title"].to_string();
  81. let body = captures["body"].to_string();
  82. Entry {
  83. kind,
  84. title,
  85. body,
  86. slug,
  87. date: None,
  88. }
  89. }
  90. }
  91. pub fn write_entry(
  92. cwd: &path::Path,
  93. layout: &str,
  94. post_template: &str,
  95. entry: &Entry,
  96. config: &Config,
  97. ) {
  98. let root_path = match entry.kind {
  99. EntryKind::Post => cwd.join("public").join("posts").join(&entry.slug),
  100. EntryKind::Page => cwd.join("public").join(&entry.slug),
  101. };
  102. match fs::create_dir(&root_path) {
  103. Ok(_) => {}
  104. Err(err) => match err.kind() {
  105. std::io::ErrorKind::AlreadyExists => {}
  106. _ => panic!(err),
  107. },
  108. }
  109. let template = layout.replace("{{ contents }}", &post_template);
  110. fs::write(
  111. &root_path.join("index.html"),
  112. entry.render(&template, &config),
  113. )
  114. .expect("Unable to write file");
  115. }
  116. pub fn write_entry_listing(
  117. cwd: &path::Path,
  118. layout: &str,
  119. post_listing_template: &str,
  120. post_item_template: &str,
  121. posts: &Vec<Entry>,
  122. config: &Config,
  123. ) {
  124. fs::write(
  125. cwd.join("public").join("index.html"),
  126. render_post_listing(
  127. layout,
  128. post_listing_template,
  129. post_item_template,
  130. posts,
  131. config,
  132. ),
  133. )
  134. .expect("Unable to write file");
  135. }
  136. pub fn render_post_listing(
  137. layout: &str,
  138. post_listing_template: &str,
  139. post_item_template: &str,
  140. posts: &Vec<Entry>,
  141. config: &Config,
  142. ) -> String {
  143. layout
  144. .replace("{{ page_title }}", &format!("{}", config.site_name))
  145. .replace(
  146. "{{ contents }}",
  147. &post_listing_template.replace(
  148. "{{ post_listing }}",
  149. &posts
  150. .iter()
  151. .map(|post| post.render(&post_item_template, &config))
  152. .collect::<Vec<String>>()
  153. .join("\n"),
  154. ),
  155. )
  156. }
  157. #[cfg(test)]
  158. mod tests {
  159. use chrono::NaiveDate;
  160. use uuid::Uuid;
  161. use std::{env, fs, path};
  162. use super::*;
  163. #[test]
  164. fn test_render_post() {
  165. let config = Config {
  166. site_name: String::from("Test Site"),
  167. };
  168. let post = Entry {
  169. title: String::from("hello world"),
  170. body: String::from("lorem ipsum dolor sit amet"),
  171. slug: String::from("hello-world"),
  172. date: Some(NaiveDate::from_ymd(2019, 1, 1)),
  173. kind: EntryKind::Post,
  174. };
  175. let output = post.render(
  176. "<html><head><title>{{ page_title }}</title></head><body><article><h1>{{ title }}</h1><div>{{ body }}</div></article></body></html>", &config
  177. )
  178. .replace("\n", "");
  179. assert_eq!(
  180. "<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>",
  181. &output,
  182. );
  183. }
  184. #[test]
  185. fn test_render_post_listing() {
  186. let config = Config {
  187. site_name: String::from("Test Site"),
  188. };
  189. let posts = vec![
  190. Entry {
  191. title: String::from("First post"),
  192. body: String::from("lorem ipsum dolor sit amet"),
  193. slug: String::from("first-post"),
  194. date: Some(NaiveDate::from_ymd(2019, 1, 1)),
  195. kind: EntryKind::Post,
  196. },
  197. Entry {
  198. title: String::from("Second post"),
  199. body: String::from("lorem ipsum dolor sit amet"),
  200. slug: String::from("second-post"),
  201. date: Some(NaiveDate::from_ymd(2019, 1, 1)),
  202. kind: EntryKind::Post,
  203. },
  204. Entry {
  205. title: String::from("Third post"),
  206. body: String::from("lorem ipsum dolor sit amet"),
  207. slug: String::from("third-post"),
  208. date: Some(NaiveDate::from_ymd(2019, 1, 1)),
  209. kind: EntryKind::Post,
  210. },
  211. ];
  212. let output = render_post_listing(
  213. "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>",
  214. "<ul>{{ post_listing }}</ul>",
  215. "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>",
  216. &posts,
  217. &config,
  218. )
  219. .replace("\n", "");
  220. assert_eq!(
  221. "<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>",
  222. &output,
  223. );
  224. }
  225. #[test]
  226. fn test_write_entry() {
  227. let temp_dir = env::temp_dir();
  228. let project_dir = temp_dir.join(&Uuid::new_v4().to_string());
  229. fs::create_dir(&project_dir).unwrap();
  230. fs::create_dir(project_dir.join("public")).unwrap();
  231. fs::create_dir(project_dir.join("public").join("posts")).unwrap();
  232. let layout =
  233. "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>";
  234. let post_template = "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>";
  235. let post = Entry {
  236. title: String::from("Hello world"),
  237. body: String::from("Lorem ipsum dolor sit amet"),
  238. slug: String::from("hello-world"),
  239. date: Some(NaiveDate::from_ymd(2019, 1, 1)),
  240. kind: EntryKind::Post,
  241. };
  242. let config = Config {
  243. site_name: "Test Site".to_string(),
  244. };
  245. write_entry(&project_dir, &layout, &post_template, &post, &config);
  246. let content = fs::read_to_string(
  247. project_dir
  248. .join("public")
  249. .join("posts")
  250. .join("hello-world")
  251. .join("index.html"),
  252. )
  253. .unwrap();
  254. assert_eq!(
  255. "<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>",
  256. content.replace("\n", "")
  257. );
  258. fs::remove_dir_all(temp_dir.join(&project_dir)).unwrap();
  259. }
  260. #[test]
  261. fn test_write_post_listing() {
  262. let temp_dir = env::temp_dir();
  263. let project_dir = temp_dir.join(&Uuid::new_v4().to_string());
  264. fs::create_dir(&project_dir).unwrap();
  265. env::set_current_dir(&project_dir).unwrap();
  266. let cwd = env::current_dir().unwrap();
  267. fs::create_dir(cwd.join("public")).unwrap();
  268. let layout =
  269. "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>";
  270. let post_listing_template = "<ul>{{ post_listing }}</ul>";
  271. let post_item_template = "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>";
  272. let posts = vec![
  273. Entry {
  274. title: String::from("First post"),
  275. body: String::from("lorem ipsum dolor sit amet"),
  276. slug: String::from("first-post"),
  277. date: Some(NaiveDate::from_ymd(2019, 1, 1)),
  278. kind: EntryKind::Post,
  279. },
  280. Entry {
  281. title: String::from("Second post"),
  282. body: String::from("lorem ipsum dolor sit amet"),
  283. slug: String::from("second-post"),
  284. date: Some(NaiveDate::from_ymd(2019, 1, 1)),
  285. kind: EntryKind::Post,
  286. },
  287. Entry {
  288. title: String::from("Third post"),
  289. body: String::from("lorem ipsum dolor sit amet"),
  290. slug: String::from("third-post"),
  291. date: Some(NaiveDate::from_ymd(2019, 1, 1)),
  292. kind: EntryKind::Post,
  293. },
  294. ];
  295. let config = Config {
  296. site_name: "Test Site".to_string(),
  297. };
  298. write_entry_listing(
  299. &cwd,
  300. &layout,
  301. &post_listing_template,
  302. &post_item_template,
  303. &posts,
  304. &config,
  305. );
  306. assert_eq!(
  307. "<html><head><title>Test Site</title></head><body><ul><li><a href\
  308. =\"/first-post\">First post</a></li><li><a href=\"/second-post\">\
  309. Second post</a></li><li><a href=\"/third-post\">Third post</a></li\
  310. ></ul></body></html>",
  311. fs::read_to_string(&cwd.join("public").join("index.html"))
  312. .unwrap()
  313. .replace("\n", ""),
  314. );
  315. fs::remove_dir_all(temp_dir.join(&project_dir)).unwrap();
  316. }
  317. #[test]
  318. fn test_parse_post_entry() {
  319. let post = parse_entry(
  320. EntryKind::Post,
  321. "# Test Title | 2000-01-01\n\nThis is the body",
  322. path::PathBuf::from("posts/one.md").as_path(),
  323. );
  324. assert_eq!(post.kind, EntryKind::Post);
  325. assert_eq!(post.slug, "one");
  326. assert_eq!(post.title, "Test Title");
  327. assert_eq!(post.body, "This is the body");
  328. assert_eq!(post.date, Some(NaiveDate::from_ymd(2000, 1, 1)));
  329. }
  330. #[test]
  331. fn test_parse_page_entry() {
  332. let post = parse_entry(
  333. EntryKind::Page,
  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. }