dw_chm.pp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. unit dw_chm;
  2. interface
  3. uses Classes, DOM,
  4. dGlobals, PasTree, dwriter, dw_html, chmwriter, chmtypes, chmsitemap;
  5. type
  6. { TCHmFileNameAllocator }
  7. TCHmFileNameAllocator = Class(TLongNameFileAllocator)
  8. // Override this, because the logic messes up the filenames for plain html files.
  9. function GetFilename(AElement: TPasElement; ASubindex: Integer): String; override;
  10. end;
  11. { TFpDocChmWriter }
  12. TFpDocChmWriter = class (TChmWriter)
  13. protected
  14. procedure FileAdded(AStream: TStream; const AEntry: TFileEntryRec); override;
  15. end;
  16. { TCHMHTMLWriter }
  17. TCHMHTMLWriter = class(THTMLWriter)
  18. private
  19. FOutChm: TStream;
  20. FChm: TFpDocChmWriter;
  21. FTempUncompressed: TStream;
  22. FTempUncompressedName: String;
  23. FChmTitle: String;
  24. FTOCName,
  25. FIndexName,
  26. FDefaultPage: String;
  27. FMakeSearchable,
  28. FNoBinToc,
  29. FNoBinIndex,
  30. FAutoTOC,
  31. FAutoIndex: Boolean;
  32. FOtherFiles: String;
  33. procedure ProcessOptions;
  34. function ResolveLinkIDAbs(const Name: String; Level : Integer = 0): DOMString;
  35. function RetrieveOtherFiles(const DataName: String; out PathInChm: String;
  36. out FileName: String; var Stream: TStream): Boolean;
  37. procedure LastFileAdded(Sender: TObject);
  38. function FindAlphaItem(AItems: TChmSiteMapItems; AName: String): TChmSiteMapItem;
  39. function GetAlphaItem(AItems: TChmSiteMapItems; AName: String): TChmSiteMapItem;
  40. procedure MultiAlphaItem(AItems: TChmSiteMapItems; AName: String;
  41. APasEl: TPasElement; Prefix:String);
  42. procedure GenerateTOC;
  43. procedure GenerateIndex;
  44. public
  45. procedure WriteDoc; override;
  46. function CreateAllocator: TFileAllocator; override;
  47. function InterPretOption(const Cmd,Arg : String): boolean; override;
  48. class procedure Usage(List: TStrings); override;
  49. Class Function FileNameExtension : String; override;
  50. Class procedure SplitImport(var AFilename, ALinkPrefix: String); override;
  51. end;
  52. implementation
  53. uses SysUtils, HTMWrite, dw_basehtml;
  54. { TCHmFileNameAllocator }
  55. function TCHmFileNameAllocator.GetFilename(AElement: TPasElement; ASubindex: Integer): String;
  56. var
  57. n,s: String;
  58. i: Integer;
  59. excl: Boolean; //search
  60. begin
  61. Result:='';
  62. excl := False;
  63. if AElement.ClassType = TPasPackage then
  64. begin
  65. Result := 'index';
  66. excl := True;
  67. end
  68. else if AElement.ClassType = TPasModule then
  69. begin
  70. Result := LowerCase(AElement.Name) + PathDelim + 'index';
  71. excl := True;
  72. end
  73. else
  74. begin
  75. if AElement is TPasOperator then
  76. begin
  77. if Assigned(AElement.Parent) then
  78. result:=LowerCase(AElement.Parent.PathName);
  79. With TPasOperator(aElement) do
  80. Result:= Result + 'op-'+OperatorTypeToOperatorName(OperatorType);
  81. s := '';
  82. N:=LowerCase(aElement.Name); // Should not contain any weird chars.
  83. Delete(N,1,Pos('(',N));
  84. i := 1;
  85. Repeat
  86. I:=Pos(',',N);
  87. if I=0 then
  88. I:=Pos(')',N);
  89. if I>1 then
  90. begin
  91. if (S<>'') then
  92. S:=S+'-';
  93. S:=S+Copy(N,1,I-1);
  94. end;
  95. Delete(N,1,I);
  96. until I=0;
  97. // First char is maybe :
  98. if (N<>'') and (N[1]=':') then
  99. Delete(N,1,1);
  100. Result:=Result + '-'+ s + '-' + N;
  101. end
  102. else
  103. begin
  104. Result := LowerCase(AElement.PathName);
  105. excl := (ASubindex > 0);
  106. end;
  107. // searching for TPasModule - it is on the 2nd level
  108. if Assigned(AElement.Parent) then
  109. while Assigned(AElement.Parent.Parent) do
  110. AElement := AElement.Parent;
  111. // cut off Package Name
  112. Result := Copy(Result, Length(AElement.Parent.Name) + 2, MaxInt);
  113. // to skip dots in unit name
  114. i := Length(AElement.Name);
  115. while (i <= Length(Result)) and (Result[i] <> '.') do
  116. Inc(i);
  117. if (i <= Length(Result)) and (i > 0) then
  118. Result[i] := PathDelim;
  119. if excl or (Length(Result)=0) then
  120. begin
  121. // exclude the from full text search index
  122. s:= '.'+ExtractFileName(Result + '.');
  123. n:= ExtractFileDir(Result);
  124. Result := n + DirectorySeparator + s;
  125. Result := Copy(Result, 1, Length(Result)-1);
  126. end;
  127. end;
  128. if ASubindex > 0 then
  129. Result := Result + '-' + IntToStr(ASubindex);
  130. Result := Result + Extension;
  131. // Writeln('Result filename : ',Result);
  132. end;
  133. { TFpDocChmWriter }
  134. procedure TFpDocChmWriter.FileAdded ( AStream: TStream;
  135. const AEntry: TFileEntryRec ) ;
  136. var FTsave : boolean;
  137. begin
  138. // Exclude Full text index for files starting from the dot
  139. if Pos('.', AEntry.Name) <> 1 then
  140. inherited FileAdded(AStream, AEntry)
  141. else
  142. begin
  143. FTsave:=FullTextSearch;
  144. FullTextSearch:=False;
  145. inherited FileAdded(AStream, AEntry);
  146. FullTextSearch:=FTsave;
  147. end;
  148. end;
  149. { TCHMHTMLWriter }
  150. function TCHMHTMLWriter.ResolveLinkIDAbs(const Name: String; Level : Integer = 0): DOMString;
  151. begin
  152. Result:=UTF8Decode(FixHTMLpath(Engine.ResolveLink(Module,Name, True)));
  153. // for global index: don't make it relative to the current document.
  154. end;
  155. procedure TCHMHTMLWriter.ProcessOptions;
  156. var
  157. TempStream: TMemoryStream;
  158. begin
  159. if FDefaultPage = '' then
  160. FDefaultPage := 'index.html'
  161. else
  162. begin
  163. DoLog('Note: --index-page not assigned. Using default "index.html"');
  164. end;
  165. if CSSFile <> '' then
  166. begin
  167. if not FileExists(CSSFile) Then
  168. Raise Exception.CreateFmt('Can''t find CSS file "%S"',[CSSFILE]);
  169. TempStream := TMemoryStream.Create;
  170. TempStream.LoadFromFile(CSSFile);
  171. TempStream.Position := 0;
  172. FChm.AddStreamToArchive('fpdoc.css', '/', TempStream, True);
  173. TempStream.Free;
  174. end;
  175. FChm.DefaultPage := FDefaultPage;
  176. if FOtherFiles <> '' then
  177. begin
  178. FChm.FilesToCompress.LoadFromFile(FOtherFiles);
  179. end;
  180. FChm.FullTextSearch := FMakeSearchable;
  181. end;
  182. function TCHMHTMLWriter.RetrieveOtherFiles(const DataName: String; out
  183. PathInChm: String; out FileName: String; var Stream: TStream): Boolean;
  184. begin
  185. Result:=True;
  186. if Stream <> nil then
  187. Stream.Free;
  188. Stream := TMemoryStream.Create;
  189. TMemoryStream(Stream).LoadFromFile(DataName);
  190. FileName := ExtractFileName(DataName);
  191. if ExtractFileDir(DataName) <> '' then
  192. PathInChm := ExtractRelativepath(GetCurrentDir, ExtractFileDir(DataName))
  193. else
  194. PathInChm := '/';
  195. FixHTMLpath(PathInChm);
  196. Stream.Position := 0;
  197. end;
  198. procedure TCHMHTMLWriter.LastFileAdded(Sender: TObject);
  199. var
  200. TmpStream: TMemoryStream;
  201. begin
  202. TmpStream := TMemoryStream.Create;
  203. if FAutoTOC then
  204. GenerateTOC
  205. else
  206. if FTOCName <> '' then
  207. begin
  208. TmpStream.LoadFromFile(FTOCName);
  209. TmpStream.Position := 0;
  210. FChm.AppendTOC(TmpStream);
  211. TmpStream.Size := 0;
  212. end;
  213. if FAutoIndex then
  214. GenerateIndex
  215. else
  216. if FIndexName <> '' then
  217. begin
  218. TmpStream.LoadFromFile(FIndexName);
  219. TmpStream.Position := 0;
  220. FChm.AppendIndex(TmpStream);
  221. end;
  222. TmpStream.Free;
  223. DoLog('Finishing compressing...');
  224. end;
  225. function TOCSort(Item1, Item2: TChmSiteMapItem): Integer;
  226. begin
  227. Result := CompareText(LowerCase(Item1.Text), LowerCase(Item2.Text));
  228. end;
  229. function TCHMHTMLWriter.FindAlphaItem(AItems: TChmSiteMapItems; AName: String
  230. ): TChmSiteMapItem;
  231. var
  232. x: Integer;
  233. begin
  234. Result := nil;
  235. for x := 0 to AItems.Count-1 do
  236. begin
  237. if AItems.Item[x].Text = AName then
  238. Exit(AItems.Item[x]);
  239. end;
  240. end;
  241. function TCHMHTMLWriter.GetAlphaItem(AItems: TChmSiteMapItems; AName: String
  242. ): TChmSiteMapItem;
  243. begin
  244. Result := FindAlphaItem(AItems, AName);
  245. if Result <> nil then Exit;
  246. Result := AItems.NewItem;
  247. Result.Text := AName;
  248. end;
  249. procedure TCHMHTMLWriter.MultiAlphaItem(AItems: TChmSiteMapItems; AName: String;
  250. APasEl: TPasElement; Prefix: String);
  251. var
  252. AChmItem, AChmChld: TChmSiteMapItem;
  253. begin
  254. AChmItem:= FindAlphaItem(AItems, AName);
  255. if AChmItem = nil then
  256. begin
  257. // add new
  258. AChmItem := AItems.NewItem;
  259. AChmItem.Text := AName;
  260. AChmItem.addLocal(FixHTMLpath(Allocator.GetFilename(APasEl, 0)));
  261. end
  262. else
  263. begin
  264. // add as child
  265. AChmChld := AChmItem.Children.NewItem;
  266. AChmChld.Text := Prefix + '.' + AName;
  267. AChmChld.addLocal(FixHTMLpath(Allocator.GetFilename(APasEl, 0)));
  268. end;
  269. end;
  270. procedure TCHMHTMLWriter.GenerateTOC;
  271. var
  272. TOC: TChmSiteMap;
  273. Element: TPasElement;
  274. j: Integer;
  275. i: Integer;
  276. AModule: TPasModule;
  277. Stream: TMemoryStream;
  278. TmpItem: TChmSiteMapItem;
  279. ObjByUnitItem,
  280. AlphaObjItem,
  281. ObjUnitItem,
  282. RoutinesByUnitItem,
  283. RoutinesUnitItem,
  284. AlphaRoutinesItem: TChmSiteMapItem;
  285. begin
  286. DoLog('Generating Table of contents...');
  287. if not Assigned(Package) then
  288. begin
  289. DoLog('Package is not assigned...');
  290. Exit;
  291. end;
  292. Toc := TChmSiteMap.Create(stTOC);
  293. Stream := TMemoryStream.Create;
  294. ObjByUnitItem := TOC.Items.NewItem;
  295. ObjByUnitItem.Text := 'Classes and Objects, by Unit';
  296. AlphaObjItem := TOC.Items.NewItem;
  297. AlphaObjItem.Text := 'Alphabetical Classes and Objects List';
  298. RoutinesByUnitItem := TOC.Items.NewItem;
  299. RoutinesByUnitItem.Text := 'Routines, by Unit';
  300. AlphaRoutinesItem := TOC.Items.NewItem;
  301. AlphaRoutinesItem.Text := 'Alphabetical Routines List';
  302. // objects and classes
  303. for i := 0 to Package.Modules.Count - 1 do
  304. begin
  305. AModule := TPasModule(Package.Modules[i]);
  306. If not assigned(AModule.InterfaceSection) Then
  307. Continue;
  308. ObjUnitItem := ObjByUnitItem.Children.NewItem;
  309. ObjUnitItem.Text := AModule.Name;
  310. RoutinesUnitItem := RoutinesByUnitItem.Children.NewItem;
  311. RoutinesUnitItem.Text := AModule.Name;
  312. for j := 0 to AModule.InterfaceSection.Classes.Count-1 do
  313. begin
  314. Element := TPasClassType(AModule.InterfaceSection.Classes[j]);
  315. // by unit
  316. TmpItem := ObjUnitItem.Children.NewItem;
  317. TmpItem.Text := Element.Name;
  318. TmpItem.addLocal(FixHTMLpath(Allocator.GetFilename(Element, 0)));
  319. //alpha
  320. TmpItem := GetAlphaItem(AlphaObjItem.Children, UpperCase(Copy(Element.Name, 1, 2))).Children.NewItem;
  321. TmpItem.Text := Element.Name;
  322. TmpItem.addLocal(FixHTMLpath(Allocator.GetFilename(Element, 0)));
  323. end;
  324. // non object procedures and functions
  325. for j := 0 to AModule.InterfaceSection.Functions.Count-1 do
  326. begin
  327. Element := TPasFunctionType(AModule.InterfaceSection.Functions[j]);
  328. // by unit
  329. TmpItem := RoutinesUnitItem.Children.NewItem;
  330. TmpItem.Text := Element.Name;
  331. TmpItem.addLocal(FixHTMLpath(Allocator.GetFilename(Element, 0)));
  332. // alpha
  333. TmpItem := GetAlphaItem(AlphaRoutinesItem.Children, UpperCase(Element.Name[1])).Children.NewItem;
  334. TmpItem.Text := Element.Name;
  335. TmpItem.addLocal(FixHTMLpath(Allocator.GetFilename(Element, 0)));
  336. end;
  337. end;
  338. // cleanup
  339. for i := ObjByUnitItem.Children.Count-1 downto 0 do
  340. begin
  341. if ObjByUnitItem.Children.Item[i].Children.Count = 0 then
  342. ObjByUnitItem.Children.Delete(i);
  343. end;
  344. for i := RoutinesByUnitItem.Children.Count-1 downto 0 do
  345. begin
  346. if RoutinesByUnitItem.Children.Item[i].Children.Count = 0 then
  347. RoutinesByUnitItem.Children.Delete(i);
  348. end;
  349. for i := TOC.Items.Count-1 downto 0 do
  350. begin
  351. if TOC.Items.Item[i].Children.Count = 0 then
  352. TOC.Items.Delete(i);
  353. end;
  354. // Sort
  355. for i := 0 to TOC.Items.Count-1 do
  356. begin
  357. TOC.Items.Item[i].Children.Sort(TListSortCompare(@TOCSort));
  358. for j := 0 to TOC.Items.Item[i].Children.Count-1 do
  359. begin
  360. TOC.Items.Item[i].Children.Item[j].Children.Sort(TListSortCompare(@TOCSort));
  361. end;
  362. end;
  363. if not fnobintoc then
  364. fchm.AppendBinaryTOCFromSiteMap(Toc);
  365. TOC.SaveToStream(Stream);
  366. TOC.Free;
  367. fchm.AppendTOC(Stream);
  368. Stream.Free;
  369. DoLog('Generating TOC done');
  370. end;
  371. type
  372. TClassMemberType = (cmtProcedure, cmtFunction, cmtConstructor, cmtDestructor,
  373. cmtInterface, cmtProperty, cmtVariable, cmtOperator, cmtConstant, cmtUnknown);
  374. function ElementType(Element: TPasElement): TClassMemberType;
  375. var
  376. ETypeName: String;
  377. begin
  378. Result := cmtUnknown;
  379. if not Assigned(Element) then Exit;
  380. ETypeName := Element.ElementTypeName;
  381. if Length(ETypeName) = 0 then Exit;
  382. // opearator
  383. if ETypeName[2] = 'p' then Exit(cmtOperator);
  384. if ETypeName[3] = 'n' then Exit(cmtConstant);
  385. // overloaded we don't care
  386. if ETypeName[1] = 'o' then ETypeName := Copy(ETypeName, 12, Length(ETypeName));
  387. if ETypeName[1] = 'f' then Exit(cmtFunction);
  388. if ETypeName[1] = 'c' then Exit(cmtConstructor);
  389. if ETypeName[1] = 'd' then Exit(cmtDestructor);
  390. if ETypeName[1] = 'v' then Exit(cmtVariable);
  391. if ETypeName[1] = 'i' then Exit(cmtInterface);
  392. // the p's
  393. if ETypeName[4] = 'c' then Exit(cmtProcedure);
  394. if ETypeName[4] = 'p' then Exit(cmtProperty);
  395. // Unknown
  396. // WriteLn(' Warning El name: '+ Element.Name+' path: '+Element.PathName+' TypeName: '+Element.ElementTypeName);
  397. end;
  398. procedure TCHMHTMLWriter.GenerateIndex;
  399. var
  400. Index: TChmSiteMap;
  401. i, j, k: Integer;
  402. TmpItem: TChmSiteMapItem;
  403. ParentItem: TChmSiteMapItem;
  404. AModule: TPasModule;
  405. TmpElement: TPasElement;
  406. ParentElement: TPasElement;
  407. MemberItem: TChmSiteMapItem;
  408. Stream: TMemoryStream;
  409. RedirectUrl,Urls,SName: String;
  410. begin
  411. DoLog('Generating Index...');
  412. if not Assigned(Package) then
  413. begin
  414. DoLog('Package is not assigned...');
  415. Exit;
  416. end;
  417. Index := TChmSiteMap.Create(stIndex);
  418. Stream := TMemoryStream.Create;
  419. for i := 0 to Package.Modules.Count - 1 do
  420. //if false then
  421. begin
  422. AModule := TPasModule(Package.Modules[i]);
  423. if not assigned(AModule.InterfaceSection) then
  424. continue;
  425. ParentItem := Index.Items.NewItem;
  426. ParentItem.Text := AModule.Name;
  427. ParentItem.addLocal(FixHTMLpath(Allocator.GetFilename(AModule, 0)));
  428. // classes
  429. for j := 0 to AModule.InterfaceSection.Classes.Count-1 do
  430. begin
  431. ParentElement := TPasClassType(AModule.InterfaceSection.Classes[j]);
  432. ParentItem := Index.Items.NewItem;
  433. ParentItem.Text := ParentELement.Name;
  434. ParentItem.addLocal(FixHTMLpath(Allocator.GetFilename(ParentElement, 0)));
  435. for k := 0 to TPasClassType(ParentElement).Members.Count-1 do
  436. begin
  437. TmpElement := TPasElement(TPasClassType(ParentElement).Members.Items[k]);
  438. if Engine.HidePrivate and(TmpElement.Visibility = visPrivate) then
  439. continue;
  440. if Engine.HideProtected and(TmpElement.Visibility = visProtected) then
  441. continue;
  442. Urls:=FixHTMLpath(Allocator.GetFilename(TmpElement, 0));
  443. RedirectUrl:='';
  444. if TmpElement is TPasEnumValue then
  445. RedirectUrl := UTF8Encode(ResolveLinkIDAbs(tmpElement.Parent.PathName))
  446. else
  447. RedirectUrl := UTF8Encode(ResolveLinkIDAbs(tmpElement.PathName));
  448. if(trim(RedirectUrl)<>'') and (RedirectUrl<>urls) then
  449. begin
  450. //writeln('Hint: Index Resolved:',urls,' to ',RedirectUrl);
  451. urls:=RedirectUrl;
  452. end;
  453. TmpItem := ParentItem.Children.NewItem;
  454. case ElementType(TmpElement) of
  455. cmtProcedure : TmpItem.Text := TmpElement.Name + ' procedure';
  456. cmtFunction : TmpItem.Text := TmpElement.Name + ' function';
  457. cmtConstructor : TmpItem.Text := TmpElement.Name + ' constructor';
  458. cmtDestructor : TmpItem.Text := TmpElement.Name + ' destructor';
  459. cmtProperty : TmpItem.Text := TmpElement.Name + ' property';
  460. cmtVariable : TmpItem.Text := TmpElement.Name + ' variable';
  461. cmtInterface : TmpItem.Text := TmpElement.Name + ' interface';
  462. cmtOperator : TmpItem.Text := TmpElement.Name + ' operator';
  463. cmtConstant : TmpItem.Text := TmpElement.Name + ' const';
  464. cmtUnknown : TmpItem.Text := TmpElement.Name;
  465. end;
  466. TmpItem.addLocal(Urls);
  467. {
  468. ParentElement = Class
  469. TmpElement = Member
  470. }
  471. MemberItem := nil;
  472. MemberItem := GetAlphaItem(Index.Items, TmpElement.Name);
  473. // ahh! if MemberItem.Local is empty MemberType is not shown!
  474. MemberItem.addLocal(Urls);
  475. TmpItem := MemberItem.Children.NewItem;
  476. TmpItem.Text := ParentElement.Name;
  477. TmpItem.AddLocal(Urls);
  478. end;
  479. end;
  480. // routines
  481. for j := 0 to AModule.InterfaceSection.Functions.Count-1 do
  482. begin
  483. // routine name
  484. ParentElement := TPasElement(AModule.InterfaceSection.Functions[j]);
  485. case ElementType(ParentElement) of
  486. cmtProcedure : SName:= ' procedure';
  487. cmtFunction : SName:= ' function';
  488. cmtOperator : SName:= ' operator';
  489. //cmtConstant : SName:= ' const';
  490. else SName:= ' unknown'
  491. end;
  492. SName:= ParentElement.Name + ' ' + SName;
  493. MultiAlphaItem(Index.Items, SName, ParentElement, AModule.Name);
  494. end;
  495. // consts
  496. for j := 0 to AModule.InterfaceSection.Consts.Count-1 do
  497. begin
  498. ParentElement := TPasElement(AModule.InterfaceSection.Consts[j]);
  499. SName:= ParentElement.Name + ' const';
  500. MultiAlphaItem(Index.Items, SName, ParentElement, AModule.Name);
  501. end;
  502. // types
  503. for j := 0 to AModule.InterfaceSection.Types.Count-1 do
  504. begin
  505. ParentElement := TPasType(AModule.InterfaceSection.Types[j]);
  506. TmpItem := Index.Items.NewItem;
  507. TmpItem.Text := ParentElement.Name;
  508. TmpItem.addLocal(FixHTMLpath(Allocator.GetFilename(ParentElement, 0)));
  509. // enums
  510. if ParentELement is TPasEnumType then
  511. begin
  512. ParentItem := TmpItem;
  513. for k := 0 to TPasEnumType(ParentElement).Values.Count-1 do
  514. begin
  515. TmpElement := TPasType(TPasEnumType(ParentElement).Values.Items[k]);
  516. // subitem
  517. TmpItem := ParentItem.Children.NewItem;
  518. TmpItem.Text := TmpElement.Name;
  519. TmpItem.addLocal(ParentItem.Local);
  520. // root level
  521. TmpItem := Index.Items.NewItem;
  522. TmpItem.Text := TmpElement.Name;
  523. TmpItem.addLocal(ParentItem.Local);
  524. end;
  525. end;
  526. end;
  527. // variables
  528. for j := 0 to AModule.InterfaceSection.Variables.Count-1 do
  529. begin
  530. ParentElement := TPasElement(AModule.InterfaceSection.Variables[j]);
  531. SName:= ParentElement.Name + ' variable';
  532. MultiAlphaItem(Index.Items, SName, ParentElement, AModule.Name);
  533. end;
  534. // declarations
  535. {
  536. for j := 0 to AModule.InterfaceSection.Declarations.Count-1 do
  537. begin
  538. ParentElement := TPasElement(AModule.InterfaceSection.Declarations[j]);
  539. TmpItem := Index.Items.NewItem;
  540. TmpItem.Text := ParentElement.Name;
  541. TmpItem.Local := FixHTMLpath(Allocator.GetFilename(ParentElement, 0));
  542. end;
  543. // resource strings
  544. for j := 0 to AModule.InterfaceSection.ResStrings.Count-1 do
  545. begin
  546. ParentElement := TPasElement(AModule.InterfaceSection.ResStrings[j]);
  547. TmpItem := Index.Items.NewItem;
  548. TmpItem.Text := ParentElement.Name;
  549. TmpItem.Local := FixHTMLpath(Allocator.GetFilename(ParentElement, 0));
  550. end;
  551. }
  552. end;
  553. // Sort
  554. Index.Items.Sort(TListSortCompare(@TOCSort));
  555. for i := 0 to Index.Items.Count-1 do
  556. begin
  557. Index.Items.Item[i].Children.Sort(TListSortCompare(@TOCSort));
  558. end;
  559. // save
  560. Index.SaveToStream(Stream);
  561. if not fnobinindex then
  562. fchm.AppendBinaryindexFromSitemap(index,false);
  563. Index.Free;
  564. Stream.Position :=0 ;
  565. FChm.AppendIndex(Stream);
  566. Stream.Free;
  567. DoLog('Generating Index Done');
  568. end;
  569. procedure TCHMHTMLWriter.WriteDoc;
  570. var
  571. i: Integer;
  572. PageDoc: TXMLDocument;
  573. FileStream: TMemoryStream;
  574. IFileName,FileName: String;
  575. FilePath: String;
  576. begin
  577. FileName := Engine.Output;
  578. if FileName = '' then
  579. Raise Exception.Create('Error: no --output option used.');
  580. if ExtractFileExt(FileName) <> FileNameExtension then
  581. FileName := ChangeFileExt(FileName, FileNameExtension);
  582. FOutChm := TFileStream.Create(FileName, fmOpenReadWrite or fmCreate);
  583. FTempUncompressedName := GetTempFileName+IntToStr(GetProcessID) +'.raw';
  584. FTempUncompressed := TFileStream.Create(FTempUncompressedName, fmOpenReadWrite or fmCreate);
  585. FChm := TFpDocChmWriter.Create(FOutChm, False);
  586. FChm.Title := FChmTitle;
  587. FChm.TempRawStream := FTempUncompressed;
  588. FChm.OnGetFileData := @RetrieveOtherFiles;
  589. FChm.OnLastFile := @LastFileAdded;
  590. FChm.hasbinarytoc:=not fnobintoc;
  591. FChm.hasbinaryindex:=not fnobinindex;
  592. //FChm.Cores:=1;
  593. ProcessOptions;
  594. FileStream := TMemoryStream.Create;
  595. for i := 0 to PageInfos.Count - 1 do
  596. with TPageInfo(PageInfos[i]) do
  597. begin
  598. PageDoc := CreateHTMLPage(Element, SubpageIndex);
  599. try
  600. FileName := ExtractFileName(Allocator.GetFilename(Element, SubpageIndex));
  601. FilePath := '/'+FixHTMLpath(ExtractFilePath(Allocator.GetFilename(Element, SubpageIndex)));
  602. try
  603. WriteHTMLFile(PageDoc, FileStream);
  604. FChm.AddStreamToArchive(FileName, FilePath, FileStream, True);
  605. except
  606. on E: Exception do
  607. DoLog(Format(SErrCouldNotCreateFile, [FileName, e.Message]));
  608. end;
  609. finally
  610. PageDoc.Free;
  611. FileStream.Size := 0;
  612. end;
  613. end;
  614. FileStream.Free;
  615. DoLog('HTML Files written. Collecting other files and compressing...this could take some time');
  616. //write any found images to CHM stream
  617. FileStream := TMemoryStream.Create;
  618. for iFilename in ImageFileList do
  619. begin
  620. {$ifdef imagetest} DoLog(' adding image: '+iFileName); {$endif}
  621. if FileExists(iFileName) then
  622. begin
  623. {$ifdef imagetest} DoLog(' - found'); {$endif}
  624. FileName := ExtractFileName(iFileName);
  625. FilePath := '/'+FixHTMLpath(ExtractFilePath(iFileName));
  626. FileStream.LoadFromFile(iFileName);
  627. FChm.AddStreamToArchive(FileName, FilePath, FileStream, True);
  628. FileStream.Size := 0;
  629. end
  630. else
  631. {$ifdef imagetest} DoLog(' - not found'){$endif};
  632. end;
  633. FileStream.Free;
  634. FChm.Execute;
  635. FChm.Free;
  636. DoLog('Collecting done');
  637. // we don't need to free FTempUncompressed it is freed into TFpDocChmWriter
  638. // FTempUncompressed.Free;
  639. FOutChm.Free;
  640. DeleteFile(FTempUncompressedName);
  641. end;
  642. function TCHMHTMLWriter.CreateAllocator: TFileAllocator;
  643. begin
  644. Result:=TCHmFileNameAllocator.Create('.html');
  645. end;
  646. function TCHMHTMLWriter.InterPretOption(const Cmd, Arg: String): boolean;
  647. begin
  648. Result:=True;
  649. FNoBinToc:=False;
  650. FnoBinIndex:=False;
  651. if Cmd = '--toc-file' then
  652. FTOCName := arg
  653. else if Cmd = '--index-file' then
  654. FIndexName := arg
  655. else if Cmd = '--default-page' then
  656. FDefaultPage := arg
  657. else if Cmd = '--other-files' then
  658. FOtherFiles := arg
  659. else if Cmd = '--auto-index' then
  660. FAutoIndex := True
  661. else if Cmd = '--auto-toc' then
  662. FAutoTOC := True
  663. else if Cmd = '--no-bintoc' then
  664. FNoBinToc := True
  665. else if Cmd = '--no-binindex' then
  666. FNoBinIndex := True
  667. else if Cmd = '--make-searchable' then
  668. FMakeSearchable := True
  669. else if Cmd = '--chm-title' then
  670. FChmTitle := arg
  671. else
  672. Result:=inherited InterPretOption(Cmd, Arg);
  673. if Length(FChmTitle) = 0 then
  674. FChmTitle := Copy(Package.Name, 2, Length(Package.Name));
  675. end;
  676. class procedure TCHMHTMLWriter.Usage(List: TStrings);
  677. begin
  678. THTMLWriter.Usage(List);
  679. List.add('--default-page');
  680. List.Add(SCHMUsageDefPage);
  681. List.add('--toc-file');
  682. List.Add(SCHMUsageTOC);
  683. List.add('--index-file');
  684. List.Add(SCHMUsageIndex);
  685. List.add('--other-files');
  686. List.Add(SCHMUsageOtrFiles);
  687. List.add('--css-file');
  688. List.Add(SCHMUsageCSSFile);
  689. List.add('--auto-index');
  690. List.Add(SCHMUsageAutoIDX);
  691. List.add('--auto-toc');
  692. List.Add(SCHMUsageAutoTOC);
  693. List.add('--make-searchable');
  694. List.Add(SCHMUsageMakeSearch);
  695. List.Add('--chm-title');
  696. List.Add(SCHMUsageChmTitle);
  697. end;
  698. class function TCHMHTMLWriter.FileNameExtension: String;
  699. begin
  700. result:='.chm';
  701. end;
  702. class procedure TCHMHTMLWriter.SplitImport(var AFilename, ALinkPrefix: String);
  703. var
  704. i: integer;
  705. begin
  706. i := Pos(',', AFilename);
  707. if i > 0 then
  708. begin //split into filename and prefix
  709. ALinkPrefix := Copy(AFilename,i+1,Length(AFilename));
  710. SetLength(AFilename, i-1);
  711. if copy(ALinkPrefix,1,2)='..' then // workaround for project files.
  712. begin
  713. ALinkPrefix := 'ms-its:' + ChangeFileExt(ExtractFileName(AFilename), '.chm') + '::/';
  714. AFilename := ChangeFileExt(AFilename, '.xct');
  715. end;
  716. end
  717. else if ALinkPrefix = '' then
  718. begin //synthesize outdir\pgk.xct, ms-its:pkg.chm::/
  719. ALinkPrefix := 'ms-its:' + ChangeFileExt(ExtractFileName(AFilename), '.chm') + '::/';
  720. AFilename := ChangeFileExt(AFilename, '.xct');
  721. end;
  722. end;
  723. initialization
  724. RegisterWriter(TCHMHTMLWriter,'chm','Compressed HTML file output using fpdoc.css stylesheet.');
  725. finalization
  726. UnRegisterWriter('chm');
  727. end.