File: wiki2xhtmlconvert.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (1413 lines) | stat: -rw-r--r-- 42,981 bytes parent folder | download | duplicates (3)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
{ Converter for wiki pages to xhtml pages

  Copyright (C) 2012  Mattias Gaertner  mattias@freepascal.org

  This source is free software; you can redistribute it and/or modify it under
  the terms of the GNU General Public License as published by the Free
  Software Foundation; either version 2 of the License, or (at your option)
  any later version.

  This code is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  details.

  A copy of the GNU General Public License is available on the World Wide Web
  at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
  to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  Boston, MA 02110-1335, USA.

}
unit Wiki2XHTMLConvert;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils,
  // LazUtils
  laz2_XMLWrite, LazUTF8, laz2_DOM, LazLoggerBase, LazFileUtils, AvgLvlTree,
  // CodeTools
  BasicCodeTools, KeywordFuncLists,
  // LazWiki
  WikiFormat, WikiParser;

type
  TW2XHTMLStackItem = record
    Node: TDOMElement;
    Token: TWPTokenType;
    Txt: string;
  end;
  PW2XHTMLStackItem = ^TW2XHTMLStackItem;

  { TW2XHTMLPage }

  TW2XHTMLPage = class(TW2FormatPage)
  protected
    BodyDOMNode: TDOMElement;
    CurDOMNode: TDOMElement; // current xhtml node
    TOCNode: TDOMElement;
    CurTOCNode: TDOMElement;
    TOCNodeCount: integer;
    SectionLevel: integer;
    Stack: PW2XHTMLStackItem;
    StackPtr: integer;
    StackCapacity: integer;
    procedure Push(Node: TDOMElement; Token: TWPTokenType);
    procedure Pop;
  public
    XHTML: TXMLDocument;
    Filename: string;
    constructor Create(TheConverter: TWiki2FormatConverter); override;
    procedure ClearConversion; override;
  end;

  { TWiki2XHTMLConverter }

  TWiki2XHTMLConverter = class(TWiki2FormatConverter)
  private
    FAddLinksToTranslations: boolean;
    FAddTOCIfHeaderCountMoreThan: integer;
    FCSSFilename: string;
    FLinkToBaseDocument: string;
    FMaxH: integer;
    FPageFileExt: string;
    FUseTemplateIcons: Boolean;
    FAddCategories: Boolean;
    FIndexOfflineLinksOnly: Boolean;
    procedure DoAddCategories(Page: TW2XHTMLPage);
    procedure DoAddLinksToTranslations(Page: TW2XHTMLPage);
    procedure DoAddLinkToBaseDocument(Page: TW2XHTMLPage);
    procedure OnHeaderToken(Token: TWPToken);
    procedure SetCSSFilename(AValue: string);
    procedure SetMaxH(AValue: integer);
    procedure SetPageFileExt(AValue: string);
  protected
    ShortFilenameToPage: TFilenameToPointerTree; // created in ConvertInit
    UsedImages: TFilenameToPointerTree; // image name to first page using the image
    procedure OnWikiToken(Token: TWPToken); virtual;
    procedure RaiseNodeNotOpen(Token: TWPToken);
    function GetImageLink(ImgFilename: string): string; virtual;
    function GetInternalImageLink(ImgFilename: string): string; virtual;
    function FindImage(const ImgFilename: string): string; virtual;
    procedure MarkImageAsUsed(const ImgFilename: string; Page: TW2XHTMLPage); virtual;
    function GetPageLink(Page: TW2XHTMLPage): string; virtual;
    function InsertLink(const LinkToken: TWPLinkToken): boolean;
    procedure InsertText(Token: TWPToken; Txt: string); virtual;
    procedure InsertCode(Token: TWPNameValueToken); virtual;
    procedure ConvertPage(Page: TW2XHTMLPage); virtual;
    procedure SavePage(Page: TW2XHTMLPage); virtual;
    procedure ConvertInit; virtual;
    procedure ConvertAllPages; virtual;
    procedure SaveAllPages; virtual;
    procedure AddIndexItem({%H-}AText, {%H-}AUrl: String); virtual;
    procedure AddTocItem({%H-}ALevel:Integer; {%H-}AText, {%H-}AUrl: String); virtual;
  public
    constructor Create; override;
    destructor Destroy; override;
    procedure Clear; override;
    procedure Convert; override;
    function GetRelativeCSSFilename: string; virtual;
    property CSSFilename: string read FCSSFilename write SetCSSFilename;
    property MaxH: integer read FMaxH write SetMaxH;
    function PageToFilename(Page: string; IsInternalLink, Full: boolean): string; virtual;
    function PageToFilename(Page: TW2XHTMLPage; Full: boolean): string; virtual;
    property PageFileExt: string read FPageFileExt write SetPageFileExt;
    property LinkToBaseDocument: string
      read FLinkToBaseDocument write FLinkToBaseDocument;
    property AddLinksToTranslations: boolean
      read FAddLinksToTranslations write FAddLinksToTranslations default true;
    property AddTOCIfHeaderCountMoreThan: integer
      read FAddTOCIfHeaderCountMoreThan write FAddTOCIfHeaderCountMoreThan default 2;
    property AddCategories: Boolean
      read FAddCategories write FAddCategories default true;
    property IndexOfflineLinksOnly: Boolean
      read FIndexOfflineLinksOnly write FIndexOfflineLinksOnly default true;
    property UseTemplateIcons: Boolean
      read FUseTemplateIcons write FUseTemplateIcons;
  end;

function EscapeToHTML(AText: String): String;

implementation

const
  NOTE_ICON = 'note.png';
  WARNING_ICON = 'warning.png';

{ Replaces some special characters by their HTML code }
function EscapeToHTML(AText: string): string;
var
  i: Integer;
  ampStr: string;
