Skip to content

Commit

Permalink
handlebars rendering complete
Browse files Browse the repository at this point in the history
  • Loading branch information
qmx committed Jan 22, 2016
1 parent fba2482 commit 4d055dc
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 60 deletions.
2 changes: 2 additions & 0 deletions fixtures/canonical-output/archive/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<li>merry xmas, doofus</li>
3 changes: 3 additions & 0 deletions fixtures/canonical-output/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-color: #fcfcfc;
}
13 changes: 13 additions & 0 deletions fixtures/canonical-output/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>

Hello World

</body>
</html>
17 changes: 17 additions & 0 deletions fixtures/canonical-output/merry-xmas/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>

<article>
<h2>merry xmas, doofus</h2>
<h1>ooh</h1>

</article>

</body>
</html>
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extern crate crypto;
extern crate handlebars;
extern crate serde;
extern crate serde_json;
extern crate walkdir;

mod document;
pub mod site;
Expand Down
138 changes: 79 additions & 59 deletions src/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use std::fs::{self, File};
use std::io::prelude::*;
use yaml_rust::YamlLoader;
use std::collections::HashMap;
use handlebars::Handlebars;
use handlebars::{Handlebars, Context};
use util;
use post::Post;
use std::fmt;
use serde_json;
use walkdir::{DirEntry, WalkDir, WalkDirIterator};

include!(concat!(env!("OUT_DIR"), "/site.rs"));

Expand All @@ -18,6 +19,7 @@ impl fmt::Debug for Site {
}

