Unverified Commit 8d1b813f authored by Erik Reider's avatar Erik Reider Committed by GitHub

Body img markup: fixed img URIs and single quotes not supported (#532)

* Fixed notifications not being able to handle body img URIs * Fixed notifications not being able to parse body img with single quotes * Simplified the single quote support
parent 2454beb3
...@@ -14,6 +14,18 @@ namespace SwayNotificationCenter { ...@@ -14,6 +14,18 @@ namespace SwayNotificationCenter {
theme.add_resource_path ("/org/erikreider/swaync/icons"); theme.add_resource_path ("/org/erikreider/swaync/icons");
} }
public static string uri_to_path (owned string uri) {
uri = uri.strip ();
const string URI_PREFIX = "file://";
bool is_uri = (uri.length >= URI_PREFIX.length
&& uri.slice (0, URI_PREFIX.length) == URI_PREFIX);
if (is_uri) {
// Try as a URI (file:// is the only URI schema supported right now)
uri = uri.slice (URI_PREFIX.length, uri.length);
}
return uri;
}
public static void set_image_uri (owned string uri, public static void set_image_uri (owned string uri,
Gtk.Image img, Gtk.Image img,
int icon_size, int icon_size,
...@@ -195,20 +207,6 @@ namespace SwayNotificationCenter { ...@@ -195,20 +207,6 @@ namespace SwayNotificationCenter {
return path; return path;
} }
public static string get_match_from_info (MatchInfo info) {
var all = info.fetch_all ();
if (all.length > 1 && all[1].length > 0) {
string img = all[1];
// Replaces "~/" with $HOME
if (img.index_of ("~/", 0) == 0) {
img = Environment.get_home_dir () +
img.slice (1, img.length);
}
return img;
}
return "";
}
/** Gets the base type of a type if it's derivited */ /** Gets the base type of a type if it's derivited */
public static Type get_base_type (Type type) { public static Type get_base_type (Type type) {
if (type.is_derived ()) { if (type.is_derived ()) {
......
...@@ -146,7 +146,7 @@ namespace SwayNotificationCenter { ...@@ -146,7 +146,7 @@ namespace SwayNotificationCenter {
tag_regex = new Regex ("<(/?(?:%s))>".printf (joined_tags)); tag_regex = new Regex ("<(/?(?:%s))>".printf (joined_tags));
string unescaped = string.joinv ("|", UNESCAPE_CHARS); string unescaped = string.joinv ("|", UNESCAPE_CHARS);
tag_unescape_regex = new Regex ("&(?=%s)".printf (unescaped)); tag_unescape_regex = new Regex ("&(?=%s)".printf (unescaped));
img_tag_regex = new Regex ("""<img[^>]* src=\"([^\"]*)\"[^>]*>"""); img_tag_regex = new Regex ("<img[^>]* src=((\"([^\"]*)\")|(\'([^\']*)\'))[^>]*>");
} catch (Error e) { } catch (Error e) {
stderr.printf ("Invalid regex: %s", e.message); stderr.printf ("Invalid regex: %s", e.message);
} }
...@@ -374,10 +374,21 @@ namespace SwayNotificationCenter { ...@@ -374,10 +374,21 @@ namespace SwayNotificationCenter {
string[] img_paths = {}; string[] img_paths = {};
MatchInfo info; MatchInfo info;
if (img_tag_regex.match (text, 0, out info)) { if (img_tag_regex.match (text, 0, out info)) {
img_paths += Functions.get_match_from_info (info); do {
while (info.next ()) { if (info == null) {
img_paths += Functions.get_match_from_info (info); break;
} }
// Use the first capture group and remove the start and end quote
string result = info.fetch (1).strip ().slice (1, -1);
// Replaces "~/" with $HOME
if (result.index_of ("~/", 0) == 0) {
result = Environment.get_home_dir () +
result.slice (1, result.length);
}
img_paths += result;
} while (info.next ());
} }
// Remove all images // Remove all images
...@@ -385,7 +396,7 @@ namespace SwayNotificationCenter { ...@@ -385,7 +396,7 @@ namespace SwayNotificationCenter {
// Set the image if exists and is valid // Set the image if exists and is valid
if (img_paths.length > 0) { if (img_paths.length > 0) {
var img = img_paths[0]; var img = Functions.uri_to_path (img_paths[0]);
var file = File.new_for_path (img); var file = File.new_for_path (img);
if (img.length > 0 && file.query_exists ()) { if (img.length > 0 && file.query_exists ()) {
var buf = new Gdk.Pixbuf.from_file_at_scale ( var buf = new Gdk.Pixbuf.from_file_at_scale (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment