opt: regTitle

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
bggRGjQaUbCoE
2024-12-29 10:57:44 +08:00
parent e9945ab63c
commit b6217f6e6e

View File

@@ -1,3 +1,5 @@
import 'package:html/parser.dart' show parse;
class Em {
static regCate(String origin) {
String str = origin;
@@ -12,32 +14,37 @@ class Em {
static regTitle(String origin) {
RegExp exp = RegExp('<[^>]*>([^<]*)</[^>]*>');
List res = [];
origin.splitMapJoin(exp, onMatch: (Match match) {
String matchStr = match[0]!;
Map map = {'type': 'em', 'text': regCate(matchStr)};
res.add(map);
return regCate(matchStr);
}, onNonMatch: (String str) {
if (str != '') {
str = decodeHtmlEntities(str);
Map map = {'type': 'text', 'text': str};
origin.splitMapJoin(
exp,
onMatch: (Match match) {
String matchStr = match[0]!;
Map map = {'type': 'em', 'text': regCate(matchStr)};
res.add(map);
}
return str;
});
return regCate(matchStr);
},
onNonMatch: (String str) {
if (str != '') {
str = parse(str).body?.text ?? str;
Map map = {'type': 'text', 'text': str};
res.add(map);
}
return str;
},
);
return res;
}
static String decodeHtmlEntities(String title) {
return title
.replaceAll('&lt;', '<')
.replaceAll('&gt;', '>')
.replaceAll('&#34;', '"')
.replaceAll('&#39;', "'")
.replaceAll('&quot;', '"')
.replaceAll('&apos;', "'")
.replaceAll('&nbsp;', " ")
.replaceAll('&amp;', "&")
.replaceAll('&#x27;', "'");
return parse(title).body?.text ?? title;
// return title
// .replaceAll('&lt;', '<')
// .replaceAll('&gt;', '>')
// .replaceAll('&#34;', '"')
// .replaceAll('&#39;', "'")
// .replaceAll('&quot;', '"')
// .replaceAll('&apos;', "'")
// .replaceAll('&nbsp;', " ")
// .replaceAll('&amp;', "&")
// .replaceAll('&#x27;', "'");
}
}