forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBBCodeTagHandler.cs
More file actions
225 lines (176 loc) · 6.78 KB
/
BBCodeTagHandler.cs
File metadata and controls
225 lines (176 loc) · 6.78 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System;
using System.Collections.Generic;
namespace PluginCore.BBCode
{
public class BBCodeTagHandler : IPairTagMatchHandler
{
protected delegate Boolean DHandler(BBCodeTagMatch tm);
public BBCodeTagHandler()
{
_handlers = new Dictionary<String, DHandler>();
_handlers["B"] = new DHandler(_hadleTag_B);
_handlers["I"] = new DHandler(_hadleTag_I);
_handlers["S"] = new DHandler(_hadleTag_S);
_handlers["U"] = new DHandler(_hadleTag_U);
_handlers["~B"] = new DHandler(_hadleTag_NotB);
_handlers["~I"] = new DHandler(_hadleTag_NotI);
_handlers["~S"] = new DHandler(_hadleTag_NotS);
_handlers["~U"] = new DHandler(_hadleTag_NotU);
_handlers["FONT"] = new DHandler(_hadleTag_Font);
_handlers["SIZE"] = new DHandler(_hadleTag_Size);
_handlers["COLOR"] = new DHandler(_hadleTag_Color);
_handlers["BGCOLOR"] = new DHandler(_hadleTag_BgColor);
_handlers["BACKCOLOR"] = new DHandler(_hadleTag_BgColor);
}
protected Dictionary<String, DHandler> _handlers;
private BBCodeTagMatch _tm;
private String _tmName;
public Boolean handleTag(IPairTagMatch tagMatch)
{
if (!isHandleable(tagMatch))
return false;
if (_tm == null || _tm.bbCodeStyle == null)
return false;
return _handlers[_tmName](_tm);
}
public Boolean isHandleable(IPairTagMatch tagMatch)
{
_tm = tagMatch as BBCodeTagMatch;
if (_tm == null || !_tm.isTagOpener)
return false;
_tmName = _tm.tagName;
if (string.IsNullOrEmpty(_tmName))
return false;
_tmName = _tmName.ToUpper();
if (!_handlers.ContainsKey(_tmName) || _handlers[_tmName] == null)
return false;
return true;
}
protected Boolean _hadleTag_B(BBCodeTagMatch tm)
{
tm.bbCodeStyle.isBold = StateMode.ON;
return true;
}
protected Boolean _hadleTag_I(BBCodeTagMatch tm)
{
tm.bbCodeStyle.isItalic = StateMode.ON;
return true;
}
protected Boolean _hadleTag_S(BBCodeTagMatch tm)
{
tm.bbCodeStyle.isStriked = StateMode.ON;
return true;
}
protected Boolean _hadleTag_U(BBCodeTagMatch tm)
{
tm.bbCodeStyle.isUnderlined = StateMode.ON;
return true;
}
protected Boolean _hadleTag_NotB(BBCodeTagMatch tm)
{
tm.bbCodeStyle.isBold = StateMode.OFF;
return true;
}
protected Boolean _hadleTag_NotI(BBCodeTagMatch tm)
{
tm.bbCodeStyle.isItalic = StateMode.OFF;
return true;
}
protected Boolean _hadleTag_NotS(BBCodeTagMatch tm)
{
tm.bbCodeStyle.isStriked = StateMode.OFF;
return true;
}
protected Boolean _hadleTag_NotU(BBCodeTagMatch tm)
{
tm.bbCodeStyle.isUnderlined = StateMode.OFF;
return true;
}
protected Boolean _hadleTag_Font(BBCodeTagMatch tm)
{
if (tm.tagParam == null || tm.tagParam.Length < 2)
return false;
String fontName = tm.tagParam;
String tmpSymbol = fontName.Substring(0, 1);
if (tmpSymbol == "\"" || tmpSymbol == "'")
fontName = fontName.Substring(1);
tmpSymbol = fontName.Substring(fontName.Length - 1, 1);
if (tmpSymbol == "\"" || tmpSymbol == "'")
fontName = fontName.Substring(0, fontName.Length - 1);
tm.bbCodeStyle.fontName = fontName.Length == 0 ? null : fontName;
return true;
}
protected Boolean _hadleTag_Size(BBCodeTagMatch tm)
{
if (string.IsNullOrEmpty(tm.tagParam))
return false;
tm.bbCodeStyle.isAbsFontSize = true;
String p = tm.tagParam;
String firstSymbol = p.Substring(0, 1);
if (firstSymbol == "+" || firstSymbol == "-")
{
// p = p.Substring(1);
tm.bbCodeStyle.isAbsFontSize = false;
}
tm.bbCodeStyle.fontSize = Convert.ToSingle(p);
return true;
}
protected Boolean _hadleTag_Color(BBCodeTagMatch tm)
{
BBCodeStyle.Color c = _extractTagColor(tm.tagParam);
if (c == null)
return false;
tm.bbCodeStyle.foreColor = c;
return true;
}
protected Boolean _hadleTag_BgColor(BBCodeTagMatch tm)
{
BBCodeStyle.Color c = _extractTagColor(tm.tagParam);
if (c == null)
return false;
tm.bbCodeStyle.backColor = c;
return true;
}
protected BBCodeStyle.Color _extractTagColor(String tagColorParam)
{
if (string.IsNullOrEmpty(tagColorParam) || tagColorParam.Substring(0, 1) != "#")
return null;
String p = tagColorParam.Substring(1);
int colonIndex = p.IndexOf(":");
String colorStr = null;
String modeStr = null;
if (p.Length >= 8 && (colonIndex < 0 || colonIndex == 8))
{
colorStr = p.Substring(0, 8);
}
else if (p.Length >= 6 && (colonIndex < 0 || colonIndex == 6))
{
colorStr = "FF" + p.Substring(0, 6);
}
else if (p.Length >= 4 && (colonIndex < 0 || colonIndex == 4))
{
colorStr = p.Substring(0, 1) + p.Substring(0, 1)
+ p.Substring(1, 1) + p.Substring(1, 1)
+ p.Substring(2, 1) + p.Substring(2, 1)
+ p.Substring(3, 1) + p.Substring(3, 1);
}
else if (p.Length >= 3 && (colonIndex < 0 || colonIndex == 3))
{
colorStr = "FF"
+ p.Substring(0, 1) + p.Substring(0, 1)
+ p.Substring(1, 1) + p.Substring(1, 1)
+ p.Substring(2, 1) + p.Substring(2, 1);
}
else
{
if (colonIndex > -1)
colorStr = p.Substring(0, colonIndex);
colorStr = "FF" + colorStr + "000000";
colorStr = colorStr.Substring(0, 6);
}
if (colonIndex > -1)
modeStr = p.Substring(colonIndex + 1).ToUpper();
return new BBCodeStyle.Color(Convert.ToUInt32(colorStr, 16), BBCodeStyle.Color.ResolveColorMode(modeStr));
}
}
}