2
0

chmfilewriter.pas 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. { Copyright (C) <2005> <Andrew Haines> chmfilewriter.pas
  2. This library is free software; you can redistribute it and/or modify it
  3. under the terms of the GNU Library General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or (at your
  5. option) any later version.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  8. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  9. for more details.
  10. You should have received a copy of the GNU Library General Public License
  11. along with this library; if not, write to the Free Software Foundation,
  12. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  13. }
  14. {
  15. See the file COPYING.FPC, included in this distribution,
  16. for details about the copyright.
  17. }
  18. unit chmfilewriter;
  19. {$mode objfpc}{$H+}
  20. interface
  21. uses
  22. Strings, Classes, SysUtils, chmwriter, inifiles, contnrs, chmsitemap, avl_tree,
  23. {for html scanning } dom,SAX_HTML,dom_html;
  24. type
  25. TChmProject = class;
  26. TChmProjectErrorKind = (chmerror,chmwarning,chmhint,chmnote,chmnone);
  27. TChmProgressCB = procedure (Project: TChmProject; CurrentFile: String) of object;
  28. TChmErrorCB = procedure (Project: TChmProject;errorkind:TChmProjectErrorKind;msg:String;detaillevel:integer=0);
  29. { TChmProject }
  30. TChmProject = class
  31. private
  32. FAutoFollowLinks: Boolean;
  33. FDefaultFont: String;
  34. FDefaultPage: String;
  35. FFiles: TStrings;
  36. FIndexFileName: String;
  37. FMakeBinaryTOC: Boolean;
  38. FMakeBinaryIndex: Boolean;
  39. FMakeSearchable: Boolean;
  40. FFileName: String;
  41. FOnProgress: TChmProgressCB;
  42. FOnError : TChmErrorCB;
  43. FOutputFileName: String;
  44. FTableOfContentsFileName: String;
  45. FTitle: String;
  46. FWindows : TObjectList;
  47. FMergeFiles : TStringlist;
  48. fDefaultWindow : string;
  49. fScanHtmlContents : Boolean;
  50. fOtherFiles : TStrings; // Files found in a scan.
  51. fAllowedExtensions: TStringList;
  52. fTotalFileList : TAvlTree;
  53. fAnchorList : TStringList;
  54. FSpareString : TStringIndex;
  55. FBasePath : String; // location of the .hhp file. Needed to resolve relative paths
  56. FReadmeMessage : String; // readme message
  57. FToc,
  58. FIndex : TCHMSiteMap;
  59. FTocStream,
  60. FIndexStream : TMemoryStream;
  61. protected
  62. function GetData(const DataName: String; out PathInChm: String; out FileName: String; var Stream: TStream): Boolean;
  63. procedure LastFileAdded(Sender: TObject);
  64. procedure readIniOptions(keyvaluepairs:tstringlist);
  65. procedure ScanHtml;
  66. procedure ScanList(toscan,newfiles:TStrings;recursion:boolean);
  67. procedure ScanSitemap(sitemap:TChmSiteMap;newfiles:TStrings;recursion:boolean);
  68. function FileInTotalList(const s:String):boolean;
  69. function SanitizeURL(const basepath, instring, localpath, localname:string; var outstring:String):Boolean;
  70. public
  71. constructor Create; virtual;
  72. destructor Destroy; override;
  73. procedure LoadFromFile(AFileName: String); virtual;
  74. procedure LoadFromhhp (AFileName:String;LeaveInclude:Boolean); virtual;
  75. procedure SaveToFile(AFileName: String); virtual;
  76. procedure WriteChm(AOutStream: TStream); virtual;
  77. procedure ShowUndefinedAnchors;
  78. function ProjectDir: String;
  79. procedure LoadSitemaps;
  80. procedure AddFileWithContext(contextid:integer;filename:ansistring;contextname:ansistring='');
  81. procedure Error(errorkind:TChmProjectErrorKind;msg:String;detaillevel:integer=0);
  82. // though stored in the project file, it is only there for the program that uses the unit
  83. // since we actually write to a stream
  84. property OutputFileName: String read FOutputFileName write FOutputFileName;
  85. property FileName: String read FFileName write FFileName;
  86. property Files: TStrings read FFiles write FFiles; // html files
  87. property OtherFiles: TStrings read FOtherFiles write FOtherFiles; // other files (.css, img etc)
  88. property AutoFollowLinks: Boolean read FAutoFollowLinks write FAutoFollowLinks;
  89. property TableOfContentsFileName: String read FTableOfContentsFileName write FTableOfContentsFileName;
  90. property MakeBinaryTOC: Boolean read FMakeBinaryTOC write FMakeBinaryTOC;
  91. property MakeBinaryIndex: Boolean read FMakeBinaryIndex write FMakeBinaryIndex;
  92. property Title: String read FTitle write FTitle;
  93. property IndexFileName: String read FIndexFileName write FIndexFileName;
  94. property MakeSearchable: Boolean read FMakeSearchable write FMakeSearchable;
  95. property DefaultPage: String read FDefaultPage write FDefaultPage;
  96. property DefaultFont: String read FDefaultFont write FDefaultFont;
  97. property Windows :TObjectList read FWindows write FWindows;
  98. property MergeFiles :TStringlist read FMergeFiles write FMergefiles;
  99. property OnProgress: TChmProgressCB read FOnProgress write FOnProgress;
  100. property OnError : TChmErrorCB read FOnError write FOnError;
  101. property DefaultWindow : String read FDefaultWindow write FDefaultWindow;
  102. property ScanHtmlContents : Boolean read fScanHtmlContents write fScanHtmlContents;
  103. property ReadmeMessage : String read FReadmeMessage write FReadmeMessage;
  104. property AllowedExtensions : TStringList read FAllowedExtensions;
  105. end;
  106. TChmContextNode = Class
  107. URLName : AnsiString;
  108. ContextNumber : Integer;
  109. ContextName : AnsiString;
  110. End;
  111. Const
  112. ChmErrorKindText : array[TCHMProjectErrorKind] of string = ('Error','Warning','Hint','Note','');
  113. implementation
  114. uses XmlCfg, CHMTypes;
  115. type
  116. { TFirstReference }
  117. TFirstReference = class
  118. public
  119. Location: string;
  120. constructor Create(Const ALocation: string);
  121. end;
  122. { TFirstReference }
  123. constructor TFirstReference.Create(const ALocation: string);
  124. begin
  125. Location := ALocation;
  126. end;
  127. { TChmProject }
  128. function TChmProject.GetData(const DataName: String; out PathInChm: String; out
  129. FileName: String; var Stream: TStream): Boolean;
  130. begin
  131. Result := False; // Return true to abort compressing files
  132. TMemoryStream(Stream).LoadFromFile(ProjectDir+DataName);
  133. // clean up the filename
  134. FileName := StringReplace(ExtractFileName(DataName), '\', '/', [rfReplaceAll]);
  135. FileName := StringReplace(FileName, '//', '/', [rfReplaceAll]);
  136. PathInChm := '/'+ExtractFilePath(DataName);
  137. if Assigned(FOnProgress) then FOnProgress(Self, DataName);
  138. end;
  139. procedure TChmProject.LastFileAdded(Sender: TObject);
  140. var
  141. Writer: TChmWriter;
  142. begin
  143. // Assign the TOC and index files
  144. Writer := TChmWriter(Sender);
  145. {$ifdef chmindex}
  146. Writeln('binindex filename ',IndexFileName);
  147. {$endif}
  148. if assigned(FIndexStream) then
  149. begin
  150. FIndexStream.position:=0;
  151. Writer.AppendIndex(FIndexStream);
  152. if MakeBinaryIndex then
  153. begin
  154. {$ifdef chmindex}
  155. Writeln('into binindex ');
  156. {$endif}
  157. Writer.AppendBinaryIndexFromSiteMap(FIndex,False);
  158. end;
  159. end;
  160. if assigned(FTocStream) then
  161. begin
  162. Writer.AppendTOC(FTOCStream);
  163. if MakeBinaryTOC then
  164. begin
  165. Writer.AppendBinaryTOCFromSiteMap(FToc);
  166. end;
  167. end;
  168. if not assigned(sender) then
  169. Writer.Free;
  170. end;
  171. constructor TChmProject.Create;
  172. begin
  173. FFiles := TStringList.Create;
  174. FOtherFiles := TStringList.Create;
  175. FAllowedExtensions:=TStringList.Create;
  176. FAllowedExtensions.add('.HTM');
  177. FAllowedExtensions.add('.HTML');
  178. FWindows:=TObjectList.Create(True);
  179. FMergeFiles:=TStringlist.Create;
  180. ScanHtmlContents:=False;
  181. FTotalFileList:=TAvlTree.Create(@CompareStrings);
  182. FSparestring :=TStringIndex.Create;
  183. fAnchorList := TStringList.Create;
  184. fAnchorList.Sorted := True;
  185. fAnchorList.OwnsObjects := True;
  186. end;
  187. destructor TChmProject.Destroy;
  188. var i : integer;
  189. begin
  190. for i:=0 to ffiles.count -1 do
  191. ffiles.objects[i].free;
  192. FMergeFiles.Free;
  193. FFiles.Free;
  194. FOtherFiles.Free;
  195. FWindows.Free;
  196. FSpareString.Free;
  197. FTotalFileList.FreeAndClear;
  198. FTotalFileList.Free;
  199. fAllowedExtensions.Free;
  200. FToc.free;
  201. FIndex.free;
  202. FTocStream.Free;
  203. FIndexStream.Free;
  204. fAnchorList.Free;
  205. inherited Destroy;
  206. end;
  207. Type
  208. TSectionEnum = (secOptions,secWindows,secFiles,secMergeFiles,secAlias,secMap,secInfoTypes,secTextPopups,secUnknown);
  209. TOptionEnum = (OPTAUTO_INDEX,OPTAUTO_TOC,OPTBINARY_INDEX,OPTBINARY_TOC,OPTCITATION,
  210. OPTCOMPRESS,OPTCOPYRIGHT,OPTCOMPATIBILITY,OPTCOMPILED_FILE,OPTCONTENTS_FILE,
  211. OPTCREATE_CHI_FILE,OPTDBCS,OPTDEFAULT_FONT,OPTDEFAULT_WINDOW,OPTDEFAULT_TOPIC,
  212. OPTDISPLAY_COMPILE_NOTES,OPTDISPLAY_COMPILE_PROGRESS,OPTENHANCED_DECOMPILATION,OPTERROR_LOG_FILE,OPTFLAT,
  213. OPTFULL_TEXT_SEARCH_STOP_LIST,OPTFULL_TEXT_SEARCH,OPTIGNORE,OPTINDEX_FILE,OPTLANGUAGE,OPTPREFIX,
  214. OPTSAMPLE_STAGING_PATH,OPTSAMPLE_LIST_FILE,OPTTMPDIR,OPTTITLE,OPTCUSTOM_TAB,OPTUNKNOWN);
  215. Const
  216. SectionNames : Array[TSectionEnum] of String =
  217. ('OPTIONS','WINDOWS','FILES','MERGE FILES','ALIAS','MAP','INFOTYPES','TEXT POPUPS','UNKNOWN');
  218. OptionKeys : array [TOptionEnum] of String =
  219. ('AUTO INDEX','AUTO TOC','BINARY INDEX','BINARY TOC','CITATION',
  220. 'COMPRESS','COPYRIGHT','COMPATIBILITY','COMPILED FILE','CONTENTS FILE',
  221. 'CREATE CHI FILE','DBCS','DEFAULT FONT','DEFAULT WINDOW','DEFAULT TOPIC',
  222. 'DISPLAY COMPILE NOTES','DISPLAY COMPILE PROGRESS','ENHANCED DECOMPILATION','ERROR LOG FILE','FLAT',
  223. 'FULL-TEXT SEARCH STOP LIST','FULL-TEXT SEARCH','IGNORE','INDEX FILE','LANGUAGE','PREFIX',
  224. 'SAMPLE STAGING PATH','SAMPLE LIST FILE','TMPDIR','TITLE','CUSTOM TAB','UNKNOWN');
  225. function FindSectionName (const name:string):TSectionEnum;
  226. begin
  227. result:=low(TSectionEnum);
  228. while (result<secUnknown) and (name<>SectionNames[Result]) do
  229. inc(result);
  230. end;
  231. function FindOptionName(Const name:string):TOptionEnum;
  232. begin
  233. result:=low(TOptionEnum);
  234. while (result<optUnknown) and (name<>OptionKeys[Result]) do
  235. inc(result);
  236. end;
  237. procedure TChmProject.readIniOptions(keyvaluepairs:tstringlist);
  238. var i : integer;
  239. Opt : TOptionEnum;
  240. OptVal,
  241. OptValUpper : string;
  242. begin
  243. for i:=0 to keyvaluepairs.count-1 do
  244. begin
  245. Opt:=findoptionname(uppercase(keyvaluepairs.names[i]));
  246. optval :=keyvaluepairs.valuefromindex[i];
  247. optvalupper:=uppercase(OptVal);
  248. case Opt Of
  249. OPTAUTO_INDEX : ;
  250. OPTAUTO_TOC : ;
  251. OPTBINARY_INDEX : MakeBinaryIndex:=optvalupper='YES';
  252. OPTBINARY_TOC : MakeBinaryToc :=optvalupper='YES';
  253. OPTCITATION : ;
  254. OPTCOMPRESS : ; // Doesn't seem to have effect in workshop
  255. OPTCOPYRIGHT : ;
  256. OPTCOMPATIBILITY : ;
  257. OPTCOMPILED_FILE : OutputFilename:=optval;
  258. OPTCONTENTS_FILE : TableOfContentsFileName:=optval;
  259. OPTCREATE_CHI_FILE : ;
  260. OPTDBCS : ; // What this field makes unicode is not known?
  261. OPTDEFAULT_FONT : defaultfont:=optval;
  262. OPTDEFAULT_WINDOW : defaultwindow:=optval;
  263. OPTDEFAULT_TOPIC : defaultpage:=optval;
  264. OPTDISPLAY_COMPILE_NOTES : ;
  265. OPTDISPLAY_COMPILE_PROGRESS : ;
  266. OPTENHANCED_DECOMPILATION : ;
  267. OPTERROR_LOG_FILE : ;
  268. OPTFLAT : ;
  269. OPTFULL_TEXT_SEARCH_STOP_LIST: ;
  270. OPTFULL_TEXT_SEARCH : MakeSearchable:=optvalupper='YES';
  271. OPTIGNORE : ;
  272. OPTINDEX_FILE : Indexfilename:=optval;
  273. OPTLANGUAGE : ;
  274. OPTPREFIX : ; // doesn't seem to have effect
  275. OPTSAMPLE_STAGING_PATH : ;
  276. OPTSAMPLE_LIST_FILE : ;
  277. OPTTMPDIR : ;
  278. OPTTITLE : Title:=optval;
  279. OPTCUSTOM_TAB : ;
  280. OPTUNKNOWN : ; // can be used for errors on unknown keys
  281. end;
  282. end;
  283. end;
  284. procedure TChmProject.LoadFromFile(AFileName: String);
  285. var
  286. Cfg: TXMLConfig;
  287. MergeFileCount,
  288. WinCount,
  289. FileCount: Integer;
  290. I : Integer;
  291. nd : TChmContextNode;
  292. win: TCHMWindow;
  293. s : String;
  294. begin
  295. Cfg := TXMLConfig.Create(nil);
  296. Cfg.Filename := AFileName;
  297. FileName := AFileName;
  298. FBasePath:=extractfilepath(expandfilename(afilename));
  299. Files.Clear;
  300. FileCount := Cfg.GetValue('Files/Count/Value', 0);
  301. for I := 0 to FileCount-1 do
  302. begin
  303. nd:=TChmContextNode.Create;
  304. nd.urlname:=Cfg.GetValue('Files/FileName'+IntToStr(I)+'/Value','');
  305. nd.contextnumber:=Cfg.GetValue('Files/FileName'+IntToStr(I)+'/ContextNumber',0);
  306. nd.contextname:=Cfg.GetValue('Files/FileName'+IntToStr(I)+'/ContextName','');
  307. Files.AddObject(nd.URLNAME,nd);
  308. end;
  309. FileCount := Cfg.GetValue('OtherFiles/Count/Value', 0);
  310. for I := 0 to FileCount-1 do
  311. begin
  312. s:=Cfg.GetValue('OtherFiles/FileName'+IntToStr(I)+'/Value','');
  313. OtherFiles.Add(s);
  314. end;
  315. WinCount:= Cfg.GetValue('Windows/Count/Value', 0);
  316. for i:=0 To WinCount-1 do
  317. begin
  318. win:=TCHMWindow.Create;
  319. win.loadfromxml(cfg,'Windows/item'+inttostr(i)+'/');
  320. fwindows.add(win);
  321. end;
  322. Mergefilecount:=Cfg.getValue('MergeFiles/Count/Value', 0);
  323. for i:=0 To MergeFileCount-1 do
  324. Mergefiles.add(Cfg.getValue('MergeFiles/FileName'+IntToStr(I)+'/value',''));
  325. // load some values that changed key backwards compatible.
  326. IndexFileName := Cfg.GetValue('Files/IndexFile/Value','');
  327. if IndexFileName='' Then
  328. IndexFileName := Cfg.GetValue('Settings/IndexFile/Value','');
  329. TableOfContentsFileName := Cfg.GetValue('Files/TOCFile/Value','');
  330. If TableOfContentsFileName='' then
  331. TableOfContentsFileName := Cfg.GetValue('Settings/TOCFile/Value','');
  332. // For chm file merging, bintoc must be false and binindex true. Change defaults in time?
  333. // OTOH, merging will be mostly done for fpdoc files, and that doesn't care about defaults.
  334. S:=Cfg.GetValue('Files/MakeBinaryTOC/Value', '');
  335. if s='' Then
  336. MakeBinaryTOC := Cfg.GetValue('Settings/MakeBinaryTOC/Value', True)
  337. else
  338. MakeBinaryTOC := Cfg.GetValue('Files/MakeBinaryTOC/Value', True);
  339. S:=Cfg.GetValue('Files/MakeBinaryIndex/Value', '');
  340. if s='' Then
  341. MakeBinaryIndex := Cfg.GetValue('Settings/MakeBinaryIndex/Value', False)
  342. else
  343. MakeBinaryIndex := Cfg.GetValue('Files/MakeBinaryIndex/Value', False);
  344. AutoFollowLinks := Cfg.GetValue('Settings/AutoFollowLinks/Value', False);
  345. MakeSearchable := Cfg.GetValue('Settings/MakeSearchable/Value', False);
  346. DefaultPage := Cfg.GetValue('Settings/DefaultPage/Value', '');
  347. Title := Cfg.GetValue('Settings/Title/Value', '');
  348. OutputFileName := Cfg.GetValue('Settings/OutputFileName/Value', '');
  349. DefaultFont := Cfg.GetValue('Settings/DefaultFont/Value', '');
  350. DefaultWindow:= Cfg.GetValue('Settings/DefaultWindow/Value', '');
  351. ScanHtmlContents:= Cfg.GetValue('Settings/ScanHtmlContents/Value', False);
  352. Cfg.Free;
  353. end;
  354. function cleanupstring(const s:string):string;
  355. var
  356. i:integer;
  357. begin
  358. i:=pos(';',s);
  359. if i>0 then
  360. result:=trim(copy(s,1,i-1))
  361. else
  362. result:=trim(s);
  363. end;
  364. procedure TChmProject.LoadFromhhp (AFileName:String;LeaveInclude:Boolean);
  365. // leaveinclude=true leaves includefiles includefiles.
  366. procedure addalias(const key,value :string);
  367. var i,j : integer;
  368. node: TCHMContextNode;
  369. keyupper,valueupper : string;
  370. begin
  371. { Defaults other than global }
  372. MakeBinaryIndex:=True;
  373. {$ifdef hhp_debug}
  374. writeln('alias entry:',key,'=',value);
  375. {$endif}
  376. keyupper:=uppercase(value);
  377. i:=0; j:=files.count;
  378. while (i<j) and (uppercase(TCHMContextnode(files.objects[i]).UrlName)<>keyupper) do
  379. inc(i);
  380. if i=j then
  381. begin
  382. {$ifdef hhp_debug}
  383. writeln('alias new node:',key);
  384. {$endif}
  385. node:=TCHMContextNode.create;
  386. valueupper:=stringReplace(value, '\', '/', [rfReplaceAll]);
  387. valueupper:= StringReplace(valueupper, '//', '/', [rfReplaceAll]);
  388. node.URLName:=valueupper;
  389. node.contextname:=key;
  390. end
  391. else
  392. begin
  393. node:=TCHMContextNode(Files.objects[i]);
  394. node.ContextName:=key;
  395. end;
  396. end;
  397. procedure processalias(strs:TStringlist);
  398. var i,j : integer;
  399. s : string;
  400. strls2:tstringlist;
  401. begin
  402. for i:=0 to strs.count-1 do
  403. begin
  404. s:=cleanupstring(strs[i]);
  405. if uppercase(copy(s,1,8))='#INCLUDE' then
  406. begin
  407. delete(s,1,8);
  408. s:=trim(s);
  409. if fileexists(s) then
  410. begin
  411. strls2:=TstringList.create;
  412. strls2.loadfromfile(s);
  413. processalias(strls2);
  414. strls2.free;
  415. end;
  416. end
  417. else
  418. begin
  419. s:=cleanupstring(s);
  420. j:=pos('=',s);
  421. if j>0 then
  422. addalias(trim(copy(s,1,j-1)),copy(s,j+1,length(s)-j));
  423. end;
  424. end;
  425. end;
  426. procedure addmap(const key,value :string);
  427. var i,j : integer;
  428. node: TCHMContextNode;
  429. keyupper : string;
  430. begin
  431. {$ifdef hhp_debug}
  432. writeln('map entry:',key,'=',value);
  433. {$endif}
  434. keyupper:=uppercase(key);
  435. i:=0; j:=files.count;
  436. while (i<j) and (uppercase(TCHMContextnode(files.objects[i]).contextname)<>keyupper) do
  437. inc(i);
  438. if i=j then
  439. raise Exception.create('context "'+key+'" not found!')
  440. else
  441. begin
  442. node:=TCHMContextNode(Files.objects[i]);
  443. node.Contextnumber:=strtointdef(value,0);
  444. end;
  445. end;
  446. procedure processmap(strs:TStringlist);
  447. var i,j : integer;
  448. s : string;
  449. strls2:tstringlist;
  450. begin
  451. for i:=0 to strs.count-1 do
  452. begin
  453. s:=cleanupstring(strs[i]);
  454. {$ifdef hhp_debug}
  455. writeln('map item:',s);
  456. {$endif}
  457. if uppercase(copy(s,1,8))='#INCLUDE' then
  458. begin
  459. delete(s,1,8);
  460. s:=trim(s);
  461. if fileexists(s) then
  462. begin
  463. strls2:=TstringList.create;
  464. strls2.loadfromfile(s);
  465. processmap(strls2);
  466. strls2.free;
  467. end;
  468. end
  469. else
  470. begin
  471. s:=cleanupstring(s);
  472. if uppercase(copy(s,1,7))='#DEFINE' Then
  473. begin
  474. delete(s,1,7);
  475. s:=trim(s);
  476. j:=pos(' ',s);
  477. if j>0 then
  478. addmap(trim(copy(s,1,j-1)),copy(s,j+1,length(s)-j));
  479. end
  480. else
  481. begin
  482. {$ifdef hhp_debug}
  483. writeln('map leftover:',s);
  484. {$endif}
  485. end;
  486. end;
  487. end;
  488. end;
  489. var
  490. Fini : TMemIniFile; // TMemInifile is more compatible with Delphi. Delphi's API based TIniFile fails on .hhp files.
  491. secs,strs : TStringList;
  492. i,j : Integer;
  493. section : TSectionEnum;
  494. nd : TChmContextNode;
  495. begin
  496. { Defaults other than global }
  497. MakeBinaryIndex:=True;
  498. FBasePath:=extractfilepath(expandfilename(afilename));
  499. Fini:=TMeminiFile.Create(AFileName);
  500. secs := TStringList.create;
  501. strs := TStringList.create;
  502. fini.readsections(secs);
  503. // Do the files section first so that we can emit errors if
  504. // other sections reference unknown files.
  505. fini.readsectionvalues(SectionNames[secFiles] ,strs);
  506. if strs.count>0 then
  507. for j:=0 to strs.count-1 do
  508. begin
  509. nd:=TChmContextNode.Create;
  510. nd.urlname:=StringReplace(strs[j],'\', '/', [rfReplaceAll]);
  511. nd.contextnumber:=0;
  512. nd.contextname:='';
  513. Files.AddObject(nd.urlname,nd);
  514. end;
  515. // aliases also add file nodes.
  516. fini.readsectionvalues(SectionNames[secAlias] ,strs); // resolve all aliases.
  517. if strs.count>0 then
  518. processalias(strs);
  519. // map files only add to existing file nodes.
  520. fini.readsectionvalues(SectionNames[secmap] ,strs);
  521. if strs.count>0 then
  522. processmap(strs);
  523. for i:=0 to secs.count-1 do
  524. begin
  525. section:=FindSectionName(Uppercase(Secs[i]));
  526. if section<>secunknown then
  527. fini.readsectionvalues(secs[i] ,strs);
  528. case section of
  529. secOptions : readinioptions(strs);
  530. secWindows : for j:=0 to strs.count-1 do
  531. FWindows.add(TCHMWindow.Create(strs[j]));
  532. secFiles : ; // already done
  533. secMergeFiles: FMergeFiles.Assign(Strs); // just a filelist
  534. secAlias : ; // already done
  535. secMap : ; // already done
  536. secInfoTypes : ; // unused for now.
  537. secTextPopups: ; // rarely used.
  538. end;
  539. end;
  540. secs.free;
  541. strs.free;
  542. fini.free;
  543. ScanHtmlContents:=true;
  544. end;
  545. procedure TChmProject.AddFileWithContext(contextid:integer;filename:ansistring;contextname:ansistring='');
  546. var x : integer;
  547. nd : TChmContextNode;
  548. begin
  549. x:=files.indexof(filename);
  550. if x=-1 then
  551. begin
  552. nd:=TChmContextNode.Create;
  553. nd.urlname:=filename;
  554. nd.contextnumber:=contextid;
  555. nd.contextname:=contextname;
  556. Files.AddObject(nd.urlname,nd);
  557. end
  558. else
  559. begin
  560. nd:=TChmContextNode(files.objects[x]);
  561. if not assigned(nd) then
  562. begin
  563. nd:=TChmContextNode.Create;
  564. nd.urlname:=filename;
  565. files.objects[x]:=nd;
  566. end;
  567. nd.contextnumber:=contextid;
  568. nd.contextname:=contextname;
  569. end;
  570. end;
  571. procedure TChmProject.SaveToFile(AFileName: String);
  572. var
  573. Cfg: TXMLConfig;
  574. I : Integer;
  575. nd : TChmContextNode;
  576. begin
  577. Cfg := TXMLConfig.Create(nil);
  578. Cfg.StartEmpty := True;
  579. Cfg.Filename := AFileName;
  580. Cfg.Clear;
  581. Cfg.SetValue('Files/Count/Value', Files.Count);
  582. for I := 0 to Files.Count-1 do
  583. begin
  584. nd:=TChmContextNode(files.objects[i]);
  585. Cfg.SetValue('Files/FileName'+IntToStr(I)+'/Value', Files.Strings[I]);
  586. if assigned(nd) then
  587. begin
  588. Cfg.SetValue('Files/FileName'+IntToStr(I)+'/ContextNumber', nd.contextnumber);
  589. Cfg.SetValue('Files/FileName'+IntToStr(I)+'/ContextName', nd.contextname);
  590. end;
  591. end;
  592. Cfg.SetValue('OtherFiles/Count/Value', OtherFiles.Count);
  593. for I := 0 to OtherFiles.Count-1 do
  594. Cfg.SetValue('OtherFiles/FileName'+IntToStr(I)+'/Value', OtherFiles.Strings[I]);
  595. Cfg.SetValue('Windows/Count/Value', FWindows.count);
  596. for i:=0 To FWindows.Count-1 do
  597. TCHMWindow(FWindows[i]).savetoxml(cfg,'Windows/item'+inttostr(i)+'/');
  598. Cfg.SetValue('MergeFiles/Count/Value', FMergeFiles.count);
  599. for i:=0 To FMergeFiles.Count-1 do
  600. Cfg.SetValue('MergeFiles/FileName'+IntToStr(I)+'/value',FMergeFiles[i]);
  601. // delete legacy keys.
  602. Cfg.DeleteValue('Files/IndexFile/Value');
  603. Cfg.DeleteValue('Files/TOCFile/Value');
  604. Cfg.DeleteValue('Files/MakeBinaryTOC/Value');
  605. Cfg.DeleteValue('Files/MakeBinaryIndex/Value');
  606. Cfg.SetValue('Settings/IndexFile/Value', IndexFileName);
  607. Cfg.SetValue('Settings/TOCFile/Value', TableOfContentsFileName);
  608. Cfg.SetValue('Settings/MakeBinaryTOC/Value',MakeBinaryTOC);
  609. Cfg.SetValue('Settings/MakeBinaryIndex/Value',MakeBinaryIndex);
  610. Cfg.SetValue('Settings/AutoFollowLinks/Value', AutoFollowLinks);
  611. Cfg.SetValue('Settings/MakeSearchable/Value', MakeSearchable);
  612. Cfg.SetValue('Settings/DefaultPage/Value', DefaultPage);
  613. Cfg.SetValue('Settings/Title/Value', Title);
  614. Cfg.SetValue('Settings/OutputFileName/Value', OutputFileName);
  615. Cfg.SetValue('Settings/DefaultFont/Value', DefaultFont);
  616. Cfg.SetValue('Settings/DefaultWindow/Value', DefaultWindow);
  617. Cfg.SetValue('Settings/ScanHtmlContents/Value', ScanHtmlContents);
  618. Cfg.Flush;
  619. Cfg.Free;
  620. end;
  621. function TChmProject.ProjectDir: String;
  622. begin
  623. Result := ExtractFilePath(FileName);
  624. end;
  625. procedure TChmProject.Error(errorkind:TChmProjectErrorKind;msg:String;detaillevel:integer=0);
  626. begin
  627. if assigned(OnError) then
  628. OnError(self,errorkind,msg,detaillevel);
  629. end;
  630. const
  631. protocols : array[0..3] of string = ('HTTP:','FTP:','MS-ITS:', 'MAILTO:');
  632. protocollen : array[0..3] of integer= ( 5 ,4 ,7, 7);
  633. function TChmProject.SanitizeURL(const basepath,instring,localpath,localname:string;var outstring:String):Boolean;
  634. var i,j,len : integer;
  635. Anchor: String;
  636. begin
  637. result:=true; outstring:='';
  638. if instring='' then
  639. exit(false);
  640. len:=length(instring);
  641. if len=0 then
  642. exit(false);
  643. { Check for protocols before adding local path }
  644. i:=0;
  645. while (i<=high(protocols)) do
  646. begin
  647. if strlicomp(@protocols[i][1],@instring[1],protocollen[i])=0 then
  648. exit(false);
  649. inc(i);
  650. end;
  651. outstring:=localpath+instring;
  652. i:=pos('#',outstring);
  653. if i<>0 then begin
  654. if i > 1 then
  655. Anchor := outstring
  656. else
  657. Anchor := localname+outstring;
  658. j := fAnchorList.IndexOf(Anchor);
  659. if j < 0 then begin
  660. fAnchorList.AddObject(Anchor,TFirstReference.Create(localname));
  661. Anchor := '(new) '+Anchor;
  662. end;
  663. Error(CHMNote, 'Anchor found '+Anchor+' while scanning '+localname,1);
  664. delete(outstring,i,length(outstring)-i+1);
  665. end;
  666. outstring:=expandfilename(StringReplace(outstring,'%20',' ',[rfReplaceAll]));// expandfilename(instring));
  667. outstring:=extractrelativepath(basepath,outstring);
  668. outstring:=StringReplace(outstring,'\','/',[rfReplaceAll]);
  669. end;
  670. function TChmProject.FileInTotalList(const s:String):boolean;
  671. begin
  672. FSpareString.TheString:=S;
  673. result:=assigned(fTotalFileList.FindKey(FSpareString,@CompareStrings));
  674. end;
  675. procedure TChmProject.ScanList(toscan,newfiles:TStrings;recursion:boolean);
  676. // toscan, list to search for htmlfiles to scan.
  677. // newfiles, the resulting list of files.
  678. // totalfilelist, the list that contains all found and specified files to check against.
  679. // localfilelist (local var), files found in this file.
  680. var
  681. localpath : string;
  682. function findattribute(node:TDomNode;attributename:string):String;
  683. var
  684. Attributes: TDOMNamedNodeMap;
  685. atnode : TDomNode;
  686. n : integer;
  687. begin
  688. Result := '';
  689. if assigned(node) then
  690. begin
  691. Attributes:=node.Attributes;
  692. if assigned(attributes) then
  693. for n:=0 to attributes.length-1 do
  694. begin
  695. atnode :=attributes[n];
  696. if assigned(atnode) and (uppercase(atnode.nodename)=attributename) then
  697. exit(atnode.nodevalue);
  698. end;
  699. end;
  700. end;
  701. procedure checkattributes(node:TDomNode;attributename:string; const localname: string; filelist :TStringList);
  702. var
  703. fn : String;
  704. val : String;
  705. begin
  706. val := findattribute(node,attributename);
  707. if sanitizeurl(fbasepath,val,localpath,localname,fn) then
  708. if (Length(fn) > 0) { Skip links to self using named anchors }
  709. and not FileInTotalList(uppercase(fn)) then
  710. filelist.add(fn);
  711. end;
  712. function scantags(prnt:TDomNode; const localname: string; filelist:TStringlist):TDomNode;
  713. // Seach first matching tag in siblings
  714. var chld: TDomNode;
  715. s : ansistring;
  716. i : Integer;
  717. begin
  718. result:=nil;
  719. if assigned(prnt ) then
  720. begin
  721. chld:=prnt.firstchild;
  722. while assigned(chld) do
  723. begin
  724. scantags(chld, localname, filelist); // depth first.
  725. if (chld is TDomElement) then
  726. begin
  727. s:=uppercase(tdomelement(chld).tagname);
  728. if s='LINK' then
  729. begin
  730. //printattributes(chld,'');
  731. checkattributes(chld,'HREF',localname,filelist);
  732. end;
  733. if s='IMG'then
  734. begin
  735. //printattributes(chld,'');
  736. checkattributes(chld,'SRC',localname,filelist);
  737. end;
  738. if s='A'then
  739. begin
  740. //printattributes(chld,'');
  741. checkattributes(chld,'HREF',localname,filelist);
  742. s := findattribute(chld,'NAME');
  743. if s <> '' then
  744. begin
  745. i := fAnchorList.IndexOf(localname+'#'+s);
  746. if i < 0 then begin
  747. fAnchorList.Add(localname+'#'+s);
  748. Error(ChmNote,'New Anchor with name '+s+' found while scanning '+localname,1);
  749. end else if fAnchorList.Objects[i] = nil then
  750. Error(chmwarning,'Duplicate anchor definitions with name '+s+' found while scanning '+localname,1)
  751. else begin
  752. fAnchorList.Objects[i].Free;
  753. fAnchorList.Objects[i] := nil;
  754. Error(ChmNote,'Anchor with name '+s+' defined while scanning '+localname,1);
  755. end;
  756. end;
  757. end;
  758. end;
  759. chld:=chld.nextsibling;
  760. end;
  761. end;
  762. end;
  763. var
  764. localfilelist: TStringList;
  765. domdoc : THTMLDocument;
  766. i,j : Integer;
  767. fn,s : string;
  768. ext : String;
  769. tmplst : Tstringlist;
  770. strrec : TStringIndex;
  771. //localpath : string;
  772. function trypath(const vn:string):boolean;
  773. var vn2: String;
  774. strrec : TStringIndex;
  775. begin
  776. vn2:=uppercase(vn);
  777. if FileInTotalList(vn2) then
  778. begin
  779. Error(ChmNote,'Found duplicate file '+vn+' while scanning '+fn,1);
  780. exit(true);
  781. end;
  782. result:=false;
  783. if fileexists(vn) then // correct for relative path .html file?
  784. begin
  785. result:=true;
  786. StrRec:=TStringIndex.Create;
  787. StrRec.TheString:=vn2;
  788. StrRec.Strid :=0;
  789. fTotalFileList.Add(StrRec);
  790. newfiles.add(vn);
  791. Error(ChmNote,'Found file '+vn+' while scanning '+fn,1);
  792. end;
  793. end;
  794. begin
  795. localfilelist:=TStringList.Create;
  796. for j:=0 to toscan.count-1 do
  797. begin
  798. fn:=toscan[j];
  799. localfilelist.clear;
  800. if (FAllowedExtensions.Indexof(uppercase(extractfileext(fn)))<>-1) then
  801. begin
  802. if fileexists(fn) then
  803. begin
  804. domdoc:=THtmlDocument.Create;
  805. try
  806. Error(chmnote,'Scanning file '+fn+'.',5);
  807. ReadHtmlFile(domdoc,fn);
  808. localpath:=extractfilepath(fn);
  809. if (length(localpath)>0) and not (localpath[length(localpath)] in ['/','\']) then
  810. localpath:=localpath+pathsep;
  811. scantags(domdoc,extractfilename(fn),localfilelist);
  812. for i:=0 to localFilelist.count-1 do
  813. begin
  814. s:=localfilelist[i];
  815. if not trypath(s) then
  816. // if not trypath(localpath+s) then
  817. Error(ChmWarning,'Found file '+s+' while scanning '+fn+', but couldn''t find it on disk',2);
  818. end;
  819. except
  820. on e:exception do
  821. Error(ChmError,'Html parsing '+fn+', failed. Please submit a bug.');
  822. end;
  823. domdoc.free;
  824. end
  825. else
  826. begin
  827. Error(chmnote,'Can''t find file '+fn+' to scan it.',5);
  828. end;
  829. end
  830. else
  831. Error(chmnote,'Not scanning file because of unknown extension '+fn,5);
  832. end;
  833. localfilelist.free;
  834. if (newfiles.count>0) and recursion then
  835. begin
  836. tmplst:=TStringList.Create;
  837. scanlist(newfiles,tmplst,true);
  838. newfiles.addstrings(tmplst);
  839. tmplst.free;
  840. end;
  841. end;
  842. procedure TChmProject.ScanSitemap(sitemap:TChmSiteMap;newfiles:TStrings;recursion:boolean);
  843. procedure scanitems(it:TChmSiteMapItems);
  844. var i : integer;
  845. x : TChmSiteMapItem;
  846. s : string;
  847. strrec : TStringIndex;
  848. begin
  849. for i:=0 to it.count -1 do
  850. begin
  851. x:=it.item[i];
  852. if sanitizeurl(fbasepath,x.local,'','Site Map for '+x.Text,S) then // sanitize, remove stuff etc.
  853. begin
  854. if not FileInTotalList(uppercase(s)) then
  855. begin
  856. if fileexists(s) then
  857. begin
  858. Error(chmnote,'Good url: '+s+'.',5);
  859. StrRec:=TStringIndex.Create;
  860. StrRec.TheString:=uppercase(s);
  861. StrRec.Strid :=0;
  862. fTotalFileList.Add(StrRec);
  863. newfiles.add(s);
  864. end
  865. else
  866. Error(chmnote,'duplicate url: '+s+'.',5);
  867. end
  868. else
  869. Error(chmnote,'duplicate url: '+s+'.',5);
  870. end
  871. else
  872. Error(chmnote,'Bad url: '+s+'.',5);
  873. if assigned(x.children) and (x.children.count>0) then
  874. scanitems(x.children);
  875. end;
  876. end;
  877. var i : integer;
  878. localfilelist: TStringList;
  879. begin
  880. localfilelist:=TStringList.Create;
  881. scanitems(sitemap.items);
  882. scanlist(newfiles,localfilelist,true);
  883. newfiles.addstrings(localfilelist);
  884. localfilelist.free;
  885. end;
  886. procedure TChmProject.ScanHtml;
  887. var
  888. helplist,
  889. localfilelist: TStringList;
  890. i : integer;
  891. strrec : TStringIndex;
  892. begin
  893. for i:=0 to otherfiles.count-1 do
  894. begin
  895. StrRec:=TStringIndex.Create;
  896. StrRec.TheString:=uppercase(otherfiles[i]);
  897. StrRec.Strid :=0;
  898. fTotalFileList.Add(StrRec);
  899. end;
  900. for i:=0 to files.count-1 do
  901. begin
  902. StrRec:=TStringIndex.Create;
  903. StrRec.TheString:=uppercase(files[i]);
  904. StrRec.Strid :=0;
  905. fTotalFileList.Add(StrRec);
  906. end;
  907. localfilelist:= TStringList.create;
  908. scanlist(ffiles,localfilelist,true);
  909. otherfiles.addstrings(localfilelist);
  910. localfilelist.clear;
  911. if (FDefaultpage<>'') and (not FileInTotalList(uppercase(fdefaultpage))) then
  912. begin
  913. Error(chmnote,'Scanning default file : '+fdefaultpage+'.',3);
  914. helplist:=TStringlist.Create;
  915. helplist.add(fdefaultpage);
  916. scanlist(helplist,localfilelist,true);
  917. otherfiles.addstrings(localfilelist);
  918. localfilelist.clear;
  919. end;
  920. if assigned(FToc) then
  921. begin
  922. Error(chmnote,'Scanning TOC file : '+FTableOfContentsFileName+'.',3);
  923. try
  924. scansitemap(ftoc,localfilelist,true);
  925. otherfiles.addstrings(localfilelist);
  926. except
  927. on e: Exception do
  928. error(chmerror,'Error scanning TOC file ('+FTableOfContentsFileName+')');
  929. end;
  930. end;
  931. LocalFileList.clear;
  932. if assigned(FIndex) then
  933. begin
  934. Error(chmnote,'Scanning Index file : '+FIndexFileName+'.',3);
  935. try
  936. scansitemap(FIndex,localfilelist,true);
  937. otherfiles.addstrings(localfilelist);
  938. except
  939. on e: Exception do
  940. error(chmerror,'Error scanning index file ('+FIndexFileName+')');
  941. end;
  942. end;
  943. localfilelist.free;
  944. end;
  945. procedure TChmProject.WriteChm(AOutStream: TStream);
  946. var
  947. Writer : TChmWriter;
  948. TOCStream,
  949. IndexStream: TFileStream;
  950. nd : TChmContextNode;
  951. I : Integer;
  952. begin
  953. LoadSiteMaps;
  954. // Scan html for "rest" files.
  955. If ScanHtmlContents Then
  956. ScanHtml; // Since this is slowing we opt to skip this step, and only do this on html load.
  957. IndexStream := nil;
  958. TOCStream := nil;
  959. Writer := TChmWriter.Create(AOutStream, False);
  960. // our callback to get data
  961. Writer.OnGetFileData := @GetData;
  962. Writer.OnLastFile := @LastFileAdded;
  963. // give it the list of html files
  964. Writer.FilesToCompress.AddStrings(Files);
  965. // give it the list of other files
  966. Writer.FilesToCompress.AddStrings(OtherFiles);
  967. // now some settings in the chm
  968. Writer.DefaultPage := DefaultPage;
  969. Writer.Title := Title;
  970. Writer.DefaultFont := DefaultFont;
  971. Writer.FullTextSearch := MakeSearchable;
  972. Writer.HasBinaryTOC := MakeBinaryTOC;
  973. Writer.HasBinaryIndex := MakeBinaryIndex;
  974. Writer.IndexName := ExtractFileName(IndexFileName);
  975. Writer.TocName := ExtractFileName(TableOfContentsFileName);
  976. Writer.ReadmeMessage := ReadmeMessage;
  977. Writer.DefaultWindow := FDefaultWindow;
  978. for i:=0 to files.count-1 do
  979. begin
  980. nd:=TChmContextNode(files.objects[i]);
  981. if not fileexists(files[i]) then
  982. Error(chmWarning,'File '+Files[i]+' does not exist');
  983. if assigned(nd) and (nd.contextnumber<>0) then
  984. Writer.AddContext(nd.ContextNumber,files[i]);
  985. end;
  986. if FWIndows.Count>0 then
  987. Writer.Windows:=FWIndows;
  988. if FMergeFiles.Count>0 then
  989. Writer.Mergefiles:=FMergeFiles;
  990. if assigned(ftoc) then
  991. Writer.TocSitemap:=ftoc;
  992. // and write!
  993. Error(chmnone,'Writing CHM '+OutputFileName,0);
  994. Writer.Execute;
  995. if Assigned(TOCStream) then TOCStream.Free;
  996. if Assigned(IndexStream) then IndexStream.Free;
  997. Writer.Free;
  998. end;
  999. procedure TChmProject.ShowUndefinedAnchors;
  1000. var
  1001. i:Integer;
  1002. begin
  1003. for i := 0 to fAnchorList.Count-1 do
  1004. if fAnchorList.Objects[i] <> nil then
  1005. Error(chmerror,'Anchor '+fAnchorList[i]+' undefined; first use '+TFirstReference(fAnchorList.Objects[i]).Location);
  1006. end;
  1007. procedure TChmProject.LoadSitemaps;
  1008. // #IDXHDR (merged files) goes into the system file, and need to keep TOC sitemap around
  1009. begin
  1010. if FTableOfContentsFileName<>'' then
  1011. begin
  1012. if fileexists(FTableOfContentsFileName) then
  1013. begin
  1014. FTocStream:=TMemoryStream.Create;
  1015. try
  1016. FTocStream.loadfromfile(FTableOfContentsFilename);
  1017. writeln(ftableofcontentsfilename, ' ' ,ftocstream.size);
  1018. FTocStream.Position:=0;
  1019. FToc:=TChmSiteMap.Create(sttoc);
  1020. FToc.loadfromstream(FTocStream);
  1021. ftoc.savetofile('bla.something');
  1022. except
  1023. on e:exception do
  1024. begin
  1025. error(chmerror,'Error loading TOC file '+FTableOfContentsFileName);
  1026. freeandnil(ftoc); freeandnil(FTocStream);
  1027. end;
  1028. end;
  1029. end
  1030. else
  1031. error(chmerror,'Can''t find TOC file'+FTableOfContentsFileName);
  1032. end;
  1033. if FIndexFileName<>'' then
  1034. begin
  1035. if fileexists(FIndexFileName) then
  1036. begin
  1037. FIndexStream:=TMemoryStream.Create;
  1038. try
  1039. FIndexStream.LoadFromFile(FIndexFileName);
  1040. FIndexStream.Position:=0;
  1041. FIndex:=TChmSiteMap.Create(stindex);
  1042. FIndex.loadfromfile(FIndexFileName);
  1043. except
  1044. on e: Exception do
  1045. begin
  1046. error(chmerror,'Error loading index file '+FIndexFileName);
  1047. freeandnil(findex); freeandnil(findexstream);
  1048. end;
  1049. end;
  1050. end
  1051. else
  1052. error(chmerror,'Can''t find index file '+FIndexFileName);
  1053. end;
  1054. end;
  1055. end.