-
Notifications
You must be signed in to change notification settings - Fork 1
/
cahtmlparser.pas
249 lines (217 loc) · 6.22 KB
/
cahtmlparser.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
unit caHTMLParser;
{$INCLUDE ca.inc}
interface
uses
// Standard Delphi units
Windows,
SysUtils,
Classes,
// ca units
caClasses,
caUtils;
type
TcaOnHTMLTagEvent = procedure(Sender: TObject; const ATag: string; AParams: Tstrings) of Object;
TcaOnHTMLTextEvent = procedure(Sender: TObject; const AText: string) of Object;
//---------------------------------------------------------------------------
// IcaHTMLParser
//---------------------------------------------------------------------------
IcaHTMLParser = interface
['{0F50D1DA-E006-4DD0-8AF0-6DDF76CA7669}']
// Property methods
function GetOnTag: TcaOnHTMLTagEvent;
function GetOnText: TcaOnHTMLTextEvent;
procedure SetOnTag(const Value: TcaOnHTMLTagEvent);
procedure SetOnText(const Value: TcaOnHTMLTextEvent);
// Interface methods
procedure Parse(AHTML: TStream);
// Event properties
property OnTag: TcaOnHTMLTagEvent read GetOnTag write SetOnTag;
property OnText: TcaOnHTMLTextEvent read GetOnText write SetOnText;
end;
//---------------------------------------------------------------------------
// TcaHTMLParser
//---------------------------------------------------------------------------
TcaHTMLParser = class(TcaInterfacedPersistent, IcaHTMLParser)
private
// Private fields
FOnTag: TcaOnHTMLTagEvent;
FOnText: TcaOnHTMLTextEvent;
// Private methods
function GetTagName(const AText: string): string;
function RemoveCRLFChars(const Ch: Char): string;
function RemoveNonBreakingSpaces(const AText: string): string;
function RemoveQuotes(const AText: string): string;
procedure ParseTagParams(const AText: string; AParams: Tstrings);
procedure ProcessText(const AText: string);
procedure ToLower(var AText: string);
protected
// Property methods
function GetOnTag: TcaOnHTMLTagEvent;
function GetOnText: TcaOnHTMLTextEvent;
procedure SetOnTag(const Value: TcaOnHTMLTagEvent);
procedure SetOnText(const Value: TcaOnHTMLTextEvent);
// Event triggers
procedure DoTag(const ATag: string; AParams: Tstrings); virtual;
procedure DoText(const AText: string); virtual;
public
// Interface methods
procedure Parse(AHTML: TStream);
published
// Event properties
property OnTag: TcaOnHTMLTagEvent read GetOnTag write SetOnTag;
property OnText: TcaOnHTMLTextEvent read GetOnText write SetOnText;
end;
implementation
// Interface methods
procedure TcaHTMLParser.Parse(AHTML: TStream);
var
Ch: Char;
ParsedStr: string;
TagName: string;
Params: Tstrings;
OnStr: Boolean;
begin
if AHTML.Size > 0 then
begin
AHTML.Seek(0, soFromBeginning);
ParsedStr := '';
Ch := #0;
OnStr := False;
repeat
AHTML.Read(Ch, 1);
if Ch= '"' then OnStr := not OnStr;
if (Ch = '<') and (not OnStr) then
begin
if Length(ParsedStr) > 0 then
ProcessText(ParsedStr);
ParsedStr := '';
end;
ParsedStr := ParsedStr + RemoveCRLFChars(Ch);
if (Ch = '>') and (not OnStr) then
begin
if Length(ParsedStr) > 0 then
begin
TagName := GetTagName(ParsedStr);
if Copy(TagName, 1, 1) <> '!' then
begin
Params := TstringList.Create;
try
ParseTagParams(ParsedStr, Params);
DoTag(GetTagName(ParsedStr), Params);
finally
Params.Free;
end;
end
else
ProcessText(ParsedStr);
end;
ParsedStr := '';
end;
until AHTML.Size <= AHTML.Position;
end;
end;
// Event triggers
procedure TcaHTMLParser.DoTag(const ATag: string; AParams: Tstrings);
begin
if Assigned(FOnTag) then FOnTag(Self, ATag, AParams);
end;
procedure TcaHTMLParser.DoText(const AText: string);
begin
if Assigned(FOnText) then FOnText(Self, AText);
end;
// Private methods
function TcaHTMLParser.GetTagName(const AText: string): string;
var
EndPos: Integer;
begin
EndPos := Pos(#32, AText);
if EndPos = 0 then
EndPos := Pos('>', AText);
Result := Copy(AText, 2, EndPos - 2);
end;
function TcaHTMLParser.RemoveCRLFChars(const Ch: Char): string;
const
CRLFChars: set of Char = [#13,#10];
begin
if (Ch in CRLFChars) then
Result := ''
else
Result := Ch;
end;
function TcaHTMLParser.RemoveNonBreakingSpaces(const AText: string): string;
begin
Result := AText;
Utils.Replace(Result, ' ', '');
end;
function TcaHTMLParser.RemoveQuotes(const AText: string): string;
begin
Result := AText;
Utils.StripChar('"', Result);
Utils.StripChar('''', Result);
end;
procedure TcaHTMLParser.ParseTagParams(const AText: string; AParams: Tstrings);
const
StartStop: set of Char = [' ', '<', '>'];
Equals: set of Char = ['='];
var
Ch: Char;
Index: Integer;
InQuote: Boolean;
TagParam: string;
begin
TagParam := '';
AParams.Clear;
InQuote := False;
for Index := 1 to Length(AText) do
begin
Ch := AText[Index];
if Ch = '"' then InQuote := not InQuote;
if (Ch in StartStop) and (not InQuote) then
begin
if Length(TagParam) > 0 then
begin
ToLower(TagParam);
AParams.Add(RemoveQuotes(TagParam));
end;
TagParam := '';
end
else
TagParam := TagParam + Ch;
end;
end;
procedure TcaHTMLParser.ProcessText(const AText: string);
var
OutText: string;
begin
OutText := RemoveNonBreakingSpaces(AText);
if OutText <> '' then
DoText(OutText);
end;
procedure TcaHTMLParser.ToLower(var AText: string);
var
Index: Integer;
begin
for Index := 1 to Length(AText) do
begin
if AText[Index] = '=' then Break;
AText[Index] := AnsiLowerCase(AText[Index])[1];
end;
end;
// Property methods
function TcaHTMLParser.GetOnTag: TcaOnHTMLTagEvent;
begin
Result := FOnTag;
end;
function TcaHTMLParser.GetOnText: TcaOnHTMLTextEvent;
begin
Result := FOnText;
end;
procedure TcaHTMLParser.SetOnTag(const Value: TcaOnHTMLTagEvent);
begin
FOnTag := Value;
end;
procedure TcaHTMLParser.SetOnText(const Value: TcaOnHTMLTextEvent);
begin
FOnText := Value;
end;
end.