summaryrefslogtreecommitdiff
path: root/fpcdocs/relinkhtml.pp
blob: 6b0c863a81704f1f98c2aaffa003ef28d9394b4d (plain)
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
unit relinkhtml;
// hackety script using DOM_HTML to fix the URLs in FPC doc.
// Note: needs dom fixes in fcl-xml (2.3.1 of after july 2009)
//
// Warning: looking at this code might damage your eyes!
//

interface
{$ifdef fpc}
{$mode delphi}
{$endif}

{$define debugoutput}
{define printattr}
{define printchildren}
uses typinfo,classes,
     dom,SAX_Html,dom_html,xmlutils,htmwrite8859;

Type
        TNavButton = (NavNone,NavNext,NavUp,NavTail,NavPrev,NavPrevTail,navfront );
        TNavArray  = array [TNavButton] of TDomNode;
        THtmlDocFile = class // container object for file read from disk
                          public
                      title,                                     // title of paragraph/section
                      redtitle : string;                         // title with some substitutions done (appendix/chapter removed)
                      Filename:string;                           // html filename.
                      dom : THTMLDocument;
                      numbers: array[0..4] of string;            // doc hierachy splitted on the dots. ('1','1','1','' = section 1.1.1 etc)
                      padnumbers: array[0..4] of string;         // same as numbers but padded with zeroes to 5 chars. for ordering purposes.

                      children : TStringlist;                    // child nodes
                      navtable : array[0..1] of TNavArray;       // nodes in navigation table. 0=top, 1=bottom
                      Localnavtable: array[TNavButton] of THtmldocfile; // Calculated up/prev/next
                      constructor create;
                      procedure read(filenamewithpath,fn:string);
                      procedure findtitle;
                      procedure parsetitle;
                      procedure scan_nav_table(scanforward:boolean;var navtab:TNavArray);
                      function  walk(prev,up:THtmlDocFile):THtmlDocFile;
                      function  walkback(next:THtmlDocFile):THtmlDocFile;
                      function  scantablenode(root:tdomnode;var resnode:Tdomnode):TNavButton;
                      procedure navcheck;
                      procedure patch;
                     end;
        TIndexes = class     // Collection of files + indexes.
                    indexes: array[0..4] of TStringlist; // files sorted according to group
                    htmls  : TStringList;                // all files.
                    prefix  :string;                     // prog, user etc. set before running
                    procedure walk;
                    procedure walkback;
                    constructor create;
                    procedure readfiles(basedir:string);
                    procedure linkfiles;
                    procedure writehtmls(basedir:string);
                    procedure patch;
                    property chapters     :Tstringlist read indexes[0];
                    property sections     :Tstringlist read indexes[1];
                    property subsections  :Tstringlist read indexes[2];
//                    property appendices   :Tstringlist read indexes[5];
                    property toc          :Tstringlist read indexes[3];
                    property rest         :Tstringlist read indexes[4];
        end;

const NavButtonCaptions : array[TNavButton] of String =('NONE!','next','up','tail','prev','prev-tail','front');

function A_NodeToURL(node:tdomnode):String;

implementation

uses sysutils,strutils;

// Helper funcs.

function A_NodeToURL(node:tdomnode):String;
var n :TDomNode;
begin
  result:='';
  if not assigned(node.Attributes) then exit;

  n:=node.attributes.GetNamedItem('href');
  if assigned(n) then
    result:=n.NodeValue;
end;

procedure setnode_tourl(node:TDomNode;value:string);
var n :TDomNode;
begin
  if not assigned(node.Attributes) then exit;

  n:=node.attributes.GetNamedItem('href');
  if assigned(n) then
    n.NodeValue:=value;
  assert(n.NodeValue=value);
end;

procedure printattributes(node:TDomNode;title:string='');
var i :integer;
    Attributes:TDOMNamedNodeMap;
begin
  if assigned(node) then
    begin
      Attributes:=node.Attributes;
      write(title,' ');
      if assigned(attributes) and (attributes.length>0) then
        for i := 0 to Attributes.Length - 1 do
          write(Attributes.item[i].nodevalue,' ');
      writeln;
    end;
