diff --git a/src/build/mod.rs b/src/build/mod.rs index 7924610..4e92d96 100644 --- a/src/build/mod.rs +++ b/src/build/mod.rs @@ -1,11 +1,11 @@ pub mod pandoc; -pub mod render; +pub mod process; use config::Config; pub fn build_input(config: Config, input: String) -> Result { let html = try!(pandoc::render(input)); - let rendered = try!(render::render(config.clone(), html)); + let rendered = try!(process::render(config.clone(), html)); return Ok(rendered); } diff --git a/src/build/render.rs b/src/build/process.rs similarity index 81% rename from src/build/render.rs rename to src/build/process.rs index 4ef3c0c..e2c8f84 100644 --- a/src/build/render.rs +++ b/src/build/process.rs @@ -1,7 +1,7 @@ use config::Config; -use renderers::html_cleanup::html_cleanup; -use renderers::strip_blank::strip_blank; +use processors::html_cleanup::html_cleanup; +use processors::strip_blank::strip_blank; pub fn render(config: Config, input: String) -> Result { let mut rendered_input = input; diff --git a/src/renderers/mod.rs b/src/html.rs similarity index 70% rename from src/renderers/mod.rs rename to src/html.rs index f2b6f0c..1f41d11 100644 --- a/src/renderers/mod.rs +++ b/src/html.rs @@ -1,21 +1,18 @@ use sciter::{Window, Element}; use std::ops::Deref; -pub mod html_cleanup; -pub mod strip_blank; - -fn get_root(frame: Window) -> Element { +pub fn get_root(frame: Window) -> Element { return frame.get_host().deref().get_root().expect("Failed to get root of window"); } -fn sciter_start(source: String) -> Element { +pub fn sciter_start(source: String) -> Element { let mut frame = Window::new(); frame.load_html(&source.as_bytes(), None); return get_root(frame); } -fn get_html(element: Element) -> String { +pub fn get_html(element: Element) -> String { element.update(true).expect("Failed to update"); return String::from_utf8(element.get_html(true)).expect(&format!( "Failed to get HTML from {}.", @@ -23,7 +20,7 @@ fn get_html(element: Element) -> String { )); } -fn find_all(root: &mut Element, selector: &str) -> Vec { +pub fn find_all(root: &mut Element, selector: &str) -> Vec { let elements = root.find_all(selector).expect(&format!("Failed to get {}.", selector)); if elements.is_none() { return Vec::new(); @@ -31,18 +28,18 @@ fn find_all(root: &mut Element, selector: &str) -> Vec { return elements.unwrap(); } -fn find_first(root: &mut Element, selector: &str) -> Element { +pub fn find_first(root: &mut Element, selector: &str) -> Element { let mut all_matches = find_all(root, selector); all_matches.reverse(); return all_matches.pop().expect(&format!("Failed to find {}.", selector)); } -fn destroy_at(root: &mut Element, index: usize) { +pub fn destroy_at(root: &mut Element, index: usize) { let mut ele = root.get(index).expect(&format!("Failed to get element at {}.", index)); ele.destroy().expect("Failed to delete."); } -fn destroy_matching(root: &mut Element, selector: &str) { +pub fn destroy_matching(root: &mut Element, selector: &str) { let matches = find_all(root, selector); if matches.is_empty() { return; @@ -52,10 +49,10 @@ fn destroy_matching(root: &mut Element, selector: &str) { } } -fn get_head(root: &mut Element) -> Element { +pub fn get_head(root: &mut Element) -> Element { return find_first(root, "head"); } -fn get_body(root: &mut Element) -> Element { +pub fn get_body(root: &mut Element) -> Element { return find_first(root, "body"); } diff --git a/src/main.rs b/src/main.rs index 1631bde..d5a37fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,27 +13,30 @@ use std::process::exit; mod args; mod config; -mod process; mod input; mod utils; mod build; mod output; -mod renderers; +mod processors; +mod html; #[cfg(test)] mod tests; use clap::ArgMatches; +use input::read_input_files; use config::Config; +use build::build_input; +use output::output; +use utils::ok_or_exit; -fn ok_or_exit(res: Result) -> T { - return match res { - Ok(k) => k, - Err(err) => { - writeln!(io::stderr(), "Error: {:?}", err).unwrap(); - exit(1); - } - }; + +fn build(config: Config) -> Result<(), String> { + let input = try!(read_input_files(config.input.clone())); + let raw_html = try!(build_input(config.clone(), input)); + println!("{}", raw_html); + try!(output(config, raw_html)); + return Ok(()); } fn get_config(args: ArgMatches) -> Config { @@ -50,7 +53,7 @@ fn main() { match subcommand { "build" => { let config = get_config(args.clone()); - ok_or_exit(process::build(config)); + utils::ok_or_exit(build(config)); } cmd => { writeln!(io::stderr(), "Unknown command {}.", cmd).unwrap(); diff --git a/src/process.rs b/src/process.rs deleted file mode 100644 index 7be5ac1..0000000 --- a/src/process.rs +++ /dev/null @@ -1,13 +0,0 @@ -use input::read_input_files; -use config::Config; -use build::build_input; -use output::output; - - -pub fn build(config: Config) -> Result<(), String> { - let input = try!(read_input_files(config.input.clone())); - let raw_html = try!(build_input(config.clone(), input)); - println!("{}", raw_html); - try!(output(config, raw_html)); - return Ok(()); -} diff --git a/src/renderers/html_cleanup.rs b/src/processors/html_cleanup.rs similarity index 84% rename from src/renderers/html_cleanup.rs rename to src/processors/html_cleanup.rs index c4d6aa6..1ab8142 100644 --- a/src/renderers/html_cleanup.rs +++ b/src/processors/html_cleanup.rs @@ -1,4 +1,4 @@ -use renderers::{sciter_start, get_html, destroy_matching, destroy_at, get_head}; +use html::{sciter_start, get_html, destroy_matching, destroy_at, get_head}; use config::Config; diff --git a/src/processors/mod.rs b/src/processors/mod.rs new file mode 100644 index 0000000..96db238 --- /dev/null +++ b/src/processors/mod.rs @@ -0,0 +1,2 @@ +pub mod html_cleanup; +pub mod strip_blank; diff --git a/src/renderers/strip_blank.rs b/src/processors/strip_blank.rs similarity index 100% rename from src/renderers/strip_blank.rs rename to src/processors/strip_blank.rs diff --git a/src/utils.rs b/src/utils.rs index 3472d7f..dc31ad3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,7 @@ use std::fmt::Debug; +use std::process::exit; +use std::io::{self, Write}; + #[inline] pub fn result_override(r: Result, msg: String) -> Result { @@ -15,3 +18,14 @@ pub fn result_prefix(r: Result, prefix: String) -> Result Err(format!("{}: {:?}", prefix, e)), }; } + + +pub fn ok_or_exit(res: Result) -> T { + return match res { + Ok(k) => k, + Err(err) => { + writeln!(io::stderr(), "Error: {:?}", err).unwrap(); + exit(1); + } + }; +}