diff --git a/Cargo.lock b/Cargo.lock index 01181d1..a615610 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -698,6 +698,7 @@ dependencies = [ name = "hammond-gtk" version = "0.1.0" dependencies = [ + "ammonia 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "dissolve 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -709,6 +710,7 @@ dependencies = [ "gtk 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "hammond-data 0.1.0", "hammond-downloader 0.1.0", + "html2pango 0.1.0 (git+https://gitlab.gnome.org/danigm/html2pango)", "humansize 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -738,6 +740,15 @@ dependencies = [ "serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "html2pango" +version = "0.1.0" +source = "git+https://gitlab.gnome.org/danigm/html2pango#5bf56226d889c6174473784d0fe437cab288b0d4" +dependencies = [ + "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "html5ever" version = "0.21.0" @@ -2106,6 +2117,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum gtk 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "414f3522f550a0b4f65e089f00ffcd3987dab8b0be284cb979aa7f6a03d60516" "checksum gtk-sys 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d9554cf5b3a85a13fb39258c65b04b262989c1d7a758f8f555b77a478621a91" "checksum handlebars 0.31.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e7bdb08e879b8c78ee90f5022d121897c31ea022cb0cc6d13f2158c7a9fbabb1" +"checksum html2pango 0.1.0 (git+https://gitlab.gnome.org/danigm/html2pango)" = "" "checksum html5ever 0.21.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba3a1fd1857a714d410c191364c5d7bf8a6487c0ab5575146d37dd7eb17ef523" "checksum html5ever 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e579ac8647178ab915d400d7d22938bda5cd351c6c62e1c294d56884ccfc75fe" "checksum httparse 1.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "c2f407128745b78abc95c0ffbe4e5d37427fdc0d45470710cfef8c44522a2e37" diff --git a/hammond-gtk/Cargo.toml b/hammond-gtk/Cargo.toml index 97f59e8..d5c9d94 100644 --- a/hammond-gtk/Cargo.toml +++ b/hammond-gtk/Cargo.toml @@ -6,6 +6,7 @@ version = "0.1.0" workspace = "../" [dependencies] +ammonia = "1.1.0" chrono = "0.4.1" dissolve = "0.2.2" gdk = "0.8.0" @@ -25,6 +26,7 @@ take_mut = "0.2.2" regex = "0.2.10" reqwest = "0.8.5" serde_json = "1.0.13" +html2pango = { git = "https://gitlab.gnome.org/danigm/html2pango" } [dependencies.gtk] features = ["v3_22"] diff --git a/hammond-gtk/src/main.rs b/hammond-gtk/src/main.rs index d5b2b4e..10d3af4 100644 --- a/hammond-gtk/src/main.rs +++ b/hammond-gtk/src/main.rs @@ -19,10 +19,12 @@ extern crate lazy_static; #[macro_use] extern crate log; +extern crate ammonia; extern crate chrono; // extern crate dissolve; extern crate hammond_data; extern crate hammond_downloader; +extern crate html2pango; extern crate humansize; extern crate loggerv; extern crate open; diff --git a/hammond-gtk/src/utils.rs b/hammond-gtk/src/utils.rs index f7c438f..c398b4c 100644 --- a/hammond-gtk/src/utils.rs +++ b/hammond-gtk/src/utils.rs @@ -257,49 +257,6 @@ pub fn time_period_to_duration(time: i64, period: &str) -> Duration { } } -/// https://gitlab.gnome.org/World/fractal/blob/da016b252a0710ffd324469cd2059e3e090695eb/fractal-gtk/src/util.rs#L44-79 -/// Converts the input `&str` to pango format, replacing special characters -/// `&, < and >` and parses URLS to show as a link -/// -/// # Examples -/// -/// ``` -/// let m = markup("this is parsed"); -/// assert_eq!(&m, "this is parsed"); -/// -/// let m = markup("this is parsed"); -/// assert_eq!(&m, "this is <parsed>"); -/// -/// let m = markup(); -/// assert_eq!(&m, "with links: http://gnome.org "); -/// ``` -pub fn html_to_pango_markup(s: &str) -> String { - let mut out = String::from(s); - - out = String::from(out.trim()); - out = out.replace('&', "&"); - out = out.replace('<', "<"); - out = out.replace('>', ">"); - - let amp = "(&)"; - let domain = "[^\\s,)(\"]+"; - let param = format!("({amp}?\\w+(=[\\w._-]+)?)", amp = amp); - let params = format!("(\\?{param}*)*", param = param); - let hash = "(#[\\w._-]+)?"; - - let regex_str = format!( - "(https?://{domain}{params}{hash})", - domain = domain, - params = params, - hash = hash - ); - - let re = Regex::new(®ex_str).unwrap(); - out = String::from(re.replace_all(&out, "$0")); - - out -} - #[cfg(test)] mod tests { use super::*; diff --git a/hammond-gtk/src/widgets/show.rs b/hammond-gtk/src/widgets/show.rs index 7917d7b..36236ca 100644 --- a/hammond-gtk/src/widgets/show.rs +++ b/hammond-gtk/src/widgets/show.rs @@ -1,8 +1,10 @@ // use dissolve; use failure::Error; // use glib; +use ammonia; use gtk; use gtk::prelude::*; +use html2pango::markup as html_to_pango_markup; use open; use hammond_data::Podcast; @@ -10,7 +12,7 @@ use hammond_data::dbqueries; // use hammond_data::utils::replace_extra_spaces; use app::Action; -use utils::{html_to_pango_markup, set_image_from_path}; +use utils::set_image_from_path; // use utils::set_image_from_path; use widgets::episode::episodes_listbox; @@ -123,7 +125,8 @@ impl ShowWidget { // markup. // let desc = dissolve::strip_html_tags(text).join(" "); // self.description.set_text(&replace_extra_spaces(&desc)); - self.description.set_markup(&html_to_pango_markup(text)); + self.description + .set_markup(&ammonia::clean(&html_to_pango_markup(text))); } /// Set scrolled window vertical adjustment.