Skip to content

Commit 9b1f413

Browse files
committed
update: hasMatch for each parser
1 parent 54145e7 commit 9b1f413

16 files changed

Lines changed: 84 additions & 68 deletions

lib/src/services/translation_service/text_structure_parser/parsers/blank_line_parser.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ class BlankLineParser implements TextParser {
77
@override
88
int get priority => 3;
99

10+
static bool hasMatch(String? line) => line == '' || line == null;
11+
1012
@override
1113
ParseResult parse(ParseContext context) {
12-
if (context.currentLineTrim == '') {
14+
if (hasMatch(context.currentLineTrim)) {
1315
context.addStructure(
1416
TextStructure(
1517
type: TextStructureType.blankLine,

lib/src/services/translation_service/text_structure_parser/parsers/html_comment_parser.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@ import '../text_parser.dart';
44

55
/// HTML 注释 `<!-- xx -->` 解析器
66
class HtmlCommentParser implements TextParser {
7-
static final beginRegex = RegExp(r'^\s*<!--');
8-
static final endRegex = RegExp(r'-->');
9-
107
@override
118
int get priority => 14;
129

10+
static bool beginHasMatch(String line) => RegExp(r'^\s*<!--').hasMatch(line);
11+
static bool endHasMatch(String line) => RegExp(r'-->').hasMatch(line);
12+
1313
@override
1414
ParseResult parse(ParseContext context) {
1515
final lineTrim = context.currentLineTrim;
1616

1717
/// HTML 注释 - 开始
18-
if (beginRegex.hasMatch(lineTrim) &&
18+
if (beginHasMatch(lineTrim) &&
1919
context.currentType != TextStructureType.htmlComment) {
2020
context.currentType = TextStructureType.htmlComment;
2121
context.startLineIndex = context.currentIndex;
2222
context.originalText.add(context.currentLine);
2323

2424
/// HTML 注释 - 单行结束
25-
if (endRegex.hasMatch(lineTrim)) {
25+
if (endHasMatch(lineTrim)) {
2626
/// 添加结构数据
2727
context.addStructure(
2828
TextStructure(
@@ -39,7 +39,7 @@ class HtmlCommentParser implements TextParser {
3939
}
4040

4141
/// HTML 注释 - 多行结束
42-
if (endRegex.hasMatch(lineTrim) &&
42+
if (endHasMatch(lineTrim) &&
4343
context.currentType == TextStructureType.htmlComment) {
4444
context.originalText.add(context.currentLine);
4545

lib/src/services/translation_service/text_structure_parser/parsers/html_tag_parser.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ import '../text_parser.dart';
55
/// 单行 HTML 标签 `<xxx``</xxx` 解析器
66
/// `<xxx`、`</xxx
77
class HtmlTagParser implements TextParser {
8-
static final regex = RegExp(r'^\s*<\/?[a-zA-Z][a-zA-Z0-9-]*');
9-
108
@override
119
int get priority => 13;
1210

11+
static bool hasMatch(String line) {
12+
final regex = RegExp(r'^\s*<\/?[a-zA-Z][a-zA-Z0-9-]*');
13+
return regex.hasMatch(line) && !line.startsWith('<br');
14+
}
15+
1316
@override
1417
ParseResult parse(ParseContext context) {
1518
final lineTrim = context.currentLineTrim;
16-
if (regex.hasMatch(lineTrim) && !lineTrim.startsWith('<br')) {
19+
if (hasMatch(lineTrim)) {
1720
context.addStructure(
1821
TextStructure(
1922
type: TextStructureType.htmlTag,

lib/src/services/translation_service/text_structure_parser/parsers/liquid_1_parser.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import '../text_parser.dart';
66
/// 单行 Liquid 语法 `{% xxx` 解析器
77
/// `{% xxx`
88
class Liquid1Parser implements TextParser {
9-
static final regex = RegExp(r'^\s*\{%');
10-
119
@override
1210
int get priority => 12;
1311

12+
static bool hasMatch(String line) => RegExp(r'^\s*\{%').hasMatch(line);
13+
1414
@override
1515
ParseResult parse(ParseContext context) {
1616
final lineTrim = context.currentLineTrim;
17-
if (regex.hasMatch(lineTrim)) {
17+
if (hasMatch(lineTrim)) {
1818
final isChinese = Utils.isChinese(context.currentLine);
1919
context.addStructure(
2020
TextStructure(

lib/src/services/translation_service/text_structure_parser/parsers/markdown_code_block_parser.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import '../text_parser.dart';
55
/// Markdown 代码块解析器
66
/// ``` 开头和结尾的部分
77
class MarkdownCodeBlockParser implements TextParser {
8-
static const delimiter = '```';
9-
108
@override
119
int get priority => 2;
1210

11+
static bool hasMatch(String line) => line.startsWith('```');
12+
1313
@override
1414
ParseResult parse(ParseContext context) {
1515
final lineTrim = context.currentLineTrim;
1616

17-
if (lineTrim.startsWith(delimiter)) {
17+
if (hasMatch(lineTrim)) {
1818
if (context.currentType != TextStructureType.markdownCodeBlock) {
1919
/// Markdown 代码块 - 开始
2020
context.currentType = TextStructureType.markdownCodeBlock;

lib/src/services/translation_service/text_structure_parser/parsers/markdown_custom_1_parser.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ import '../text_parser.dart';
55
/// 单行 Markdown 自定义语法1 `{:xxx}` 解析器
66
/// `{:xxx}`
77
class MarkdownCustom1Parser implements TextParser {
8-
static final regex = RegExp(r'\{:\s*([^}]+?)\s*\}');
9-
108
@override
119
int get priority => 10;
1210

11+
static bool hasMatch(String line) =>
12+
RegExp(r'\{:\s*([^}]+?)\s*\}').hasMatch(line);
13+
1314
@override
1415
ParseResult parse(ParseContext context) {
1516
final lineTrim = context.currentLineTrim;
16-
if (regex.hasMatch(lineTrim)) {
17+
if (hasMatch(lineTrim)) {
1718
context.addStructure(
1819
TextStructure(
1920
type: TextStructureType.markdownCustom1,

lib/src/services/translation_service/text_structure_parser/parsers/markdown_custom_2_parser.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import '../text_parser.dart';
55
/// 单行 Markdown 自定义语法2 `<?xxx` 解析器
66
/// `<?xxx`
77
class MarkdownCustom2Parser implements TextParser {
8-
static final regex = RegExp(r'^\s*\<\?');
9-
108
@override
119
int get priority => 11;
1210

11+
static bool hasMatch(String line) => RegExp(r'^\s*\<\?').hasMatch(line);
12+
1313
@override
1414
ParseResult parse(ParseContext context) {
1515
final lineTrim = context.currentLineTrim;
16-
if (regex.hasMatch(lineTrim)) {
16+
if (hasMatch(lineTrim)) {
1717
context.addStructure(
1818
TextStructure(
1919
type: TextStructureType.markdownCustom2,

lib/src/services/translation_service/text_structure_parser/parsers/markdown_custom_aside_type_parser.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ import '../text_parser.dart';
66
/// 单行 Markdown 自定义 Aside 语法 解析器
77
/// `:::类型 标题`
88
class MarkdownCustomAsideTypeParser implements TextParser {
9-
static final regex = RegExp(r'^\s*(:::)\s*([a-zA-Z0-9-]*)\s*(.*)$');
10-
119
@override
1210
int get priority => 9;
1311

12+
static RegExp regex = RegExp(r'^\s*(:::)\s*([a-zA-Z0-9-]*)\s*(.*)$');
13+
static bool hasMatch(String line) => regex.hasMatch(line);
14+
1415
@override
1516
ParseResult parse(ParseContext context) {
1617
final lineTrim = context.currentLineTrim;
17-
if (regex.hasMatch(lineTrim)) {
18+
if (hasMatch(lineTrim)) {
1819
final match = regex.firstMatch(lineTrim);
1920
// final delimiter = match!.group(1)!; // 必为 :::
2021
final type = match?.group(2)?.trim() != '' ? match?.group(2) : null;

lib/src/services/translation_service/text_structure_parser/parsers/markdown_define_link_parser.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ import '../text_parser.dart';
55
/// 单行 Markdown 定义的链接 解析器
66
/// `[xx]: xxx`
77
class MarkdownDefineLinkParser implements TextParser {
8-
static final regex = RegExp(r'^\s*\[([^\]]+)\]:\s*(.+)$');
9-
108
@override
119
int get priority => 7;
1210

11+
static bool hasMatch(String line) =>
12+
RegExp(r'^\s*\[([^\]]+)\]:\s*(.+)$').hasMatch(line);
13+
1314
@override
1415
ParseResult parse(ParseContext context) {
1516
final lineTrim = context.currentLineTrim;
16-
if (regex.hasMatch(lineTrim)) {
17+
if (hasMatch(lineTrim)) {
1718
context.addStructure(
1819
TextStructure(
1920
type: TextStructureType.markdownDefineLink,

lib/src/services/translation_service/text_structure_parser/parsers/markdown_horizontal_rule_parser.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import '../text_parser.dart';
55
/// 单行 Markdown 分割横线解析器
66
/// `---``- - -``* * *``_ _ _`
77
class MarkdownHorizontalRuleParser implements TextParser {
8-
static final regex = RegExp(r'^\s*([-*_])(?:\s*\1){2,}\s*$');
98
@override
109
int get priority => 4;
1110

11+
static bool hasMatch(String line) =>
12+
RegExp(r'^\s*([-*_])(?:\s*\1){2,}\s*$').hasMatch(line);
13+
1214
@override
1315
ParseResult parse(ParseContext context) {
1416
final lineTrim = context.currentLineTrim;
15-
if (regex.hasMatch(lineTrim)) {
17+
if (hasMatch(lineTrim)) {
1618
context.addStructure(
1719
TextStructure(
1820
type: TextStructureType.markdownHorizontalRule,

0 commit comments

Comments
 (0)