123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- extern crate clap;
- extern crate comrak;
- #[macro_use]
- extern crate lazy_static;
- extern crate regex;
- extern crate uuid;
-
- use std::env;
- use std::fs;
- use std::path;
-
- use clap::{App, Arg};
-
- use post::parse_post;
- use write::{write_post, write_post_listing};
-
- mod post;
- mod render;
- mod write;
-
- fn read_posts_dir(cwd: &path::PathBuf) -> fs::ReadDir {
- match fs::read_dir(cwd) {
- Ok(posts) => posts,
- Err(err) => panic!(err),
- }
- }
-
- fn build() {
- let cwd = env::current_dir().expect("Couldn't read current directory");
-
- match fs::create_dir(cwd.join("public")) {
- Ok(_) => {}
- Err(err) => match err.kind() {
- std::io::ErrorKind::AlreadyExists => {}
- _ => panic!(err),
- },
- }
-
- 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 post_paths = read_posts_dir(&cwd.join("posts"));
- let posts = post_paths
- .into_iter()
- .map(|path| match path {
- Ok(p) => {
- let post = parse_post(p.path());
- write_post(&cwd, &layout_template, &post_template, &post);
- post
- }
- Err(err) => panic!(err),
- }).collect();
-
- write_post_listing(
- &cwd,
- &layout_template,
- &post_listing_template,
- &post_item_template,
- &posts,
- );
- }
-
- fn new(name: &str) {
- let cwd = env::current_dir().expect("Couldn't read current directory");
- let project_path = cwd.join(name);
-
- fs::create_dir(&project_path).expect(&format!("Couldn't create directory '{}'", &name));
-
- for dir in &["posts", "public", "templates"] {
- fs::create_dir(&project_path.join(&dir))
- .expect(&format!("Couldn't create {} directory", &dir));
- }
-
- for file in &["layout", "post_listing", "post", "post_listing_item"] {
- fs::write(
- &project_path
- .join("templates")
- .join(format!("{}.html", file)),
- "",
- ).expect(&format!("Couldn't write templates/{}.html", file));
- }
- }
-
- fn main() {
- let matches = App::new("tlon")
- .version("0.1.0")
- .author("Dylan Baker")
- .about("Highly opinionated static site generator")
- .arg(
- Arg::with_name("command")
- .required(true)
- .possible_values(&["build", "new"])
- .index(1),
- ).arg(
- Arg::with_name("name")
- .required_if("command", "new")
- .index(2),
- ).get_matches();
-
- let command = matches.value_of("command").unwrap();
- if command == "build" {
- build();
- } else if command == "new" {
- new(&matches.value_of("name").unwrap());
- }
- }
|