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:
@@ -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;
|
||||
|
||||
@@ -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 <parsed>");
|
||||
///
|
||||
/// 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('&', "&");
|
||||
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, "<a href=\"$0\">$0</a>"));
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user