File: wikisearchmain.pas

package info (click to toggle)
lazarus 1.2.4%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 170,220 kB
  • ctags: 115,165
  • sloc: pascal: 1,386,898; xml: 257,878; sh: 2,935; java: 603; makefile: 549; perl: 297; sql: 174; ansic: 137
file content (704 lines) | stat: -rw-r--r-- 20,077 bytes parent folder | download | duplicates (2)
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
{ Browse and search form for offline wiki

  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., 59 Temple Place - Suite 330, Boston,
  MA 02111-1307, USA.

}
unit WikiSearchMain;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, math, FileUtil, LazLogger, LazUTF8, LazFileUtils, laz2_DOM,
  Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls, ComCtrls, LCLIntf,
  Menus, IpHtml, Ipfilebroker, IpMsg, CodeToolManager, CodeCache, SynEdit,
  SynHighlighterHTML, SynEditHighlighter, WikiHelpManager, WikiSearchOptions,
  WikiParser, WikiFormat;

type

  { TWikiIpHtmlDataProvider }

  TWikiIpHtmlDataProvider = class(TIpHtmlDataProvider)
  private
  protected
    function DoGetStream(const URL: string): TStream; override;
  public
  end;

  { TWikiPageHistoryItem }

  TWikiPageHistoryItem = class
  public
    DocumentName: string;
    Anchor: string;
    Title: string;
    constructor Create(TheDocumentName, TheAnchor, TheTitle: string);
  end;

  TWikiPageHistory = class
  private
    FCurrentIndex: integer;
    FItems: TFPList; // list of TWikiPageHistoryItem
    FMaxCount: integer;
    function GetCurrent: TWikiPageHistoryItem;
    function GetItems(Index: integer): TWikiPageHistoryItem;
    procedure SetCurrentIndex(AValue: integer);
    procedure SetMaxCount(AValue: integer);
  public
    constructor Create;
    destructor Destroy; override;
    procedure Clear;
    property CurrentIndex: integer read FCurrentIndex write SetCurrentIndex;
    property Current: TWikiPageHistoryItem read GetCurrent;
    property Items[Index: integer]: TWikiPageHistoryItem read GetItems; default;
    function Count: integer;
    property MaxCount: integer read FMaxCount write SetMaxCount;
    function AddAfterCurrent(DocumentName, Anchor, Title: string): TWikiPageHistoryItem;
    procedure Delete(Index: integer);
  end;

  { TWikiSearchDemoForm }

  TWikiSearchDemoForm = class(TForm)
    HideSearchButton: TButton;
    ImageList1: TImageList;
    ViewWikiSourceMenuItem: TMenuItem;
    ViewHTMLMenuItem: TMenuItem;
    OptionsButton: TButton;
    PagePanel: TPanel;
    PageIpHtmlPanel: TIpHtmlPanel;
    PagePopupMenu: TPopupMenu;
    PageToolBar: TToolBar;
    ProgressLabel: TLabel;
    ResultsIpHtmlPanel: TIpHtmlPanel;
    SearchEdit: TEdit;
    SearchPanel: TPanel;
    SearchLabel: TLabel;
    Splitter1: TSplitter;
    SynHTMLSyn1: TSynHTMLSyn;
    Timer1: TTimer;
    ShowSearchToolButton: TToolButton;
    SearchSepToolButton: TToolButton;
    BackToolButton: TToolButton;
    ForwardToolButton: TToolButton;
    procedure BackToolButtonClick(Sender: TObject);
    function DataProviderCanHandle(Sender: TObject; const {%H-}URL: string): Boolean;
    procedure DataProviderGetImage(Sender: TIpHtmlNode; const URL: string;
      var Picture: TPicture);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ForwardToolButtonClick(Sender: TObject);
    procedure HideSearchButtonClick(Sender: TObject);
    procedure LanguagesEditChange(Sender: TObject);
    procedure IpHtmlPanelHotClick(Sender: TObject);
    procedure OptionsButtonClick(Sender: TObject);
    procedure PagePopupMenuPopup(Sender: TObject);
    procedure SearchEditChange(Sender: TObject);
    procedure ViewHTMLMenuItemClick(Sender: TObject);
    procedure ShowSearchToolButtonClick(Sender: TObject);
    procedure ViewWikiSourceMenuItemClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    fLastSearchText: string;
    fLastLanguages: string;
    fLastScoring: TWHScoring;
    FIdleConnected: boolean;
    FPageHistory: TWikiPageHistory;
    FURLDataProvider: TWikiIpHtmlDataProvider;
    FPageDocumentName: string;
    FPageAnchor: string;
    FPageSource: string;
    FPageTitle: string;
    procedure GoToHistoryPage(Index: integer);
    procedure QueryChanged;
    procedure SetIdleConnected(AValue: boolean);
    procedure UpdateHistoryButtons;
    procedure UpdateProgress;
    procedure LoadWikiPage(Documentname, Anchor: string; AddToHistory: boolean);
    procedure LoadHTML(Target: TIpHtmlPanel; HTML: string; Anchor: string = ''); overload;
    procedure LoadHTML(Target: TIpHtmlPanel; aStream: TStream; Anchor: string = ''); overload;
    procedure ViewSource(aTitle, aSource: string; aHighlighter: TSynCustomHighlighter);
    procedure WikiSearchOptsWndOptionsChanged(Sender: TObject);
    procedure WikiHelpScanned(Sender: TObject);
    procedure WikiHelpSearched(Sender: TObject);
    procedure OnIdle(Sender: TObject; var {%H-}Done: Boolean);
  public
    property IdleConnected: boolean read FIdleConnected write SetIdleConnected;
    procedure ShowOptions;
    function GetLanguages: string;
    property PageHistory: TWikiPageHistory read FPageHistory;
  end;

