archive
/
md-pdf-rs
Archived
1
Fork 0

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 render;
pub mod process;
use config::Config;
pub fn build_input(config: Config, input: String) -> Result<String, String> {
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);
}

View File

@ -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<String, String> {
let mut rendered_input = input;

View File

@ -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<Element> {
pub fn find_all(root: &mut Element, selector: &str) -> Vec<Element> {
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<Element> {
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");
}

View File

@ -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<T>(res: Result<T, String>) -> 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();

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;

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::process::exit;
use std::io::{self, Write};
#[inline]
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)),
};
}
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);
}
};
}