Skip to content

Commit

Permalink
feat(debug): Dump intermediate state
Browse files Browse the repository at this point in the history
This is useful for
- Seeing what variables are available
- Isolate bugs to be either in cobalt or liquid
  • Loading branch information
epage committed Jun 24, 2017
1 parent 5d739b5 commit fec65b3
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 14 deletions.
33 changes: 33 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Expand Up @@ -23,7 +23,7 @@ doc = false

[dependencies]
clap = "2.24"
liquid = "0.10"
liquid = {version = "0.10", features=["serde"]}
walkdir = "1.0"
yaml-rust = "0.3"
chrono = "0.3"
Expand All @@ -39,6 +39,8 @@ error-chain = "0.10"
lazy_static = "0.2"
itertools = "0.5"
ignore = "0.2"
serde = "1.0"
serde_yaml = "0.7"

[dependencies.hyper]
version = "0.10"
Expand Down
38 changes: 36 additions & 2 deletions src/cobalt.rs
Expand Up @@ -3,14 +3,15 @@ use std::collections::HashMap;
use std::io::Write;
use std::path::Path;
use std::ffi::OsStr;
use liquid::Value;
use liquid::{Value, Object};
use chrono::{UTC, FixedOffset};
use chrono::offset::TimeZone;
use rss::{Channel, Rss};
use serde_yaml;

use document::Document;
use error::{ErrorKind, Result};
use config::Config;
use config::{Config, Dump};
use files::FilesBuilder;

/// The primary build function that transforms a directory into a site
Expand Down Expand Up @@ -151,6 +152,10 @@ pub fn build(config: &Config) -> Result<()> {
}
}

if config.dump.contains(&Dump::Liquid) {
create_liquid_dump(dest, &post.path, &post.content, &post.attributes)?;
}

let mut context = post.get_render_context(&simple_posts_data);

try!(post.render_excerpt(&mut context, source, &config.excerpt_separator));
Expand All @@ -174,6 +179,10 @@ pub fn build(config: &Config) -> Result<()> {
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)?;
}

let mut context = doc.get_render_context(&posts_data);
let doc_html = try!(doc.render(&mut context, source, &layouts, &mut layouts_cache));
try!(create_document_file(&doc_html, &doc.path, dest));
Expand Down Expand Up @@ -216,6 +225,31 @@ pub fn build(config: &Config) -> Result<()> {
Ok(())
}

fn create_liquid_dump(dest: &Path, path: &str, content: &str, attributes: &Object) -> Result<()> {
let mut liquid_file_path = dest.join(path);
let mut liquid_file_name = OsStr::new("_").to_os_string();
{
let original_file_name = liquid_file_path.file_name().ok_or("File name missing")?;
liquid_file_name.push(original_file_name);
liquid_file_name.push(".liquid");
}
liquid_file_path.set_file_name(liquid_file_name);

let mut dump_file_path = liquid_file_path.clone();
dump_file_path.set_extension(".yml");

info!("Dumping content at {}", liquid_file_path.display());
let mut liquid_out = fs::File::create(liquid_file_path)?;
liquid_out.write_all(content.as_bytes())?;

info!("Dumping attributes at {}", dump_file_path.display());
let mut dump_out = fs::File::create(dump_file_path)?;
let values = serde_yaml::to_string(attributes)?;
dump_out.write_all(values.as_bytes())?;

Ok(())
}

