-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathHTMLp.Formatter.pas
523 lines (449 loc) · 13.8 KB
/
HTMLp.Formatter.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
unit HTMLp.Formatter;
interface
uses
HTMLp.DomCore, HTMLp.HtmlTags;
const
SHOW_ALL = $FFFFFFFF;
SHOW_ELEMENT = $00000001;
SHOW_ATTRIBUTE = $00000002;
SHOW_TEXT = $00000004;
SHOW_CDATA_SECTION = $00000008;
SHOW_ENTITY_REFERENCE = $00000010;
SHOW_ENTITY = $00000020;
SHOW_PROCESSING_INSTRUCTION = $00000040;
SHOW_COMMENT = $00000080;
SHOW_DOCUMENT = $00000100;
SHOW_DOCUMENT_TYPE = $00000200;
SHOW_DOCUMENT_FRAGMENT = $00000400;
SHOW_NOTATION = $00000800;
type
TStringBuilder = class
private
FCapacity: Integer;
FLength: Integer;
FValue: WideString;
public
constructor Create(ACapacity: Integer);
function EndWithWhiteSpace: Boolean;
function TailMatch(const Tail: WideString): Boolean;
function ToString: WideString;
procedure AppendText(const TextStr: WideString);
property Length: Integer read FLength;
end;
TBaseFormatter = class
private
procedure ProcessNode(Node: TNode);
protected
FDocument: TDocument;
FStringBuilder: TStringBuilder;
FHTMLTagList: THTMLTagList;
FDepth: Integer;
FWhatToShow: Integer;
FExpandEntities: Boolean;
FPreserveWhiteSpace: Boolean;
FInAttributes: Boolean;
procedure AppendNewLine;
procedure AppendParagraph;
procedure AppendText(const TextStr: WideString); virtual;
procedure ProcessAttribute(Attr: TAttr); virtual;
procedure ProcessAttributes(Element: TElement); virtual;
procedure ProcessCDataSection(CDataSection: TCDataSection); virtual;
procedure ProcessComment(Comment: TComment); virtual;
procedure ProcessDocumentElement; virtual;
procedure ProcessElement(Element: TElement); virtual;
procedure ProcessEntityReference(EntityReference: TEntityReference); virtual;
// procedure ProcessNotation(Notation: TNotation); virtual;
procedure ProcessProcessingInstruction(ProcessingInstruction: TProcessingInstruction); virtual;
procedure ProcessTextNode(TextNode: TTextNode); virtual;
public
constructor Create;
destructor Destroy; override;
function getText(document: TDocument): WideString;
end;
THtmlFormatter = class(TBaseFormatter)
private
FIndent: Integer;
function OnlyTextContent(Element: TElement): Boolean;
protected
procedure ProcessAttribute(Attr: TAttr); override;
procedure ProcessComment(Comment: TComment); override;
procedure ProcessElement(Element: TElement); override;
procedure ProcessTextNode(TextNode: TTextNode); override;
public
constructor Create;
property Indent: Integer read FIndent write FIndent;
end;
TTextFormatter = class(TBaseFormatter)
protected
FInsideAnchor: Boolean;
function GetAnchorText(Node: TElement): WideString; virtual;
function GetImageText(Node: TElement): WideString; virtual;
procedure AppendText(const TextStr: WideString); override;
procedure ProcessElement(Element: TElement); override;
procedure ProcessEntityReference(EntityReference: TEntityReference); override;
procedure ProcessTextNode(TextNode: TTextNode); override;
public
constructor Create;
end;
implementation
uses
SysUtils,
HTMLp.Entities;
const
CRLF: WideString = #13#10;
PARAGRAPH_SEPARATOR: WideString = #13#10#13#10;
ViewAsBlockTags: THTMLTagSet = [
ADDRESS_TAG, BLOCKQUOTE_TAG, CAPTION_TAG, CENTER_TAG, DD_TAG, DIV_TAG,
DL_TAG, DT_TAG, FIELDSET_TAG, FORM_TAG, FRAME_TAG, H1_TAG, H2_TAG, H3_TAG,
H4_TAG, H5_TAG, H6_TAG, HR_TAG, IFRAME_TAG, LI_TAG, NOFRAMES_TAG, NOSCRIPT_TAG,
OL_TAG, P_TAG, PRE_TAG, TABLE_TAG, TD_TAG, TH_TAG, TITLE_TAG, UL_TAG
];
function IsWhiteSpace(W: WideChar): Boolean;
begin
Result := (Ord(W) in WhiteSpace);
end;
function normalizeWhiteSpace(const TextStr: WideString): WideString;
var
I, J, Count: Integer;
begin
SetLength(Result, Length(TextStr));
J := 0;
Count := 0;
for I := 1 to Length(TextStr) do
begin
if IsWhiteSpace(TextStr[I]) then
begin
Inc(Count);
Continue
end;
if Count <> 0 then
begin
Count := 0;
Inc(J);
Result[J] := ' ';
end;
Inc(J);
Result[J] := TextStr[I];
end;
if Count <> 0 then
begin
Inc(J);
Result[J] := ' '
end;
SetLength(Result, J);
end;
function Spaces(Count: Integer): WideString;
var
I: Integer;
begin
SetLength(Result, Count);
for I := 1 to Count do Result[I] := ' ';
end;
function TrimLeftSpaces(const S: WideString): WideString;
var
I: Integer;
begin
I := 1;
while (I <= Length(S)) and (Ord(S[I]) = SP) do Inc(I);
Result := Copy(S, I, Length(S) - I + 1);
end;
constructor TStringBuilder.Create(ACapacity: Integer);
begin
inherited Create;
FCapacity := ACapacity;
SetLength(FValue, FCapacity);
end;
function TStringBuilder.EndWithWhiteSpace: Boolean;
begin
Result := IsWhiteSpace(FValue[FLength]);
end;
function TStringBuilder.TailMatch(const Tail: WideString): Boolean;
var
TailLen, I: Integer;
begin
Result := false;
TailLen := System.Length(Tail);
if TailLen > FLength then Exit;
for I := 1 to TailLen do
begin
if FValue[FLength - TailLen + I] <> Tail[I] then Exit;
end;
Result := True;
end;
function TStringBuilder.ToString: WideString;
begin
SetLength(FValue, FLength);
Result := FValue;
end;
procedure TStringBuilder.AppendText(const TextStr: WideString);
var
TextLen, I: Integer;
begin
if (FLength + System.Length(TextStr)) > FCapacity then
begin
FCapacity := 2 * FCapacity;
SetLength(FValue, FCapacity)
end;
TextLen := System.Length(TextStr);
for I := 1 to TextLen do FValue[FLength + I] := TextStr[I];
Inc(FLength, TextLen);
end;
constructor TBaseFormatter.Create;
begin
inherited Create;
FWhatToShow := Integer(SHOW_ALL);
FHTMLTagList := THTMLTagList.Create;
end;
destructor TBaseFormatter.Destroy;
begin
FreeAndNil(FHTMLTagList);
inherited Destroy;
end;
procedure TBaseFormatter.ProcessNode(Node: TNode);
begin
case Node.NodeType of
ELEMENT_NODE: ProcessElement(Node as TElement);
TEXT_NODE: if (FWhatToShow and SHOW_TEXT) <> 0 then ProcessTextNode(Node as TTextNode);
CDATA_SECTION_NODE: if (FWhatToShow and SHOW_CDATA_SECTION) <> 0 then ProcessCDataSection(Node as TCDataSection);
ENTITY_REFERENCE_NODE: if (FWhatToShow and SHOW_ENTITY_REFERENCE) <> 0 then ProcessEntityReference(Node as TEntityReference);
PROCESSING_INSTRUCTION_NODE: if (FWhatToShow and SHOW_PROCESSING_INSTRUCTION) <> 0 then ProcessProcessingInstruction(Node as TProcessingInstruction);
COMMENT_NODE: if (FWhatToShow and SHOW_COMMENT) <> 0 then ProcessComment(Node as TComment);
// NOTATION_NODE: if (FWhatToShow and SHOW_NOTATION) <> 0 then ProcessNotation(Node as Notation)
end
end;
procedure TBaseFormatter.AppendNewLine;
begin
if FStringBuilder.Length > 0 then
begin
if not FStringBuilder.TailMatch(CRLF) then FStringBuilder.AppendText(CRLF)
end;
end;
procedure TBaseFormatter.AppendParagraph;
begin
if FStringBuilder.Length > 0 then
begin
if not FStringBuilder.TailMatch(CRLF) then FStringBuilder.AppendText(PARAGRAPH_SEPARATOR)
else if not FStringBuilder.TailMatch(PARAGRAPH_SEPARATOR) then FStringBuilder.AppendText(CRLF);
end
end;
procedure TBaseFormatter.AppendText(const TextStr: WideString);
begin
FStringBuilder.AppendText(TextStr);
end;
procedure TBaseFormatter.ProcessAttribute(Attr: TAttr);
var
I: Integer;
begin
for I := 0 to Attr.ChildNodes.Count - 1 do ProcessNode(Attr.ChildNodes.Items[I]);
end;
procedure TBaseFormatter.ProcessAttributes(Element: TElement);
var
I: Integer;
begin
if (FWhatToShow and SHOW_ATTRIBUTE) <> 0 then
begin
FInAttributes := true;
for I := 0 to Element.Attributes.Count - 1 do ProcessAttribute(Element.Attributes.Items[I] as TAttr);
FInAttributes := False;
end
end;
procedure TBaseFormatter.ProcessCDataSection(CDataSection: TCDataSection);
begin
// TODO
end;
procedure TBaseFormatter.ProcessComment(Comment: TComment);
begin
AppendText('<!--');
AppendText(Comment.Data);
AppendText('-->');
end;
procedure TBaseFormatter.ProcessDocumentElement;
begin
if Assigned(FDocument.DocumentElement) then
begin
FDepth := 0;
ProcessElement(FDocument.DocumentElement)
end;
end;
procedure TBaseFormatter.ProcessElement(Element: TElement);
var
I: Integer;
begin
Inc(FDepth);
for I := 0 to Element.ChildNodes.Count - 1 do ProcessNode(Element.ChildNodes.Items[I]);
Dec(FDepth)
end;
procedure TBaseFormatter.ProcessEntityReference(EntityReference: TEntityReference);
begin
if FExpandEntities then AppendText(GetEntValue(EntityReference.Name))
else AppendText('&' + EntityReference.Name + ';');
end;
{
procedure TBaseFormatter.ProcessNotation(Notation: TNotation);
begin
// TODO
end;
}
procedure TBaseFormatter.ProcessProcessingInstruction(ProcessingInstruction: TProcessingInstruction);
begin
// TODO
end;
procedure TBaseFormatter.ProcessTextNode(TextNode: TTextNode);
begin
AppendText(TextNode.Data);
end;
function TBaseFormatter.getText(document: TDocument): WideString;
begin
FDocument := document;
FStringBuilder := TStringBuilder.Create(65530);
try
ProcessDocumentElement;
Result := FStringBuilder.ToString;
finally
FStringBuilder.Free;
end
end;
constructor THtmlFormatter.Create;
begin
inherited Create;
FIndent := 2;
end;
function THtmlFormatter.OnlyTextContent(Element: TElement): Boolean;
var
I: Integer;
Node: TNode;
begin
Result := False;
for I := 0 to Element.ChildNodes.Count - 1 do
begin
Node := Element.ChildNodes.Items[I];
if not (Node.NodeType in [TEXT_NODE, ENTITY_REFERENCE_NODE]) then Exit
end;
Result := True;
end;
procedure THtmlFormatter.ProcessAttribute(Attr: TAttr);
begin
if Attr.HasChildNodes then
begin
AppendText(' ' + Attr.Name + '="');
inherited ProcessAttribute(Attr);
AppendText('"');
end
else AppendText(' ' + Attr.Name + '="' + Attr.Name + '"');
end;
procedure THtmlFormatter.ProcessComment(Comment: TComment);
begin
AppendNewLine;
AppendText(Spaces(FIndent * FDepth));
inherited ProcessComment(Comment)
end;
procedure THtmlFormatter.ProcessElement(Element: TElement);
var
HTMLTag: THTMLTag;
begin
HTMLTag := FHTMLTagList.GetTagByName(Element.TagName);
AppendNewLine;
AppendText(Spaces(FIndent * FDepth));
AppendText('<' + Element.TagName);
ProcessAttributes(Element);
if Element.HasChildNodes then
begin
AppendText('>');
if HTMLTag.Number in PreserveWhiteSpaceTags then FPreserveWhiteSpace := True;
inherited ProcessElement(Element);
FPreserveWhiteSpace := False;
if not OnlyTextContent(Element) then
begin
AppendNewLine;
AppendText(Spaces(FIndent * FDepth))
end;
AppendText('</' + Element.TagName + '>')
end
else AppendText(' />');
end;
procedure THtmlFormatter.ProcessTextNode(TextNode: TTextNode);
var
TextStr: WideString;
begin
if FPreserveWhiteSpace then AppendText(TextNode.Data)
else
begin
TextStr := normalizeWhiteSpace(TextNode.Data);
if TextStr <> ' ' then AppendText(TextStr)
end;
end;
constructor TTextFormatter.Create;
begin
inherited Create;
FWhatToShow := SHOW_ELEMENT or SHOW_TEXT or SHOW_ENTITY_REFERENCE;
FExpandEntities := True;
end;
function TTextFormatter.GetAnchorText(Node: TElement): WideString;
var
Attr: TAttr;
begin
Result := '';
if Node.HasAttribute('href') then
begin
Attr := Node.GetAttributeNode('href');
Result := ' ';
if UrlSchemes.GetScheme(Attr.Value) = '' then Result := Result + 'http://';
Result := Result + Attr.Value;
end
end;
function TTextFormatter.GetImageText(Node: TElement): WideString;
begin
if Node.HasAttribute('alt') then Result := Node.GetAttributeNode('alt').Value
else Result := '';
end;
procedure TTextFormatter.AppendText(const TextStr: WideString);
begin
if (FStringBuilder.Length = 0)
or FStringBuilder.EndWithWhiteSpace then
begin
inherited AppendText(TrimLeftSpaces(TextStr))
end
else
begin
inherited AppendText(TextStr);
end;
end;
procedure TTextFormatter.ProcessElement(Element: TElement);
var
HTMLTag: THTMLTag;
begin
HTMLTag := FHTMLTagList.GetTagByName(Element.TagName);
if HTMLTag.Number in ViewAsBlockTags then AppendParagraph;
case HTMLTag.Number of
A_TAG: FInsideAnchor := True;
LI_TAG: AppendText('* ');
end;
if HTMLTag.Number in PreserveWhiteSpaceTags then FPreserveWhiteSpace := True;
inherited ProcessElement(Element);
FPreserveWhiteSpace := False;
case HTMLTag.Number of
BR_TAG:
AppendNewLine;
A_TAG:
begin
AppendText(GetAnchorText(Element));
FInsideAnchor := false
end;
IMG_TAG:
begin
if FInsideAnchor then AppendText(GetImageText(Element));
end
end;
if HTMLTag.Number in ViewAsBlockTags then AppendParagraph;
end;
procedure TTextFormatter.ProcessEntityReference(EntityReference: TEntityReference);
begin
if EntityReference.Name = 'nbsp' then AppendText(' ')
else inherited ProcessEntityReference(EntityReference);
end;
procedure TTextFormatter.ProcessTextNode(TextNode: TTextNode);
begin
if FPreserveWhiteSpace then AppendText(TextNode.Data)
else AppendText(normalizeWhiteSpace(TextNode.Data));
end;
end.