var
  WikiSearchDemoForm: TWikiSearchDemoForm = nil;

implementation

function TWikiPageHistory.GetItems(Index: integer): TWikiPageHistoryItem;
begin
  Result:=TWikiPageHistoryItem(FItems[Index]);
end;

function TWikiPageHistory.GetCurrent: TWikiPageHistoryItem;
begin
  if (CurrentIndex>=0) and (CurrentIndex<Count) then
    Result:=Items[CurrentIndex]
  else
    Result:=nil;
end;

procedure TWikiPageHistory.SetCurrentIndex(AValue: integer);
var
  NewValue: Integer;
begin
  NewValue:=AValue;
  if NewValue<-1 then NewValue:=-1;
  if NewValue>=Count then NewValue:=Count-1;
  if FCurrentIndex=NewValue then Exit;
  FCurrentIndex:=NewValue;
end;

procedure TWikiPageHistory.SetMaxCount(AValue: integer);
begin
  if FMaxCount=AValue then Exit;
  FMaxCount:=AValue;
end;

constructor TWikiPageHistory.Create;
begin
  FItems:=TFPList.Create;
end;

destructor TWikiPageHistory.Destroy;
begin
  Clear;
  FreeAndNil(FItems);
  inherited Destroy;
end;

procedure TWikiPageHistory.Clear;
var
  i: Integer;
begin
  for i:=0 to FItems.Count-1 do
    TObject(FItems[i]).Free;
  FItems.Clear;
  FCurrentIndex:=-1;
end;

function TWikiPageHistory.Count: integer;
begin
  Result:=FItems.Count;
end;

function TWikiPageHistory.AddAfterCurrent(DocumentName, Anchor, Title: string
  ): TWikiPageHistoryItem;
begin
  while Count>CurrentIndex+1 do
    Delete(Count-1);
  Result:=TWikiPageHistoryItem.Create(DocumentName,Anchor,Title);
  FItems.Add(Result);
  FCurrentIndex:=FItems.Count-1;
end;

procedure TWikiPageHistory.Delete(Index: integer);
begin
  TObject(FItems[Index]).Free;
  FItems.Delete(Index);
end;

{ TWikiPageHistoryItem }

constructor TWikiPageHistoryItem.Create(TheDocumentName, TheAnchor,
  TheTitle: string);
begin
  DocumentName:=TheDocumentName;
  Anchor:=TheAnchor;
  Title:=TheTitle;
end;

{ TWikiIpHtmlDataProvider }