begin
  Result := '';

  for i := 1 to Length(AText) do
  begin
    case AText[i] of
      '&':  begin
              ampStr := Copy(AText, i, 6);
              if (Pos('&amp;',  ampStr) = 1) or
                 (Pos('&lt;',   ampStr) = 1) or
                 (Pos('&gt;',   ampStr) = 1) or
                 (Pos('&quot;', ampStr) = 1) or
                 (Pos('&apos;', ampStr) = 1) or
                 (Pos('&#37;',  ampStr) = 1)     // %
              then
                //'&' is the first char of a special char, it must not be converted
                Result := Result + AText[i]
              else
                Result := Result + '&amp;';
            end;
      '<':  Result := Result + '&lt;';
      '>':  Result := Result + '&gt;';
      '"':  Result := Result + '&quot;';
      '''': Result := Result + '&apos;';
      '%':  Result := Result + '&#37;';
      '\':  Result := Result + '&#92;';
      else  Result := Result + AText[i];
    end;
  end;
end;


{ TWiki2XHTMLConverter }

procedure TWiki2XHTMLConverter.SetCSSFilename(AValue: string);
begin
  if FCSSFilename=AValue then Exit;
  FCSSFilename:=AValue;
end;

procedure TWiki2XHTMLConverter.DoAddCategories(Page: TW2XHTMLPage);
var
  doc: TXMLDocument;
  CategoriesNode: TDOMElement;
  node: TDOMElement;
  category, url, txt: String;
  i: Integer;
begin
  doc := Page.XHTML;
  url := Page.WikiPage.BaseURL;
  if (url <> '') and (url[Length(url)] <> '/') then
    url += '/';

  if Page.CategoryList.Count = 0 then
    exit;

  CategoriesNode := doc.CreateElement('div');
  CategoriesNode.SetAttribute('class', 'catlinks');
  Page.BodyDOMNode.AppendChild(CategoriesNode);

  // Add "Categories:"
  node := doc.CreateElement('a');
  node.SetAttribute('href', url+'Special:Categories');
  node.AppendChild(doc.CreateTextNode('Categories:'));
  CategoriesNode.AppendChild(node);
  if not FIndexOfflineLinksOnly then
    AddIndexItem('Categories (external)', url+'Special:Categories');
  CategoriesNode.AppendChild(doc.CreateTextNode('&nbsp;&nbsp;'));
  for i:=0 to Page.CategoryList.Count-1 do begin
    category := Page.CategoryList[i];
    // Add link to category
    node := doc.CreateElement('a');
    node.SetAttribute('href', url + category);
    txt := Copy(category, pos(':', category)+1, MaxInt);
    node.AppendChild(doc.CreateTextNode(txt));
    CategoriesNode.AppendChild(node);
    if not FIndexOfflineLinksOnly then
      AddIndexItem('Category: '+txt+' (external)', url+category);

    // Add separator
    if i < Page.CategoryList.Count-1 then
      CategoriesNode.AppendChild(doc.CreateTextNode(' | '));
  end;
end;


procedure TWiki2XHTMLConverter.DoAddLinksToTranslations(Page: TW2XHTMLPage);
var
  TranslationPage: TW2XHTMLPage;
  Lang: String;
  S2PItem: PStringToPointerTreeItem;
  LangToPage: TStringToPointerTree;
  TranslationsNode: TDOMElement;
  LinkCaption: String;
  LinkNode: TDOMElement;
  Captions: TStringList;
  i: Integer;
  doc: TXMLDocument;
begin
  doc:=Page.XHTML;
  GetPageTranslations(Page.WikiDocumentName, LangToPage);
  //debugln(['TWiki2XHTMLConverter.DoAddLinksToTranslations ',Page.WikiDocumentName,' ',LangToPage.Count]);
  Captions:=TStringList.Create;
  try
    if (LangToPage=nil) or (LangToPage.Count<2) then exit;
    // add translations
    TranslationsNode:=doc.CreateElement('p');
    TranslationsNode.SetAttribute('class','translationLinks');
    Page.BodyDOMNode.AppendChild(TranslationsNode);
    // get all translations
    for S2PItem in LangToPage do begin
      Lang:=S2PItem^.Name;
      TranslationPage:=TW2XHTMLPage(S2PItem^.Value);
      LinkCaption:=WikiLangCodeToCaption(Lang);
      if Lang<>'' then LinkCaption+=' ('+Lang+')';
      Captions.AddObject(LinkCaption,TranslationPage);
    end;
    // sort them alphabetically
    Captions.CustomSort(@CompareStrListUTF8LowerCase);
    // add links
    for i:=0 to Captions.Count-1 do begin
      LinkCaption:=Captions[i];
      TranslationPage:=TW2XHTMLPage(Captions.Objects[i]);
      // add separator |
      if TranslationsNode.FirstChild<>nil then
        TranslationsNode.AppendChild(doc.CreateTextNode(' | '));
      if TranslationPage=Page then
        // add current page as normal text
        TranslationsNode.AppendChild(doc.CreateTextNode(LinkCaption))
      else begin
        // add link to other translations
        LinkNode:=doc.CreateElement('a');
        LinkNode.SetAttribute('href',TranslationPage.FileName);
        TranslationsNode.AppendChild(LinkNode);
        LinkNode.AppendChild(doc.CreateTextNode(LinkCaption));
      end;
    end;
  finally
    LangToPage.Free;
    Captions.Free;
  end;
end;

procedure TWiki2XHTMLConverter.DoAddLinkToBaseDocument(Page: TW2XHTMLPage);
var
  Link: String;
  MainNode: TDOMElement;
  Node: TDOMElement;
  doc: TXMLDocument;
begin
  // add <a href="BaseURL+WikiDocumentName">LinkToBaseDocument</a><br>
  doc:=Page.XHTML;
  MainNode := doc.CreateElement('p');
  Page.BodyDOMNode.AppendChild(MainNode);

  Node:=doc.CreateElement('a');
  MainNode.AppendChild(Node);
  Link:=Page.WikiPage.BaseURL;
  if (Link<>'') and (Link[length(Link)]<>'/') then
    Link+='/';
  Link+=Page.WikiDocumentName;
  Node.SetAttribute('href', Link);
  Node.AppendChild(doc.CreateTextNode(LinkToBaseDocument));

  if not FIndexOfflineLinksOnly then
    AddIndexItem(LinkToBaseDocument + ' (external)', Link);
end;

procedure TWiki2XHTMLConverter.OnHeaderToken(Token: TWPToken);
var
  LinkNode: TDOMElement;
  HeaderTxt: DOMString;
  Page: TW2XHTMLPage;
  doc: TXMLDocument;
  NodeName: String;
  NodeClass: String;
  Node: TDOMElement;
  HRef: String;
  LINode: TDOMElement;
begin
  Page:=TW2XHTMLPage(Token.UserData);
  doc:=Page.XHTML;
  NodeClass:='';
  if Page.SectionLevel<=1 then
    NodeName:='h1'
  else if Page.SectionLevel<=MaxH then
    NodeName:='h'+IntToStr(Page.SectionLevel)
  else if Page.SectionLevel>MaxH then begin
    NodeName:='h'+IntToStr(MaxH);
    NodeClass:='subTitle';
  end;
  if Token.Range=wprOpen then begin
    // open header
    Node:=doc.CreateElement(NodeName);
    Page.CurDOMNode.AppendChild(Node);
    if NodeClass<>'' then
      Node.SetAttribute('class', NodeClass);
    Page.CurDOMNode:=Node;
    Page.Push(Node, wptHeader);
  end else if Token.Range=wprClose then begin
    // close header
    if Page.CurDOMNode.TagName<>NodeName then
      RaiseNodeNotOpen(Token);
    HeaderTxt:='';
    if Page.CurDOMNode.FirstChild is TDOMText then
      HeaderTxt:=TDOMText(Page.CurDOMNode.FirstChild).Data;
    if HeaderTxt<>'' then begin
      HRef:=WikiHeaderToLink(HeaderTxt);
      // add anchor - use both old an new syntax:
      // modern version: <h2 id="something">Text</h> - commented because not understood by IpHTMLPanel:
      Page.CurDOMNode.SetAttribute('id', HRef);
      // old version: <h2><a name="something">Text</a></h2>
      Node := doc.CreateElement('a');
      Node.SetAttribute('name', HRef);
      Node.AppendChild(Page.CurDOMNode.DetachChild(Page.CurDOMNode.FirstChild));
      Page.CurDOMNode.AppendChild(Node);

      // add TOC link
      //   <li class="toclevel-2"><a href="#something">Text</a></li>
      LINode:=doc.CreateElement('li');
      LINode.SetAttribute('class', 'toclevel-'+IntToStr(Page.SectionLevel));
      Page.CurTOCNode.AppendChild(LINode);
      LinkNode:=doc.CreateElement('a');
      LinkNode.SetAttribute('href', '#'+HRef);
      LinkNode.AppendChild(doc.CreateTextNode(HeaderTxt));
      LINode.AppendChild(LinkNode);
      inc(Page.TOCNodeCount);
      AddTocItem(Page.SectionLevel, HeaderTxt, Page.Filename+'#'+HRef);
      AddIndexItem(HeaderTxt, page.Filename+'#'+HRef);
    end;
    Page.CurDOMNode:=Page.CurDOMNode.ParentNode as TDOMElement;
    Page.Pop;
  end;
end;

procedure TWiki2XHTMLConverter.SetMaxH(AValue: integer);
begin
  if AValue<1 then AValue:=1;
  if AValue>MaxHeaderDepth then AValue:=MaxHeaderDepth;
  if FMaxH=AValue then Exit;
  FMaxH:=AValue;
end;

procedure TWiki2XHTMLConverter.SetPageFileExt(AValue: string);
var
  NewValue: String;
begin
  NewValue:=Trim(AValue);
  if (NewValue<>'') and (NewValue[1]<>'.') then
    NewValue:='.'+NewValue;
  if FPageFileExt=NewValue then Exit;
  FPageFileExt:=NewValue;
end;

function TWiki2XHTMLConverter.InsertLink(const LinkToken: TWPLinkToken
  ): boolean;
var
  Page: TW2XHTMLPage;
  W: TWikiPage;
  doc: TXMLDocument;

  function WarnURL(URL: string): boolean;
  var
    p: Integer;
  begin
    p:=1;
    while p<=length(URL) do begin
      if URL[p]='/' then begin
        if NoWarnBaseURLs.Contains(copy(URL,1,p-1)) then exit(false);
      end;
      inc(p);
    end;
    Result:=true;
  end;

  procedure HandleImgLink(const AFileName, Value: String);
  type
    THorPos = (hpDefault, hpLeft, hpCenter, hpRight);
  var
    L: TStringList;
    hpos: THorPos;
    thumb: Boolean;
    factor: Double;
    frame: Boolean;
    alt: String;
    cap: String;
    w, h: Integer;   // -2 auto, -1 not used, else: pixels
    i, p: Integer;
    s: String;
    node: TDOMElement;
    imgnode: TDOMElement;
    capnode: TDOMElement;
  begin
    hpos := hpDefault;
    w := -1;  // not used
    h := -1;
    thumb := false;
    frame := false;
    factor := 1.0;
    cap := '';
    alt := '';

    L := TStringList.Create;
    try
      L.Delimiter := '|';
      L.StrictDelimiter := true;
      L.DelimitedText := Value;

      for i:=0 to L.Count-1 do begin
        s := L[i];
        case Lowercase(s) of
          'thumb', 'thumbnail':
            begin
              w := 220;
              h := -2;  // auto
              thumb := true;
              factor := 1.0;
              frame := true;
            end;
          'frame':
            frame := true;
          'frameless':
            frame := false;
          'left':
            hpos := hpLeft;
          'center':
            hpos := hpCenter;
          'right':
            hpos := hpRight;
          'none':
            hpos := hpDefault;
          else
            if pos('alt=', s) = 1 then
              alt := copy(s, Length('alt='), MaxInt)
            else
            if pos('link=', s) = 1 then
              // currently not supported
            else
            if pos('px', s) = Length(s)-1 then
            begin
              if s[1]='x' then   // e.g: "x100px" -- height
                h := StrToInt(Copy(s, 2, Length(s)-3))
              else begin
                p := pos('x', s);
                if p = Length(s) then   // e.g: "200px" --> width
                  w := StrToInt(Copy(s, 1, Length(s)-2))
                else
                if p > 0 then   // e.g: "200x100px" --> width x height
                begin
                  w := StrToInt(copy(s, 1, p-1));
                  h := StrToInt(copy(s, p+1, Length(s)-p-2));
                end else
                  cap := s;
              end;
            end else
            if (pos('upright', s) = 1) then
            begin
              thumb := true;
              s := trim(copy(s, Length('upright')+1, MaxInt));
              if s <> '' then val(s, factor, p) else factor := 1.0;
            end
            else
              cap := s;
        end;
      end;

      if thumb or frame then begin
        node := doc.CreateElement('div');
        if frame then
          node.SetAttribute('class', 'image')
        else
          node.SetAttribute('class', 'image-no-border');
        case hpos of
          hpDefault: node.SetAttribute('style', 'float:right;');
          hpLeft   : node.SetAttribute('style', 'float:left;');
          hpCenter : node.SetAttribute('style', 'display:block; margin:0px auto;');
          hpRight  : node.SetAttribute('style', 'float:right;');
        end;
        Page.CurDOMNode.AppendChild(node);
        imgnode := doc.CreateElement('img');
        node.AppendChild(imgnode);
        if cap <> '' then begin
          capnode := doc.CreateElement('figcaption');
          capnode.SetAttribute('style', 'margin-top: 4px');
          capnode.AppendChild(doc.CreateTextNode(cap));
          node.AppendChild(capnode);
        end;
      end else
      begin
        imgnode := doc.CreateElement('img');
        imgnode.SetAttribute('class', 'image-no-border');
        Page.CurDOMNode.AppendChild(imgnode);
      end;

      imgnode.SetAttribute('src', AFileName);
      if alt <> '' then
        imgnode.SetAttribute('alt', alt);
      if w > 0 then
      begin
        if thumb and (factor <> 1.0) then w := round(w*factor);
        imgnode.SetAttribute('width', IntToStr(w)+'px');
      end;
      if h = -2 then
        imgnode.SetAttribute('height', 'auto')
      else
      if h > 0 then
      begin
        if thumb and (factor <> 1.0) then h := round(h * factor);
        imgnode.SetAttribute('height', IntToStr(h)+'px');
      end;
      case hpos of
         hpLeft  : imgnode.SetAttribute('style', 'float:left');
         hpCenter: imgnode.SetAttribute('style', 'display:block; margin:0px auto;');
         hpRight : imgNode.SetAttribute('style', 'float:right');
      end;

    finally
      L.Free;
    end;
  end;

  function HandleLink(var URL, Caption: string): boolean;
  var
    p: SizeInt;
    Scheme: String;
    Filename: String;
    TargetPage: TW2XHTMLPage;
    FoundImgFile: String;
    DocumentName: String;
    Anchor: String;
  begin
    Result:=false;
    if LinkToken.Token=wptExternLink then exit;

    p:=Pos(':', URL);
    if p>1 then begin
      // check special pages
      Scheme:=lowercase(copy(URL, 1, p-1));
      if Scheme='user' then begin
        URL:=''; // show name without link
        exit;
      end
      else if Scheme='category' then begin
        Page.CategoryList.Add(URL);
        URL:='';
        Caption := '';
        exit;
      end
      else if (Scheme='image') or (Scheme='file') then begin
        URL:=copy(URL,p+1,length(URL));
        URL:=UTF8Trim(URL);
        URL:=WikiTitleToPage(URL);
        if URL='' then exit;
        Filename:=WikiImageToFilename(URL,false,true,true);
        FoundImgFile:=FindImage(Filename);
        if FoundImgFile<>'' then begin
          Filename:=GetImageLink(FoundImgFile);
          MarkImageAsUsed(Filename,Page);
          HandleImgLink(FileName, Caption);
          exit(true);
        end;
        if WarnURL(LinkToken.Link) then
          Log('WARNING: TWiki2XHTMLConverter.InsertLink "'+dbgstr(LinkToken.Link)+'": image file not found: "'+Filename+'" at '+W.PosToStr(LinkToken.LinkStartPos,true));
        URL:='';
        exit;
      end;
    end;

    // convert %hh and remove special characters
    URL:=WikiTitleToPage(URL);

    // check if link to wiki page but with a full baseurl
    if (Page.WikiPage.BaseURL<>'')
    and (LeftStr(URL,length(Page.WikiPage.BaseURL))=Page.WikiPage.BaseURL)
    then begin
      Delete(URL,1,length(Page.WikiPage.BaseURL));
      while (URL<>'') and (URL[1]='/') do Delete(URL,1,1);
    end;

    // check if an external link but accidentally marked as internal
    p:=pos('://',URL);
    if p>1 then begin
      Scheme:=lowercase(copy(URL, 1, p-1));
      if (Scheme='http') or (Scheme='https') then begin
        // external link
        exit;
      end;
    end;

    // default: a page
    if URL<>'' then begin
      p:=Pos('#',URL);
      if p<1 then begin
        DocumentName:=URL;
        Anchor:='';
      end else begin
        DocumentName:=LeftStr(URL,p-1);
        Anchor:=copy(URL,p+1,length(URL));
      end;
      Filename:=PageToFilename(DocumentName,true,false);
      TargetPage:=TW2XHTMLPage(ShortFilenameToPage[Filename]);
      if (TargetPage<>nil) then begin
        URL:=GetPageLink(TargetPage);
        if Anchor<>'' then
          URL+='#'+Anchor;
      end else
      if (FileName <> '') then begin
        if FileExistsUTF8(OutputDir+Filename) or
           (GetPageWithDocumentName(DocumentName) <> nil)  // will be converted lated
        then
          URL := Filename
        else begin
          if WarnMissingPageLinks and WarnURL(LinkToken.Link) then
            Log('WARNING: TWiki2XHTMLConverter.InsertLink "'+
                dbgstr(LinkToken.Link)+'": file not found: "'+Filename+'" at '+
                W.PosToStr(LinkToken.LinkStartPos,true)+'. Linking to online version.');
          URL := Page.WikiPage.BaseURL + '/' + DocumentName;
        end;
      end;
    end;
  end;

var
  Caption: String;
  URL: String;
  Node: TDOMElement;
begin
  Result:=false;
  Page:=TW2XHTMLPage(LinkToken.UserData);
  W:=Page.WikiPage;
  doc:=Page.XHTML;
  URL:=LinkToken.Link;
  if URL='' then exit;
  Caption:=copy(W.Src, LinkToken.CaptionStartPos, LinkToken.CaptionEndPos-
    LinkToken.CaptionStartPos);
  if HandleLink(URL,Caption) then exit;

  if URL<>'' then begin
    Node:=doc.CreateElement('a');
    Node.SetAttribute('href', URL);
    if Caption<>'' then begin
      Node.AppendChild(doc.CreateTextNode(Caption));
      if ((pos('http', URL) = 1) or (pos('wiki.', URL) = 1) or (pos('bugs.', URL) = 1))
      then begin
        if not FIndexOfflineLinksOnly then
          AddIndexItem(Caption + ' (external)', URL);
      end else begin
        if URL[1]='#' then
          URL := Page.Filename + URL;
        AddIndexItem(Caption, URL);
      end;
    end;
    Page.CurDOMNode.AppendChild(Node);
  end else if Caption<>'' then begin
    InsertText(LinkToken, Caption);
    Result:=true;
  end;
end;

procedure TWiki2XHTMLConverter.ConvertPage(Page: TW2XHTMLPage);
var
  doc: TXMLDocument;
  RootNode: TDOMElement;
  HeadNode: TDOMElement;
  MetaNode: TDOMElement;
  TitleNode: TDOMElement;
  CSSNode: TDOMElement;
  Node: TDOMElement;
  CurCSSFilename: String;
begin
  AddTocItem(1, UTF8Trim(Page.WikiPage.Title), Page.FileName);

  Page.ClearConversion;
  if Page.WikiPage=nil then exit;
  Page.XHTML:=TXMLDocument.Create;
  doc:=Page.XHTML;
  // <html>
  RootNode:=doc.CreateElement('html');
  doc.AppendChild(RootNode);
  // <head>
  HeadNode:=doc.CreateElement('head');
  RootNode.AppendChild(HeadNode);
  // <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  MetaNode:=doc.CreateElement('meta');
  HeadNode.AppendChild(MetaNode);
  MetaNode.SetAttribute('content','text/html; charset=utf-8');
  MetaNode.SetAttribute('http-equiv','Content-Type');
  // <title>
  TitleNode:=doc.CreateElement('title');
  HeadNode.AppendChild(TitleNode);
  TitleNode.AppendChild(doc.CreateTextNode(Page.WikiPage.Title));
  CurCSSFilename:=GetRelativeCSSFilename;
  if CurCSSFilename<>'' then begin
    // stylesheet <link href="wiki.css" type="text/css" rel="stylesheet">
    CSSNode:=doc.CreateElement('link');
    HeadNode.AppendChild(CSSNode);
    CSSNode.SetAttribute('href',CurCSSFilename);
    CSSNode.SetAttribute('type','text/css');
    CSSNode.SetAttribute('rel','stylesheet');
  end;
  // <body>
  Page.BodyDOMNode:=doc.CreateElement('body');
  RootNode.AppendChild(Page.BodyDOMNode);
  // title <h1 class="firstHeading">
  Node:=doc.CreateElement('h1');
  Page.BodyDOMNode.AppendChild(Node);
  Node.SetAttribute('class','firstHeading');
  Node.AppendChild(doc.CreateTextNode(Page.WikiPage.Title));

  // links to translations
  if AddLinksToTranslations then
    DoAddLinksToTranslations(Page);

  try
    Page.SectionLevel:=0;
    Page.TOCNode:=doc.CreateElement('ul');
    Page.TOCNodeCount:=0;
    Page.CurTOCNode:=Page.TOCNode;
    Page.BodyDOMNode.AppendChild(Page.TOCNode);
    Page.CurDOMNode:=Page.BodyDOMNode;

    Page.WikiPage.Parse(@OnWikiToken,Page);

    if FAddCategories then
      DoAddCategories(Page);

    if LinkToBaseDocument<>'' then
      DoAddLinkToBaseDocument(Page);

    if Page.TOCNodeCount<=AddTOCIfHeaderCountMoreThan then
      Page.TOCNode.Free;
  finally
    Page.BodyDOMNode:=nil;
    Page.CurDOMNode:=nil;
    Page.TOCNode:=nil;
    Page.CurTOCNode:=nil;
  end;
end;

procedure TWiki2XHTMLConverter.OnWikiToken(Token: TWPToken);
var
  Page: TW2XHTMLPage;
  W: TWikiPage;

  procedure MissingNodeName;
  begin
    raise Exception.Create('TWiki2XHTMLConverter.OnWikiToken have no node name:'
      +' Token='+dbgs(Token.Token)+' '+DbgSName(Token));
  end;

var
  doc: TXMLDocument;
  NodeName: string;
  Node: TDOMElement;
  childNode1, childNode2: TDOMElement;
  LinkToken: TWPLinkToken;
  NodeClass: String;
  NameValueToken: TWPNameValueToken;
  CurName: String;
  CurValue: String;
  fn: String;
  captn, iconfile: String;
begin
  Page:=TW2XHTMLPage(Token.UserData);
  W:=Page.WikiPage;
  doc:=Page.XHTML;
  //Log(['TWiki2XHTMLConverter.OnWikiToken Token='+dbgs(Token.Token)+' '+dbgs(Token));
  case Token.Token of
  wptText:
    if Token is TWPTextToken then begin
      InsertText(Token,copy(W.Src,TWPTextToken(Token).StartPos,
                 TWPTextToken(Token).EndPos-TWPTextToken(Token).StartPos));
      exit;
    end;

  wptLineBreak:
    begin
      // only append a br if there is something in front
      if Page.CurDOMNode.FirstChild<>nil then
        Page.CurDOMNode.AppendChild(doc.CreateElement('br'));
      exit;
    end;

  wptHorizontalRow:
    begin
      // only append a hr if there is something in front
      if Page.CurDOMNode.FirstChild<>nil then
        Page.CurDOMNode.AppendChild(doc.CreateElement('hr'));
      exit;
    end;

  wptP, wptBold, wptItalic, wptStrikeTagShort, wptUnderlineTag, wptTT,
  wptSup, wptSub, wptSmall, wptEm, wptSpan, wptString, wptVar, wptKey,
  wptPre, wptCenter,
  wptBulletList, wptNumberedList, wptDefinitionList,
  wptTable, wptTableRow, wptTableCell, wptTableHeadCell:
    begin
      // simple range
      NodeClass:='';
      NodeName:='';
      case Token.Token of
      wptP: NodeName:='p';
      wptBold: NodeName:='b';
      wptItalic: NodeName:='i';
      wptStrikeTagShort: NodeName:='s';
      wptUnderlineTag: NodeName:='u';
      wptTT: NodeName:='tt';
      wptSup: NodeName:='sup';
      wptSub: NodeName:='sub';
      wptSmall: NodeName:='small';
      wptEm: NodeName:='em';
      wptSpan: NodeName:='span';
      wptString: begin NodeName:='span'; NodeClass:='string'; end;
      wptVar: begin NodeName:='span'; NodeClass:='var'; end;
      wptKey: begin NodeName:='span'; NodeClass:='key'; end;

      wptCode: begin NodeName:='pre'; NodeClass:='code'; end;
      wptPre: NodeName:='pre';
      wptCenter: NodeName:='center';

      wptNumberedList: NodeName:='ol';
      wptBulletList: NodeName:='ul';
      wptDefinitionList: NodeName:='dl';

      wptTable: NodeName:='table';
      wptTableRow: NodeName:='tr';
      wptTableCell: NodeName:='td';
      wptTableHeadCell: NodeName:='th';
      end;
      if NodeName='' then
        MissingNodeName;

      if Token.Range=wprOpen then begin
        Node:=doc.CreateElement(NodeName);
        Page.CurDOMNode.AppendChild(Node);
        if NodeClass<>'' then
          Node.SetAttribute('class',NodeClass);
        Page.CurDOMNode:=Node;
        exit;
      end else if Token.Range=wprClose then begin
        if Page.CurDOMNode.TagName<>NodeName then
          RaiseNodeNotOpen(Token);
        Page.CurDOMNode:=Page.CurDOMNode.ParentNode as TDOMElement;
        exit;
      end;
    end;

  wptSection:
    begin
      NodeName:='div';
      if Token.Range=wprOpen then begin
        inc(Page.SectionLevel);
        // start div
        Node:=doc.CreateElement(NodeName);
        Page.CurDOMNode.AppendChild(Node);
        Node.SetAttribute('class','section');
        Page.CurDOMNode:=Node;
        // start TOC list
        Node:=doc.CreateElement('ul');
        Page.CurTOCNode.AppendChild(Node);
        Page.CurTOCNode:=Node;
        Page.Push(Node,Token.Token);
        exit;
      end else if Token.Range=wprClose then begin
        dec(Page.SectionLevel);
        // end div
        if Page.CurDOMNode.TagName<>NodeName then
          RaiseNodeNotOpen(Token);
        Page.CurDOMNode:=Page.CurDOMNode.ParentNode as TDOMElement;
        // end TOC list
        Node:=Page.CurTOCNode;
        Page.CurTOCNode:=Node.ParentNode as TDOMElement;
        if Node.FirstChild=nil then
          Node.Free;
        Page.Pop;
        exit;
      end;
    end;

  wptHeader:
    begin
      OnHeaderToken(Token);
      exit;
    end;

  wptListItem:
    if Token.Range=wprOpen then begin
      if Page.CurDOMNode.TagName='dl' then
        NodeName:='dd'
      else
        NodeName:='li';
      Node:=doc.CreateElement(NodeName);
      Page.CurDOMNode.AppendChild(Node);
      Page.CurDOMNode:=Node;
      exit;
    end else if Token.Range=wprClose then begin
      if (Page.CurDOMNode.TagName<>'dd')
      and (Page.CurDOMNode.TagName<>'li') then
        RaiseNodeNotOpen(Token);
      Page.CurDOMNode:=Page.CurDOMNode.ParentNode as TDOMElement;
      exit;
    end;

  wptAttribute:
    if Token is TWPNameValueToken then begin
      NameValueToken:=TWPNameValueToken(Token);
      CurName:=copy(W.Src,NameValueToken.NameStartPos,NameValueToken.NameEndPos-NameValueToken.NameStartPos);
      CurValue:=copy(W.Src,NameValueToken.ValueStartPos,NameValueToken.ValueEndPos-NameValueToken.ValueStartPos);
      Page.CurDOMNode.SetAttribute(CurName,CurValue);
      exit;
    end;

  wptSpecial:
    if Token is TWPNameValueToken then begin
      NameValueToken:=TWPNameValueToken(Token);
      CurName:=copy(W.Src,NameValueToken.NameStartPos,NameValueToken.NameEndPos-NameValueToken.NameStartPos);
      if CurName='' then begin
        // special without class = alternative title
        // the title is already written
        exit;
      end;
      CurValue:=copy(W.Src,NameValueToken.ValueStartPos,NameValueToken.ValueEndPos-NameValueToken.ValueStartPos);

      case Lowercase(CurName) of
        'note', 'warning':
          begin
            case Lowercase(CurName) of
              'note'   : begin captn := 'Note'; iconfile := NOTE_ICON; end;
              'warning': begin captn := 'Warning'; iconfile := WARNING_ICON; end;
            end;
            Node := doc.CreateElement('div');
            Node.SetAttribute('class', 'template-with-icon');
            Page.CurDOMNode.AppendChild(Node);

            childnode1 := doc.CreateElement('div');
            Node.AppendChild(childnode1);
            childnode1.SetAttribute('class', 'icon');
            if FUseTemplateIcons then begin
              fn := FindImage('internal/'+iconfile);
              if fn <> '' then
                fn := GetInternalImageLink(fn);
              MarkImageAsUsed(fn, Page);
              childnode2 := doc.CreateElement('img');
              childnode2.SetAttribute('src', fn);
              childnode1.AppendChild(childnode2);
            end;

            childnode2 := doc.CreateElement('b');
            childnode2.AppendChild(doc.createTextNode(captn + ': '));
            Node.AppendChild(childnode2);
            Node.AppendChild(doc.CreateTextNode(CurValue));
              // to do: CurValue can contain further html tags!
            exit;
          end;

        'mantislink':
          begin
            Node := doc.CreateElement('a');
            Node.SetAttribute('href', 'http://bugs.freepascal.org/view.php?id='+CurValue);
            Node.AppendChild(doc.CreateTextNode('MantisLink #'));
            Node.AppendChild(doc.CreateTextNode(Curvalue));
            Page.CurDOMNode.AppendChild(Node);
            if not FIndexOfflineLinksOnly then
              AddIndexItem('MantisLink #'+CurValue+' (external)', 'http://bugs.freepascal.org/view.php?id='+CurValue);
            exit;
          end;

        'menutranslate':
          // Translation menu is written by DoAddLinksToTranslations
          exit;

        else
          Node := doc.CreateElement('span');
          if CurName <> '' then Node.SetAttribute('class', CurName);
          Page.CurDOMNode.AppendChild(Node);
          if CurValue <> '' then
            Node.AppendChild(doc.CreateTextNode(CurValue));
          exit;
      end;

      Node:=doc.CreateElement('span');
      if CurName<>'' then
        Node.SetAttribute('class',CurName);
      Page.CurDOMNode.AppendChild(Node);
      if CurValue<>'' then
        Node.AppendChild(doc.CreateTextNode(CurValue));
      exit;
    end;

  wptCode:
    if Token is TWPNameValueToken then begin
      InsertCode(TWPNameValueToken(Token));
      exit;
    end;

  wptInternLink, wptExternLink:
    if Token is TWPLinkToken then begin
      LinkToken:=TWPLinkToken(Token);
      InsertLink(LinkToken);
      exit;
    end;

  end;

  Log('TWiki2XHTMLConverter.OnWikiToken ToDo: Token='+dbgs(Token.Token)+' Range='+dbgs(Token.Range)+' Class='+Token.ClassName+' '+W.PosToStr(W.CurrentPos));
end;

procedure TWiki2XHTMLConverter.RaiseNodeNotOpen(Token: TWPToken);
var
  Page: TW2XHTMLPage;
begin
  Page:=TW2XHTMLPage(Token.UserData);
  raise Exception.Create('TWiki2XHTMLConverter.OnWikiToken can not close:'
    +' Token='+dbgs(Token.Token)+' '+DbgSName(Token)+' CurNode='+Page.CurDOMNode.TagName);
end;

function TWiki2XHTMLConverter.GetImageLink(ImgFilename: string): string;
begin
  Result:=CreateRelativePath(ImgFilename,OutputDir);
end;

function TWiki2XHTMLConverter.GetInternalImagelink(ImgFilename: String): string;
begin
  Result := CreateRelativepath(ImgFilename,OutputDir);
end;

function TWiki2XHTMLConverter.FindImage(const ImgFilename: string): string;
begin
  if ImagesDir='' then exit('');
  Result:=ImgFilename;
  if (not FilenameIsAbsolute(Result)) then
    Result:=ImagesDir+Result;
  if not FileExistsUTF8(Result) then
    Result:='';
end;

procedure TWiki2XHTMLConverter.MarkImageAsUsed(const ImgFilename: string;
  Page: TW2XHTMLPage);
begin
  if not UsedImages.Contains(ImgFilename) then
    UsedImages[ImgFilename]:=Page;
end;

function TWiki2XHTMLConverter.GetPageLink(Page: TW2XHTMLPage): string;
begin
  Result:=PageToFilename(Page,false);
end;

procedure TWiki2XHTMLConverter.InsertText(Token: TWPToken; Txt: string);
var
  Page: TW2XHTMLPage;
  doc: TXMLDocument;
  CurNode: TDOMElement;
begin
  Page:=TW2XHTMLPage(Token.UserData);
  doc:=Page.XHTML;
  //Log(['TWiki2XHTMLConverter.InsertText Txt="'+dbgstr(Txt)+'"']);
  if Txt='' then exit;
  CurNode:=Page.CurDOMNode;
  if CurNode.TagName<>'pre' then begin
    if UTF8Trim(Txt)='' then begin
      // skip empty text
      exit;
    end;
    if CurNode.FirstChild=nil then
      Txt:=UTF8Trim(Txt,[u8tKeepEnd]);
  end;
  //Log('TWiki2XHTMLConverter.InsertText Node="'+Page.CurDOMNode.TagName+'" Text="'+Txt+'"');
  CurNode.AppendChild(doc.CreateTextNode(Txt));
end;

procedure TWiki2XHTMLConverter.InsertCode(Token: TWPNameValueToken);
type
  TPascalToken = (
    pNone,
    pString,
    pKey,
    pNumber,
    pSymbol,
    pComment
    );
const
  PascalTokenClass: array[TPascalToken] of string = (
    '', // pNone,
    'string', // pString,
    'key', // pKey,
    'num', // pNumber,
    'sym', // pSymbol
    'cmt' // pComment
    );
var
  doc: TXMLDocument;
  LastToken: TPascalToken;
  LastRangeStart: PChar;
  CodeNode: TDOMElement;

  procedure Flush(TxtStart: PChar);
  var
    l: PtrUInt;
    Code: string;
    TokenNode: TDOMElement;
  begin
    l:=TxtStart-LastRangeStart;
    if l=0 then exit;
    SetLength(Code,l);
    Move(LastRangeStart^,Pointer(Code)^,l);
    if LastToken=pNone then begin
      CodeNode.AppendChild(doc.CreateTextNode(Code))
    end else begin
      TokenNode:=doc.CreateElement('span');
      TokenNode.SetAttribute('class',PascalTokenClass[LastToken]);
      TokenNode.AppendChild(doc.CreateTextNode(Code));
      CodeNode.AppendChild(TokenNode);
    end;
    LastRangeStart:=TxtStart;
  end;

  procedure AddSpan(PascalToken: TPascalToken; TxtStart: PChar);
  begin
    if PascalToken<>LastToken then
      Flush(TxtStart);
    LastToken:=PascalToken;
  end;

var
  Page: TW2XHTMLPage;
  W: TWikiPage;
  CurName: String;
  CurValue: String;
  p: PChar;
  AtomStart: PChar;
begin
  Page:=TW2XHTMLPage(Token.UserData);
  W:=Page.WikiPage;
  doc:=Page.XHTML;
  CurName:=lowercase(copy(W.Src,Token.NameStartPos,Token.NameEndPos-Token.NameStartPos));
  CurValue:=copy(W.Src,Token.ValueStartPos,Token.ValueEndPos-Token.ValueStartPos);

  if CurName = 'code' then begin
    CodeNode := doc.CreateElement('code');
    Page.CurDomNode.AppendChild(CodeNode);
    CodeNode.AppendChild(doc.CreateTextNode(CurValue));
  end else
  begin
    CodeNode:=doc.CreateElement('pre');
    if (CurName='pascal')
    or (CurName='delphi')
    or (CurName='syntaxhighlight')
    or (CurName='source')
    or (CurName='fpc')
    then
      CurName:='pascal';
    if CurName<>'' then
      CodeNode.SetAttribute('class',CurName);
    Page.CurDOMNode.AppendChild(CodeNode);
    if CurValue<>'' then begin
      if (CurName='pascal') then begin
        p:=PChar(CurValue);
        AtomStart:=p;
        LastToken:=pNone;
        LastRangeStart:=p;
        repeat
          // skip space
          while p^ in [#1..#31,' '] do inc(p);
          // read token
          if (p^='{') or ((p^='/') and (p[1]='/')) or ((p^='(') and (p[1]='*'))
          then begin
            // comment
            AddSpan(pComment,p);
            p:=FindCommentEnd(p,false);
          end else begin
            ReadRawNextPascalAtom(p,AtomStart);
            if AtomStart^=#0 then break;
            case AtomStart^ of
            '''','#':
              AddSpan(pString,AtomStart);
            '0'..'9','%','$','&':
              AddSpan(pNumber,AtomStart);
            'a'..'z','A'..'Z','_':
              if WordIsKeyWord.DoIdentifier(AtomStart) then
                AddSpan(pKey,AtomStart)
              else
                AddSpan(pNone,AtomStart);
            else
              AddSpan(pSymbol,AtomStart);
            end;
          end;
        until false;
        Flush(p);
      end else begin
        // default: add as text
        CodeNode.AppendChild(doc.CreateTextNode(CurValue));
      end;
    end;
  end;
