Restructure processors etc

This commit is contained in:
Jake Howard 2017-09-06 18:20:45 +01:00
parent a7b1e7c466
commit 8603dbcc77
Signed by: jake
GPG key ID: 57AFB45680EDD477
9 changed files with 44 additions and 41 deletions

View file

@ -1,11 +1,11 @@
pub mod pandoc; pub mod pandoc;
pub mod render; pub mod process;
use config::Config; use config::Config;
pub fn build_input(config: Config, input: String) -> Result<String, String> { pub fn build_input(config: Config, input: String) -> Result<String, String> {
let html = try!(pandoc::render(input)); 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); return Ok(rendered);
} }

View file

@ -1,7 +1,7 @@
use config::Config; use config::Config;
use renderers::html_cleanup::html_cleanup; use processors::html_cleanup::html_cleanup;
use renderers::strip_blank::strip_blank; use processors::strip_blank::strip_blank;
pub fn render(config: Config, input: String) -> Result<String, String> { pub fn render(config: Config, input: String) -> Result<String, String> {
let mut rendered_input = input; let mut rendered_input = input;

View file

@ -1,21 +1,18 @@
use sciter::{Window, Element}; use sciter::{Window, Element};
use std::ops::Deref; use std::ops::Deref;
pub mod html_cleanup;
pub mod strip_blank;
pub fn get_root(frame: Window) -> Element {
fn get_root(frame: Window) -> Element {
return frame.get_host().deref().get_root().expect("Failed to get root of window"); 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(); let mut frame = Window::new();
frame.load_html(&source.as_bytes(), None); frame.load_html(&source.as_bytes(), None);
return get_root(frame); return get_root(frame);
} }
fn get_html(element: Element) -> String { pub fn get_html(element: Element) -> String {
element.update(true).expect("Failed to update"); element.update(true).expect("Failed to update");
return String::from_utf8(element.get_html(true)).expect(&format!( return String::from_utf8(element.get_html(true)).expect(&format!(
"Failed to get HTML from {}.", "Failed to get HTML from {}.",
@ -23,7 +20,7 @@ fn get_html(element: Element) -> String {
)); ));
} }
fn find_all(root: &mut Element, selector: &str) -> Vec<Element> { pub fn find_all(root: &mut Element, selector: &str) -> Vec<Element> {
let elements = root.find_all(selector).expect(&format!("Failed to get {}.", selector)); let elements = root.find_all(selector).expect(&format!("Failed to get {}.", selector));
if elements.is_none() { if elements.is_none() {
return Vec::new(); return Vec::new();
@ -31,18 +28,18 @@ fn find_all(root: &mut Element, selector: &str) -> Vec<Element> {
return elements.unwrap(); 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); let mut all_matches = find_all(root, selector);
all_matches.reverse(); all_matches.reverse();
return all_matches.pop().expect(&format!("Failed to find {}.", selector)); 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)); let mut ele = root.get(index).expect(&format!("Failed to get element at {}.", index));
ele.destroy().expect("Failed to delete."); 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); let matches = find_all(root, selector);
if matches.is_empty() { if matches.is_empty() {
return; 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"); 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"); return find_first(root, "body");
} }

View file

@ -13,27 +13,30 @@ use std::process::exit;
mod args; mod args;
mod config; mod config;
mod process;
mod input; mod input;
mod utils; mod utils;
mod build; mod build;
mod output; mod output;
mod renderers; mod processors;
mod html;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
use clap::ArgMatches; use clap::ArgMatches;
use input::read_input_files;
use config::Config; use config::Config;
use build::build_input;
use output::output;
use utils::ok_or_exit;
fn ok_or_exit<T>(res: Result<T, String>) -> T {
return match res { fn build(config: Config) -> Result<(), String> {
Ok(k) => k, let input = try!(read_input_files(config.input.clone()));
Err(err) => { let raw_html = try!(build_input(config.clone(), input));
writeln!(io::stderr(), "Error: {:?}", err).unwrap(); println!("{}", raw_html);
exit(1); try!(output(config, raw_html));
} return Ok(());
};
} }
fn get_config(args: ArgMatches) -> Config { fn get_config(args: ArgMatches) -> Config {
@ -50,7 +53,7 @@ fn main() {
match subcommand { match subcommand {
"build" => { "build" => {
let config = get_config(args.clone()); let config = get_config(args.clone());
ok_or_exit(process::build(config)); utils::ok_or_exit(build(config));
} }
cmd => { cmd => {
writeln!(io::stderr(), "Unknown command {}.", cmd).unwrap(); writeln!(io::stderr(), "Unknown command {}.", cmd).unwrap();

View file

@ -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(());
}

View file

@ -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; use config::Config;

2
src/processors/mod.rs Normal file
View file

@ -0,0 +1,2 @@
pub mod html_cleanup;
pub mod strip_blank;

View file

@ -1,4 +1,7 @@
use std::fmt::Debug; use std::fmt::Debug;
use std::process::exit;
use std::io::{self, Write};
#[inline] #[inline]
pub fn result_override<T, E: Debug>(r: Result<T, E>, msg: String) -> Result<T, String> { pub fn result_override<T, E: Debug>(r: Result<T, E>, msg: String) -> Result<T, String> {
@ -15,3 +18,14 @@ pub fn result_prefix<T, E: Debug>(r: Result<T, E>, prefix: String) -> Result<T,
Err(e) => Err(format!("{}: {:?}", prefix, e)), Err(e) => Err(format!("{}: {:?}", prefix, e)),
}; };
} }
pub fn ok_or_exit<T>(res: Result<T, String>) -> T {
return match res {
Ok(k) => k,
Err(err) => {
writeln!(io::stderr(), "Error: {:?}", err).unwrap();
exit(1);
}
};
}