comparison 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
comparison
equal deleted inserted replaced
321:8141f409d52c 322:c32467cd06bb
7 #include <QByteArray> 7 #include <QByteArray>
8 #include <QDebug> 8 #include <QDebug>
9 #include <QLocale> 9 #include <QLocale>
10 #include <QString> 10 #include <QString>
11 #include <QTextDocument> 11 #include <QTextDocument>
12 #include <QCoreApplication>
12 13
13 #include <algorithm> 14 #include <algorithm>
14 #include <cctype> 15 #include <cctype>
15 #include <codecvt> 16 #include <codecvt>
16 #include <iostream> 17 #include <iostream>
65 string.replace(pos, find.length(), replace); 66 string.replace(pos, find.length(), replace);
66 pos += replace.length(); 67 pos += replace.length();
67 } 68 }
68 } 69 }
69 70
70 void SanitizeLineEndings(std::string& string) {
71 /* LOL */
72 ReplaceAll(string, "\r\n", "\n");
73 ReplaceAll(string, "</p>", "\n");
74 ReplaceAll(string, "<br>", "\n");
75 ReplaceAll(string, "<br />", "\n");
76 ReplaceAll(string, "\n\n\n", "\n\n");
77 }
78
79 void ConvertRomanNumerals(std::string& string) { 71 void ConvertRomanNumerals(std::string& string) {
80 static const std::vector<std::pair<std::string_view, std::string_view>> vec = { 72 static const std::vector<std::pair<std::string_view, std::string_view>> vec = {
81 {"2", "II"}, {"3", "III"}, {"4", "IV"}, {"5", "V"}, {"6", "VI"}, 73 {"2", "II"}, {"3", "III"}, {"4", "IV"}, {"5", "V"}, {"6", "VI"},
82 {"7", "VII"}, {"8", "VIII"}, {"9", "IX"}, {"11", "XI"}, {"12", "XII"}, 74 {"7", "VII"}, {"8", "VIII"}, {"9", "IX"}, {"11", "XI"}, {"12", "XII"},
83 {"13", "XIII"} 75 {"13", "XIII"}
117 NormalizeUnicode(string); 109 NormalizeUnicode(string);
118 RemoveLeadingChars(string, ' '); 110 RemoveLeadingChars(string, ' ');
119 RemoveTrailingChars(string, ' '); 111 RemoveTrailingChars(string, ' ');
120 } 112 }
121 113
122 /* removes dumb HTML tags because anilist is aids and
123 * gives us HTML for synopses :/
124 */
125 void RemoveHtmlTags(std::string& string) {
126 while (string.find("<") != std::string::npos) {
127 auto startpos = string.find("<");
128 auto endpos = string.find(">") + 1;
129
130 if (endpos != std::string::npos)
131 string.erase(startpos, endpos - startpos);
132 }
133 }
134
135 /* e.g. "&lt;" for "<" */
136 void ParseHtmlEntities(std::string& string) {
137 const std::unordered_map<std::string, std::string> map = {
138 {"&lt;", "<"},
139 {"&rt;", ">"},
140 {"&nbsp;", "\xA0"},
141 {"&amp;", "&"},
142 {"&quot;", "\""},
143 {"&apos;", "'"},
144 {"&cent;", "¢"},
145 {"&pound;", "£"},
146 {"&euro;", "€"},
147 {"&yen;", "¥"},
148 {"&copy;", "©"},
149 {"&reg;", "®"},
150 {"&times;", "×"},
151 {"&rsquo;", "’" } // Haibane Renmei, AniList
152 };
153
154 for (const auto& item : map)
155 ReplaceAll(string, item.first, item.second);
156 }
157
158 /* removes stupid HTML stuff */
159 void TextifySynopsis(std::string& string) { 114 void TextifySynopsis(std::string& string) {
160 #if 1
161 /* Just let Qt deal with it. */ 115 /* Just let Qt deal with it. */
162 QTextDocument text; 116 QTextDocument text;
163 text.setHtml(Strings::ToQString(string)); 117 text.setHtml(Strings::ToQString(string));
164 string = Strings::ToUtf8String(text.toPlainText()); 118 string = Strings::ToUtf8String(text.toPlainText());
165 #else
166 /* old implementation here... beware of jankiness */
167 SanitizeLineEndings(string);
168 RemoveHtmlTags(string);
169 ParseHtmlEntities(string);
170 #endif
171 } 119 }
172 120
173 /* let Qt handle the heavy lifting of locale shit 121 /* let Qt handle the heavy lifting of locale shit
174 * I don't want to deal with 122 * I don't want to deal with
175 */ 123 */
303 return false; 251 return false;
304 252
305 return true; 253 return true;
306 } 254 }
307 255
256 std::string Translate(const char* str) {
257 return Strings::ToUtf8String(QCoreApplication::tr(str));
258 }
259
308 } // namespace Strings 260 } // namespace Strings