Fix local image paths
This commit is contained in:
parent
97dc08acc5
commit
1132c78234
3 changed files with 27 additions and 1 deletions
19
src/processors/images.rs
Normal file
19
src/processors/images.rs
Normal file
|
@ -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<String, String> {
|
||||||
|
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));
|
||||||
|
}
|
|
@ -4,12 +4,14 @@ mod head_cleanup;
|
||||||
mod strip_blank;
|
mod strip_blank;
|
||||||
mod rebrand;
|
mod rebrand;
|
||||||
mod references;
|
mod references;
|
||||||
|
mod images;
|
||||||
|
|
||||||
|
|
||||||
pub const PROCESSORS: [fn(Config, String) -> Result<String, String>; 4] =
|
pub const PROCESSORS: [fn(Config, String) -> Result<String, String>; 5] =
|
||||||
[
|
[
|
||||||
head_cleanup::head_cleanup,
|
head_cleanup::head_cleanup,
|
||||||
rebrand::rebrand,
|
rebrand::rebrand,
|
||||||
references::references,
|
references::references,
|
||||||
|
images::images,
|
||||||
strip_blank::strip_blank,
|
strip_blank::strip_blank,
|
||||||
];
|
];
|
||||||
|
|
|
@ -51,3 +51,8 @@ pub fn resolve_path(path: String) -> PathBuf {
|
||||||
pub fn get_temp_file() -> PathBuf {
|
pub fn get_temp_file() -> PathBuf {
|
||||||
return Temp::new_file().expect("Failed to create temporary file").to_path_buf();
|
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");
|
||||||
|
}
|
||||||
|
|
Reference in a new issue