end;


function searchtag(prnt:TDomNode;tag:string):TDomNode;
// Seach first matching tag in siblings
var chld: TDomNode;
begin
  result:=nil;
  if assigned(prnt )  then
    begin
      chld:=prnt.firstchild;
      while assigned(chld) do
        begin
          if (chld is TDomElement) then
            begin
              if (TDomElement(chld).tagname=tag) then
                begin
                  result:=chld;
                  exit;
                 end;
            end;
          chld:=chld.nextsibling;
        end;
    end;
end;

function searchtagback(prnt:TDomNode;tag:string):TDomNode;
// Seach last matching tag in siblings

var chld: TDomNode;
begin
  result:=nil;
  if assigned(prnt )  then
    begin
      chld:=prnt.LastChild;
      while assigned(chld) do
        begin
          if (chld is TDomElement) then
            begin
              if (TDomElement(chld).tagname=tag) then
                begin
                  result:=chld;
                  exit;
                 end;
            end;
          chld:=chld.PreviousSibling;
        end;
    end;
end;

function reducefilename(name:string):string;
var i,j : integer;
begin
  i:=1; j:=length(name);
  {while not (name[i] in ['0'..'9']) do inc(i);
 } result:=ChangeFileExt(copy(name,i,j-i+1),'');
  while length(result)<5 do result:='0'+result;
end;

function padzero(s:string):string;

begin
 result:=s;
 while length(result)<5 do result:='0'+result;
end;

procedure printchildren(root:tdomnode);
var
    node :TDomNode;
    children:TDOMNodeList;
    i: integer;
begin
   children:=root.ChildNodes;
   for i:= 0 to children.Length- 1 do
     begin
       node:=children.Item[i];
       write(node.classname,'"',node.NodeName,'" ',node.Nodevalue,' - ' );
     end;
   writeln;
end;

{ THtmlDocFile }