function TWikiIpHtmlDataProvider.DoGetStream(const URL: string): TStream;
begin
  Result:=nil;
  if URL='' then exit;
  if URL=WikiHelp.ResultsCSSURL then begin
    //debugln(['TWikiIpHtmlDataProvider.DoGetStream loading css ']);
    Result:=TMemoryStream.Create;
    if WikiHelp.ResultsCSS<>'' then
      Result.Write(WikiHelp.ResultsCSS[1],length(WikiHelp.ResultsCSS));
    Result.Position:=0;
    exit;
  end;
  debugln(['TWikiIpHtmlDataProvider.DoGetStream ',URL]);
end;

{$R *.lfm}

{ TWikiSearchDemoForm }

procedure TWikiSearchDemoForm.FormCreate(Sender: TObject);
var
  Code: TCodeBuffer;
begin
  FPageHistory:=TWikiPageHistory.Create;

  Caption:='Search Wiki (Proof of concept)';

  // search panel
  SearchLabel.Caption:='Search:';
  SearchEdit.Text:='translations';
  SearchEdit.Hint:='Type one or more words separated by space, use " for phrases with spaces';
  HideSearchButton.Caption:='Hide';
  OptionsButton.Caption:='Options';

  // page panel
  ShowSearchToolButton.Hint:='Search';
  BackToolButton.Hint:='Go back one page';
  ForwardToolButton.Hint:='Go forward one page';
  ViewHTMLMenuItem.Caption:='View HTML Source';
  ViewWikiSourceMenuItem.Caption:='View Wiki Source';

  FURLDataProvider:=TWikiIpHtmlDataProvider.Create(nil);
  ResultsIpHtmlPanel.DataProvider:=FURLDataProvider;
  PageIpHtmlPanel.DataProvider:=FURLDataProvider;
  FURLDataProvider.OnCanHandle:=@DataProviderCanHandle;
  FURLDataProvider.OnGetImage:=@DataProviderGetImage;

  WikiHelp:=TWikiHelp.Create(nil);
  fLastScoring:=TWHScoring.Create;
  fLastScoring.Assign(WikiHelp.DefaultScoring);
  WikiHelp.XMLDirectory:=SetDirSeparators('../wikixml');
  WikiHelp.ImagesDirectory:=SetDirSeparators('../images');
  WikiHelp.Converter.OutputDir:='';
  WikiHelp.Converter.CSSFilename:='wiki.css';
  WikiHelp.Converter.WarnMissingPageLinks:=true;
  WikiHelp.OnScanned:=@WikiHelpScanned;
  WikiHelp.OnSearched:=@WikiHelpSearched;
  WikiHelp.ResultsCSSURL:='wiki.css';
  Code:=CodeToolBoss.LoadFile(TrimAndExpandFilename(SetDirSeparators('../html/wiki.css')),true,false);
  if Code<>nil then
    WikiHelp.ResultsCSS:=Code.Source;

  UpdateHistoryButtons;
  LoadHTML(ResultsIpHtmlPanel,'');
  LoadHTML(PageIpHtmlPanel,'');

  WikiHelp.StartLoading;
  UpdateProgress;
  Timer1.Enabled:=true;
end;

function TWikiSearchDemoForm.DataProviderCanHandle(Sender: TObject;
  const URL: string): Boolean;
begin
  //debugln(['TWikiSearchDemoForm.DataProviderCanHandle URL=',URL]);
  Result:=false;
end;

procedure TWikiSearchDemoForm.BackToolButtonClick(Sender: TObject);
begin
  if PageHistory.CurrentIndex<=0 then exit;
  GoToHistoryPage(PageHistory.CurrentIndex-1);
end;

procedure TWikiSearchDemoForm.DataProviderGetImage(Sender: TIpHtmlNode;
  const URL: string; var Picture: TPicture);
var
  Filename: String;
  PicCreated: Boolean;
begin
  //debugln(['TWikiSearchDemoForm.DataProviderGetImage URL=',URL]);
  Filename:=WikiHelp.ImagesDirectory+URL;
  if not FileExistsUTF8(Filename) then begin
    debugln(['TWikiSearchDemoForm.DataProviderGetImage image not found "',Filename,'"']);
    exit;
  end;
  PicCreated := False;
  try
    if Picture=nil then begin
      Picture:=TPicture.Create;
      PicCreated := True;
    end;
    Picture.LoadFromFile(Filename);
  except
    if PicCreated then
      Picture.Free;
    Picture := nil;
  end;
end;

