diff --git a/Cargo.lock b/Cargo.lock index e859d3d9..bf9d8601 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ [root] name = "cobalt-bin" -version = "0.6.1" +version = "0.7.1" dependencies = [ "assert_cli 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/src/cobalt.rs b/src/cobalt.rs index f9b7a352..8e45841e 100644 --- a/src/cobalt.rs +++ b/src/cobalt.rs @@ -176,12 +176,15 @@ pub fn build(config: &Config) -> Result<()> { .collect(); trace!("Generating other documents"); + let timestamp = Value::Str(UTC::now().timestamp().to_string()); for mut doc in documents { trace!("Generating {}", doc.path); if config.dump.contains(&Dump::Liquid) { create_liquid_dump(dest, &doc.path, &doc.content, &doc.attributes)?; } + doc.attributes + .insert("timestamp".to_owned(), timestamp.clone()); let mut context = doc.get_render_context(&posts_data); let doc_html = try!(doc.render(&mut context, source, &layouts, &mut layouts_cache)); diff --git a/tests/fixtures/timestamp/_layouts/default.liquid b/tests/fixtures/timestamp/_layouts/default.liquid new file mode 100644 index 00000000..b522876e --- /dev/null +++ b/tests/fixtures/timestamp/_layouts/default.liquid @@ -0,0 +1,13 @@ + + + + test + + + +

{{ path }}

+ + {{ content }} + + + diff --git a/tests/fixtures/timestamp/_layouts/posts.liquid b/tests/fixtures/timestamp/_layouts/posts.liquid new file mode 100644 index 00000000..46d21c6d --- /dev/null +++ b/tests/fixtures/timestamp/_layouts/posts.liquid @@ -0,0 +1,10 @@ + + + + My blog - {{ title }} + + + {{ content }} + + + diff --git a/tests/fixtures/timestamp/index.liquid b/tests/fixtures/timestamp/index.liquid new file mode 100644 index 00000000..93de6018 --- /dev/null +++ b/tests/fixtures/timestamp/index.liquid @@ -0,0 +1,7 @@ +extends: default.liquid +--- +This is my Index page! + +{% for post in posts %} + {{ post.title }} +{% endfor %} diff --git a/tests/fixtures/timestamp/posts/2014-08-24-my-first-blogpost.md b/tests/fixtures/timestamp/posts/2014-08-24-my-first-blogpost.md new file mode 100644 index 00000000..982e38c4 --- /dev/null +++ b/tests/fixtures/timestamp/posts/2014-08-24-my-first-blogpost.md @@ -0,0 +1,10 @@ +extends: posts.liquid + +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/mod.rs b/tests/mod.rs index da93efed..ea95a771 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -3,6 +3,8 @@ extern crate difference; extern crate cobalt; extern crate tempdir; extern crate walkdir; +extern crate chrono; +extern crate regex; use std::path::Path; use std::fs::{self, File}; @@ -11,6 +13,8 @@ use tempdir::TempDir; use walkdir::WalkDir; use std::error::Error; use cobalt::Config; +use chrono::UTC; +use regex::Regex; fn run_test(name: &str) -> Result<(), cobalt::Error> { let target = format!("tests/target/{}/", name); @@ -226,3 +230,32 @@ pub fn empty_frontmatter() { pub fn querystrings() { run_test("querystrings").expect("Build error"); } + +#[test] +pub fn timestamp() { + // timestamp adds current ms time so need to build the target folder at run time + let mut config = Config::from_file("tests/fixtures/timestamp/.cobalt.yml").unwrap_or_default(); + config.source = "tests/fixtures/timestamp/".to_string(); + config.dest = "tests/target/timestamp/".to_string(); + + fs::create_dir_all(&config.dest).is_ok(); + + let timestamp = UTC::now().timestamp().to_string(); + let result = cobalt::build(&config); + + if result.is_ok() { + // check timestamp is in the target layout + let mut content = String::new(); + let _file = File::open("tests/target/timestamp/index.html") + .unwrap() + .read_to_string(&mut content); + let re = Regex::new(×tamp.as_str()).unwrap(); + assert!(re.is_match(&content.as_str())); + + // run standard test to make sure it doesn't fall over + run_test("timestamp").expect("Build error"); + + // delete file with timestamp otherwise next test build will fail + let _deleted = fs::remove_file("tests/target/timestamp/index.html"); + } +} \ No newline at end of file diff --git a/tests/target/timestamp/posts/2014-08-24-my-first-blogpost.html b/tests/target/timestamp/posts/2014-08-24-my-first-blogpost.html new file mode 100644 index 00000000..163d0665 --- /dev/null +++ b/tests/target/timestamp/posts/2014-08-24-my-first-blogpost.html @@ -0,0 +1,13 @@ + + + + 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..

+ + + +