2021-02-02 10:16:59 +01:00
|
|
|
public class Tootle.HtmlUtils {
|
2018-06-06 16:19:11 +02:00
|
|
|
|
2020-07-30 21:02:03 +02:00
|
|
|
public const string FALLBACK_TEXT = _("[ There was an error parsing this text :c ]");
|
|
|
|
|
|
|
|
public static string remove_tags (string content) {
|
|
|
|
try {
|
2021-02-02 10:16:59 +01:00
|
|
|
//TODO: remove this when simplify() uses the HTML parsing class
|
2020-07-30 21:02:03 +02:00
|
|
|
var fixed_paragraphs = simplify (content);
|
2021-02-02 10:16:59 +01:00
|
|
|
|
2020-07-30 21:02:03 +02:00
|
|
|
var all_tags = new Regex ("<(.|\n)*?>", RegexCompileFlags.CASELESS);
|
|
|
|
return Widgets.RichLabel.restore_entities (all_tags.replace (fixed_paragraphs, -1, 0, ""));
|
|
|
|
}
|
|
|
|
catch (Error e) {
|
|
|
|
warning (e.message);
|
|
|
|
return FALLBACK_TEXT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 10:16:59 +01:00
|
|
|
//TODO: Perhaps this should use the HTML parser class
|
|
|
|
// since we depend on it anyway
|
2020-07-30 21:02:03 +02:00
|
|
|
public static string simplify (string str) {
|
|
|
|
try {
|
|
|
|
var divided = str
|
|
|
|
.replace("<br>", "\n")
|
|
|
|
.replace("</br>", "")
|
|
|
|
.replace("<br />", "\n")
|
|
|
|
.replace("<p>", "")
|
|
|
|
.replace("</p>", "\n\n");
|
|
|
|
|
|
|
|
var html_params = new Regex ("(class|target|rel)=\"(.|\n)*?\"", RegexCompileFlags.CASELESS);
|
|
|
|
var simplified = html_params.replace (divided, -1, 0, "");
|
|
|
|
|
|
|
|
while (simplified.has_suffix ("\n"))
|
|
|
|
simplified = simplified.slice (0, simplified.last_index_of ("\n"));
|
|
|
|
|
|
|
|
return simplified;
|
|
|
|
}
|
|
|
|
catch (Error e) {
|
2021-02-02 10:16:59 +01:00
|
|
|
warning (@"Can't simplify string \"$str\":\n$(e.message)");
|
|
|
|
return remove_tags (str);
|
2020-07-30 21:02:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string uri_encode (string str) {
|
|
|
|
var restored = Widgets.RichLabel.restore_entities (str);
|
|
|
|
return Soup.URI.encode (restored, ";&+");
|
|
|
|
}
|
2019-03-09 11:48:58 +01:00
|
|
|
|
2018-05-30 21:49:16 +02:00
|
|
|
}
|