accent.dart
3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
part of '../functions.dart';
EncodeResult _accentEncoder(GreenNode node) {
final accentNode = node as AccentNode;
final commandCandidates = accentCommandMapping.entries
.where((entry) => entry.value == accentNode.label)
.map((entry) => entry.key)
.toList(growable: false);
final textCommandCandidates = commandCandidates
.where((candidate) => functions[candidate]?.allowedInText == true);
final mathCommandCandidates = commandCandidates
.where((candidate) => functions[candidate]?.allowedInMath == true);
if (commandCandidates.isEmpty) {
return NonStrictEncodeResult(
'unknown accent',
'Unrecognized accent symbol encountered during TeX encoding: '
'${unicodeLiteral(accentNode.label)}',
encodeTex(node.children.first),
);
}
bool isCommandMatched(String command) =>
accentNode.isStretchy == !nonStretchyAccents.contains(command) &&
accentNode.isShifty ==
(!accentNode.isStretchy || shiftyAccents.contains(command));
final mathCommand = mathCommandCandidates.firstWhereOrNull(isCommandMatched);
final math = mathCommand != null
? TexCommandEncodeResult(command: mathCommand, args: node.children)
: mathCommandCandidates.firstOrNull != null
? NonStrictEncodeResult(
'imprecise accent',
'No strict match for accent symbol under math mode: '
'${unicodeLiteral(accentNode.label)}, '
'${accentNode.isStretchy ? '' : 'not '}stretchy and '
'${accentNode.isShifty ? '' : 'not '}shifty',
TexCommandEncodeResult(
command: mathCommandCandidates.first,
args: node.children,
),
)
: NonStrictEncodeResult(
'unknown accent',
'No strict match for accent symbol under math mode: '
'${unicodeLiteral(accentNode.label)}, '
'${accentNode.isStretchy ? '' : 'not '}stretchy and '
'${accentNode.isShifty ? '' : 'not '}shifty',
TexCommandEncodeResult(
command: commandCandidates.first, args: node.children),
);
final textCommand =
accentNode.isStretchy == false && accentNode.isShifty == true
? textCommandCandidates.firstOrNull
: null;
final text = textCommand != null
? TexCommandEncodeResult(command: textCommand, args: node.children)
: textCommandCandidates.firstOrNull != null
? NonStrictEncodeResult(
'imprecise accent',
'No strict match for accent symbol under text mode: '
'${unicodeLiteral(accentNode.label)}, '
'${accentNode.isStretchy ? '' : 'not '}stretchy and '
'${accentNode.isShifty ? '' : 'not '}shifty',
TexCommandEncodeResult(
command: textCommandCandidates.first,
args: node.children,
),
)
: NonStrictEncodeResult(
'unknown accent',
'No strict match for accent symbol under text mode: '
'${unicodeLiteral(accentNode.label)}, '
'${accentNode.isStretchy ? '' : 'not '}stretchy and '
'${accentNode.isShifty ? '' : 'not '}shifty',
TexCommandEncodeResult(
command: commandCandidates.first, args: node.children),
);
return ModeDependentEncodeResult(
math: math,
text: text,
);
}