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
extern crate chrono; extern crate comrak; extern crate fs_extra; #[macro_use] extern crate lazy_static; extern crate notify; extern crate regex; extern crate toml; extern crate uuid; use std::sync::mpsc::channel; use std::time::Duration; use std::{fs, path}; use fs_extra::dir; use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher}; use toml::Value; use config::Config; use entry::{parse_entry, read_entry_dir, write_entry, write_entry_listing, Entry}; mod config; mod entry; mod page; mod post; pub fn build(include_drafts: bool, cwd: &path::Path) { let config = match fs::read_to_string(cwd.join("casaubon.toml")) { Ok(contents) => match contents.parse::<Value>() { Ok(config) => Config { site_name: String::from(config["site_name"].as_str().unwrap()), }, Err(_) => panic!("Invalid casaubon.toml"), }, Err(_) => { panic!("Can't find casaubon.toml"); } }; match fs::read_dir(cwd.join("public")) { Ok(_) => { fs::remove_dir_all(cwd.join("public")).unwrap(); } Err(_) => {} } fs::create_dir(cwd.join("public")).expect("Couldn't create public directory"); fs::create_dir(cwd.join("public").join("posts")).expect("Couldn't create posts directory"); let layout_template = fs::read_to_string(&cwd.join("templates").join("layout.html")) .expect("Couldn't find layout template"); let post_template = fs::read_to_string(cwd.join("templates").join("post.html")) .expect("Couldn't find post template"); let post_listing_template = fs::read_to_string(cwd.join("templates").join("post_listing.html")) .expect("Couldn't find post listing item template"); let post_item_template = fs::read_to_string(cwd.join("templates").join("post_listing_item.html")) .expect("Couldn't find post listing item template"); let page_template = fs::read_to_string(cwd.join("templates").join("page.html")) .expect("Couldn't find page template"); let post_paths = match include_drafts { true => { let mut posts = read_entry_dir(&cwd.join("posts")); posts.append(&mut read_entry_dir(&cwd.join("drafts"))); posts } false => read_entry_dir(&cwd.join("posts")), }; let page_paths = read_entry_dir(&cwd.join("pages")); let mut posts: Vec<Entry> = post_paths .into_iter() .map(|entry| { let path = entry.path(); let contents = fs::read_to_string(&path).expect("Couldn't read post file"); parse_entry(&contents, &path) }) .collect::<Vec<Entry>>(); posts.sort_by(|a, b| b.date.cmp(&a.date)); for post in &posts { write_entry(&cwd, &layout_template, &post_template, &post, &config); } for entry in page_paths.into_iter() { let path = entry.path(); let contents = fs::read_to_string(&path).expect("Couldn't read page file"); let page = parse_entry(&contents, &path); write_entry(&cwd, &layout_template, &page_template, &page, &config); } write_entry_listing( &cwd, &layout_template, &post_listing_template, &post_item_template, &posts, &config, ); fs_extra::copy_items( &vec![cwd.join("css"), cwd.join("js")], cwd.join("public"), &dir::CopyOptions::new(), ) .expect("Couldn't copy css/js directories"); } pub fn new(name: &str, cwd: &path::Path) { let project_path = cwd.join(name); fs::create_dir(&project_path).expect(&format!("Couldn't create directory '{}'", &name)); fs::write( project_path.join("casaubon.toml"), format!("site_name = \"{}\"", &name), ) .expect("Could not create casaubon.toml"); for dir in &[ "drafts", "posts", "pages", "public", "templates", "css", "js", ] { fs::create_dir(&project_path.join(&dir)) .expect(&format!("Couldn't create {} directory", &dir)); } fs::write(project_path.join("css").join("style.css"), "") .expect("Couldn't create css/style.css"); fs::write(project_path.join("js").join("index.js"), "").expect("Couldn't create js/index.js"); let default_layout_template = format!( "<html> <head> <title>{}</title> </head> <body> <h1>{}</h1> {{{{ contents }}}} </body> </html>\n", name, name ); let default_post_listing_template = format!( "<div> <h3>Posts</h3> <ul>{{{{ post_listing }}}}</ul> </div>\n" ); let default_post_template = format!( "<article> <h1>{{{{ title }}}}</h1> <div>{{{{ body }}}}</div> </article>\n" ); let default_page_template = format!( "<article> <h1>{{{{ title }}}}</h1> <div>{{{{ body }}}}</div> </article>\n" ); let default_post_listing_item_template = format!( "<li> {{ date }} <a href=\"/posts/{{{{ slug }}}}/\">{{{{ title }}}}</a> </li>\n" ); for (filename, contents) in &[ ("layout", &default_layout_template), ("post_listing", &default_post_listing_template), ("post", &default_post_template), ("page", &default_page_template), ("post_listing_item", &default_post_listing_item_template), ] { fs::write( &project_path .join("templates") .join(format!("{}.html", filename)), &contents, ) .expect(&format!("Couldn't write templates/{}.html", filename)); } } fn should_rebuild(cwd: &path::Path, path: &path::PathBuf) -> bool { let path_string = path.to_str().unwrap().to_string(); let change_is_from_public = path_string.contains(cwd.join("public").to_str().unwrap()); let change_is_from_git = path_string.contains(cwd.join(".git").to_str().unwrap()); !change_is_from_public && !change_is_from_git } pub fn watch(include_drafts: bool, cwd: &path::Path) -> notify::Result<()> { let (tx, rx) = channel(); let mut watcher: RecommendedWatcher = try!(Watcher::new(tx, Duration::from_secs(2))); try!(watcher.watch(&cwd, RecursiveMode::Recursive)); println!("Watching {}", cwd.to_str().unwrap()); let handle_event = |path: &path::PathBuf| { if should_rebuild(&cwd, &path) { println!("Rebuilding"); build(include_drafts, &cwd); } }; loop { match rx.recv() { Ok(e) => match e { DebouncedEvent::Create(path) => { handle_event(&path); } DebouncedEvent::Write(path) => { handle_event(&path); } _ => {} }, Err(e) => println!("watch error: {:?}", e), } } } #[cfg(test)] mod tests { #[allow(unused_imports)] use super::*; #[allow(unused_imports)] use uuid::Uuid; use std::env; #[test] fn test_should_rebuild() { let cwd = env::current_dir().unwrap(); assert_eq!( false, should_rebuild(&cwd, &cwd.join("public").join("index.html")) ); assert_eq!( false, should_rebuild(&cwd, &cwd.join(".git").join("index.html")) ); assert_eq!( true, should_rebuild(&cwd, &cwd.join("posts").join("test.md")) ); assert_eq!( true, should_rebuild(&cwd, &cwd.join("drafts").join("test.md")) ); assert_eq!( true, should_rebuild(&cwd, &cwd.join("css").join("style.css")) ); assert_eq!(true, should_rebuild(&cwd, &cwd.join("js").join("index.js"))); } }