Unverified Commit b7c94361 authored by Erik Reider's avatar Erik Reider Committed by GitHub

Fixed markup parsing unofficial tags (#117)

* Fixed markup parsing unofficial tags * Added comments and some small refactoring
parent 042f1e61
......@@ -106,7 +106,6 @@ namespace SwayNotificationCenter {
"body",
"body-markup",
"body-images",
"body-hyperlinks",
"persistence",
"synchronous",
"private-synchronous",
......
......@@ -51,6 +51,20 @@ namespace SwayNotificationCenter {
private int carousel_empty_widget_index = 0;
private static Regex tag_regex;
private static Regex tag_replace_regex;
private const string[] TAGS = { "b", "u", "i" };
construct {
try {
string joined_tags = string.joinv ("|", TAGS);
tag_regex = new Regex (@"</?($joined_tags)>");
tag_replace_regex = new Regex ("</?|>");
} catch (Error e) {
stderr.printf ("Invalid regex: %s", e.message);
}
}
public Notification (NotifyParams param,
NotiDaemon notiDaemon) {
build_noti (param, notiDaemon);
......@@ -184,12 +198,12 @@ namespace SwayNotificationCenter {
var img = img_paths[0];
var file = File.new_for_path (img);
if (img.length > 0 && file.query_exists ()) {
const int max_width = 200;
const int max_height = 100;
const int MAX_WIDTH = 200;
const int MAX_HEIGHT = 100;
var buf = new Gdk.Pixbuf.from_file_at_scale (
file.get_path (),
max_width,
max_height,
MAX_WIDTH,
MAX_HEIGHT,
true);
this.body_image.set_from_pixbuf (buf);
this.body_image.show ();
......@@ -200,29 +214,49 @@ namespace SwayNotificationCenter {
}
}
// Markup
try {
// Escapes text just incase it's not escaped yet
text = Markup.escape_text (text);
// Turns it back to markdown, defaults to escaped if not valid
// Escapes all characters
string escaped = Markup.escape_text (text);
// Replace all valid tags brackets with <,</,> so that the
// markup parser only parses valid tags
escaped = tag_regex.replace_eval (escaped,
escaped.length,
0,
RegexMatchFlags.NOTEMPTY,
this.regex_tag_eval_cb);
// Turns it back to markdown, defaults to original if not valid
Pango.AttrList ? attr = null;
string ? buf = null;
Pango.parse_markup (text, -1, 0, out attr, out buf, null);
Pango.parse_markup (escaped, -1, 0, out attr, out buf, null);
this.body.set_markup (buf);
this.body.set_text (buf);
if (attr != null) this.body.set_attributes (attr);
// Something has gone wrong... Use the escaped text instead
if (this.body.get_text ().length == 0 && buf.length != 0) {
stderr.printf ("Could for some reason not set markup. Text: %s\n",
text);
this.body.set_markup (text);
}
} catch (Error e) {
stderr.printf ("Could not parse Pango markup %s: %s\n",
text, e.message);
this.body.set_markup (text);
// Sets the original text
this.body.set_text (text);
}
}
/**
* Replaces "&gt;" and "&lt;" with their character counterpart if the
* tag is valid.
*/
private bool regex_tag_eval_cb (MatchInfo match_info,
StringBuilder result) {
try {
string tag = match_info.fetch (0);
// Removes the tag backets: </b> -> b
var res = tag_replace_regex.replace (tag, tag.length, 0, "");
if (!(res in TAGS)) return false;
result.append (tag.replace ("&lt;", "<").replace ("&gt;", ">"));
} catch (Error e) {
stderr.printf ("Regex eval error: %s\n", e.message);
}
return false;
}
public void click_default_action () {
......
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