2018-05-30 19:49:16 +00:00
|
|
|
public class Tootle.Html {
|
2018-06-06 14:19:11 +00:00
|
|
|
|
2018-05-30 19:49:16 +00:00
|
|
|
public static string remove_tags (string content) {
|
2020-05-29 12:19:35 +00:00
|
|
|
var all_tags = new Regex ("<(.|\n)*?>", RegexCompileFlags.CASELESS);
|
|
|
|
return GLib.Markup.escape_text (all_tags.replace (content, -1, 0, ""));
|
2018-05-30 19:49:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 12:19:35 +00:00
|
|
|
public static string escape_pango_entities (string str) {
|
|
|
|
return str
|
|
|
|
.replace (" ", " ")
|
|
|
|
.replace ("'", "'")
|
|
|
|
.replace ("& ", "&");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string restore_entities (string str) {
|
|
|
|
return str
|
|
|
|
.replace ("&", "&")
|
|
|
|
.replace ("<", "<")
|
|
|
|
.replace (">", ">")
|
|
|
|
.replace ("'", "'")
|
|
|
|
.replace (""", "\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string simplify (string str) {
|
|
|
|
var divided = str
|
2018-05-30 19:49:16 +00:00
|
|
|
.replace("<br>", "\n")
|
|
|
|
.replace("</br>", "")
|
|
|
|
.replace("<br />", "\n")
|
|
|
|
.replace("<p>", "")
|
|
|
|
.replace("</p>", "\n\n");
|
2019-03-09 10:48:58 +00:00
|
|
|
|
2020-05-29 12:19:35 +00:00
|
|
|
var html_params = new Regex ("(class|target|rel)=\"(.|\n)*?\"", RegexCompileFlags.CASELESS);
|
|
|
|
var simplified = html_params.replace (divided, -1, 0, "");
|
2019-03-09 10:48:58 +00:00
|
|
|
|
2018-05-30 19:49:16 +00:00
|
|
|
while (simplified.has_suffix ("\n"))
|
|
|
|
simplified = simplified.slice (0, simplified.last_index_of ("\n"));
|
2019-03-09 10:48:58 +00:00
|
|
|
|
2020-05-29 12:19:35 +00:00
|
|
|
return escape_pango_entities (simplified);
|
2018-05-30 19:49:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 12:19:35 +00:00
|
|
|
public static string uri_encode (string str) {
|
|
|
|
var restored = restore_entities (str);
|
|
|
|
return Soup.URI.encode (restored, ";&+");
|
2018-05-30 19:49:16 +00:00
|
|
|
}
|
2019-03-09 10:48:58 +00:00
|
|
|
|
2018-05-30 19:49:16 +00:00
|
|
|
}
|