end;

procedure TWiki2XHTMLConverter.SavePage(Page: TW2XHTMLPage);
var
  Filename: String;
begin
  if OutputDir='' then exit;
  if Page.XHTML=nil then exit;
  Filename:=PageToFilename(Page,true);
  Log('TWiki2HTMLConverter.SavePage '+Filename);
  WriteXMLFile(Page.XHTML,Filename);
end;

procedure TWiki2XHTMLConverter.ConvertInit;
var
  i: Integer;
  Page: TW2XHTMLPage;
begin
  ShortFilenameToPage.Clear;
  UsedImages.Clear;
  for i:=0 to Count-1 do begin
    Page:=TW2XHTMLPage(Pages[i]);
    Page.Filename:=PageToFilename(Page,false);
    if Page.WikiPage<>nil then
      Page.WikiPage.LanguageTags:=CodeTags;
    ShortFilenameToPage[Page.Filename]:=Page;
  end;
end;

procedure TWiki2XHTMLConverter.ConvertAllPages;
var
  i: Integer;
begin
  for i:=0 to Count-1 do
    ConvertPage(TW2XHTMLPage(Pages[i]));
end;

procedure TWiki2XHTMLConverter.SaveAllPages;
var
  i: Integer;
begin
  for i:=0 to Count-1 do
    SavePage(TW2XHTMLPage(Pages[i]));
