parse em html

Signed-off-by: dom <githubaccount56556@proton.me>
This commit is contained in:
dom
2026-07-01 13:56:52 +08:00
parent ee770f4e2e
commit 8dd86b856f

View File

@@ -5,26 +5,14 @@ abstract final class Em {
); );
static String regCate(String origin) { static String regCate(String origin) {
Iterable<Match> matches = _exp.allMatches(origin); final matches = _exp.firstMatch(origin);
return matches.lastOrNull?.group(1) ?? origin; return matches?.group(1) ?? origin;
} }
static List<({bool isEm, String text})> regTitle(String origin) { static String parseHtml(String str) {
List<({bool isEm, String text})> res = []; return str.replaceAllMapped(
origin.splitMapJoin(
_exp,
onMatch: (Match match) {
String matchStr = match[0]!;
res.add((isEm: true, text: regCate(matchStr)));
return '';
},
onNonMatch: (String str) {
if (str != '') {
res.add((
isEm: false,
text: str.replaceAllMapped(
_htmlRegExp, _htmlRegExp,
(m) => switch (m.group(1)) { (match) => switch (match.group(1)) {
'lt' => '<', 'lt' => '<',
'gt' => '>', 'gt' => '>',
'quot' => '"', 'quot' => '"',
@@ -34,10 +22,22 @@ abstract final class Em {
var i? when (i.startsWith('#x')) => String.fromCharCode( var i? when (i.startsWith('#x')) => String.fromCharCode(
int.parse(i.substring(2), radix: 16), int.parse(i.substring(2), radix: 16),
), ),
_ => m.group(0)!, _ => match.group(0)!,
}, },
), );
)); }
static List<({bool isEm, String text})> regTitle(String origin) {
List<({bool isEm, String text})> res = [];
origin.splitMapJoin(
_exp,
onMatch: (Match match) {
res.add((isEm: true, text: parseHtml(match[1] ?? match[0]!)));
return '';
},
onNonMatch: (String str) {
if (str != '') {
res.add((isEm: false, text: parseHtml(str)));
} }
return ''; return '';
}, },