procedure TWikiSearchDemoForm.FormDestroy(Sender: TObject);
begin
  IdleConnected:=false;

  // free tool windows
  FreeAndNil(WikiSearchOptsWnd);

  // free pages before dataprovider
  FreeAndNil(ResultsIpHtmlPanel);
  FreeAndNil(PageIpHtmlPanel);
  FreeAndNil(FURLDataProvider);

  FreeAndNil(FPageHistory);
  FreeAndNil(WikiHelp);
  FreeAndNil(fLastScoring);
end;

procedure TWikiSearchDemoForm.ForwardToolButtonClick(Sender: TObject);
begin
  if PageHistory.CurrentIndex>=PageHistory.Count-1 then exit;
  GoToHistoryPage(PageHistory.CurrentIndex+1);
end;

procedure TWikiSearchDemoForm.HideSearchButtonClick(Sender: TObject);
begin
  DisableAutoSizing;
  try
    SearchPanel.Hide;
    Splitter1.Hide;
    ShowSearchToolButton.Visible:=true;
  finally
    EnableAutoSizing;
  end;
end;

procedure TWikiSearchDemoForm.LanguagesEditChange(Sender: TObject);
begin
  IdleConnected:=true;
end;

procedure TWikiSearchDemoForm.OnIdle(Sender: TObject; var Done: Boolean);
begin
  QueryChanged;
  IdleConnected:=false;
end;

function TWikiSearchDemoForm.GetLanguages: string;
begin
  if WikiSearchOptsWnd<>nil then
    Result:=WikiSearchOptsWnd.Languages
  else
    Result:='';
  //debugln(['TWikiSearchDemoForm.GetLanguages "',Result,'"']);
end;

procedure TWikiSearchDemoForm.IpHtmlPanelHotClick(Sender: TObject);
var
  HotNode: TIpHtmlNode;
  HRef: String;
  Panel: TIpHtmlPanel;
  DocumentName: String;
  p: SizeInt;
  AnchorName: String;
begin
  Panel:=Sender as TIpHtmlPanel;
  HotNode:=Panel.HotNode;
  if HotNode is TIpHtmlNodeA then begin
    HRef := TIpHtmlNodeA(HotNode).HRef;
    //Target := TIpHtmlNodeA(HotNode).Target;
  end else begin
    HRef := TIpHtmlNodeAREA(HotNode).HRef;
    //Target := TIpHtmlNodeAREA(HotNode).Target;
  end;
  debugln(['TWikiSearchDemoForm.ResultsIpHtmlPanelHotClick href=',href]);
  if WikiIsExternalLink(HRef) then begin
    // open external page
    OpenURL(HRef);
    exit;
  end;

  // load wiki page
  DocumentName:=HRef;
  p:=Pos('#',DocumentName);
  if p>0 then begin
    AnchorName:=copy(DocumentName,p+1,length(DocumentName));
    DocumentName:=LeftStr(DocumentName,p-1);
  end;
  LoadWikiPage(DocumentName,AnchorName,true);
end;

procedure TWikiSearchDemoForm.OptionsButtonClick(Sender: TObject);
begin
  ShowOptions;
end;

procedure TWikiSearchDemoForm.PagePopupMenuPopup(Sender: TObject);
begin
  ViewHTMLMenuItem.Enabled:=FPageDocumentName<>'';
  ViewWikiSourceMenuItem.Enabled:=FPageDocumentName<>'';
end;

procedure TWikiSearchDemoForm.SearchEditChange(Sender: TObject);
begin
  IdleConnected:=true;
end;

procedure TWikiSearchDemoForm.ViewHTMLMenuItemClick(Sender: TObject);
begin
  if (FPageDocumentName='') then exit;
  ViewSource('HTML of '+FPageDocumentName,FPageSource,SynHTMLSyn1);
end;

procedure TWikiSearchDemoForm.ShowSearchToolButtonClick(Sender: TObject);
begin
  DisableAutoSizing;
  try
    Splitter1.Show;
    SearchPanel.Show;
    SearchPanel.Width:=Max(SearchPanel.Width,30);
    Splitter1.Left:=Max(Splitter1.Left,SearchPanel.Width);
    ShowSearchToolButton.Visible:=false;
  finally
    EnableAutoSizing;
  end;
end;

procedure TWikiSearchDemoForm.ViewWikiSourceMenuItemClick(Sender: TObject);
var
  Page: TW2FormatPage;