// creates a new RSS file with the contents of the site blog
fn create_rss(path: &str, dest: &Path, config: &Config, posts: &[Document]) -> Result<()> {
match (&config.name, &config.description, &config.link) {
Expand Down
9 changes: 9 additions & 0 deletions src/config.rs
Expand Up @@ -5,6 +5,13 @@ use std::io::Read;
use error::Result;
use yaml_rust::YamlLoader;

arg_enum! {
#[derive(Debug, PartialEq)]
pub enum Dump {
Liquid
}
}

#[derive(Debug, PartialEq)]
pub struct Config {
pub source: String,
Expand All @@ -22,6 +29,7 @@ pub struct Config {
pub link: Option<String>,
pub ignore: Vec<String>,
pub excerpt_separator: String,
pub dump: Vec<Dump>,
}

impl Default for Config {
Expand All @@ -42,6 +50,7 @@ impl Default for Config {
link: None,
ignore: vec![],
excerpt_separator: "\n\n".to_owned(),
dump: vec![],
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Expand Up @@ -4,6 +4,7 @@ use yaml_rust::scanner;
use walkdir;
use liquid;
use ignore;
use serde_yaml;

error_chain! {

Expand All @@ -15,6 +16,7 @@ error_chain! {
Liquid(liquid::Error);
WalkDir(walkdir::Error);
Yaml(scanner::ScanError);
SerdeYaml(serde_yaml::Error);
Ignore(ignore::Error);
}

Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Expand Up @@ -17,6 +17,8 @@ extern crate regex;
extern crate rss;
extern crate walkdir;
extern crate yaml_rust;
extern crate serde;
extern crate serde_yaml;

extern crate itertools;

Expand All @@ -26,6 +28,9 @@ extern crate syntect;
#[macro_use]
extern crate log;

#[macro_use]
extern crate clap;

#[macro_use]
extern crate error_chain;

Expand All @@ -35,6 +40,7 @@ extern crate lazy_static;
pub use cobalt::build;
pub use error::Error;
pub use config::Config;
pub use config::Dump;
pub use new::{create_new_project, create_new_document};

pub mod error;
Expand Down
54 changes: 43 additions & 11 deletions src/main.rs
Expand Up @@ -9,20 +9,24 @@
))]

extern crate cobalt;
#[macro_use]
extern crate clap;
extern crate env_logger;
extern crate notify;
extern crate ghp;

extern crate hyper;

#[macro_use]
extern crate error_chain;

#[macro_use]
extern crate clap;

#[macro_use]
extern crate log;

use clap::{Arg, App, SubCommand, AppSettings};
use std::fs;
use cobalt::Config;
use cobalt::{Config, Dump};
use log::{LogRecord, LogLevelFilter};
use env_logger::LogBuilder;
use hyper::server::{Server, Request, Response};
Expand All @@ -38,7 +42,24 @@ use std::io::prelude::*;
use std::io::Result as IoResult;
use std::fs::File;

fn main() {
error_chain! {

links {
}

foreign_links {
Cobalt(cobalt::Error);
Notify(notify::Error);
Clap(clap::Error);
}

errors {
}
}

quick_main!(run);

fn run() -> Result<()> {
let global_matches = App::new("Cobalt")
.version(crate_version!())
.author("Benny Klotz <r3qnbenni@gmail.com>, Johann Hofmann")
Expand Down Expand Up @@ -102,6 +123,13 @@ fn main() {
.help("Suppress all output")
.global(true)
.takes_value(false))
.arg(Arg::with_name("dump")
.long("dump")
.possible_values(&Dump::variants())
.help("Dump the specified internal state")
.global(true)
.multiple(true)
.takes_value(true))
.subcommand(SubCommand::with_name("init")
.about("create a new cobalt project")
.arg(Arg::with_name("DIRECTORY")
Expand Down Expand Up @@ -262,6 +290,12 @@ fn main() {

config.include_drafts = matches.is_present("drafts");

if global_matches.is_present("dump") {
let dump = values_t!(global_matches, "dump", Dump)?;
config.dump = dump;
info!("Setting: {:?}", config.dump);
}

match command {
"init" => {
let directory = matches.value_of("DIRECTORY").unwrap();
Expand Down Expand Up @@ -332,10 +366,7 @@ fn main() {

match w {
Ok(mut watcher) => {
// TODO: clean up this unwrap
watcher
.watch(&config.source, RecursiveMode::Recursive)
.unwrap();
watcher.watch(&config.source, RecursiveMode::Recursive)?;
info!("Watching {:?} for changes", &config.source);

loop {
Expand Down Expand Up @@ -367,10 +398,11 @@ fn main() {
}

_ => {
println!("{}", global_matches.usage());
return;
bail!(global_matches.usage());
}
}
};

Ok(())
}

fn build(config: &Config) {
Expand Down

0 comments on commit fec65b3

Please sign in to comment.