-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmltortf.pas
More file actions
1122 lines (1063 loc) · 29.1 KB
/
Copy pathhtmltortf.pas
File metadata and controls
1122 lines (1063 loc) · 29.1 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
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-----------------------------------------------------------------------------------
// RichKit Package © 2026 by Alexander Tverskoy
// Licensed under the MIT License
// You may obtain a copy of the License at https://opensource.org/licenses/MIT
//-----------------------------------------------------------------------------------
unit HtmlToRtf;
{$mode objfpc}{$H+}
interface
// Convert HTML string to RTF.
// If AsFragment is True, returns only RTF content without header/footer.
// If UseInlineFormatting is True, inline formatting tags (b, i, u, strike)
// are emitted as RTF control words instead of groups.
function ConvertHtmlToRtf(const Html: string; DefaultFontSizePt: integer; AsFragment: boolean = False;
UseInlineFormatting: boolean = False): string;
implementation
uses
SysUtils, Classes, StrUtils, LazUTF8;
const
MaxListLevel = 9; // Maximum nesting level for lists
type
TFormatState = record
Bold: integer;
Italic: integer;
Underline: integer;
Code: integer;
Pre: integer;
Link: integer;
LinkUrl: string;
Strike: integer; // Strikethrough level
FontIndex: integer; // Index of current font in FFontTable
FontSize: integer; // Font size in half-points, 0 means default
end;
TListInfo = record
ListType: string; // 'ul' or 'ol'
Counter: integer;
end;
THtmlParser = class
private
FHtml: string;
FPos: integer;
FResult: string;
FFormat: TFormatState;
FFontTable: TStringList; // Font names used in RTF
FListStack: array[0..MaxListLevel] of TListInfo;
FListLevel: integer;
FInTag: boolean;
FInComment: boolean;
FInSkipTag: string;
FSkipDepth: integer;
FTagContent: string;
FTextBuffer: string;
FLastWasBlock: boolean;
FPendingSpace: boolean;
FDefaultFontSize: integer; // Default font size in half-points (pt * 2)
FAsFragment: boolean; // If True, output only RTF body without wrapping
FUseInlineFormatting: boolean; // If True, inline formatting uses control words
procedure Parse;
procedure ProcessText(const S: string);
procedure FlushTextBuffer;
procedure ProcessTag(const Tag: string);
procedure AddText(const AText: string; Format: TFormatState);
function EscapeRTF(const S: string; PreMode: boolean): string;
function EscapeAsciiRTF(const S: string): string;
function EscapeUnicode(const S: string): string;
function DecodeEntities(const S: string): string;
procedure AddParIfNeeded;
function IsOnlySpaces(const S: string): boolean;
function NormalizeSpaces(const S: string): string;
function ExtractAttribute(const Tag, Attr: string): string;
function ExtractFontFamily(const Style: string): string;
procedure ProcessOpeningTag(const Name, AttrString: string);
procedure ProcessClosingTag(const Name: string);
procedure IncreaseListLevel(const ListType: string; StartValue: integer);
procedure DecreaseListLevel;
procedure AddListItemMarker;
function ExtractFontSize(const Style: string): integer;
public
constructor Create(const Html: string; ADefaultFontSizePt: integer; AAsFragment: boolean; AUseInlineFormatting: boolean);
destructor Destroy; override;
function Convert: string;
end;
constructor THtmlParser.Create(const Html: string; ADefaultFontSizePt: integer; AAsFragment: boolean; AUseInlineFormatting: boolean);
var
i: integer;
begin
inherited Create;
FHtml := Html;
FDefaultFontSize := ADefaultFontSizePt * 2;
FPos := 1;
FResult := '';
FFormat.Bold := 0;
FFormat.Italic := 0;
FFormat.Underline := 0;
FFormat.Code := 0;
FFormat.Pre := 0;
FFormat.Link := 0;
FFormat.LinkUrl := '';
FFormat.Strike := 0;
FFormat.FontIndex := 0; // default Arial
FFormat.FontSize := 0; // Default size, will use RTF default
FAsFragment := AAsFragment;
FUseInlineFormatting := AUseInlineFormatting;
FFontTable := TStringList.Create;
FFontTable.Add('Arial'); // index 0
FFontTable.Add('Courier New'); // index 1
FListLevel := 0;
for i := 0 to MaxListLevel do
begin
FListStack[i].ListType := '';
FListStack[i].Counter := 0;
end;
FInTag := False;
FInComment := False;
FInSkipTag := '';
FSkipDepth := 0;
FTagContent := '';
FTextBuffer := '';
FLastWasBlock := False;
FPendingSpace := False;
end;
destructor THtmlParser.Destroy;
begin
FFontTable.Free;
inherited Destroy;
end;
function THtmlParser.Convert: string;
var
FontTbl: string;
i: integer;
begin
Parse;
if FAsFragment then
begin
// Return only RTF body without wrapper and font table
Result := FResult;
Exit;
end;
// Build font table from used fonts
FontTbl := '';
for i := 0 to FFontTable.Count - 1 do
begin
if Trim(FFontTable[i]) = '' then
FFontTable[i] := 'Arial'; // fallback to avoid empty font name
FontTbl := FontTbl + '{\f' + IntToStr(i) + '\fnil\fcharset0 ' + FFontTable[i] + ';}';
end;
Result := '{\rtf1\ansi\ansicpg1251\deff0' + '{\fonttbl' + FontTbl + '}' + '{\colortbl ;\red0\green0\blue255;}' +
'\fs' + IntToStr(FDefaultFontSize) + ' ' + FResult + '}';
end;
procedure THtmlParser.Parse;
var
ch: char;
len: integer;
begin
while FPos <= Length(FHtml) do
begin
ch := FHtml[FPos];
if FInSkipTag <> '' then
begin
len := Length(FInSkipTag);
if (FPos + len <= Length(FHtml)) and (LowerCase(Copy(FHtml, FPos, len)) = FInSkipTag) then
begin
FSkipDepth := 0;
FInSkipTag := '';
Inc(FPos, len);
Continue;
end;
Inc(FPos);
Continue;
end;
if FInComment then
begin
if (FPos + 2 <= Length(FHtml)) and (Copy(FHtml, FPos, 3) = '-->') then
begin
FInComment := False;
Inc(FPos, 3);
end
else
Inc(FPos);
Continue;
end;
if FInTag then
begin
if ch = '>' then
begin
ProcessTag(FTagContent);
FTagContent := '';
FInTag := False;
Inc(FPos);
Continue;
end
else if ch = '"' then
begin
FTagContent := FTagContent + ch;
Inc(FPos);
while (FPos <= Length(FHtml)) and (FHtml[FPos] <> '"') do
begin
FTagContent := FTagContent + FHtml[FPos];
Inc(FPos);
end;
if FPos <= Length(FHtml) then
begin
FTagContent := FTagContent + '"';
Inc(FPos);
end;
Continue;
end
else
begin
FTagContent := FTagContent + ch;
Inc(FPos);
Continue;
end;
end;
// Not inside tag or comment
if ch = '<' then
begin
if (FPos + 3 <= Length(FHtml)) and (Copy(FHtml, FPos, 4) = '<!--') then
begin
FlushTextBuffer;
FInComment := True;
Inc(FPos, 4);
Continue;
end;
FlushTextBuffer;
FInTag := True;
FTagContent := '';
Inc(FPos);
Continue;
end;
FTextBuffer := FTextBuffer + ch;
Inc(FPos);
end;
FlushTextBuffer;
end;
procedure THtmlParser.FlushTextBuffer;
begin
if FTextBuffer <> '' then
begin
ProcessText(FTextBuffer);
FTextBuffer := '';
end;
end;
procedure THtmlParser.ProcessText(const S: string);
var
Decoded: string;
Normalized: string;
HasLeadingSpace: boolean;
HasTrailingSpace: boolean;
Line: string;
k: integer;
begin
Decoded := DecodeEntities(S);
if FFormat.Pre > 0 then
begin
// In pre, preserve line breaks and spaces exactly
Decoded := StringReplace(Decoded, #13#10, #10, [rfReplaceAll]);
Decoded := StringReplace(Decoded, #13, #10, [rfReplaceAll]);
Line := '';
for k := 1 to Length(Decoded) do
begin
if Decoded[k] = #10 then
begin
if Line <> '' then
AddText(Line, FFormat);
FResult := FResult + '\par ';
Line := '';
end
else
Line := Line + Decoded[k];
end;
if Line <> '' then
AddText(Line, FFormat);
FPendingSpace := False;
FLastWasBlock := False;
end
else
begin
HasLeadingSpace := (Decoded <> '') and (Decoded[1] in [' ', #9, #10, #13, #12]);
HasTrailingSpace := (Decoded <> '') and (Decoded[Length(Decoded)] in [' ', #9, #10, #13, #12]);
if IsOnlySpaces(Decoded) then
begin
if not FLastWasBlock then
FPendingSpace := True;
Exit;
end;
Normalized := NormalizeSpaces(Decoded);
if Normalized <> '' then
begin
if (FPendingSpace or HasLeadingSpace) and (not FLastWasBlock) then
Normalized := ' ' + Normalized;
AddText(Normalized, FFormat);
FPendingSpace := HasTrailingSpace;
FLastWasBlock := False;
end
else
begin
FPendingSpace := HasTrailingSpace;
end;
end;
end;
function THtmlParser.IsOnlySpaces(const S: string): boolean;
var
i: integer;
begin
Result := True;
for i := 1 to Length(S) do
if not (S[i] in [' ', #9, #10, #13, #12]) then
begin
Result := False;
Exit;
end;
end;
function THtmlParser.NormalizeSpaces(const S: string): string;
var
i: integer;
inSpace: boolean;
ch: char;
begin
Result := '';
inSpace := False;
for i := 1 to Length(S) do
begin
ch := S[i];
if (ch = ' ') or (ch = #9) or (ch = #10) or (ch = #13) or (ch = #12) then
begin
if not inSpace then
begin
if Result <> '' then
Result := Result + ' ';
inSpace := True;
end;
end
else
begin
Result := Result + ch;
inSpace := False;
end;
end;
Result := Trim(Result);
end;
procedure THtmlParser.AddText(const AText: string; Format: TFormatState);
var
Escaped: string;
fmtPrefix: string;
begin
Escaped := EscapeRTF(AText, Format.Pre > 0);
// Build a single formatting prefix group.
// Inline formatting commands are not added here if UseInlineFormatting is active.
fmtPrefix := '';
if Format.FontIndex > 0 then
fmtPrefix := fmtPrefix + '\f' + IntToStr(Format.FontIndex) + ' ';
if Format.Code > 0 then
fmtPrefix := fmtPrefix + '\f1 ';
if Format.FontSize > 0 then
fmtPrefix := fmtPrefix + '\fs' + IntToStr(Format.FontSize) + ' ';
if not FUseInlineFormatting then
begin
if Format.Bold > 0 then
fmtPrefix := fmtPrefix + '\b ';
if Format.Italic > 0 then
fmtPrefix := fmtPrefix + '\i ';
if Format.Underline > 0 then
fmtPrefix := fmtPrefix + '\ul ';
if Format.Strike > 0 then
fmtPrefix := fmtPrefix + '\strike ';
end;
if Format.Link > 0 then
fmtPrefix := fmtPrefix + '\ul\cf1 '; // Link styling, kept as group
if fmtPrefix <> '' then
Escaped := '{' + fmtPrefix + ' ' + Escaped + '}';
FResult := FResult + Escaped;
end;
function THtmlParser.EscapeUnicode(const S: string): string;
begin
Result := EscapeRTF(S, False);
end;
function THtmlParser.EscapeAsciiRTF(const S: string): string;
begin
Result := S;
Result := StringReplace(Result, '\', '\\', [rfReplaceAll]);
Result := StringReplace(Result, '{', '\{', [rfReplaceAll]);
Result := StringReplace(Result, '}', '\}', [rfReplaceAll]);
end;
function THtmlParser.EscapeRTF(const S: string; PreMode: boolean): string;
var
i: integer;
ch: char;
asciiPart: string;
cp: integer;
len: integer;
begin
Result := '';
asciiPart := '';
i := 1;
while i <= Length(S) do
begin
ch := S[i];
if Ord(ch) < 128 then
begin
asciiPart := asciiPart + ch;
Inc(i);
end
else
begin
if asciiPart <> '' then
begin
Result := Result + EscapeAsciiRTF(asciiPart);
asciiPart := '';
end;
{$NOTES OFF}
len := UTF8CodepointSize(@S[i]);
{$NOTES ON}
if len = 0 then len := 1;
cp := UTF8CodepointToUnicode(@S[i], len);
Result := Result + '\u' + IntToStr(cp) + '?';
Inc(i, len);
end;
end;
if asciiPart <> '' then
Result := Result + EscapeAsciiRTF(asciiPart);
end;
function THtmlParser.DecodeEntities(const S: string): string;
var
i, j: integer;
entity: string;
numStr: string;
codePoint: integer;
outStr: string;
begin
outStr := '';
i := 1;
while i <= Length(S) do
begin
if S[i] = '&' then
begin
j := PosEx(';', S, i);
if j > 0 then
begin
entity := LowerCase(Copy(S, i, j - i + 1));
if entity = ' ' then
outStr := outStr + ' '
else if entity = '&' then
outStr := outStr + '&'
else if entity = '<' then
outStr := outStr + '<'
else if entity = '>' then
outStr := outStr + '>'
else if entity = '"' then
outStr := outStr + '"'
else if entity = ''' then
outStr := outStr + ''''
else if entity = '©' then
outStr := outStr + '©'
else if entity = '®' then
outStr := outStr + '®'
else if entity = '™' then
outStr := outStr + '™'
else if entity = '…' then
outStr := outStr + '…'
else if entity = '–' then
outStr := outStr + '–'
else if entity = '—' then
outStr := outStr + '—'
else if entity = '«' then
outStr := outStr + '«'
else if entity = '»' then
outStr := outStr + '»'
else if entity = '“' then
outStr := outStr + '“'
else if entity = '”' then
outStr := outStr + '”'
else if entity = '‘' then
outStr := outStr + '‘'
else if entity = '’' then
outStr := outStr + '’'
else if (Length(entity) > 3) and (entity[2] = '#') then
begin
numStr := Copy(entity, 3, Length(entity) - 3);
if numStr <> '' then
begin
if numStr[1] = 'x' then
codePoint := StrToIntDef('$' + Copy(numStr, 2, Length(numStr) - 1), 0)
else
codePoint := StrToIntDef(numStr, 0);
if codePoint > 0 then
outStr := outStr + UTF8Encode(widechar(codePoint));
end;
end
else
begin
outStr := outStr + Copy(S, i, j - i + 1);
end;
i := j + 1;
end
else
begin
outStr := outStr + '&';
Inc(i);
end;
end
else
begin
outStr := outStr + S[i];
Inc(i);
end;
end;
Result := outStr;
end;
procedure THtmlParser.AddParIfNeeded;
begin
if (FResult <> '') and (not FLastWasBlock) then
begin
FResult := FResult + '\par ';
end;
// Always mark as block after paragraph break or block start
FLastWasBlock := True;
FPendingSpace := False;
end;
function THtmlParser.ExtractAttribute(const Tag, Attr: string): string;
var
lowerTag, lowerAttr: string;
attrPos, eqPos, startPos, endPos: integer;
quoteChar: char;
begin
Result := '';
lowerTag := LowerCase(Tag);
lowerAttr := LowerCase(Attr) + '=';
attrPos := Pos(lowerAttr, lowerTag);
while attrPos > 0 do
begin
if (attrPos = 1) or (lowerTag[attrPos - 1] in [' ', #9, #10, #13]) then
break;
attrPos := PosEx(lowerAttr, lowerTag, attrPos + 1);
end;
if attrPos = 0 then Exit;
eqPos := attrPos + Length(lowerAttr);
while (eqPos <= Length(Tag)) and (Tag[eqPos] in [' ', #9, #10, #13]) do
Inc(eqPos);
if eqPos > Length(Tag) then Exit;
quoteChar := Tag[eqPos];
if quoteChar in ['''', '"'] then
begin
Inc(eqPos);
startPos := eqPos;
while (eqPos <= Length(Tag)) and (Tag[eqPos] <> quoteChar) do
Inc(eqPos);
endPos := eqPos - 1;
end
else
begin
startPos := eqPos;
while (eqPos <= Length(Tag)) and not (Tag[eqPos] in [' ', #9, #10, #13, '>']) do
Inc(eqPos);
endPos := eqPos - 1;
end;
Result := Copy(Tag, startPos, endPos - startPos + 1);
end;
function THtmlParser.ExtractFontFamily(const Style: string): string;
var
lower: string;
pos1, pos2: integer;
begin
Result := '';
lower := LowerCase(Style);
pos1 := Pos('font-family:', lower);
if pos1 = 0 then Exit;
pos1 := pos1 + Length('font-family:');
// Skip spaces
while (pos1 <= Length(Style)) and (Style[pos1] in [' ', #9]) do Inc(pos1);
if pos1 > Length(Style) then Exit;
// Find end of value (semicolon or end)
pos2 := PosEx(';', Style, pos1);
if pos2 = 0 then pos2 := Length(Style) + 1;
Result := Copy(Style, pos1, pos2 - pos1);
// Remove quotes
Result := Trim(Result);
if (Length(Result) >= 2) and (Result[1] in ['''', '"']) and (Result[Length(Result)] = Result[1]) then
Result := Copy(Result, 2, Length(Result) - 2);
// Take first family before comma
pos1 := Pos(',', Result);
if pos1 > 0 then
Result := Trim(Copy(Result, 1, pos1 - 1));
end;
procedure THtmlParser.ProcessTag(const Tag: string);
var
lowerTag, Name, attrStr: string;
closeTag: boolean;
i: integer;
begin
lowerTag := Trim(Tag);
if lowerTag = '' then Exit;
if lowerTag[1] = '/' then
begin
closeTag := True;
lowerTag := Copy(lowerTag, 2, Length(lowerTag) - 1);
end
else
closeTag := False;
if (Length(lowerTag) > 0) and (lowerTag[Length(lowerTag)] = '/') then
lowerTag := Copy(lowerTag, 1, Length(lowerTag) - 1);
i := 1;
while (i <= Length(lowerTag)) and not (lowerTag[i] in [' ', #9, #10, #13]) do
Inc(i);
Name := LowerCase(Copy(lowerTag, 1, i - 1));
attrStr := Trim(Copy(lowerTag, i, Length(lowerTag) - i + 1));
if closeTag then
ProcessClosingTag(Name)
else
ProcessOpeningTag(Name, attrStr);
end;
procedure THtmlParser.ProcessOpeningTag(const Name, AttrString: string);
var
StartAttr: string;
StyleStr: string;
FontName: string;
FontIdx: integer;
SizeAttr: string;
SizeVal: integer;
SizeStr: string;
FontSizeVal: integer;
begin
if Name = 'b' then
begin
Inc(FFormat.Bold);
if FUseInlineFormatting then FResult := FResult + '\b ';
end
else if Name = 'strong' then
begin
Inc(FFormat.Bold);
if FUseInlineFormatting then FResult := FResult + '\b ';
end
else if Name = 'i' then
begin
Inc(FFormat.Italic);
if FUseInlineFormatting then FResult := FResult + '\i ';
end
else if Name = 'em' then
begin
Inc(FFormat.Italic);
if FUseInlineFormatting then FResult := FResult + '\i ';
end
else if Name = 'u' then
begin
Inc(FFormat.Underline);
if FUseInlineFormatting then FResult := FResult + '\ul ';
end
else if Name = 'ins' then
begin
Inc(FFormat.Underline);
if FUseInlineFormatting then FResult := FResult + '\ul ';
end
else if (Name = 'del') or (Name = 's') or (Name = 'strike') then
begin
Inc(FFormat.Strike);
if FUseInlineFormatting then FResult := FResult + '\strike ';
end
else if Name = 'code' then Inc(FFormat.Code)
else if Name = 'kbd' then Inc(FFormat.Code)
else if Name = 'samp' then Inc(FFormat.Code)
else if Name = 'var' then Inc(FFormat.Code)
else if Name = 'pre' then Inc(FFormat.Pre)
else if Name = 'a' then
begin
if FFormat.Link = 0 then
begin
FFormat.LinkUrl := ExtractAttribute(AttrString, 'href');
end;
Inc(FFormat.Link);
end
else if Name = 'span' then
begin
// Handle inline style with font-family
StyleStr := ExtractAttribute(AttrString, 'style');
if StyleStr <> '' then
begin
FontName := ExtractFontFamily(StyleStr);
if FontName <> '' then
begin
FontIdx := FFontTable.IndexOf(FontName);
if FontIdx < 0 then
begin
FFontTable.Add(FontName);
FontIdx := FFontTable.Count - 1;
end;
FFormat.FontIndex := FontIdx;
end;
end;
// Handle inline style with font-size
SizeStr := ExtractAttribute(AttrString, 'style');
if SizeStr <> '' then
begin
FontSizeVal := ExtractFontSize(SizeStr);
if FontSizeVal > 0 then
// Convert pt to half-points
FFormat.FontSize := FontSizeVal * 2;
end;
end
else if Name = 'font' then
begin
// Handle <font face="...">
FontName := ExtractAttribute(AttrString, 'face');
if FontName <> '' then
begin
FontIdx := FFontTable.IndexOf(FontName);
if FontIdx < 0 then
begin
FFontTable.Add(FontName);
FontIdx := FFontTable.Count - 1;
end;
FFormat.FontIndex := FontIdx;
end;
// Handle inline style with font-size
SizeStr := ExtractAttribute(AttrString, 'style');
if SizeStr <> '' then
begin
FontSizeVal := ExtractFontSize(SizeStr);
if FontSizeVal > 0 then
// Convert pt to half-points
FFormat.FontSize := FontSizeVal * 2;
end;
// Handle <font size="...">
// HTML size can be 1..7 or relative like +1, -1, but we accept only absolute numbers for simplicity
// Convert HTML size (1..7) to half-points roughly: 8pt, 10pt, 12pt, 14pt, 18pt, 24pt, 36pt
// 1 => 16, 2 => 20, 3 => 24, 4 => 28, 5 => 36, 6 => 48, 7 => 72
SizeAttr := ExtractAttribute(AttrString, 'size');
if SizeAttr <> '' then
begin
SizeVal := StrToIntDef(SizeAttr, 0);
if SizeVal > 0 then
begin
case SizeVal of
1: FFormat.FontSize := FDefaultFontSize - 8; // base - 4pt
2: FFormat.FontSize := FDefaultFontSize - 4; // base - 2pt
3: FFormat.FontSize := FDefaultFontSize; // base
4: FFormat.FontSize := FDefaultFontSize + 4; // base + 2pt
5: FFormat.FontSize := FDefaultFontSize + 8; // base + 4pt
6: FFormat.FontSize := FDefaultFontSize + 12; // base + 6pt
7: FFormat.FontSize := FDefaultFontSize + 16; // base + 8pt
end;
end;
end;
end
else if Name = 'p' then
begin
AddParIfNeeded;
end
else if Name = 'div' then
begin
AddParIfNeeded;
end
else if Name = 'br' then
begin
FResult := FResult + '\par ';
FLastWasBlock := True;
FPendingSpace := False;
end
else if Name = 'hr' then
begin
AddParIfNeeded;
FResult := FResult + '\pard\brdrb\brdrs\brdrw10\brsp20 \par ';
FLastWasBlock := True;
FPendingSpace := False;
end
else if (Name = 'h1') or (Name = 'h2') or (Name = 'h3') or (Name = 'h4') or (Name = 'h5') or (Name = 'h6') then
begin
AddParIfNeeded;
Inc(FFormat.Bold);
if FUseInlineFormatting then FResult := FResult + '\b ';
// Set heading sizes relative to default font size.
// Additions are in half-points: 12pt -> 24, 8pt -> 16, 6pt -> 12, 4pt -> 8, 2pt -> 4, 1pt -> 2
if Name = 'h1' then FFormat.FontSize := FDefaultFontSize + 24
else if Name = 'h2' then FFormat.FontSize := FDefaultFontSize + 16
else if Name = 'h3' then FFormat.FontSize := FDefaultFontSize + 12
else if Name = 'h4' then FFormat.FontSize := FDefaultFontSize + 8
else if Name = 'h5' then FFormat.FontSize := FDefaultFontSize + 4
else if Name = 'h6' then FFormat.FontSize := FDefaultFontSize + 2;
end
else if Name = 'ul' then
IncreaseListLevel('ul', 0)
else if Name = 'ol' then
begin
StartAttr := ExtractAttribute(AttrString, 'start');
if StartAttr <> '' then
IncreaseListLevel('ol', StrToIntDef(StartAttr, 1))
else
IncreaseListLevel('ol', 1);
end
else if Name = 'li' then
begin
if FListLevel > 0 then
begin
if FResult <> '' then
AddParIfNeeded;
AddListItemMarker;
end;
end
else if Name = 'table' then
begin
AddParIfNeeded;
end
else if Name = 'tr' then
begin
AddParIfNeeded;
end
else if Name = 'td' then
begin
FResult := FResult + '\tab ';
FLastWasBlock := False;
end
else if Name = 'th' then
begin
FResult := FResult + '\tab ';
FLastWasBlock := False;
end
else if Name = 'head' then
begin
FInSkipTag := '</head>';
FSkipDepth := 1;
end
else if Name = 'script' then
begin
FInSkipTag := '</script>';
FSkipDepth := 1;
end
else if Name = 'style' then
begin
FInSkipTag := '</style>';
FSkipDepth := 1;
end
else if Name = 'svg' then
begin
FInSkipTag := '</svg>';
FSkipDepth := 1;
end
else if Name = 'noscript' then
begin
FInSkipTag := '</noscript>';
FSkipDepth := 1;
end;
end;
procedure THtmlParser.ProcessClosingTag(const Name: string);
begin
if Name = 'b' then
begin
if FFormat.Bold > 0 then
begin
Dec(FFormat.Bold);
if FUseInlineFormatting then FResult := FResult + '\b0';
end;
end
else if Name = 'strong' then
begin
if FFormat.Bold > 0 then
begin
Dec(FFormat.Bold);
if FUseInlineFormatting then FResult := FResult + '\b0';
end;
end
else if Name = 'i' then
begin
if FFormat.Italic > 0 then
begin
Dec(FFormat.Italic);
if FUseInlineFormatting then FResult := FResult + '\i0';
end;
end
else if Name = 'em' then
begin
if FFormat.Italic > 0 then
begin
Dec(FFormat.Italic);
if FUseInlineFormatting then FResult := FResult + '\i0';
end;
end
else if Name = 'u' then
begin
if FFormat.Underline > 0 then
begin
Dec(FFormat.Underline);
if FUseInlineFormatting then FResult := FResult + '\ul0';
end;
end
else if Name = 'ins' then
begin
if FFormat.Underline > 0 then
begin
Dec(FFormat.Underline);
if FUseInlineFormatting then FResult := FResult + '\ul0';
end;
end
else if (Name = 'del') or (Name = 's') or (Name = 'strike') then
begin
if FFormat.Strike > 0 then
begin
Dec(FFormat.Strike);
if FUseInlineFormatting then FResult := FResult + '\strike0';
end;
end
else if Name = 'code' then
begin
if FFormat.Code > 0 then Dec(FFormat.Code);
end
else if Name = 'kbd' then
begin
if FFormat.Code > 0 then Dec(FFormat.Code);
end
else if Name = 'samp' then
begin
if FFormat.Code > 0 then Dec(FFormat.Code);
end
else if Name = 'var' then
begin
if FFormat.Code > 0 then Dec(FFormat.Code);
end
else if Name = 'pre' then
begin
if FFormat.Pre > 0 then Dec(FFormat.Pre);
end
else if Name = 'a' then
begin
if FFormat.Link > 0 then
begin
Dec(FFormat.Link);
if FFormat.Link = 0 then
FFormat.LinkUrl := '';
end;
end
else if Name = 'span' then
begin
// Reset font to default when closing span (simplistic approach)
FFormat.FontIndex := 0;
FFormat.FontSize := 0; // Reset to default
end
else if Name = 'font' then
begin
FFormat.FontIndex := 0;
FFormat.FontSize := 0; // Reset to default
end
else if Name = 'p' then
begin
AddParIfNeeded;
// Add an empty line after paragraph to match HTML vertical spacing
FResult := FResult + '\par ';
FLastWasBlock := True;
FPendingSpace := False;
end
else if Name = 'div' then