end;

procedure TWiki2XHTMLConverter.AddIndexItem(AText, AUrl: String);
begin
  // nothing to do here - will be overridden by CHMConverter
end;

procedure TWiki2XHTMLConverter.AddTocItem(ALevel: Integer; AText, AUrl: String);
begin
  // nothing to do here - will be overridden by CHMConverter
end;

constructor TWiki2XHTMLConverter.Create;
begin
  inherited Create;
  FPageClass:=TW2XHTMLPage;
  FOutputDir:='xhtml';
  FMaxH:=MaxHeaderDepth;
  FPageFileExt:='.xhtml';
  ShortFilenameToPage:=TFilenameToPointerTree.Create(false);
  UsedImages:=TFilenameToPointerTree.Create(false);
  fLinkToBaseDocument:='Online version';
  FAddCategories := true;
  FAddLinksToTranslations:=true;
  FAddTOCIfHeaderCountMoreThan:=2;
  FUseTemplateIcons := true;
  FIndexOfflineLinksOnly := true;
end;

destructor TWiki2XHTMLConverter.Destroy;
begin
  inherited Destroy;
  FreeAndNil(ShortFilenameToPage);
  FreeAndNil(UsedImages);
end;

procedure TWiki2XHTMLConverter.Clear;
begin
  ShortFilenameToPage.Clear;
  UsedImages.Clear;
  inherited Clear;
