From 55aec9df49150178cf544bd745fea4bf59b33c3b Mon Sep 17 00:00:00 2001 From: Ethan Frei Date: Fri, 4 Dec 2015 19:37:25 +0000 Subject: [PATCH] Adding a config yaml file --- Cargo.toml | 1 + src/main.rs | 105 +++++++++++++++++- .../config_example/_my_layouts/default.tpl | 12 ++ .../config_example/_my_layouts/posts.tpl | 10 ++ .../_my_posts/2014-08-24-my-first-blogpost.md | 10 ++ tests/fixtures/config_example/config.yml | 5 + tests/fixtures/config_example/index.tpl | 7 ++ tests/mod.rs | 1 + .../2014-08-24-my-first-blogpost.html | 15 +++ tests/target/config_example/config.yml | 5 + tests/target/config_example/index.html | 18 +++ 11 files changed, 183 insertions(+), 6 deletions(-) create mode 100644 tests/fixtures/config_example/_my_layouts/default.tpl create mode 100644 tests/fixtures/config_example/_my_layouts/posts.tpl create mode 100644 tests/fixtures/config_example/_my_posts/2014-08-24-my-first-blogpost.md create mode 100644 tests/fixtures/config_example/config.yml create mode 100644 tests/fixtures/config_example/index.tpl create mode 100644 tests/target/config_example/_my_posts/2014-08-24-my-first-blogpost.html create mode 100644 tests/target/config_example/config.yml create mode 100644 tests/target/config_example/index.html diff --git a/Cargo.toml b/Cargo.toml index fdf97ac..4ff16f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ markdown = "0.1" liquid = "0.2" walkdir = "0.1" crossbeam = "0.1.5" +yaml-rust = "0.2.2" [dev-dependencies] difference = "0.4" diff --git a/src/main.rs b/src/main.rs index 8553f12..2db1ecf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,10 +2,16 @@ extern crate cobalt; extern crate getopts; +extern crate yaml_rust; -use getopts::Options; +use getopts::{ Matches, Options }; use std::env; -use std::path::PathBuf; +use std::fs::File; +use std::io::Result as IoResult; +use std::io::prelude::*; +use std::path::{ Path, PathBuf }; +use yaml_rust::Yaml; +use yaml_rust::scanner::ScanError; fn print_version() { // TODO parse this from Cargo.toml @@ -48,11 +54,29 @@ fn main() { return; } + // Fetch config information if available + let config_contents_result = get_config_contents("./config.yml"); + let yaml = if let Ok(config_contents) = config_contents_result { + match parse_yaml(config_contents) { + Ok(y) => { + y + } + Err(e) => { + // Trouble parsing yaml file + panic!(e.to_string()) + } + } + } else { + // No config file or error reading it. + Yaml::from_str("") + }; + + // join("") makes sure path has a trailing slash - let source = PathBuf::from(&matches.opt_str("s").unwrap_or("./".to_string())).join(""); - let dest = PathBuf::from(&matches.opt_str("d").unwrap_or("./".to_string())).join(""); - let layouts = matches.opt_str("layouts").unwrap_or("_layouts".to_string()); - let posts = matches.opt_str("posts").unwrap_or("_posts".to_string()); + let source = PathBuf::from(&get_setting("s", "source", "./", &matches, &yaml)).join(""); + let dest = PathBuf::from(&get_setting("d", "dest", "./", &matches, &yaml)).join(""); + let layouts = get_setting("layouts", "layouts", "_layouts", &matches, &yaml); + let posts = get_setting("posts", "posts", "_posts", &matches, &yaml); let command = if !matches.free.is_empty() { matches.free[0].clone() @@ -76,3 +100,72 @@ fn main() { } } } + +fn get_setting(arg_str: &str, config_str: &str, default: &str, matches: &Matches, yaml: &Yaml) -> String { + if let Some(arg_val) = matches.opt_str(arg_str) { + arg_val + } else if let Some(config_val) = yaml[config_str].as_str() { + config_val.to_string() + } else { + default.to_string() + } +} + +fn get_config_contents>(config_file: P) -> IoResult { + let mut buffer = String::new(); + let mut f = try!(File::open(config_file)); + try!(f.read_to_string(&mut buffer)); + Ok(buffer) +} + +fn parse_yaml(file_contents: String) -> Result { + use yaml_rust::YamlLoader; + + let doc_list = try!(YamlLoader::load_from_str(&file_contents)); + + // Cannot return parsed document directly as list goes out of scope + // Cloning as of now + Ok(doc_list[0].clone()) +} + + +// Private method tests + +#[test] +fn get_config_contents_ok() { + let result = get_config_contents("tests/fixtures/config_example/config.yml"); + assert!(result.is_ok()); + assert!(result.unwrap().len() != 0); +} + +#[test] +fn get_config_contents_err() { + let result = get_config_contents("tests/fixtures/config_example/config_does_not_exist.yml"); + assert!(result.is_err()); +} + +#[test] +fn parse_yaml_ok() { + let source = "test_source"; + let dest = "test_dest"; + let posts = "test_posts"; + let layouts = "test_layouts"; + + let file_contents = format!("source: {}\r\ndest: {}\r\nposts: {}\r\nlayouts: {}\r", + source, + dest, + posts, + layouts); + let result = parse_yaml(file_contents); + assert!(result.is_ok()); + let doc = result.unwrap(); + assert_eq!(doc["source"].as_str(), Some(source)); +} + +#[test] +fn parse_yaml_err() { + let file_contents = "!@%!\\@#%!\r\n#ASDF@#%".to_string(); + let result = parse_yaml(file_contents); + assert!(result.is_err()); +} + diff --git a/tests/fixtures/config_example/_my_layouts/default.tpl b/tests/fixtures/config_example/_my_layouts/default.tpl new file mode 100644 index 0000000..3a75097 --- /dev/null +++ b/tests/fixtures/config_example/_my_layouts/default.tpl @@ -0,0 +1,12 @@ + + + + test + + +

{{ name }}

+ + {{ content }} + + + diff --git a/tests/fixtures/config_example/_my_layouts/posts.tpl b/tests/fixtures/config_example/_my_layouts/posts.tpl new file mode 100644 index 0000000..46d21c6 --- /dev/null +++ b/tests/fixtures/config_example/_my_layouts/posts.tpl @@ -0,0 +1,10 @@ + + + + My blog - {{ title }} + + + {{ content }} + + + diff --git a/tests/fixtures/config_example/_my_posts/2014-08-24-my-first-blogpost.md b/tests/fixtures/config_example/_my_posts/2014-08-24-my-first-blogpost.md new file mode 100644 index 0000000..69313c3 --- /dev/null +++ b/tests/fixtures/config_example/_my_posts/2014-08-24-my-first-blogpost.md @@ -0,0 +1,10 @@ +@extends: posts.tpl + +title: My first Blogpost +date: 24/08/2014 at 15:36 +--- +# {{ title }} + +Hey there this is my first blogpost and this is super awesome. + +My Blog is lorem ipsum like, yes it is.. diff --git a/tests/fixtures/config_example/config.yml b/tests/fixtures/config_example/config.yml new file mode 100644 index 0000000..be8807e --- /dev/null +++ b/tests/fixtures/config_example/config.yml @@ -0,0 +1,5 @@ +dest: ../../target/config_example + +layouts: _my_layouts + +posts: _my_posts diff --git a/tests/fixtures/config_example/index.tpl b/tests/fixtures/config_example/index.tpl new file mode 100644 index 0000000..bfe4342 --- /dev/null +++ b/tests/fixtures/config_example/index.tpl @@ -0,0 +1,7 @@ +@extends: default.tpl +--- +This is my Index page! + +{% for post in posts %} + {{ post.title }} +{% endfor %} diff --git a/tests/mod.rs b/tests/mod.rs index 7c5df11..557d978 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -64,3 +64,4 @@ pub fn no_extends_error() { assert!(err.is_err()); assert_eq!(err.unwrap_err().description(), "No @extends line creating _posts/2014-08-24-my-first-blogpost.md"); } + diff --git a/tests/target/config_example/_my_posts/2014-08-24-my-first-blogpost.html b/tests/target/config_example/_my_posts/2014-08-24-my-first-blogpost.html new file mode 100644 index 0000000..93ce4a5 --- /dev/null +++ b/tests/target/config_example/_my_posts/2014-08-24-my-first-blogpost.html @@ -0,0 +1,15 @@ + + + + My blog - My first Blogpost + + +

My first Blogpost

+ +

Hey there this is my first blogpost and this is super awesome.

+ +

My Blog is lorem ipsum like, yes it is..

+ + + + diff --git a/tests/target/config_example/config.yml b/tests/target/config_example/config.yml new file mode 100644 index 0000000..be8807e --- /dev/null +++ b/tests/target/config_example/config.yml @@ -0,0 +1,5 @@ +dest: ../../target/config_example + +layouts: _my_layouts + +posts: _my_posts diff --git a/tests/target/config_example/index.html b/tests/target/config_example/index.html new file mode 100644 index 0000000..5fa9653 --- /dev/null +++ b/tests/target/config_example/index.html @@ -0,0 +1,18 @@ + + + + test + + +

index

+ + +This is my Index page! + + + My first Blogpost + + + + +