begin
  if FPageDocumentName='' then exit;
  Page:=WikiHelp.Converter.GetPageWithDocumentName(FPageDocumentName);
  if (Page=nil) or (Page.WikiPage=nil) then begin
    MessageDlg('Wiki source not found','Unable to find Wiki source of page "'+FPageDocumentName+'".',mtError,[mbCancel],0);
    exit;
  end;
  ViewSource('Wiki source of '+FPageDocumentName,Page.WikiPage.Src,nil);
end;

procedure TWikiSearchDemoForm.Timer1Timer(Sender: TObject);
begin
  ProgressLabel.Caption:=WikiHelp.GetProgressCaption;
  //debugln(['TWikiSearchDemoForm.Timer1Timer ',ProgressLabel.Caption]);
  Timer1.Enabled:=WikiHelp.Busy;
end;

procedure TWikiSearchDemoForm.WikiSearchOptsWndOptionsChanged(Sender: TObject);
begin
  QueryChanged;
end;

procedure TWikiSearchDemoForm.WikiHelpScanned(Sender: TObject);
begin
  UpdateProgress;
  if WikiSearchOptsWnd<>nil then
    WikiSearchOptsWnd.UpdateAvailableLanguages;
end;

procedure TWikiSearchDemoForm.WikiHelpSearched(Sender: TObject);
var
  HTML: String;
begin
  UpdateProgress;
  HTML:=WikiHelp.ResultsHTML;
  if HTML='' then exit;
  LoadHTML(ResultsIpHtmlPanel,HTML);
end;

procedure TWikiSearchDemoForm.QueryChanged;
var
  NewSearchText: String;
  NewLanguages: String;
begin
  if ComponentState*[csDestroying,csLoading]<>[] then exit;
  NewSearchText:=UTF8Trim(SearchEdit.Text);
  NewLanguages:=GetLanguages;
  if (NewSearchText=fLastSearchText) and (NewLanguages=fLastLanguages)
  and ((WikiSearchOptsWnd=nil) or fLastScoring.Equals(WikiSearchOptsWnd.Scoring))
  then
    exit;
  fLastSearchText:=NewSearchText;
  fLastLanguages:=NewLanguages;
  if WikiSearchOptsWnd<>nil then
    fLastScoring.Assign(WikiSearchOptsWnd.Scoring);
  WikiHelp.Search(NewSearchText,NewLanguages,fLastScoring);
  Timer1.Enabled:=true;
end;

procedure TWikiSearchDemoForm.GoToHistoryPage(Index: integer);
var
  CurPage: TWikiPageHistoryItem;
begin
  PageHistory.CurrentIndex:=Index;
  CurPage:=PageHistory.Current;
  LoadWikiPage(CurPage.DocumentName, CurPage.Anchor, false);
  UpdateHistoryButtons;
end;

procedure TWikiSearchDemoForm.SetIdleConnected(AValue: boolean);
begin
  if FIdleConnected=AValue then Exit;
  FIdleConnected:=AValue;
  if IdleConnected then
    Application.AddOnIdleHandler(@OnIdle)
  else
    Application.RemoveOnIdleHandler(@OnIdle);
end;

procedure TWikiSearchDemoForm.UpdateHistoryButtons;
begin
  BackToolButton.Enabled:=PageHistory.CurrentIndex>0;
  ForwardToolButton.Visible:=PageHistory.CurrentIndex+1<PageHistory.Count;
end;

procedure TWikiSearchDemoForm.UpdateProgress;
begin
  ProgressLabel.Caption:=WikiHelp.GetProgressCaption;
  Timer1.Enabled:=WikiHelp.Busy;
end;

procedure TWikiSearchDemoForm.LoadWikiPage(Documentname, Anchor: string;
  AddToHistory: boolean);
var
  ms: TMemoryStream;
  Src: string;
  Page: TW2FormatPage;
