diff src/core/strings.cc @ 322:c32467cd06bb

core/strings: add Strings::Translate function as tr() -> ToUtf8String
author Paper <paper@paper.us.eu.org>
date Wed, 12 Jun 2024 22:15:53 -0400
parents 1b5c04268d6a
children a0aa8c8c4307
line wrap: on
line diff
--- a/src/core/strings.cc	Wed Jun 12 20:42:44 2024 -0400
+++ b/src/core/strings.cc	Wed Jun 12 22:15:53 2024 -0400
@@ -9,6 +9,7 @@
 #include <QLocale>
 #include <QString>
 #include <QTextDocument>
+#include <QCoreApplication>
 
 #include <algorithm>
 #include <cctype>
@@ -67,15 +68,6 @@
 	}
 }
 
-void SanitizeLineEndings(std::string& string) {
-	/* LOL */
-	ReplaceAll(string, "\r\n",   "\n");
-	ReplaceAll(string, "</p>",   "\n");
-	ReplaceAll(string, "<br>",   "\n");
-	ReplaceAll(string, "<br />", "\n");
-	ReplaceAll(string, "\n\n\n", "\n\n");
-}
-
 void ConvertRomanNumerals(std::string& string) {
 	static const std::vector<std::pair<std::string_view, std::string_view>> vec = {
 		{"2", "II"}, {"3", "III"}, {"4", "IV"}, {"5", "V"}, {"6", "VI"},
@@ -119,55 +111,11 @@
 	RemoveTrailingChars(string, ' ');
 }
 
-/* removes dumb HTML tags because anilist is aids and
- * gives us HTML for synopses :/
- */
-void RemoveHtmlTags(std::string& string) {
-	while (string.find("<") != std::string::npos) {
-		auto startpos = string.find("<");
-		auto endpos = string.find(">") + 1;
-
-		if (endpos != std::string::npos)
-			string.erase(startpos, endpos - startpos);
-	}
-}
-
-/* e.g. "&lt;" for "<" */
-void ParseHtmlEntities(std::string& string) {
-	const std::unordered_map<std::string, std::string> map = {
-	    {"&lt;",    "<"},
-        {"&rt;",    ">"},
-        {"&nbsp;",  "\xA0"},
-        {"&amp;",   "&"},
-        {"&quot;",  "\""},
-	    {"&apos;",  "'"},
-        {"&cent;",  "¢"},
-        {"&pound;", "£"},
-        {"&euro;",  "€"},
-        {"&yen;",   "¥"},
-	    {"&copy;",  "©"},
-        {"&reg;",   "®"},
-        {"&times;", "×"},
-        {"&rsquo;", "’" }  // Haibane Renmei, AniList
-	};
-
-	for (const auto& item : map)
-		ReplaceAll(string, item.first, item.second);
-}
-
-/* removes stupid HTML stuff */
 void TextifySynopsis(std::string& string) {
-#if 1
 	/* Just let Qt deal with it. */
 	QTextDocument text;
 	text.setHtml(Strings::ToQString(string));
 	string = Strings::ToUtf8String(text.toPlainText());
-#else
-	/* old implementation here... beware of jankiness */
-	SanitizeLineEndings(string);
-	RemoveHtmlTags(string);
-	ParseHtmlEntities(string);
-#endif
 }
 
 /* let Qt handle the heavy lifting of locale shit
@@ -305,4 +253,8 @@
 	return true;
 }
 
+std::string Translate(const char* str) {
+	return Strings::ToUtf8String(QCoreApplication::tr(str));
+}
+
 } // namespace Strings