comparison src/core/strings.cc @ 260:dd211ff68b36

pages/seasons: add initial functionality the menu doesn't work yet, but it's a good start
author Paper <paper@paper.us.eu.org>
date Wed, 03 Apr 2024 19:48:38 -0400
parents 862d0d8619f6
children 9a04802848c0
comparison
equal deleted inserted replaced
259:0362f3c4534c 260:dd211ff68b36
68 } 68 }
69 69
70 /* This function is really only used for cleaning up the synopsis of 70 /* This function is really only used for cleaning up the synopsis of
71 * horrible HTML debris from AniList :) 71 * horrible HTML debris from AniList :)
72 */ 72 */
73 std::string ReplaceAll(std::string string, const std::string& find, const std::string& replace) { 73 void ReplaceAll(std::string& string, std::string_view find, std::string_view replace) {
74 size_t pos = 0; 74 size_t pos = 0;
75 while ((pos = string.find(find, pos)) != std::string::npos) { 75 while ((pos = string.find(find, pos)) != std::string::npos) {
76 string.replace(pos, find.length(), replace); 76 string.replace(pos, find.length(), replace);
77 pos += replace.length(); 77 pos += replace.length();
78 } 78 }
79 return string; 79 }
80 } 80
81 81 void SanitizeLineEndings(std::string& string) {
82 std::string SanitizeLineEndings(const std::string& string) {
83 /* LOL */ 82 /* LOL */
84 return ReplaceAll(ReplaceAll(ReplaceAll(ReplaceAll(ReplaceAll(string, "\r\n", "\n"), "</p>", "\n"), "<br>", "\n"), 83 ReplaceAll(string, "\r\n", "\n");
85 "<br />", "\n"), 84 ReplaceAll(string, "</p>", "\n");
86 "\n\n\n", "\n\n"); 85 ReplaceAll(string, "<br>", "\n");
86 ReplaceAll(string, "<br />", "\n");
87 ReplaceAll(string, "\n\n\n", "\n\n");
88 }
89
90 void ConvertRomanNumerals(std::string& string) {
91 static const std::vector<std::pair<std::string_view, std::string_view>> vec = {
92 {"2", "II"}, {"3", "III"}, {"4", "IV"}, {"5", "V"}, {"6", "VI"},
93 {"7", "VII"}, {"8", "VIII"}, {"9", "IX"}, {"11", "XI"}, {"12", "XII"},
94 {"13", "XIII"}
95 };
96
97 for (const auto& item : vec)
98 ReplaceAll(string, item.second, item.first);
87 } 99 }
88 100
89 /* removes dumb HTML tags because anilist is aids and 101 /* removes dumb HTML tags because anilist is aids and
90 * gives us HTML for synopses :/ 102 * gives us HTML for synopses :/
91 */ 103 */
92 std::string RemoveHtmlTags(std::string string) { 104 void RemoveHtmlTags(std::string& string) {
93 while (string.find("<") != std::string::npos) { 105 while (string.find("<") != std::string::npos) {
94 auto startpos = string.find("<"); 106 auto startpos = string.find("<");
95 auto endpos = string.find(">") + 1; 107 auto endpos = string.find(">") + 1;
96 108
97 if (endpos != std::string::npos) 109 if (endpos != std::string::npos)
98 string.erase(startpos, endpos - startpos); 110 string.erase(startpos, endpos - startpos);
99 } 111 }
100 return string;
101 } 112 }
102 113
103 /* e.g. "&lt;" for "<" */ 114 /* e.g. "&lt;" for "<" */
104 std::string ParseHtmlEntities(std::string string) { 115 void ParseHtmlEntities(std::string& string) {
116 /* The only one of these I can understand using are the first
117 * three. why do the rest of these exist?
118 *
119 * probably mojibake.
120 */
105 const std::unordered_map<std::string, std::string> map = { 121 const std::unordered_map<std::string, std::string> map = {
106 /* The only one of these I can understand using are the first
107 * three. why do the rest of these exist?
108 *
109 * probably mojibake.
110 */
111 {"&lt;", "<" }, 122 {"&lt;", "<" },
112 {"&rt;", ">" }, 123 {"&rt;", ">" },
113 {"&nbsp;", "\xA0"}, 124 {"&nbsp;", "\xA0"},
114 {"&amp;", "&" }, 125 {"&amp;", "&" },
115 {"&quot;", "\"" }, 126 {"&quot;", "\"" },
122 {"&reg;", "®" }, 133 {"&reg;", "®" },
123 {"&rsquo;", "’" } // Haibane Renmei, AniList 134 {"&rsquo;", "’" } // Haibane Renmei, AniList
124 }; 135 };
125 136
126 for (const auto& item : map) 137 for (const auto& item : map)
127 string = ReplaceAll(string, item.first, item.second); 138 ReplaceAll(string, item.first, item.second);
128 return string;
129 } 139 }
130 140
131 /* removes stupid HTML stuff */ 141 /* removes stupid HTML stuff */
132 std::string TextifySynopsis(const std::string& string) { 142 void TextifySynopsis(std::string& string) {
133 return ParseHtmlEntities(RemoveHtmlTags(SanitizeLineEndings(string))); 143 SanitizeLineEndings(string);
144 RemoveHtmlTags(string);
145 ParseHtmlEntities(string);
134 } 146 }
135 147
136 /* let Qt handle the heavy lifting of locale shit 148 /* let Qt handle the heavy lifting of locale shit
137 * I don't want to deal with 149 * I don't want to deal with
138 */ 150 */