123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- use config::Config;
- use post::Post;
- use render::{render_post, render_post_listing};
- use std::fs;
- use std::path;
-
- pub fn write_post(
- cwd: &path::PathBuf,
- layout: &str,
- post_template: &str,
- post: &Post,
- config: &Config,
- ) {
- match fs::create_dir(cwd.join("public").join(&post.slug)) {
- Ok(_) => {}
- Err(err) => match err.kind() {
- std::io::ErrorKind::AlreadyExists => {}
- _ => panic!(err),
- },
- }
- fs::write(
- cwd.join("public").join(&post.slug).join("index.html"),
- render_post(layout, post_template, post, config),
- ).expect("Unable to write file");
- }
-
- pub fn write_post_listing(
- cwd: &path::PathBuf,
- layout: &str,
- post_listing_template: &str,
- post_item_template: &str,
- posts: &Vec<Post>,
- config: &Config,
- ) {
- fs::write(
- cwd.join("public").join("index.html"),
- render_post_listing(
- layout,
- post_listing_template,
- post_item_template,
- posts,
- config,
- ),
- ).expect("Unable to write file");
- }
-
- #[cfg(test)]
- mod tests {
- #[allow(unused_imports)]
- use super::*;
- #[allow(unused_imports)]
- use std::{env, fs};
- #[allow(unused_imports)]
- use uuid::Uuid;
-
- #[test]
- fn test_write_post() {
- let temp_dir = env::temp_dir();
- let working_dir = temp_dir.join(&Uuid::new_v4().to_string());
- fs::create_dir(&working_dir).unwrap();
- env::set_current_dir(&working_dir).unwrap();
-
- let cwd = env::current_dir().unwrap();
- fs::create_dir(cwd.join("public")).unwrap();
-
- let layout =
- "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>";
- let post_template = "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>";
- let post = Post {
- title: String::from("Hello world"),
- body: String::from("Lorem ipsum dolor sit amet"),
- slug: String::from("hello-world"),
- };
- let config = Config {
- site_name: "Test Site".to_string(),
- };
-
- write_post(&cwd, &layout, &post_template, &post, &config);
-
- let content =
- fs::read_to_string(cwd.join("public").join("hello-world").join("index.html")).unwrap();
-
- assert_eq!(
- "<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>",
- content.replace("\n", "")
- );
-
- fs::remove_dir_all(temp_dir.join(&working_dir)).unwrap();
- }
-
- #[test]
- fn test_write_post_listing() {
- let temp_dir = env::temp_dir();
- let working_dir = temp_dir.join(&Uuid::new_v4().to_string());
- fs::create_dir(&working_dir).unwrap();
- env::set_current_dir(&working_dir).unwrap();
-
- let cwd = env::current_dir().unwrap();
- fs::create_dir(cwd.join("public")).unwrap();
-
- let layout =
- "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>";
- let post_listing_template = "<ul>{{ post_listing }}</ul>";
- let post_item_template = "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>";
- let posts = vec![
- Post {
- title: String::from("First post"),
- body: String::from("lorem ipsum dolor sit amet"),
- slug: String::from("first-post"),
- },
- Post {
- title: String::from("Second post"),
- body: String::from("lorem ipsum dolor sit amet"),
- slug: String::from("second-post"),
- },
- Post {
- title: String::from("Third post"),
- body: String::from("lorem ipsum dolor sit amet"),
- slug: String::from("third-post"),
- },
- ];
- let config = Config {
- site_name: "Test Site".to_string(),
- };
- write_post_listing(
- &cwd,
- &layout,
- &post_listing_template,
- &post_item_template,
- &posts,
- &config,
- );
-
- assert_eq!(
- "<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>",
- fs::read_to_string(&cwd.join("public").join("index.html"))
- .unwrap()
- .replace("\n", ""),
- );
-
- fs::remove_dir_all(temp_dir.join(&working_dir)).unwrap();
- }
- }
|