-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.cpp
executable file
·1253 lines (1181 loc) · 39 KB
/
html.cpp
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
/////////////////////////////////////////////////
// Html-Lexical-Chars
void THtmlLxChDef::SetUcCh(const char& UcCh, const char& LcCh){
// update upper-case (more lower cases may have one upper case)
IAssert(
(UcChV[LcCh-TCh::Mn]==TCh(0))||
(UcChV[LcCh-TCh::Mn]==TCh(LcCh)));
UcChV[LcCh-TCh::Mn]=TCh(UcCh);
// update lower-case (one upper case may have only one lower case)
if ((LcChV[UcCh-TCh::Mn]==TCh(0))||(LcChV[UcCh-TCh::Mn]==TCh(UcCh))){
LcChV[UcCh-TCh::Mn]=TCh(LcCh);
}
}
void THtmlLxChDef::SetUcCh(const TStr& Str){
// set type of characters as letters
SetChTy(hlctAlpha, Str);
// first char in string is upper-case, rest are lower-case
for (int ChN=1; ChN<Str.Len(); ChN++){
SetUcCh(Str[0], Str[ChN]);
}
}
void THtmlLxChDef::SetChTy(const THtmlLxChTy& ChTy, const TStr& Str){
for (int ChN=0; ChN<Str.Len(); ChN++){
ChTyV[Str[ChN]-TCh::Mn]=TInt(ChTy);}
}
void THtmlLxChDef::SetEscStr(const TStr& SrcStr, const TStr& DstStr){
EscStrH.AddDat(SrcStr, DstStr);
}
TStr THtmlLxChDef::GetEscStr(const TStr& Str) const {
int EscStrId;
if ((EscStrId=EscStrH.GetKeyId(Str))!=-1){
return EscStrH[EscStrId];
} else
if ((Str.Len()>=2)&&(Str[0]=='&')&&(Str[1]=='#')){
int ChCd=0;
for (int ChN=2; ChN<Str.Len(); ChN++){
if (ChCd<=0xFFFF){ChCd=ChCd*10+Str[ChN]-'0';}}
return TStr((char)ChCd);
} else {
return TStr(' ');
}
}
THtmlLxChDef::THtmlLxChDef():
ChTyV(TCh::Vals), UcChV(TCh::Vals), LcChV(TCh::Vals), EscStrH(100){
// Character-Types
ChTyV.PutAll(TInt(hlctSpace));
SetChTy(hlctAlpha, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
SetChTy(hlctAlpha, "abcdefghijklmnopqrstuvwxyz");
SetChTy(hlctAlpha, "@_");
SetChTy(hlctNum, "0123456789");
SetChTy(hlctSym, "`~!#$%^&*()-=+[{]}\\|;:'\",<.>/?");
SetChTy(hlctLTag, "<"); SetChTy(hlctRTag, ">");
SetChTy(hlctEof, TStr(TCh::EofCh));
//for (int Ch=TCh::Mn; Ch<=TCh::Mx; Ch++){
// if ((Ch<0)||(127<Ch)){SetChTy(hlctAlpha, TStr(TCh(char(Ch))));}}
//SetChTy(hlctSpace, TStr(TCh(char(160))));
// Upper-Case
{for (int Ch=TCh::Mn; Ch<=TCh::Mx; Ch++){
SetUcCh(char(Ch), char(Ch));}}
SetUcCh("Aa"); SetUcCh("Àà"); SetUcCh("Áá"); SetUcCh("Ââ");
SetUcCh("Ãã"); SetUcCh("Ää"); SetUcCh("Åå"); SetUcCh("Ææ");
SetUcCh("Bb"); SetUcCh("Cc"); SetUcCh("Çç"); SetUcCh("Dd");
SetUcCh("Ðð"); SetUcCh("Ee"); SetUcCh("Èè"); SetUcCh("Éé");
SetUcCh("Êê"); SetUcCh("Ëë"); SetUcCh("Ff"); SetUcCh("Gg");
SetUcCh("Hh"); SetUcCh("Ii"); SetUcCh("Ìì"); SetUcCh("Íí");
SetUcCh("Îî"); SetUcCh("Ïï"); SetUcCh("Jj"); SetUcCh("Kk");
SetUcCh("Ll"); SetUcCh("Mm"); SetUcCh("Nn"); SetUcCh("Ññ");
SetUcCh("Oo"); SetUcCh("Òò"); SetUcCh("Óó"); SetUcCh("Ôô");
SetUcCh("Õõ"); SetUcCh("Öö"); SetUcCh("Øø"); SetUcCh("Pp");
SetUcCh("Qq"); SetUcCh("Rr"); SetUcCh("Ss"); SetUcCh("Šš");
SetUcCh("Tt"); SetUcCh("Uu"); SetUcCh("Ùù"); SetUcCh("Úú");
SetUcCh("Ûû"); SetUcCh("Üü"); SetUcCh("Vv"); SetUcCh("Ww");
SetUcCh("Xx"); SetUcCh("Yyÿ"); SetUcCh("Ýý"); SetUcCh("Zz");
SetUcCh("Žž");
// ISO-CE
//SetUcCh(uchar(169), uchar(185)); /*Sh - ©¹*/
//SetUcCh(uchar(174), uchar(190)); /*Zh - ®¾*/
//SetUcCh(uchar(200), uchar(232)); /*Ch - Èè*/
//SetUcCh(uchar(198), uchar(230)); /*Cs - Ææ*/
//SetUcCh(uchar(208), uchar(240)); /*Dz - Ðð*/
// Annoying Unicode-characters
SetChTy(hlctSpace, "Âï");
// Escape-Sequences
SetEscStr(""", "\""); SetEscStr("&", "&");
SetEscStr("<", "<"); SetEscStr(">", ">");
SetEscStr(" ", " ");
SetEscStr("ä", "ä"); SetEscStr("Ä", "Ä");
SetEscStr("ö", "ö"); SetEscStr("Ö", "Ö");
SetEscStr("ü", "ü"); SetEscStr("Ü", "Ü");
SetEscStr("å", "å"); SetEscStr("Å", "Å");
SetEscStr("ø", "ø"); SetEscStr("Ø", "Ø");
SetEscStr("&Aelig", "Æ"); SetEscStr("æ", "æ");
SetEscStr("é", "e"); SetEscStr("É", "E");
SetEscStr("è", "e"); SetEscStr("È", "E");
SetEscStr("à", "a"); SetEscStr("À", "A");
}
PHtmlLxChDef THtmlLxChDef::ChDef=PHtmlLxChDef(new THtmlLxChDef());
TStr THtmlLxChDef::GetCSZFromYuascii(const TChA& ChA){
TChA DstChA;
for (int ChN=0; ChN<ChA.Len(); ChN++){
char Ch=ChA[ChN];
switch (Ch){
case '~': DstChA+='c'; break;
case '^': DstChA+='C'; break;
case '}': DstChA+='c'; break;
case ']': DstChA+='C'; break;
case '|': DstChA+='d'; break;
case '\\': DstChA+='D'; break;
case '{': DstChA+='s'; break;
case '[': DstChA+='S'; break;
case '`': DstChA+='z'; break;
case '@': DstChA+='Z'; break;
default: DstChA+=Ch;
}
}
return DstChA;
}
TStr THtmlLxChDef::GetCSZFromWin1250(const TChA& ChA){
TChA DstChA;
for (int ChN=0; ChN<ChA.Len(); ChN++){
char Ch=ChA[ChN];
switch (Ch){
case 232: DstChA+='c'; break;
case 200: DstChA+='C'; break;
case 154: DstChA+='s'; break;
case 138: DstChA+='S'; break;
case 158: DstChA+='z'; break;
case 142: DstChA+='Z'; break;
default: DstChA+=Ch;
}
}
return DstChA;
}
TStr THtmlLxChDef::GetWin1250FromYuascii(const TChA& ChA){
TChA DstChA;
for (int ChN=0; ChN<ChA.Len(); ChN++){
char Ch=ChA[ChN];
switch (Ch){
case '~': DstChA+=uchar(232); break;
case '^': DstChA+=uchar(200); break;
case '}': DstChA+='c'; break;
case ']': DstChA+='C'; break;
case '|': DstChA+='d'; break;
case '\\': DstChA+='D'; break;
case '{': DstChA+=uchar(154); break;
case '[': DstChA+=uchar(138); break;
case '`': DstChA+=uchar(158); break;
case '@': DstChA+=uchar(142); break;
default: DstChA+=Ch;
}
}
return DstChA;
}
TStr THtmlLxChDef::GetIsoCeFromYuascii(const TChA& ChA){
TChA DstChA;
for (int ChN=0; ChN<ChA.Len(); ChN++){
char Ch=ChA[ChN];
switch (Ch){
case '~': DstChA+=uchar(232); break;
case '^': DstChA+=uchar(200); break;
case '}': DstChA+=uchar(230); break;
case ']': DstChA+=uchar(198); break;
case '|': DstChA+=uchar(240); break;
case '\\': DstChA+=uchar(208); break;
case '{': DstChA+=uchar(185); break;
case '[': DstChA+=uchar(169); break;
case '`': DstChA+=uchar(190); break;
case '@': DstChA+=uchar(174); break;
default: DstChA+=Ch;
}
}
return DstChA;
}
/////////////////////////////////////////////////
// Html-Lexical
THtmlLxChDef THtmlLx::ChDef;
void THtmlLx::GetEscCh(){
GetCh();
EscCh=(Ch=='&');
if (EscCh){
EscChA.Clr(); EscChA.AddCh(Ch); GetCh();
if (Ch=='#'){
EscChA.AddCh(Ch); GetCh();
if (('0'<=Ch)&&(Ch<='9')){
do {EscChA.AddCh(Ch); GetCh();} while (('0'<=Ch)&&(Ch<='9'));
if (Ch==';'){GetCh();}
PutStr(ChDef.GetEscStr(EscChA));
} else {
PutCh('#'); PutCh('&');
}
} else
if (('a'<=Ch)&&(Ch<='z')||('A'<=Ch)&&(Ch<='Z')){
do {
EscChA.AddCh(Ch); GetCh();
} while (('A'<=Ch)&&(Ch<='Z')||('a'<=Ch)&&(Ch<='z')||('0'<=Ch)&&(Ch<='9'));
if (Ch==';'){GetCh();}
PutStr(ChDef.GetEscStr(EscChA));
} else {
PutCh('&');
}
}
}
void THtmlLx::GetMetaTag(){
Sym=hsyMTag;
if (Ch=='-'){
char PCh=' ';
while ((Ch!=TCh::EofCh) && ((PCh!='-')||(Ch!='>'))){PCh=Ch; GetCh();}
} else {
while ((Ch!=TCh::EofCh) && (Ch!='>')){GetCh();}
}
if (Ch!=TCh::EofCh){GetEscCh();}
}
void THtmlLx::GetTag(){
if (Ch=='/'){Sym=hsyETag; GetCh();} else {Sym=hsyBTag;}
UcChA.AddCh('<');
while (ChDef.IsAlNum(Ch)||(Ch==':')){
UcChA.AddCh(ChDef.GetUc(Ch)); GetCh();}
UcChA.AddCh('>');
ChA=UcChA;
if (DoParseArg){
while ((Ch!='>')&&(Ch!=TCh::EofCh)){
while ((!ChDef.IsAlpha(Ch))&&(Ch!='>')&&(Ch!=TCh::EofCh)){GetCh();}
if (ChDef.IsAlpha(Ch)){
ArgNm.Clr(); ArgVal.Clr();
while (ChDef.IsAlNum(Ch)||(Ch=='-')){ArgNm.AddCh(ChDef.GetUc(Ch)); GetCh();}
while (ChDef.IsWs(Ch)){GetCh();}
if (Ch=='='){
GetCh(); while (ChDef.IsWs(Ch)){GetCh();}
if (Ch=='"'){
GetCh();
while ((Ch!=TCh::EofCh)&&(Ch!='"')&&(Ch!='>')){
if (!ChDef.IsEoln(Ch)){ArgVal.AddCh(Ch);} GetCh();}
if (Ch=='"'){GetCh();}
} else if (Ch=='\''){
GetCh();
while ((Ch!=TCh::EofCh)&&(Ch!='\'')&&(Ch!='>')){
if (!ChDef.IsEoln(Ch)){ArgVal.AddCh(Ch);} GetCh();}
if (Ch=='\''){GetCh();}
} else {
while ((!ChDef.IsWs(Ch))&&(Ch!='>')&&(Ch!=TCh::EofCh)){
ArgVal.AddCh(Ch); GetCh();}
}
ArgNmValV.Add(TStrKd(ArgNm, ArgVal));
}
}
}
} else {
while ((Ch!='>')&&(Ch!=TCh::EofCh)){GetCh();}
}
if (Ch!=TCh::EofCh){GetEscCh();}
}
THtmlLxSym THtmlLx::GetSym(){
// prepare symbol descriptions
ChA.Clr(); UcChA.Clr();
PreSpaces=0; PreSpaceChA.Clr();
ArgNmValV.Clr();
// skip white-space
while (ChDef.IsSpace(Ch)){
if (ChX>0){PreSpaceChA+=Ch; PreSpaces++;} GetEscCh();}
// parse symbol
SymChA.Clr(); SymChA+=Ch; SymBChX=ChX;
switch (ChDef.GetChTy(Ch)){
case hlctAlpha:
Sym=hsyStr;
forever{
do {
ChA.AddCh(Ch); UcChA.AddCh(ChDef.GetUc(Ch)); GetEscCh();
} while (ChDef.IsAlNum(Ch));
if (Ch=='.'){
GetCh();
if (ChDef.IsAlNum(Ch)){ChA.AddCh('.'); UcChA.AddCh('.');}
else {PutCh(Ch); Ch='.'; break;}
} else {break;}
}
break;
case hlctNum:
Sym=hsyNum;
forever{
do {
ChA.AddCh(Ch); UcChA.AddCh(Ch); GetEscCh();
} while (ChDef.IsNum(Ch));
if (Ch=='.'){
GetCh();
if (ChDef.IsAlNum(Ch)){ChA.AddCh('.'); UcChA.AddCh('.');}
else {PutCh(Ch); Ch='.'; break;}
} else if (ChDef.IsAlpha(Ch)){
Sym=hsyStr;
} else {
break;
}
}
break;
case hlctSym:
Sym=hsySSym; ChA.AddCh(Ch); UcChA.AddCh(Ch); GetEscCh();
if ((ChA.LastCh()=='.')&&(ChDef.IsAlNum(Ch))){
Sym=hsyStr;
do {
ChA.AddCh(Ch); UcChA.AddCh(ChDef.GetUc(Ch)); GetEscCh();
} while (ChDef.IsAlNum(Ch));
}
break;
case hlctLTag:
if (EscCh){
Sym=hsySSym; ChA.AddCh(Ch); UcChA.AddCh(Ch); GetEscCh();
} else {
GetCh();
if (Ch=='!'){GetCh(); GetMetaTag();} else {GetTag();}
}
break;
case hlctRTag:
if (EscCh){
Sym=hsySSym; ChA.AddCh(Ch); UcChA.AddCh(Ch); GetEscCh();
} else {
Sym=hsySSym; ChA.AddCh(Ch); UcChA.AddCh(Ch); GetEscCh();
}
break;
case hlctEof: Sym=hsyEof; break;
default: Sym=hsyUndef; GetEscCh();
}
// set symbol last-character-position
SymEChX=ChX-1;
// delete last character
if (!SymChA.Empty()){SymChA.Pop();}
// return symbol
return Sym;
}
PHtmlTok THtmlLx::GetTok(const bool& DoUc){
if (DoUc){return PHtmlTok(new THtmlTok(Sym, UcChA, ArgNmValV));}
else {return PHtmlTok(new THtmlTok(Sym, ChA, ArgNmValV));}
}
TStr THtmlLx::GetFullBTagStr() const {
IAssert(Sym==hsyBTag);
TChA BTagChA;
BTagChA+=ChA; BTagChA.Pop();
for (int ArgN=0; ArgN<GetArgs(); ArgN++){
BTagChA+=' '; BTagChA+=GetArgNm(ArgN);
BTagChA+='='; BTagChA+='"'; BTagChA+=GetArgVal(ArgN); BTagChA+='"';
}
BTagChA+='>';
return BTagChA;
}
void THtmlLx::MoveToStrOrEof(const TStr& Str){
do {
GetSym();
} while ((Sym!=hsyEof)&&((Sym!=hsyStr)||(ChA!=Str)));
}
void THtmlLx::MoveToBTagOrEof(const TStr& TagNm){
do {
GetSym();
} while ((Sym!=hsyEof)&&((Sym!=hsyBTag)||(UcChA!=TagNm)));
}
void THtmlLx::MoveToBTag2OrEof(const TStr& TagNm1, const TStr& TagNm2){
do {
GetSym();
} while ((Sym!=hsyEof)&&((Sym!=hsyBTag)||((UcChA!=TagNm1)&&(UcChA!=TagNm2))));
}
void THtmlLx::MoveToBTag3OrEof(const TStr& TagNm1, const TStr& TagNm2, const TStr& TagNm3){
do {
GetSym();
} while ((Sym!=hsyEof)&&((Sym!=hsyBTag)||((UcChA!=TagNm1)&&(UcChA!=TagNm2)&&(UcChA!=TagNm3))));
}
void THtmlLx::MoveToBTagArgOrEof(
const TStr& TagNm, const TStr& ArgNm, const TStr& ArgVal){
forever {
GetSym();
if (Sym==hsyEof){break;}
if ((Sym==hsyBTag)&&(UcChA==TagNm)&&
(IsArg(ArgNm))&&(GetArg(ArgNm)==ArgVal)){break;}
}
}
void THtmlLx::MoveToBTagArg2OrEof(const TStr& TagNm,
const TStr& ArgNm1, const TStr& ArgVal1,
const TStr& ArgNm2, const TStr& ArgVal2){
forever {
GetSym();
if (Sym==hsyEof){break;}
if ((Sym==hsyBTag)&&(UcChA==TagNm)&&
(IsArg(ArgNm1))&&(GetArg(ArgNm1)==ArgVal1)&&
(IsArg(ArgNm2))&&(GetArg(ArgNm2)==ArgVal2)){break;}
}
}
void THtmlLx::MoveToBTagOrEof(
const TStr& TagNm1, const TStr& ArgNm1, const TStr& ArgVal1,
const TStr& TagNm2, const TStr& ArgNm2, const TStr& ArgVal2){
forever {
GetSym();
if (Sym==hsyEof){break;}
if ((Sym==hsyBTag)&&(UcChA==TagNm1)&&
(IsArg(ArgNm1))&&(GetArg(ArgNm1)==ArgVal1)){break;}
if ((Sym==hsyBTag)&&(UcChA==TagNm2)&&
(IsArg(ArgNm2))&&(GetArg(ArgNm2)==ArgVal2)){break;}
}
}
void THtmlLx::MoveToETagOrEof(const TStr& TagNm){
do {
GetSym();
} while ((Sym!=hsyEof)&&((Sym!=hsyETag)||(UcChA!=TagNm)));
}
TStr THtmlLx::GetStrToBTag(const TStr& TagNm, const bool& TxtOnlyP){
TChA OutChA;
forever {
GetSym();
if ((Sym==hsyEof)||((Sym==hsyBTag)&&(UcChA==TagNm))){
break;
} else {
if (PreSpaces>0){OutChA+=' ';}
if ((TxtOnlyP&&(Sym!=hsyBTag)&&(Sym!=hsyETag))||(!TxtOnlyP)){
OutChA+=ChA;}
}
}
return OutChA;
}
TStr THtmlLx::GetStrToBTag(const TStr& TagNm, const TStr& ArgNm,
const TStr& ArgVal, const bool& TxtOnlyP){
TChA OutChA;
forever {
GetSym();
if ((Sym==hsyEof)||((Sym==hsyBTag)&&(UcChA==TagNm)&&
(IsArg(ArgNm))&&(GetArg(ArgNm)==ArgVal))){
break;
} else {
if (PreSpaces>0){OutChA+=' ';}
if ((TxtOnlyP&&(Sym!=hsyBTag)&&(Sym!=hsyETag))||(!TxtOnlyP)){
OutChA+=ChA;}
}
}
return OutChA;
}
TStr THtmlLx::GetStrToETag(const TStr& TagNm, const bool& TxtOnlyP){
TChA OutChA;
forever {
GetSym();
if ((Sym==hsyEof)||((Sym==hsyETag)&&(UcChA==TagNm))){
break;
} else {
if (PreSpaces>0){OutChA+=' ';}
if ((TxtOnlyP&&(Sym!=hsyBTag)&&(Sym!=hsyETag))||(!TxtOnlyP)){
OutChA+=ChA;}
}
}
return OutChA;
}
TStr THtmlLx::GetStrInTag(const TStr& TagNm, const bool& TxtOnlyP){
MoveToBTagOrEof(TagNm);
return GetStrToETag(TagNm, TxtOnlyP);
}
TStr THtmlLx::GetHRefBeforeStr(const TStr& Str){
TStr HRefStr;
forever {
GetSym();
if (Sym==hsyEof){HRefStr=""; break;}
if ((Sym==hsyBTag)&&(UcChA=="<A>")){HRefStr=GetArg("HREF");}
if ((Sym==hsyStr)&&(ChA==Str)){break;}
}
return HRefStr;
}
bool THtmlLx::IsGetBTag(const TStr& TagNm){
if (GetSym()==hsyBTag){
return ChA==TagNm;
} else {return false;}
}
TStr THtmlLx::GetSymStr(const THtmlLxSym& Sym){
switch (Sym){
case hsyUndef: return "Undef";
case hsyStr: return "Str";
case hsyNum: return "Num";
case hsySSym: return "SSym";
case hsyUrl: return "Url";
case hsyBTag: return "BTag";
case hsyETag: return "ETag";
case hsyMTag: return "MTag";
case hsyEof: return "Eof";
default: Fail; return TStr();
}
}
TStr THtmlLx::GetEscapedStr(const TChA& ChA){
TChA EscapedChA;
for (int ChN=0; ChN<ChA.Len(); ChN++){
char Ch=ChA[ChN];
switch (Ch){
case '"': EscapedChA+="""; break;
case '&': EscapedChA+="&"; break;
case '\'': EscapedChA+="'"; break;
case '<': EscapedChA+="<"; break;
case '>': EscapedChA+=">"; break;
default: EscapedChA+=Ch;
}
}
return EscapedChA;
}
TStr THtmlLx::GetAsciiStr(const TChA& ChA, const char& GenericCh){
TChA AsciiChA;
for (int ChN=0; ChN<ChA.Len(); ChN++){
char Ch=ChA[ChN];
if ((Ch<' ')||('~'<Ch)){
Ch=GenericCh;}
AsciiChA+=Ch;
}
return AsciiChA;
}
void THtmlLx::GetTokStrV(const TStr& Str, TStrV& TokStrV){
PSIn SIn=TStrIn::New(Str);
THtmlLx Lx(SIn);
Lx.GetSym();
TokStrV.Clr();
while (Lx.Sym!=hsyEof){
TokStrV.Add(Lx.ChA);
Lx.GetSym();
}
}
/////////////////////////////////////////////////
// Html-Token
TStr THtmlTok::GetFullStr() const {
if ((Sym==hsyBTag)&&(ArgNmValV.Len()>0)){
TChA FullChA;
FullChA+=Str.GetSubStr(0, Str.Len()-2);
for (int ArgNmValN=0; ArgNmValN<ArgNmValV.Len(); ArgNmValN++){
FullChA+=' '; FullChA+=ArgNmValV[ArgNmValN].Key; FullChA+='=';
FullChA+='"'; FullChA+=ArgNmValV[ArgNmValN].Dat; FullChA+='"';
}
FullChA+='>';
return FullChA;
} else
if (Sym==hsyETag){
TChA FullChA;
FullChA+='<'; FullChA+='/'; FullChA+=Str.GetSubStr(1, Str.Len()-1);
return FullChA;
} else {
return GetStr();
}
}
bool THtmlTok::IsUrlTok(TStr& RelUrlStr) const {
if (GetSym()==hsyBTag){
TStr TagNm=GetStr();
if ((TagNm==ATagNm)&&(IsArg(HRefArgNm))){
RelUrlStr=GetArg(HRefArgNm); return true;}
else if ((TagNm==AreaTagNm)&&(IsArg(HRefArgNm))){
RelUrlStr=GetArg(HRefArgNm); return true;}
else if ((TagNm==FrameTagNm)&&(IsArg(SrcArgNm))){
RelUrlStr=GetArg(SrcArgNm); return true;}
else if ((TagNm==ImgTagNm)&&(IsArg(SrcArgNm))){
RelUrlStr=GetArg(SrcArgNm); return true;}
else if ((TagNm==MetaTagNm)&&(IsArg(HttpEquivArgNm))){
TStr HttpEquivArgVal=GetArg(HttpEquivArgNm).GetUc();
if ((HttpEquivArgVal=="REFRESH")&&IsArg("CONTENT")){
TStr ContentStr=GetArg("CONTENT");
TStr LeftStr; TStr RightStr; TStr UrlEqStr="URL=";
ContentStr.GetUc().SplitOnStr(LeftStr, UrlEqStr, RightStr);
RelUrlStr=ContentStr.GetSubStr(
LeftStr.Len()+UrlEqStr.Len(), ContentStr.Len());
return !RelUrlStr.Empty();
} else {
return false;
}
}
}
return false;
}
bool THtmlTok::IsRedirUrlTok() const {
if (GetSym()==hsyBTag){
TStr TagNm=GetStr();
if ((TagNm==MetaTagNm)&&(IsArg(HttpEquivArgNm))){
TStr HttpEquivArgVal=GetArg(HttpEquivArgNm).GetUc();
if ((HttpEquivArgVal=="REFRESH")&&IsArg("CONTENT")){
return true;
} else {
return false;
}
}
}
return false;
}
void THtmlTok::SaveTxt(const PSOut& SOut, const bool& TxtMode){
if (TxtMode){
SOut->PutStr(GetFullStr()); SOut->PutStr(" ");
} else {
SOut->PutStr(THtmlLx::GetSymStr(Sym)); SOut->PutStr(" ");
SOut->PutStr(GetFullStr()); SOut->PutStr(" ");
}
}
const TStr THtmlTok::ATagNm="<A>";
const TStr THtmlTok::AreaTagNm="<AREA>";
const TStr THtmlTok::BrTagNm="<BR>";
const TStr THtmlTok::CardTagNm="<CARD>";
const TStr THtmlTok::CenterTagNm="<CENTER>";
const TStr THtmlTok::FrameTagNm="<FRAME>";
const TStr THtmlTok::H1TagNm="<H1>";
const TStr THtmlTok::H2TagNm="<H2>";
const TStr THtmlTok::H3TagNm="<H3>";
const TStr THtmlTok::H4TagNm="<H4>";
const TStr THtmlTok::H5TagNm="<H5>";
const TStr THtmlTok::H6TagNm="<H6>";
const TStr THtmlTok::ImgTagNm="<IMG>";
const TStr THtmlTok::LiTagNm="<LI>";
const TStr THtmlTok::MetaTagNm="<META>";
const TStr THtmlTok::PTagNm="<P>";
const TStr THtmlTok::UlTagNm="<UL>";
const TStr THtmlTok::TitleTagNm="<TITLE>";
const TStr THtmlTok::TitleETagNm="</TITLE>";
const TStr THtmlTok::AltArgNm="ALT";
const TStr THtmlTok::HRefArgNm="HREF";
const TStr THtmlTok::SrcArgNm="SRC";
const TStr THtmlTok::TitleArgNm="TITLE";
const TStr THtmlTok::HttpEquivArgNm="HTTP-EQUIV";
bool THtmlTok::IsBreakTag(const TStr& TagNm){
static TStrH BreakTagNmH(50);
if (BreakTagNmH.Len()==0){
BreakTagNmH.AddKey(TStr("<H1>")); BreakTagNmH.AddKey(TStr("<H2>"));
BreakTagNmH.AddKey(TStr("<H3>")); BreakTagNmH.AddKey(TStr("<H4>"));
BreakTagNmH.AddKey(TStr("<H5>")); BreakTagNmH.AddKey(TStr("<H6>"));
BreakTagNmH.AddKey(TStr("<BR>")); BreakTagNmH.AddKey(TStr("<HR>"));
BreakTagNmH.AddKey(TStr("<P>")); BreakTagNmH.AddKey(TStr("<DL>"));
BreakTagNmH.AddKey(TStr("<UL>")); BreakTagNmH.AddKey(TStr("<OL>"));
BreakTagNmH.AddKey(TStr("<LI>")); BreakTagNmH.AddKey(TStr("<DT>"));
BreakTagNmH.AddKey(TStr("<DD>")); BreakTagNmH.AddKey(TStr("<HEAD>"));
BreakTagNmH.AddKey(TStr("<TITLE>")); BreakTagNmH.AddKey(TStr("<META>"));
BreakTagNmH.AddKey(TStr("<SCRIPT>"));
BreakTagNmH.AddKey(TStr("<HEAD>")); BreakTagNmH.AddKey(TStr("<BODY>"));
}
return BreakTagNmH.IsKey(TagNm);
}
bool THtmlTok::IsBreakTok(const PHtmlTok& Tok){
if ((Tok->GetSym()==hsyBTag)||(Tok->GetSym()==hsyETag)){
return IsBreakTag(Tok->GetStr());
} else {
return false;
}
}
bool THtmlTok::IsHTag(const TStr& TagNm, int& HTagN){
if ((TagNm.Len()==4)&&(TagNm[0]=='<')&&(TagNm[1]=='H')&&(TagNm[3]=='>')){
char Ch=TagNm[2];
if (('1'<=Ch)&&(Ch<='6')){HTagN=Ch-'0'; return true;}
else {HTagN=-1; return false;}
} else {
HTagN=-1; return false;
}
}
PHtmlTok THtmlTok::GetHTok(const bool& IsBTag, const int& HTagN){
THtmlLxSym HTagSym=IsBTag?hsyBTag:hsyETag;
TStr HTagNm;
switch (HTagN){
case 1: HTagNm=H1TagNm; break;
case 2: HTagNm=H2TagNm; break;
case 3: HTagNm=H3TagNm; break;
case 4: HTagNm=H4TagNm; break;
case 5: HTagNm=H5TagNm; break;
case 6: HTagNm=H6TagNm; break;
default: Fail;
}
return PHtmlTok(new THtmlTok(HTagSym, HTagNm));
}
/////////////////////////////////////////////////
// Html-Document
THtmlDoc::THtmlDoc(const PSIn& SIn, const THtmlDocType& Type, const bool& DoUc):
TokV(1000, 0){
THtmlLx Lx(SIn);
bool MkTok=false; bool InUL=false;
while (Lx.GetSym()!=hsyEof){
switch (Type){
case hdtAll: MkTok=true; break;
case hdtStr: MkTok=(Lx.Sym==hsyStr); break;
case hdtStrNum: MkTok=(Lx.Sym==hsyStr)||(Lx.Sym==hsyNum); break;
case hdtTag: MkTok=(Lx.Sym==hsyBTag)||(Lx.Sym==hsyETag); break;
case hdtA: MkTok=(Lx.Sym==hsyBTag)&&(Lx.UcChA==THtmlTok::ATagNm); break;
case hdtHRef:
MkTok=(Lx.Sym==hsyBTag)&&
((Lx.UcChA==THtmlTok::ATagNm)||(Lx.UcChA==THtmlTok::AreaTagNm)||
(Lx.UcChA==THtmlTok::FrameTagNm)||(Lx.UcChA==THtmlTok::ImgTagNm)||
(Lx.UcChA==THtmlTok::MetaTagNm));
break;
case hdtUL:
if ((Lx.Sym==hsyBTag)&&(Lx.UcChA==THtmlTok::UlTagNm)){InUL=true;}
MkTok=InUL;
if ((Lx.Sym==hsyETag)&&(Lx.UcChA==THtmlTok::UlTagNm)){InUL=false;}
break;
default: Fail;
}
if (MkTok){TokV.Add(Lx.GetTok(DoUc));}
}
TokV.Add(PHtmlTok(new THtmlTok(hsyEof)));
}
TStr THtmlDoc::GetTxtLnDoc(const TStr& HtmlStr){
TChA LnDocChA;
// prepare html parsing
PSIn HtmlSIn=TStrIn::New(HtmlStr);
THtmlLx HtmlLx(HtmlSIn);
bool InScript=false;
// save text
while (HtmlLx.GetSym()!=hsyEof){
TStr Str=HtmlLx.ChA;
switch (HtmlLx.Sym){
case hsyStr:
case hsyNum:
case hsySSym:
if (InScript){break;}
if (HtmlLx.PreSpaces>0){LnDocChA+=' ';}
LnDocChA+=Str.CStr();
break;
case hsyBTag:
if ((!LnDocChA.Empty())&&(LnDocChA.LastCh()!=' ')){LnDocChA+=' ';}
if ((!InScript)&&(Str=="<SCRIPT>")){InScript=true;}
break;
case hsyETag:
if ((!LnDocChA.Empty())&&(LnDocChA.LastCh()!=' ')){LnDocChA+=' ';}
if ((InScript)&&(Str=="<SCRIPT>")){InScript=false;}
break;
default: break;
}
}
// return result
return LnDocChA;
}
TStr THtmlDoc::GetTxtLnDoc(const TStr& HtmlStr,
const TStr& BaseUrlStr, const bool& OutUrlP, const bool& OutTagsP){
// prepare output-string
TChA OutChA; OutChA+=' ';
// prepare html parsing
PSIn HtmlSIn=TStrIn::New(HtmlStr);
THtmlLx HtmlLx(HtmlSIn);
bool InScript=false;
// save text
while (HtmlLx.GetSym()!=hsyEof){
TStr Str=HtmlLx.ChA;
switch (HtmlLx.Sym){
case hsyUndef:
case hsyUrl:
case hsyMTag:
break;
case hsyStr:
case hsyNum:
case hsySSym:
if (InScript){break;}
if (HtmlLx.PreSpaces>0){if (OutChA.LastCh()!=' '){OutChA+=' ';}}
OutChA+=Str;
break;
case hsyBTag:
// extract tag name
Str=Str.GetSubStr(1, Str.Len()-2);
// process tag
if (!InScript){
// check script tag
if (Str=="SCRIPT"){
InScript=true; break;}
// output tag
if (OutTagsP){
OutChA+='<'; OutChA+=Str; OutChA+='>';
} else {
if (OutChA.LastCh()!=' '){OutChA+=' ';}
}
// check if URL present
PHtmlTok Tok=HtmlLx.GetTok();
TStr RelUrlStr;
if (Tok->IsUrlTok(RelUrlStr)){
PUrl Url=TUrl::New(RelUrlStr, BaseUrlStr);
if (Url->IsOk()){
if (OutUrlP){
TStr XmlUrlStr=TXmlLx::GetXmlStrFromPlainStr(Url->GetUrlStr());
OutChA+="<Url>"; OutChA+=XmlUrlStr; OutChA+="</Url>";
}
}
}
}
break;
case hsyETag:
// extract tag name
Str=Str.GetSubStr(1, Str.Len()-2);
// process tag
if (InScript){
if (Str=="SCRIPT"){
InScript=false; break;}
} else {
if (OutTagsP){
OutChA+="</"; OutChA+=Str; OutChA+='>';
} else {
if (OutChA.LastCh()!=' '){OutChA+=' ';}
}
}
break;
case hsyEof: break;
default: Fail;
}
}
// return string
return OutChA;
}
void THtmlDoc::SaveTxt(const PSOut& SOut, const bool& TxtMode) const {
if (TxtMode){
for (int TokN=0; TokN<TokV.Len(); TokN++){TokV[TokN]->SaveTxt(SOut);}
SOut->PutLn();
} else {
for (int TokN=0; TokN<TokV.Len(); TokN++){
SOut->PutStr(TInt::GetStr(TokN)); SOut->PutStr(": ");
TokV[TokN]->SaveTxt(SOut);
SOut->PutLn();
}
}
}
void THtmlDoc::SaveHtmlToTxt(
const TStr& HtmlStr, const PSOut& TxtSOut, const TStr& BaseUrlStr,
const bool& OutUrlP, const bool& OutTagsP){
// get text-string from html-string
TStr TxtStr=GetTxtLnDoc(HtmlStr, BaseUrlStr, OutUrlP, OutTagsP);
// save text-string
TxtStr.SaveTxt(TxtSOut);
}
void THtmlDoc::SaveHtmlToTxt(
const TStr& HtmlStr, const TStr& TxtFNm, const TStr& BaseUrlStr,
const bool& OutUrlP, const bool& OutTagsP){
// create output file
PSOut TxtSOut=TFOut::New(TxtFNm);
// save to output file
SaveHtmlToTxt(HtmlStr, TxtSOut, BaseUrlStr, OutUrlP, OutTagsP);
}
void THtmlDoc::SaveHtmlToXml(
const TStr& HtmlStr, const PSOut& XmlSOut, const TStr& BaseUrlStr,
const bool& OutTextP, const bool& OutUrlP, const bool& OutToksP,
const bool& OutTagsP, const bool& OutArgsP){
// prepare output-file-id
TFileId fXml=XmlSOut->GetFileId();
// create outgoing url
TStrV OutUrlStrV;
// open top tag
fprintf(fXml, "<HtmlDoc>\n");
// save url
if (!BaseUrlStr.Empty()){
TStr XmlBaseUrlStr=TXmlLx::GetXmlStrFromPlainStr(BaseUrlStr);
fprintf(fXml, "<BaseUrl>%s</BaseUrl>\n", XmlBaseUrlStr.CStr());
}
// prepare html parsing
PSIn HtmlSIn=TStrIn::New(HtmlStr);
THtmlLx HtmlLx(HtmlSIn);
TChA ContTextChA; bool InScript=false;
// save text
fprintf(fXml, "<Body>\n");
while (HtmlLx.GetSym()!=hsyEof){
TStr Str=HtmlLx.ChA;
switch (HtmlLx.Sym){
case hsyUndef:
case hsyUrl:
case hsyMTag:
break;
case hsyStr:
if (InScript){break;}
Str=TXmlLx::GetXmlStrFromPlainStr(Str);
if (OutToksP){
fprintf(fXml, " <Str>%s</Str>\n", Str.CStr());}
if (!ContTextChA.Empty()){ContTextChA+=' ';} ContTextChA+=Str;
break;
case hsyNum:
if (InScript){break;}
Str=TXmlLx::GetXmlStrFromPlainStr(Str);
if (OutToksP){
fprintf(fXml, " <Num>%s</Num>\n", Str.CStr());}
if (!ContTextChA.Empty()){ContTextChA+=' ';} ContTextChA+=Str;
break;
case hsySSym:
if (InScript){break;}
Str=TXmlLx::GetXmlStrFromPlainStr(Str);
if (OutToksP){
fprintf(fXml, " <Sym>%s</Sym>\n", Str.CStr());}
if (!ContTextChA.Empty()){ContTextChA+=' ';} ContTextChA+=Str;
break;
case hsyBTag:{
// save continuos text
if (!ContTextChA.Empty()){
if (OutTextP){
fprintf(fXml, " <Text>%s</Text>\n", ContTextChA.CStr());}
ContTextChA.Clr();
}
// extract tag name
Str=Str.GetSubStr(1, Str.Len()-2);
Str=TXmlLx::GetXmlStrFromPlainStr(Str);
// process tag
if (!InScript){
// check script tag
if (Str=="SCRIPT"){
InScript=true; break;}
// output tag
if (OutTagsP){
if (OutArgsP){
fprintf(fXml, " <BTag Nm=\"%s\">\n", Str.CStr());
for (int ArgN=0; ArgN<HtmlLx.GetArgs(); ArgN++){
TStr ArgNm=TXmlLx::GetXmlStrFromPlainStr(HtmlLx.GetArgNm(ArgN));
TStr ArgVal=TXmlLx::GetXmlStrFromPlainStr(HtmlLx.GetArgVal(ArgN));
fprintf(fXml, " <Arg Nm=\"%s\" Val=\"%s\"/>", ArgNm.CStr(), ArgVal.CStr());
}
fprintf(fXml, " </BTag>\n");
} else {
fprintf(fXml, " <BTag Nm=\"%s\"/>\n", Str.CStr());
}
}
// check if URL present
PHtmlTok Tok=HtmlLx.GetTok();
TStr RelUrlStr;
if (Tok->IsUrlTok(RelUrlStr)){
PUrl Url=TUrl::New(RelUrlStr, BaseUrlStr);
if (Url->IsOk()){
OutUrlStrV.Add(Url->GetUrlStr());
if (OutUrlP){
TStr XmlUrlStr=TXmlLx::GetXmlStrFromPlainStr(Url->GetUrlStr());
fprintf(fXml, " <Url>%s</Url>\n", XmlUrlStr.CStr());
}
}
}
}
break;}
case hsyETag:{
// save continuos text
if (!ContTextChA.Empty()){
if (OutTextP){
fprintf(fXml, " <Text>%s</Text>\n", ContTextChA.CStr());}
ContTextChA.Clr();
}
// extract tag name
Str=Str.GetSubStr(1, Str.Len()-2);
Str=TXmlLx::GetXmlStrFromPlainStr(Str);
// process tag
if (InScript){
if (Str=="SCRIPT"){
InScript=false; break;}
} else {
if (OutTagsP){
fprintf(fXml, " <ETag Nm=\"%s\"/>\n", Str.CStr());}
}
break;}
case hsyEof: break;
default: Fail;
}
}
// save continuos text
if (!ContTextChA.Empty()){
if (OutTextP){
fprintf(fXml, " <Text>%s</Text>\n", ContTextChA.CStr());}
ContTextChA.Clr();
}
fprintf(fXml, "</Body>\n");
// save outgoing urls
fprintf(fXml, "<OutUrls>\n");
for (int UrlN=0; UrlN<OutUrlStrV.Len(); UrlN++){
TStr XmlUrlStr=TXmlLx::GetXmlStrFromPlainStr(OutUrlStrV[UrlN]);