html_to_pango: Switch to use the new library spawn from this.

Thanks to @danigm for spinning that part of fractal to a shared library.
This commit is contained in:
Jordan Petridis
2018-04-03 19:42:09 +00:00
parent af1cb43bd6
commit a946ddfab1
5 changed files with 21 additions and 45 deletions
+2
View File
@@ -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;
-43
View File
@@ -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 <span>parsed</span>");
/// assert_eq!(&m, "this is &lt;parsed&gt;");
///
/// let m = markup();
/// assert_eq!(&m, "with links: <a href=\"http://gnome.org\">http://gnome.org</a> ");
/// ```
pub fn html_to_pango_markup(s: &str) -> String {
let mut out = String::from(s);
out = String::from(out.trim());
out = out.replace('&', "&amp;");
out = out.replace('<', "&lt;");
out = out.replace('>', "&gt;");
let amp = "(&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(&regex_str).unwrap();
out = String::from(re.replace_all(&out, "<a href=\"$0\">$0</a>"));
out
}
#[cfg(test)]
mod tests {
use super::*;
+5 -2
View File
@@ -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.