strip blank lines from HTML

This commit is contained in:
Jake Howard 2017-09-01 14:41:14 +01:00
parent 0a943bbfa5
commit cbddf42043
Signed by: jake
GPG key ID: 57AFB45680EDD477
4 changed files with 24 additions and 2 deletions

View file

@ -1,10 +1,14 @@
use config::Config; use config::Config;
use std::char;
use renderers::html_cleanup::html_cleanup; use renderers::html_cleanup::html_cleanup;
use renderers::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;
for renderer in vec![html_cleanup] { let renderers: Vec<fn(Config, String) -> Result<String, String>> =
vec![html_cleanup, strip_blank];
for renderer in renderers {
rendered_input = try!(renderer(config.clone(), rendered_input)); rendered_input = try!(renderer(config.clone(), rendered_input));
} }
return Ok(rendered_input); return Ok(rendered_input);

View file

@ -10,4 +10,3 @@ pub fn html_cleanup(config: Config, input: String) -> Result<String, String> {
let html = get_html(root); let html = get_html(root);
return Ok(html); return Ok(html);
} }

View file

@ -3,3 +3,4 @@ use config::Config;
mod helpers; mod helpers;
pub mod html_cleanup; pub mod html_cleanup;
pub mod strip_blank;

View file

@ -0,0 +1,18 @@
use std::char;
use config::Config;
fn remove_blank_lines(line: &&str) -> bool {
if line.is_empty() {
return false;
}
let whitespace_chars: Vec<&str> = line.matches(char::is_whitespace).collect();
if whitespace_chars.len() == line.len() {
return false;
}
return true;
}
pub fn strip_blank(config: Config, input: String) -> Result<String, String> {
let split: Vec<&str> = input.lines().filter(remove_blank_lines).collect();
return Ok(split.join("\n"));
}