diff src/core/strings.cc @ 274:f6a756c19bfb

anime_list.cc: use mutexes so we don't sex the stack strings.cc: use Qt to convert from HTML to plain text.
author Paper <paper@paper.us.eu.org>
date Thu, 18 Apr 2024 17:24:42 -0400
parents f31305b9f60a
children 1b5c04268d6a
line wrap: on
line diff
--- a/src/core/strings.cc	Thu Apr 18 16:53:17 2024 -0400
+++ b/src/core/strings.cc	Thu Apr 18 17:24:42 2024 -0400
@@ -8,6 +8,7 @@
 #include <QDebug>
 #include <QLocale>
 #include <QString>
+#include <QTextDocument>
 
 #include <algorithm>
 #include <cctype>
@@ -148,24 +149,20 @@
 
 /* e.g. "&lt;" for "<" */
 void ParseHtmlEntities(std::string& string) {
-	/* The only one of these I can understand using are the first
-	 * three. why do the rest of these exist?
-	 *
-	 * probably mojibake.
-	 */
 	const std::unordered_map<std::string, std::string> map = {
-	    {"&lt;",    "<"   },
-        {"&rt;",    ">"   },
+	    {"&lt;",    "<"},
+        {"&rt;",    ">"},
         {"&nbsp;",  "\xA0"},
-        {"&amp;",   "&"   },
-        {"&quot;",  "\""  },
-	    {"&apos;",  "'"   },
-        {"&cent;",  "¢"  },
-        {"&pound;", "£"  },
-        {"&euro;",  "€" },
-        {"&yen;",   "¥"  },
-	    {"&copy;",  "©"  },
-        {"&reg;",   "®"  },
+        {"&amp;",   "&"},
+        {"&quot;",  "\""},
+	    {"&apos;",  "'"},
+        {"&cent;",  "¢"},
+        {"&pound;", "£"},
+        {"&euro;",  "€"},
+        {"&yen;",   "¥"},
+	    {"&copy;",  "©"},
+        {"&reg;",   "®"},
+        {"&times;", "×"},
         {"&rsquo;", "’" }  // Haibane Renmei, AniList
 	};
 
@@ -175,9 +172,17 @@
 
 /* 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
@@ -273,7 +278,7 @@
 std::string BytesToHumanReadableSize(uint64_t bytes, int precision) {
 #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
 	/* QLocale in Qt >= 5.10.0 has a function for this */
-	return Strings::ToUtf8String(session.config.locale.GetLocale().formattedDataSize(bytes), precision);
+	return Strings::ToUtf8String(session.config.locale.GetLocale().formattedDataSize(bytes, precision));
 #else
 	static const std::unordered_map<uint64_t, std::string> map = {
 		{1ull << 10, "KiB"},