end;

procedure TWiki2XHTMLConverter.Convert;
var
  Filename: String;
begin
  inherited Convert;
  if (CSSFilename<>'') and (OutputDir<>'') then begin
    Filename:=TrimAndExpandFilename(CSSFilename);
    if not FileExistsUTF8(Filename) then
      raise Exception.Create('css file not found: "'+Filename+'"');
  end;

  ConvertInit;
  ConvertAllPages;
  SaveAllPages;
end;

function TWiki2XHTMLConverter.GetRelativeCSSFilename: string;
begin
  Result:=CSSFilename;
  if (OutputDir='') or not FilenameIsAbsolute(OutputDir) then
    exit;
  Result:=TrimAndExpandFilename(Result);
  if Result='' then exit;
  Result:=CreateRelativePath(Result,OutputDir);
end;

function TWiki2XHTMLConverter.PageToFilename(Page: string; IsInternalLink,
  Full: boolean): string;
begin
  Result:=WikiPageToFilename(Page,IsInternalLink,true);
  if Result='' then exit;
  Result:=Result+PageFileExt;
  if Full then
    Result:=AppendPathDelim(OutputDir)+Result;
end;

function TWiki2XHTMLConverter.PageToFilename(Page: TW2XHTMLPage; Full: boolean
  ): string;