impl Site {

pub fn new(src_path: &Path) -> Site {
let config_path = src_path.join("_limonite.yml");
let mut config_content = String::new();
Expand Down Expand Up @@ -54,38 +56,53 @@ impl Site {

let mut files_to_render = Vec::new();
let mut files_to_copy = Vec::new();
//for entry in fs::read_dir(&src_path).unwrap() {
//let file_path = entry.unwrap().path();
//let metadata = fs::metadata(&file_path).unwrap();
//if (!metadata.is_dir()) {
//let fname = file_path.file_name().unwrap().to_str().unwrap();
//if !fname.starts_with("_") {
//let relative_file_path = util::relative_from(&file_path, &src_path).unwrap();
//match util::parse_front_matter_and_content(&file_path) {
//Ok(_) => {
//files_to_render.push(relative_file_path.to_str().unwrap().to_owned());
//},
//Err(_) => {
//files_to_copy.push(relative_file_path.to_str().unwrap().to_owned());
//}
//}
//}
//} else {
//}
//}
//println!("{}", src_path.display());
let mut dirs_to_create = Vec::new();

fn is_invalid(entry: &DirEntry) -> bool {
entry.file_name()
.to_str()
.map(|s| s.starts_with(".") || s.starts_with("_"))
.unwrap_or(false)
}
fn is_to_be_rendered(entry: &DirEntry) -> bool {
entry.file_name()
.to_str()
.map(|s| s.ends_with("hbs"))
.unwrap_or(false)
}

fn is_dir(entry: &DirEntry) -> bool {
entry.path().is_dir()
}
let walker = WalkDir::new(&src_path).into_iter();
for entry in walker.filter_entry(|e| !is_invalid(e)) {
let entry = entry.unwrap();
let relative_name = util::relative_from(entry.path(), &src_path).unwrap().to_str().unwrap().to_owned();
if (is_to_be_rendered(&entry)) {
let mut content = String::new();
let mut f = File::open(entry.path()).ok().expect(&format!("can't open {}", relative_name));
let _ = f.read_to_string(&mut content);
handlebars.register_template_string(&relative_name, content);
files_to_render.push(relative_name);
} else if (is_dir(&entry)) {
dirs_to_create.push(relative_name);
} else {
files_to_copy.push(relative_name);
}
}

Site {
src_path: src_path.to_path_buf(),
base_url: base_url,
posts: posts,
handlebars: handlebars,
files_to_render: files_to_render,
files_to_copy: files_to_copy
files_to_copy: files_to_copy,
dirs_to_create: dirs_to_create,
}
}

pub fn generate(&self, output_path: &Path) {
pub fn generate(self, output_path: &Path) {
for post in self.posts.iter() {
let dir = output_path.join(post.slug());
let _ = fs::create_dir_all(&dir);
Expand All @@ -94,33 +111,36 @@ impl Site {
let _ = f.write_all(output.as_bytes());
}

//for file in self.files_to_copy.iter() {
//let src = self.src_path.join(file);
//let target = output_path.join(file);
//match fs::copy(&src, &target) {
//Ok(_) => {
//println!("{:?}=>{:?}", src, target);
//},
//Err(_) => {
//println!("failed {:?}=>{:?}", src, target);
//}
//}
//}

//for file in self.files_to_render.iter() {
//let target = output_path.join(file);
//match util::parse_front_matter_and_content(&self.src_path.join(file)) {
//Ok((front_matter, content)) => {
//let mut data = HashMap::new();
//let result = util::render_liquid(&content, data).expect("couldn't render");
//let mut f = File::create(target).ok().expect("file not found");
//let _ = f.write_all(result.as_bytes());
//},
//Err(why) => {
//println!("ooo {}", why);
//}
//}
//}
for dir in self.dirs_to_create.iter() {
let target = output_path.join(dir);
match fs::create_dir_all(&target) {
Ok(_) => {},
Err(why) => {
println!("failed creating dir {} - {}", target.display(), why);
}
}
}
for file in self.files_to_copy.iter() {
let src = self.src_path.join(file);
let target = output_path.join(file);
match fs::copy(&src, &target) {
Ok(_) => {
println!("{:?}=>{:?}", src, target);
},
Err(why) => {
println!("failed {:?}=>{:?} - {:?}", src, target, why);
}
}
}

for file in self.files_to_render.iter() {
let target_name = file.split(".hbs").next().unwrap();
let target = output_path.join(target_name);
if let Ok(ref mut target_file) = File::create(target) {
self.handlebars.renderw(file, &Context::wraps(&self), target_file);
}
println!("render {}", output_path.join(target_name).display());
}
}
}

Expand All @@ -141,26 +161,26 @@ pub mod tests {
}

#[test]
fn copies_files_without_front_matter() {
let site = super::Site::new(Path::new("fixtures/007"));
fn copies_static_files() {
let site = super::Site::new(Path::new("fixtures/canonical"));
let outdir = get_temp_output_path();
site.generate(&outdir);
assert!(compare_paths(Path::new("fixtures/007/index.html"), &outdir.join("index.html")));
assert!(compare_paths(Path::new("fixtures/canonical/css/style.css"), &outdir.join("css/style.css")));
}

#[test]
fn renders_files_with_front_matter() {
let site = super::Site::new(Path::new("fixtures/010"));
fn renders_handlebars_templates() {
let site = super::Site::new(Path::new("fixtures/canonical"));
let outdir = get_temp_output_path();
site.generate(&outdir);
assert!(compare_paths(Path::new("fixtures/010-output/index.html"), &outdir.join("index.html")));
assert!(compare_paths(Path::new("fixtures/canonical-output/archive/index.html"), &outdir.join("archive/index.html")));
}

#[test]
fn renders_files_with_front_matter2() {
let site = super::Site::new(Path::new("fixtures/011"));
fn renders_posts() {
let site = super::Site::new(Path::new("fixtures/canonical"));
let outdir = get_temp_output_path();
site.generate(&outdir);
assert!(compare_paths(Path::new("fixtures/011-output/main.html"), &outdir.join("main.html")));
assert!(compare_paths(Path::new("fixtures/canonical-output/merry-xmas/index.html"), &outdir.join("merry-xmas/index.html")));
}
}
4 changes: 3 additions & 1 deletion src/site.rs.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ pub struct Site {
#[serde(skip_serializing)]
files_to_copy: Vec<String>,
#[serde(skip_serializing)]
files_to_render: Vec<String>
files_to_render: Vec<String>,
#[serde(skip_serializing)]
dirs_to_create: Vec<String>,
}

0 comments on commit 4d055dc

Please sign in to comment.