-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlParsUnit.pas
380 lines (351 loc) · 12.5 KB
/
htmlParsUnit.pas
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
unit htmlParsUnit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, Buttons;
type
THTMLParam = class
private
fRaw: string;
fKey: string;
fValue: string;
procedure SetKey(Key:string);
public
constructor Create;
destructor Destroy; override;
published
property Key: string read fKey write SetKey;
property Value: string read fValue;
property Raw: string read fRaw;
end;
THTMLTag = class
private
fName: string;
fRaw: string;
procedure SetName(Name: string);
public
Params: TList;
constructor Create;
destructor Destroy; override;
published
property Name: string read fName write SetName;
property Raw: string read fRaw;
end;
THTMLText = class
private
fLine: string;
fRawLine: string;
procedure SetLine(Line: string);
public
constructor Create;
destructor Destroy; override;
published
property Line: string read fLine write SetLine;
property Raw: string read fRawLine;
end;
THTMLParser = class(TObject)
private
Text: string;
Tag: string;
isTag: boolean;
procedure AddText;
procedure AddTag;
public
Parsed: TList;
Lines: TStringlist;
constructor Create;
destructor Destroy; override;
procedure Execute;
procedure Clear;
published
end;
implementation
constructor THTMLParser.Create;
begin
inherited Create;
Lines:=TStringlist.Create;
Parsed:=TList.Create
end;
destructor THTMLParser.Destroy;
begin
Lines.Free;
Parsed.Free;
inherited Destroy
end;
procedure THTMLParser.AddText;
var
HTMLText: THTMLText;
begin
if not isTag then
if Text<>'' then
begin
HTMLText:=THTMLText.Create;
HTMLText.Line:=Text;
Text:='';
Parsed.Add(HTMLText)
end
end;
procedure THTMLParser.AddTag;
var
HTMLTag: THTMLTag;
begin
isTag:=false;
HTMLTag:=THTMLTag.Create;
HTMLTag.Name:=Tag;
Tag:='';
Parsed.Add(HTMLTag)
end;
procedure THTMLParser.Execute;
var
i: integer;
s: string;
begin
Text:=''; Tag:='';
isTag:=false;
for i:=1 to Lines.Count do
begin
s:=Lines[i-1];
while length(s) > 0 do
begin
if s[1]='<' then
begin
AddText; isTag:=true
end
else
if s[1]='>' then AddTag
else
if isTag then Tag:=Tag+s[1]
else
Text:=Text+s[1];
delete(s,1,1)
end;
if (not isTag) and (Text<>'') then Text:=Text+#10
end;
if (isTag) and (Tag<>'') then AddTag;
if (not isTag) and (Text<>'') then AddText
end;
procedure THTMLParser.Clear;
var
i: integer;
obj: TObject;
begin
for i:=Parsed.Count downto 1 do
begin
obj:=Parsed[i-1];
if obj.ClassType = THTMLTag then
THTMLTag(Parsed[i-1]).Free
else
if obj.ClassType = THTMLText then
THTMLText(Parsed[i-1]).Free;
Parsed.Delete(i-1)
end
end;
constructor THTMLTag.Create;
begin
inherited Create;
Params:=Tlist.Create
end;
destructor THTMLTag.Destroy;
var
i: integer;
begin
for i:=Params.Count downto 1 do
begin
THTMLparam(Params[i-1]).Free;
Params.delete(i-1)
end;
Params.Free;
inherited Destroy
end;
procedure THTMLTag.SetName(Name: string);
var
Tag: string;
Param: string;
HTMLParam: THTMLParam;
iosQuote: boolean;
begin
fRaw:=Name;
Params.clear;
while (Length(Name)>0) and (Name[1]<>' ') do
begin
Tag:=Tag+Name[1];
delete(Name,1,1)
end;
fName:=uppercase(Tag);
while (length(Name)>0) do
begin
param:='';
iosQuote:=false;
while (Length(Name)>0) and ( not ((Name[1]=' ') and (iosQuote=false))) do
begin
if Name[1]='"' then
iosQuote:=not(iosQuote);
Param:=param+Name[1];
delete(Name,1,1)
end;
if (Length(Name)>0) and (Name[1]=' ') then Delete(Name,1,1);
if param<>'' then
begin
HTMLParam:=THTMLParam.Create;
HTMLParam.key:=param;
Params.add(HTMLParam)
end
end
end;
const Entities:array [1..100,1..2] of string=(('"', '"'), ('&', '&'), ('<', '<'),
('>', '>'), (' ', ' '),('¡', '¡'),
('¢', '¢'),('£', '£'),('¤','¤'),
('¥', '¥'),('¦','¦'),('§', '§'),
('¨', '¨'),('©', '©'),('ª', 'ª'),
('«', '«'),('¬', '¬'),('­', '­'),
('®', '®'),('¯', '¯'),('°', '°'),
('±','±'),('²', '²'),('³', '³'),
('´', '´'),('µ', 'µ'),('¶', '¶'),
('·','·'),('¸', '¸'),('¹', '¹'),
('º', 'º'),('»', '»'),('¼','¼'),
('½','½'),('¾','¾'),('¿','¿'),
('À','À'),('Á','Á'),('Â', 'Â'),
('Ã','Ã'),('Ä', 'Ä'),('Å', 'Å'),
('Æ', 'Æ'),('Ç','Ç'),('È','È'),
('É','É'),('Ê', 'Ê'),('Ë', 'Ë'),
('Ì','Ì'),('Í','Í'),('Î', 'Î'),
('Ï', 'Ï'),('Ð', 'Ð'),('Ñ','Ñ'),
('Ò','Ò'),('Ó','Ó'),('Ô', 'Ô'),
('Õ','Õ'),('Ö', 'Ö'),('×', '×'),
('Ø','Ø'),('Ù','Ù'),('Ú','Ú'),
('Û', 'Û'),('Ü', 'Ü'),('Ý','Ý'),
('Þ', 'Þ'),('ß', 'ß'),('à','à'),
('á','á'),('â', 'â'),('ã','ã'),
('ä', 'ä'),('å', 'å'),('æ', 'æ'),
('ç','ç'),('è','è'),('é','é'),
('ê', 'ê'),('ë', 'ë'),('ì','ì'),
('í','í'),('î', 'î'),('ï', 'ï'),
('ð', 'ð'),('ñ','ñ'),('ò','ò'),
('ó','ó'),('ô', 'ô'),('õ','õ'),
('ö', 'ö'),('÷','÷'),('ø','ø'),
('ù','ù'),('ú','ú'),('û', 'û'),
('ü', 'ü'),('ý','ý'),('þ', 'þ'),
('ÿ', 'ÿ'));
const CharSet:array [0..255] of char=(' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ','!','"','#','$','%','&','''',
'(',')','*','+',',','-','.','/','0','1',
'2','3','4','5','6','7','8','9',':',';',
'<','=','>','?','@','A','B','C','D','E',
'F','G','H','I','J','K','L','M','N','O',
'P','Q','R','S','T','U','V','W','X','Y',
'Z','[','\',']','^','_','`','a','b','c',
'd','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w',
'x','y','z','{','|','}','~','','','',
'','','','
','','','','','','',
'','','','','','','','','','',
'','','','','','','','','','',
' ','¡','¢','£','¤','¥','¦','§','¨','©',
'ª','«','¬','','®','¯','°','±','²','³',
'´','µ','¶','·','¸','¹','º','»','¼','½',
'¾','¿','À','Á','Â','Ã','Ä','Å','Æ','Ç',
'È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ',
'Ò','Ó','Ô','Õ','Ö','×','Ø','Ù','Ú','Û',
'Ü','Ý','Þ','ß','à','á','â','ã','ä','å',
'æ','ç','è','é','ê','ë','ì','í','î','ï',
'ð','ñ','ò','ó','ô','õ','ö','÷','ø','ù',
'ú','û','ü','ý','þ','ÿ');
procedure THTMLText.SetLine(Line: string);
var
j,i: integer;
isEntity: boolean;
Entity: string;
EnLen, EnPos: integer;
d, c: integer;
begin
fRawLine:=Line;
while pos(#10,Line)>0 do Line[Pos(#10,Line)]:=' ';
while pos(' ',Line)>0 do delete(Line,pos(' ',Line),1);
i:=1; isEntity:=false; EnPos:=0;
while (i<=Length(Line)) do
begin
if Line[i]='&' then
begin
EnPos:=i;
isEntity:=true;
Entity:=''
end;
if isEntity then Entity:=Entity+Line[i];
if isEntity then
if (Line[i]=';') or (Line[i]=' ') then
begin
EnLen:=Length(Entity);
if (EnLen>2) and (Entity[2]='#') then
begin
delete(Entity,EnLen,1);
delete(Entity,1,2);
if uppercase(Entity[1])='X' then Entity[1]:='$';
if (Length(Entity)<=3) then
begin
val(Entity,d,c);
if c=0 then
begin
delete(Line,EnPos,EnLen);
insert(Charset[d],Line,EnPos);
i:=EnPos
end
end
end
else
begin
j:=1;
while (j <= 100) do
begin
if Entity = (Entities[j,1]) then
begin
delete(Line,EnPos,EnLen);
insert(Entities[j,2],Line,Enpos);
j:=102
end;
j:=j+1
end;
if j=103 then i:=EnPos-1
else i:=EnPos;
end;
IsEntity:=false
end;
i:=i+1
end;
fLine:=Line
end;
procedure THTMLParam.SetKey(Key: string);
begin
fValue:=''; fRaw:=Key;
if pos('=',key)<>0 then
begin
fValue:=Key;
delete(fValue,1,pos('=',key));
key:=copy(Key,1,pos('=',key)-1);
if length(fValue)>1 then
if (fValue[1]='"') and (fValue[Length(fValue)]='"') then
begin
delete(fValue,1,1);
delete(fValue,Length(fValue),1)
end
end;
fKey:=uppercase(key)
end;
constructor THTMLParam.Create;
begin
inherited Create
end;
destructor THTMLParam.Destroy;
begin
inherited Destroy
end;
constructor THTMLText.Create;
begin
inherited Create
end;
destructor THTMLText.Destroy;
begin
inherited Destroy
end;
end.