begin
  if (FPageDocumentName=Documentname)
  and (Anchor=FPageAnchor) then
    exit;
  if Documentname<>'' then begin
    // open page in PageIpHtmlPanel
    ms:=TMemoryStream.Create;
    try
      try
        WikiHelp.SavePageToStream(DocumentName,ms);
        Page:=WikiHelp.Converter.GetPageWithDocumentName(Documentname);
        ms.Position:=0;
        SetLength(Src,ms.Size);
        if Src<>'' then
          ms.Read(Src[1],length(Src));
        ms.Position:=0;
        LoadHTML(PageIpHtmlPanel,ms,Anchor);
        FPageDocumentName:=DocumentName;
        FPageAnchor:=Anchor;
        FPageSource:=Src;
        if (Page<>nil) and (Page.WikiPage<>nil) then
          FPageTitle:=Page.WikiPage.Title
        else
          FPageTitle:=Documentname;
      except
        on E: Exception do begin
          FPageDocumentName:='';
          FPageAnchor:='';
          FPageSource:=Src;
          FPageTitle:='Error: '+E.Message;
          Src:='<html><body>'+EncodeLesserAndGreaterThan(FPageTitle)+'</body></html>';
          LoadHTML(PageIpHtmlPanel,Src);
        end;
      end;
    finally
      ms.Free;
    end;
  end else if Anchor<>'' then begin
    // same page
    PageIpHtmlPanel.MakeAnchorVisible(Anchor+'/'); // ipHTML stores anchor names with / at end
  end else begin
    exit;
  end;
  if AddToHistory and (FPageDocumentName<>'') then begin
    PageHistory.AddAfterCurrent(FPageDocumentName,FPageAnchor,FPageTitle);
    UpdateHistoryButtons;
  end;
end;

procedure TWikiSearchDemoForm.LoadHTML(Target: TIpHtmlPanel; HTML: string;
  Anchor: string);
var
  ms: TMemoryStream;
begin
  if HTML='' then
    HTML:='<html><body></body></html>';
  ms:=TMemoryStream.Create;
  try
    try
      ms.Write(HTML[1],length(HTML));
      ms.Position:=0;
      LoadHTML(Target,ms,Anchor);
    except
      on E: Exception do begin
        debugln(['TWikiSearchDemoForm.LoadHTML ',E.Message]);
      end;
    end;
  finally
    ms.Free;
  end;
end;

procedure TWikiSearchDemoForm.LoadHTML(Target: TIpHtmlPanel; aStream: TStream;
  Anchor: string);
var
  NewHTML: TIpHtml;
begin
  try
    NewHTML:=TIpHtml.Create; // Beware: Will be freed automatically by IpHtmlPanel
    Target.SetHtml(NewHTML);
    NewHTML.LoadFromStream(aStream);
    // ToDo: fix TIpHtmlNodeA.MakeVisible, the areas are all 0,0,0,0 at this time
    if Anchor<>'' then
      Target.MakeAnchorVisible(Anchor+'/'); // ipHTML stores anchor names with / at end
  except
    on E: Exception do begin
      debugln(['TWikiSearchDemoForm.LoadHTML ',E.Message]);
    end;
  end;
end;

procedure TWikiSearchDemoForm.ShowOptions;
begin
  if WikiSearchOptsWnd=nil then begin
    WikiSearchOptsWnd:=TWikiSearchOptsWnd.Create(Self);
    WikiSearchOptsWnd.OnOptionsChanged:=@WikiSearchOptsWndOptionsChanged;
  end;
  WikiSearchOptsWnd.UpdateAvailableLanguages;
  WikiSearchOptsWnd.ShowOnTop;
end;

procedure TWikiSearchDemoForm.ViewSource(aTitle, aSource: string;
  aHighlighter: TSynCustomHighlighter);
var
  SrcEdit: TSynEdit;
  ViewSrcForm: TForm;
begin
  ViewSrcForm:=TForm.Create(Self);
  ViewSrcForm.DisableAutoSizing;
  try
    ViewSrcForm.Caption:=aTitle;
    ViewSrcForm.Position:=poScreenCenter;
    ViewSrcForm.Width:=500;
    ViewSrcForm.Height:=500;

    SrcEdit:=TSynEdit.Create(ViewSrcForm);
    SrcEdit.Align:=alClient;
    SrcEdit.Text:=aSource;
    SrcEdit.Highlighter:=aHighlighter;
    SrcEdit.Parent:=ViewSrcForm;
    SrcEdit.ReadOnly:=true;

    ViewSrcForm.Show;
  finally
    ViewSrcForm.EnableAutoSizing;
  end;
end;

end.