Regex harder, better, stronger (close #10)

This commit is contained in:
bleakgrey 2018-05-17 20:29:01 +03:00
parent b9c0489019
commit 3ca73ee3e0
3 changed files with 18 additions and 21 deletions

View File

@ -40,13 +40,13 @@ public class Tootle.Status {
status.created_at = obj.get_string_member ("created_at");
status.reblogs_count = obj.get_int_member ("reblogs_count");
status.favourites_count = obj.get_int_member ("favourites_count");
status.content = Utils.escape_html ( obj.get_string_member ("content"));
status.content = Utils.simplify_html ( obj.get_string_member ("content"));
status.sensitive = obj.get_boolean_member ("sensitive");
status.visibility = StatusVisibility.from_string (obj.get_string_member ("visibility"));
var spoiler = obj.get_string_member ("spoiler_text");
if (spoiler != "")
status.spoiler_text = Utils.escape_html (spoiler);
status.spoiler_text = Utils.simplify_html (spoiler);
if(obj.has_member ("reblogged"))
status.reblogged = obj.get_boolean_member ("reblogged");

View File

@ -67,11 +67,10 @@ public class Tootle.Notificator : GLib.Object {
}
private void toast (Notification obj) {
var tags = new Regex("<(.|\n)*?>", RegexCompileFlags.CASELESS);
var title = tags.replace(obj.type.get_desc (obj.account), -1, 0, "");
var title = Utils.escape_html (obj.type.get_desc (obj.account));
var notification = new GLib.Notification (title);
if (obj.status != null)
notification.set_body (tags.replace(obj.status.content, -1, 0, ""));
notification.set_body (Utils.escape_html (obj.status.content));
Tootle.app.send_notification (Tootle.app.application_id + ":" + obj.id.to_string (), notification);
}

View File

@ -4,28 +4,26 @@ public class Tootle.Utils{
Gtk.show_uri (null, url, Gdk.CURRENT_TIME);
}
public static string escape_html (string content) {
var str = content
public static string escape_html (string content) {
var all_tags = new Regex("<(.|\n)*?>", RegexCompileFlags.CASELESS);
return all_tags.replace(content, -1, 0, "");
}
public static string simplify_html (string content) {
var divided = content
.replace("<br>", "\n")
.replace("</br>", "")
.replace("<br />", "\n")
.replace("rel=\"tag\"", "")
.replace("rel=\"nofollow noopener\"", "")
.replace("class=\"mention hashtag\"", "")
.replace("class=\"mention\"", "")
.replace("class=\"h-card\"", "")
.replace("class=\"invisible\"", "")
.replace("class=\"ellipsis\"", "")
.replace("class=\"u-url mention\"", "")
.replace("class=\"\"", "")
.replace("<p>", "")
.replace("</p>", "\n\n")
.replace("target=\"_blank\"", "");
.replace("</p>", "\n\n");
while (str.has_suffix ("\n"))
str = str.slice (0, str.last_index_of ("\n"));
var html_params = new Regex("(class|target|rel)=\"(.|\n)*?\"", RegexCompileFlags.CASELESS);
var simplified = html_params.replace(divided, -1, 0, "");
return str;
while (simplified.has_suffix ("\n"))
simplified = simplified.slice (0, simplified.last_index_of ("\n"));
return simplified;
}
}