diff --git a/src/processors/images.rs b/src/processors/images.rs new file mode 100644 index 0000000..40a1118 --- /dev/null +++ b/src/processors/images.rs @@ -0,0 +1,19 @@ +use html::{sciter_start, get_html, get_body, find_all}; +use sciter::{Element, Value}; +use config::Config; +use utils::{resolve_path, path_to_string}; + + +pub fn images(config: Config, input: String) -> Result { + let mut root = sciter_start(input); + let mut body = get_body(&mut root); + let mut images = find_all(&mut body, "img[src]"); + for mut image in images { + let image_src = image.get_attribute("src").expect("Image doesn't have a src"); + let image_path = resolve_path(image_src); + if image_path.exists() && image_path.is_file() { + image.set_attribute("src", path_to_string(&image_path.canonicalize().unwrap())); + } + } + return Ok(get_html(root)); +} diff --git a/src/processors/mod.rs b/src/processors/mod.rs index a7473ee..16d0631 100644 --- a/src/processors/mod.rs +++ b/src/processors/mod.rs @@ -4,12 +4,14 @@ mod head_cleanup; mod strip_blank; mod rebrand; mod references; +mod images; -pub const PROCESSORS: [fn(Config, String) -> Result; 4] = +pub const PROCESSORS: [fn(Config, String) -> Result; 5] = [ head_cleanup::head_cleanup, rebrand::rebrand, references::references, + images::images, strip_blank::strip_blank, ]; diff --git a/src/utils.rs b/src/utils.rs index 6e8f043..b11d58f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -51,3 +51,8 @@ pub fn resolve_path(path: String) -> PathBuf { pub fn get_temp_file() -> PathBuf { return Temp::new_file().expect("Failed to create temporary file").to_path_buf(); } + +#[inline] +pub fn path_to_string<'a>(path: &'a PathBuf) -> &'a str { + return path.to_str().expect("Failed to parse path to string"); +}