begin
  Result:=ChangeFileExt(ExtractFilename(Page.WikiFilename),PageFileExt);
  if Full then
    Result:=AppendPathDelim(OutputDir)+Result;
end;

{ TW2XHTMLPage }

procedure TW2XHTMLPage.Push(Node: TDOMElement; Token: TWPTokenType);
var
  s: PW2XHTMLStackItem;
  OldCapacity: Integer;
begin
  inc(StackPtr);
  if StackPtr=StackCapacity then begin
    OldCapacity:=StackCapacity;
    StackCapacity:=StackCapacity*2+8;
    ReAllocMem(Stack,SizeOf(TW2XHTMLStackItem)*StackCapacity);
    FillByte(Stack[StackPtr],SizeOf(TW2XHTMLStackItem)*(StackCapacity-OldCapacity),0);
  end;
  s:=@Stack[StackPtr];
  s^.Node:=Node;
  s^.Token:=Token;
end;

procedure TW2XHTMLPage.Pop;
begin
  if StackPtr<0 then
    raise Exception.Create('bug'); // push and pop are not balanced
  dec(StackPtr);
end;

constructor TW2XHTMLPage.Create(TheConverter: TWiki2FormatConverter);
begin
  inherited Create(TheConverter);
  StackPtr:=-1;
end;

procedure TW2XHTMLPage.ClearConversion;
begin
  BodyDOMNode:=nil;
  CurDOMNode:=nil;
  SectionLevel:=0;
  ReAllocMem(Stack,0);
  StackPtr:=-1;
  StackCapacity:=0;
  FreeAndNil(XHTML);
end;

end.