constructor THtmlDocFile.create;
begin
  fillchar(navtable,sizeof(navtable),#0);
  dom:=THTMLDocument.create;
  children:=TStringList.Create;
  children.Sorted:=true;
end;

procedure THtmlDocFile.findtitle;
// Tries to find object(section,chapter etc) true title. Not necessarily title tag.
procedure trytitle(node: TDomNode);
// finds most section's title.
var
    span,txt: TDomNode;
begin
  if assigned(node) then
    begin
      span:=searchtag(node,'span');
      if assigned(span) then
        begin
           txt:=span.firstchild;
           while assigned(txt) do
             begin
               if txt is TDomText then
                   title:=title+trim(tDomText(txt).nodevalue);
               txt:=txt.nextsibling;
             end;
        end;
    end;
end;

procedure examinenode(txt:TDomNode;var title:ansistring);
var s:ansistring;
begin
  while assigned(txt) do
    begin
      if txt is TDomText then
        begin
          s:=trim(tDomText(txt).nodevalue);
          s:=ansireplacestr(s,chr(160),' ');
          title:=trim(title)+' '+s;
        end
      else
        examinenode(txt.firstchild,title);
      txt:=txt.nextsibling;
    end;
end;

procedure try_hx_a(nr:integer);
// finds some sections with clickable titles
var
    node: TDomNode;
    span,txt: TDomNode;
begin
  node:=searchtag(dom.body,'h'+inttostr(nr));
  if assigned(node) then
    begin
      examinenode(node.firstchild,title);
  (*    span:=searchtag(node,'a');
//      printchildren(span);
      if assigned(span) then
        begin
           examinenode(span,title);
           printchildren(span);
    //       examinenode(span.firstchild,title);
        end;
*)
    end; 
end;
var node: TDomNode;
    l,
    i : integer;
begin
  title:='';
  for i := 1 to 4 do
    begin
      if title='' then
        begin
          node:=searchtag(dom.body,'h'+inttostr(i));
          trytitle(node);
        end;
    end;
    // alternate locations:
  if title='' then
    try_hx_a(2);
  if title='' then
   try_hx_a(3);

  if title='' then
   try_hx_a(4);

  {$ifdef debugtitle}
  if title='' then
     writeln('no title:',filename)
  else
     writeln('title:',title,' in ',filename);
  {$endif}
  // strip appendix,chapter from title for easy indexing.
  redtitle:=title;
  if ansiStartsText('Chapter',redtitle) then
     delete(redtitle,1,8);
  if ansiStartsText('Appendix',redtitle) then
    delete(redtitle,1,9);
  l:=length(redtitle);
  redtitle:=trim(redtitle);
  i:=1;
  while (i<l) and (redtitle[i] in ['0'..'9','.']) do
    inc(i); 
  if i<>l then
   dec(i);
  setlength(redtitle,i);
  {$ifdef debugtitle}
  writeln('redtitle:"',redtitle,'"');
  {$endif}
end;

procedure THtmlDocFile.navcheck;
//unused debugproc to do a sanity  check if top and bottom nodes don't match
var isfirst:boolean;
    n :TNavButton;
begin
  isfirst:=true;
  for n := low(n) to high(n) do
    if assigned(navtable[0][n]) and assigned(navtable[1][n]) and (navtable[0][n]<>navtable[1][n]) then
      begin
        if isfirst then
          write(':',title,'  ');
        write(getenumname(typeinfo(tnavbutton),ord(n)),' ' );
        isfirst:=false;
      end;
  if not isfirst then writeln;
end;

procedure THtmlDocFile.parsetitle;
var i,j,k : Integer;
   s2:ansistring;
   code : longint;
begin
 i:=1;
 j:=length(title); k:=0;
 if title<>'' then
 while (i<=j) and (k<=high(numbers)) do
   begin
     s2:='';
     while (i<=j) and (title[i] in ['0'..'9','A'..'Z']) do
       begin
         s2:=s2+title[i];
         inc(i);
       end;
     if length(s2)>0 then
       begin
         numbers[k]:=s2;  padnumbers[k]:=padzero(s2);
          inc(k);
//       val(s2,numbers[k],code); inc(k);
       end
     else
       i:=j;
     inc(i);
   end;
end;

procedure THtmlDocFile.patch;
// tries to patch up/down/next nodes etc.
var n : TNavButton;
    fn : string;
begin
  for n:= succ(low(n)) to high(n) do
  begin
    if assigned(Localnavtable[n]) then
      begin
        fn:=localnavtable[n].Filename;
        if assigned(navtable[0][n]) then
           setnode_tourl(navtable[0][n],fn);
        if assigned(navtable[1][n]) then
           setnode_tourl(navtable[1][n],fn);
      end;
      if n=NavTail then
          begin
            if assigned(navtable[0][n]) then
              setnode_tourl(navtable[0][n],'#tail'+filename);
            if assigned(navtable[1][n]) then
              setnode_tourl(navtable[1][n],'#tail'+filename);
          end;
      if (n=NavPrevTail) and assigned(localnavtable[navprev]) then
          begin
            fn:=localnavtable[navprev].Filename;
            if assigned(navtable[0][n]) then
              setnode_tourl(navtable[0][n],fn+'#'+fn);
            if assigned(navtable[1][n]) then
              setnode_tourl(navtable[1][n],fn+'#'+fn);
          end;
      if n=navfront then
          begin
            if assigned(navtable[0][n]) then
              setnode_tourl(navtable[0][n],filename);
            if assigned(navtable[1][n]) then
              setnode_tourl(navtable[1][n],filename);
          end;
  end;
end;

procedure THtmlDocFile.read(filenamewithpath,fn: string);
begin
  ReadHtmlFile(dom,filenamewithpath);
  filename:=fn;
  findtitle;
  parsetitle;
  scan_nav_table(true,navtable[0]);
  scan_nav_table(false,navtable[1]);
end;

function THtmlDocFile.scantablenode(root: tdomnode;var resnode:Tdomnode):TNavbutton;
var
    children: TDOMNodeList;
    i       : integer;
    j       : TNavButton;
    node    : TDomNode;
    s       : domstring;
begin
   result:=NavNone;
   children:=root.ChildNodes;
   {$ifdef printattributes}
     writeln('root:', root.nodevalue);
     printattributes(root,'');
   {$endif}  
   for i:= 0 to children.Length- 1 do
     begin
      node:=children.Item[i];
      s:=node.nodevalue;
      if s='' then
        begin
          result:=scantablenode(node,resnode);
          if result<>navnone then 
           begin
             exit(result);
           end;
        end;
     {$ifdef printattributes}
      writeln('navchild:',node.nodename,' ',s,' * ');
      writeln('attr:');
      printattributes(node,'');  
      printchildren(node);
     {$endif}
      j:=low(NavButtonCaptions);
     {$R-}
      while (j<=high(NavButtonCaptions)) and (navbuttoncaptions[j]<>s) do inc(j);
      if j<=high(navbuttoncaptions) then
        begin
          resnode:=node;
          exit(j);
        end;
     {$R+}
     end;
end;

procedure THtmlDocFile.scan_nav_table(scanforward:boolean;var navtab:TNavArray);
var resnode,divnode,pnode,node :TDomNode;
    Attributes:TDOMNamedNodeMap;
    children:TDOMNodeList;
    i : integer;
    but : tnavbutton;
begin
   if scanforward then
     divnode:=searchtag(dom.body,'div')
    else
     divnode:=searchtagback(dom.body,'div');
  if assigned(divnode) then
    begin
      //printchildren(divnode);
      pnode:=searchtag(divnode,'p');
      if assigned(pnode) then
        begin
        {$ifdef sdebugoutput}
        writeln(title,' ');
        {$endif}
        children:=pnode.ChildNodes;
         for i:= 0 to children.Length- 1 do
          begin
            node:=children.Item[i];
            if node.NodeName='a' then
              begin
                 resnode:=nil; 
                 but:=scantablenode(node,resnode);
                 if but<>navnone then
                  begin
                 {$ifdef printchildren}
                   writeln('found',but,' ',resnode.nodename,' ',resnode.nodevalue);
                 {$endif}
                   navtab[but]:=node;
                  end;
              end;
          {$ifdef printchildren}
            write('   "',children.Item[i].NodeName,'" ',children.Item[i].Nodevalue,' - ' );
            writeln;
          {$endif}
          end;
        end;
      {$ifdef printattributes}
        printattributes(pnode,title);  
      {$endif}
    end;
end;

function THtmlDocFile.walk(prev,up: THtmlDocFile): THtmlDocFile;
var cur : THtmlDocFile;
    i   : Integer;
begin
  localnavtable[NavUp]:=up;
  localnavtable[Navprev]:=prev;
  prev:=self;
  up:=self;
  for i := 0 to children.Count - 1 do
    begin
      cur:=THtmlDocFile(children.Objects[i]);
      prev:=cur.walk(prev,up)
    end;
  result:=prev;
end;

function THtmlDocFile.walkback(next: THtmlDocFile): THtmlDocFile;
var cur : THtmlDocFile;
    i   : Integer;
begin
  for i := children.Count - 1 downto 0 do
    begin
      cur:=THtmlDocFile(children.Objects[i]);
      next:=cur.walkback(next)
    end;
  localnavtable[Navnext]:=next;
  result:=self;
end;

{ TIndexes }

constructor TIndexes.create;
var i:integer;
begin
 htmls:=TStringList.Create; htmls.sorted:=true;
 for i := 0 to high(indexes) do
   begin
    indexes[i]:=Tstringlist.Create; indexes[i].Sorted:=true;
   end;
end;

procedure TIndexes.linkfiles;
var
    i,j : integer;
    sect,sect2,chap :  THtmlDocFile;
    s : string;
begin
  // collection chapters is zeropadded, rest not.
  // all "children" must be zero padded, since it is final ordering.
  for  i:= 0 to sections.Count - 1 do
    begin
      sect:=THtmlDocFile(sections.Objects[i]);
      s:=sect.padnumbers[0];
      j:=chapters.IndexOf(s);
      if j<>-1 then
        begin
          chap:=THtmlDocFile(chapters.Objects[j]);
          chap.children.AddObject(sect.padnumbers[1],sect);
          {$ifdef debugoutput}
          writeln('linking ',sect.redtitle,' to ',chap.redtitle);
          {$endif}
       end
      else
        writeln(sect.redtitle,' not found');
    end;
  {$ifdef debugoutput}
  writeln('finished linking sections');
  {$endif}
  for  i:= 0 to subsections.Count - 1 do
    begin
      sect:=THtmlDocFile(subsections.Objects[i]);
      s:=sect.padnumbers[0];
      j:=chapters.IndexOf(s);
      if j<>-1 then
        begin
          chap:=THtmlDocFile(chapters.Objects[j]);
          s:=sect.padnumbers[1];
          j:=chap.children.IndexOf(s);
          if j<>-1 then
            begin
              sect2:=THtmlDocFile(chap.children.Objects[j]);
              sect2.children.addobject(sect.padnumbers[2],sect);
              {$ifdef debugoutput}
              writeln('linking ',sect.redtitle,' to ',sect2.redtitle);
              {$endif}
            end
          else
            writeln('Section for ',sect.redtitle,' not found');
       end
      else
        writeln('Chapter for ',sect.redtitle,' not found');
    end;

end;

procedure TIndexes.patch;
var i : integer;
begin
 for i := 0 to htmls.Count - 1 do
   THtmlDocFile(htmls.Objects[i]).patch;
end;

procedure TIndexes.readfiles(basedir:string);
// load files, and sort them into an index of node types based on filename.
var
  d :  TSearchRec;
  v :  THtmlDocFile;
  redname : string;

begin
  if basedir<>'' then
    basedir:=IncludeTrailingPathDelimiter(basedir);
  if findfirst(basedir+'*.html',faanyfile,d)=0 then
    begin
      repeat
        v:=THtmlDocFile.create;
        v.read(basedir+d.Name,d.name); 
        {$ifdef debugoutput}
          Writeln('  ',d.name);
        {$endif}
        delete(d.Name,1,length(prefix));
        redname:=v.redtitle;                 // redtitle is paragraph title minus appendix/chapter etc.
        if ansiStartsText('ch',d.Name) then
         begin
           writeln('chap:',redname);
          chapters.AddObject(padzero(redName),v) // must be sorted from the start, so padded.
         end
        else
          if ansiStartsText('se',d.Name) then
            sections.AddObject(redName,v)
          else
            if ansiStartsText('su',d.Name) then
              subsections.AddObject(redName,v)
            else
              if ansiStartsText('ap',d.Name) then
                chapters.AddObject(padzero(redName),v)    // treat appendix as chapter. "A" sorts after numbers
              else
                if ansiStartsText('li',d.Name) then
                  toc.AddObject(redName,v)
                else
                  rest.AddObject(redName,v);
        htmls.addobject(d.name,v);
      until findnext(d)<>0;
      findclose(d);
    end;
end;

procedure TIndexes.walk;
// walk all chapters recursively and generate a prev, up nodes.
var i : integer;
    up,prev: THtmlDocFile;
    cur : THtmlDocFile;
begin
  prev:=nil;
  up:=nil; // should init to index or so.
  for i := 0 to chapters.Count - 1 do
    begin
      cur:=THtmlDocFile(chapters.Objects[i]);
      prev:=cur.walk(prev,up);
    end;
end;

procedure TIndexes.walkback;
// walk all chapters backwards recursively and generate a next nodes.
var i : integer;
    next: THtmlDocFile;
    cur : THtmlDocFile;
begin
  next:=nil;
  for i := chapters.Count - 1 downto 0 do
    begin
      cur:=THtmlDocFile(chapters.Objects[i]);
      next:=cur.walkback(next);
    end;
end;

procedure TIndexes.writehtmls(basedir: string);
var I : integer;
    sx :THTMLDocument;
    fnhtml:string;
begin
  for i:=0 to htmls.count-1 do
   begin
     fnhtml:=basedir+pathdelim+prefix+htmls[i];
     sx:=THtmlDocFile(htmls.objects[i]).dom;
     writehtml(sx,fnhtml);
   end;
end;

end.