chmsitemap.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. { Copyright (C) <2005> <Andrew Haines> chmsitemap.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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  13. }
  14. {
  15. See the file COPYING.FPC, included in this distribution,
  16. for details about the copyright.
  17. }
  18. unit chmsitemap;
  19. {$mode objfpc}{$H+}
  20. interface
  21. uses
  22. Classes, SysUtils, fasthtmlparser;
  23. type
  24. TChmSiteMapItems = class; // forward
  25. TChmSiteMap = class;
  26. { TChmSiteMapItem }
  27. TChmSiteMapItem = class(TPersistent)
  28. private
  29. FChildren: TChmSiteMapItems;
  30. FComment: String;
  31. FImageNumber: Integer;
  32. FIncreaseImageIndex: Boolean;
  33. FKeyWord: String;
  34. FLocal: String;
  35. FOwner: TChmSiteMapItems;
  36. FSeeAlso: String;
  37. FText: String;
  38. FURL: String;
  39. procedure SetChildren(const AValue: TChmSiteMapItems);
  40. published
  41. constructor Create(AOwner: TChmSiteMapItems);
  42. destructor Destroy; override;
  43. property Children: TChmSiteMapItems read FChildren write SetChildren;
  44. property Text: String read FText write FText; // Name for TOC; KeyWord for index
  45. property KeyWord: String read FKeyWord write FKeyWord;
  46. property Local: String read FLocal write FLocal;
  47. property URL: String read FURL write FURL;
  48. property SeeAlso: String read FSeeAlso write FSeeAlso;
  49. property ImageNumber: Integer read FImageNumber write FImageNumber default -1;
  50. property IncreaseImageIndex: Boolean read FIncreaseImageIndex write FIncreaseImageIndex;
  51. property Comment: String read FComment write FComment;
  52. property Owner: TChmSiteMapItems read FOwner;
  53. //property FrameName: String read FFrameName write FFrameName;
  54. //property WindowName: String read FWindowName write FWindowName;
  55. //property Type_: Integer read FType_ write FType_; either Local or URL
  56. //property Merge: Boolean read FMerge write FMerge;
  57. end;
  58. { TChmSiteMapItems }
  59. TChmSiteMapItems = class(TPersistent)
  60. private
  61. FList: TList;
  62. FOwner: TChmSiteMap;
  63. FParentItem: TChmSiteMapItem;
  64. function GetCount: Integer;
  65. function GetItem(AIndex: Integer): TChmSiteMapItem;
  66. procedure SetItem(AIndex: Integer; const AValue: TChmSiteMapItem);
  67. public
  68. constructor Create(AOwner: TChmSiteMap; AParentItem: TChmSiteMapItem);
  69. destructor Destroy; override;
  70. procedure Delete(AIndex: Integer);
  71. function Add(AItem: TChmSiteMapItem): Integer;
  72. function NewItem: TChmSiteMapItem;
  73. function Insert(AItem: TChmSiteMapItem; AIndex: Integer): Integer;
  74. procedure Clear;
  75. procedure Sort(Compare: TListSortCompare);
  76. property Item[AIndex: Integer]: TChmSiteMapItem read GetItem write SetItem;
  77. property Count: Integer read GetCount;
  78. property ParentItem: TChmSiteMapItem read FParentItem;
  79. property Owner: TChmSiteMap read FOwner;
  80. end;
  81. { TChmSiteMapTree }
  82. TSiteMapType = (stTOC, stIndex);
  83. TSiteMapTag = (smtUnknown, smtNone, smtHTML, smtHEAD, smtBODY);
  84. TSiteMapTags = set of TSiteMapTag;
  85. TSiteMapBodyTag = (smbtUnknown, smbtNone, smbtUL, smbtLI, smbtOBJECT, smbtPARAM);
  86. TSiteMapBodyTags = set of TSiteMapBodyTag;
  87. TLIObjectParamType = (ptName, ptLocal, ptKeyword);
  88. TChmSiteMap = class
  89. private
  90. FAutoGenerated: Boolean;
  91. FBackgroundColor: LongInt;
  92. FCurrentItems: TChmSiteMapItems;
  93. FExWindowStyles: LongInt;
  94. FFont: String;
  95. FForegroundColor: LongInt;
  96. FFrameName: String;
  97. FImageList: String;
  98. FImageWidth: Integer;
  99. FSiteMapTags: TSiteMapTags;
  100. FSiteMapBodyTags: TSiteMapBodyTags;
  101. FHTMLParser: THTMLParser;
  102. FItems: TChmSiteMapItems;
  103. FSiteMapType: TSiteMapType;
  104. FUseFolderImages: Boolean;
  105. FWindowName: String;
  106. FLevel: Integer;
  107. FWindowStyles: LongInt;
  108. procedure SetItems(const AValue: TChmSiteMapItems);
  109. protected
  110. procedure FoundTag (ACaseInsensitiveTag, AActualTag: string);
  111. procedure FoundText(AText: string);
  112. public
  113. constructor Create(AType: TSiteMapType);
  114. destructor Destroy; override;
  115. procedure LoadFromFile(AFileName: String);
  116. procedure LoadFromStream(AStream: TStream);
  117. procedure SaveToStream(AStream: TStream);
  118. property Items: TChmSiteMapItems read FItems write SetItems;
  119. property SiteMapType: TSiteMapType read FSiteMapType;
  120. // SiteMap properties. most of these are invalid for the index
  121. property FrameName: String read FFrameName write FFrameName;
  122. property WindowName: String read FWindowName write FWindowName;
  123. property ImageList: String read FImageList write FImageList;
  124. property ImageWidth: Integer read FImageWidth write FImageWidth;
  125. property BackgroundColor: LongInt read FBackgroundColor write FBackgroundColor;
  126. property ForegroundColor: LongInt read FForegroundColor write FForegroundColor;
  127. property ExWindowStyles: LongInt read FExWindowStyles write FExWindowStyles;
  128. property WindowStyles: LongInt read FWindowStyles write FWindowStyles;
  129. property UseFolderImages: Boolean read FUseFolderImages write FUseFolderImages;
  130. property Font: String read FFont write FFont;
  131. property AutoGenerated: Boolean read FAutoGenerated write FAutoGenerated;
  132. end;
  133. implementation
  134. uses HTMLUtil;
  135. { TChmSiteMapTree }
  136. procedure TChmSiteMap.SetItems(const AValue: TChmSiteMapItems);
  137. begin
  138. if FItems=AValue then exit;
  139. FItems:=AValue;
  140. end;
  141. procedure TChmSiteMap.FoundTag(ACaseInsensitiveTag, AActualTag: string);
  142. function ActiveItem: TChmSiteMapItem;
  143. begin
  144. Result := FCurrentItems.Item[FCurrentItems.Count-1]
  145. end;
  146. procedure IncreaseULevel;
  147. begin
  148. if FCurrentItems = nil then FCurrentItems := Items
  149. else begin
  150. //WriteLn('NewLevel. Count = ', FCurrentItems.Count, ' Index = ',Items.Count-1);
  151. FCurrentItems := ActiveItem.Children;
  152. end;
  153. Inc(FLevel);
  154. end;
  155. procedure DecreaseULevel;
  156. begin
  157. if Assigned(FCurrentItems) and Assigned(FCurrentItems.ParentItem) then
  158. FCurrentItems := FCurrentItems.ParentItem.Owner
  159. else FCurrentItems := nil;
  160. Dec(FLevel);
  161. end;
  162. procedure NewSiteMapItem;
  163. begin
  164. FCurrentItems.Add(TChmSiteMapItem.Create(FCurrentItems));
  165. end;
  166. var
  167. TagName,
  168. TagAttribute,
  169. TagAttributeName,
  170. TagAttributeValue: String;
  171. I: Integer;
  172. begin
  173. //WriteLn('TAG:', AActualTag);
  174. TagName := GetTagName(ACaseInsensitiveTag);
  175. if not (smtHTML in FSiteMapTags) then begin
  176. if TagName = 'HTML' then Include(FSiteMapTags, smtHTML);
  177. end
  178. else begin // looking for /HTML
  179. if TagName = '/HTML' then Exclude(FSiteMapTags, smtHTML);
  180. end;
  181. if (smtHTML in FSiteMapTags) then begin
  182. if not (smtBODY in FSiteMapTags) then begin
  183. if TagName = 'BODY' then Include(FSiteMapTags, smtBODY);
  184. end
  185. else begin
  186. if TagName = '/BODY' then Exclude(FSiteMapTags, smtBODY);
  187. end;
  188. if (smtBODY in FSiteMapTags) then begin
  189. //WriteLn('GOT TAG: ', AActualTag);
  190. if TagName = 'UL' then begin
  191. //WriteLN('Inc Level');
  192. IncreaseULevel;
  193. end
  194. else if TagName = '/UL' then begin
  195. //WriteLN('Dec Level');
  196. DecreaseULevel;
  197. end
  198. else if TagName = 'OBJECT' then begin
  199. Include(FSiteMapBodyTags, smbtOBJECT);
  200. If FLevel > 0 then // if it is zero it is the site properties
  201. NewSiteMapItem;
  202. end
  203. else if TagName = '/OBJECT' then begin
  204. Exclude(FSiteMapBodyTags, smbtOBJECT);
  205. end
  206. else begin // we are the properties of the object tag
  207. if (smbtOBJECT in FSiteMapBodyTags) then begin
  208. if LowerCase(GetTagName(AActualTag)) = 'param' then begin
  209. TagAttributeName := GetVal(AActualTag, 'name');
  210. TagAttributeValue := GetVal(AActualTag, 'value');
  211. if TagAttributeName <> '' then begin
  212. if CompareText(TagAttributeName, 'keyword') = 0 then begin
  213. ActiveItem.Text := TagAttributeValue;
  214. end
  215. else if CompareText(TagAttributeName, 'name') = 0 then begin
  216. if ActiveItem.Text = '' then ActiveItem.Text := TagAttributeValue;
  217. end
  218. else if CompareText(TagAttributeName, 'local') = 0 then begin
  219. ActiveItem.Local := TagAttributeValue;
  220. end
  221. else if CompareText(TagAttributeName, 'URL') = 0 then begin
  222. ActiveItem.URL := TagAttributeValue;
  223. end
  224. else if CompareText(TagAttributeName, 'ImageNumber') = 0 then begin
  225. ActiveItem.ImageNumber := StrToInt(TagAttributeValue);
  226. end
  227. else if CompareText(TagAttributeName, 'New') = 0 then begin
  228. ActiveItem.IncreaseImageIndex := (LowerCase(TagAttributeValue) = 'yes');
  229. end
  230. else if CompareText(TagAttributeName, 'Comment') = 0 then begin
  231. ActiveItem.Comment := TagAttributeValue;
  232. end;
  233. //else if CompareText(TagAttributeName, '') = 0 then begin
  234. //end;
  235. end;
  236. end;
  237. end;
  238. end;
  239. end;
  240. end
  241. end;
  242. procedure TChmSiteMap.FoundText(AText: string);
  243. begin
  244. //WriteLn('TEXT:', AText);
  245. end;
  246. constructor TChmSiteMap.Create(AType: TSiteMapType);
  247. begin
  248. Inherited Create;
  249. FSiteMapType := AType;
  250. FSiteMapTags := [smtNone];
  251. FSiteMapBodyTags := [smbtNone];
  252. FItems := TChmSiteMapItems.Create(Self, nil); ;
  253. end;
  254. destructor TChmSiteMap.Destroy;
  255. begin
  256. if Assigned(FHTMLParser) then FHTMLParser.Free;
  257. FItems.Free;
  258. Inherited Destroy;
  259. end;
  260. procedure TChmSiteMap.LoadFromFile(AFileName: String);
  261. var
  262. Buffer: String;
  263. TmpStream: TMemoryStream;
  264. begin
  265. if Assigned(FHTMLParser) then FHTMLParser.Free;
  266. TmpStream := TMemoryStream.Create;
  267. TmpStream.LoadFromFile(AFileName);
  268. SetLength(Buffer, TmpStream.Size);
  269. TmpStream.Position := 0;
  270. TmpStream.Read(Buffer[1], TmpStream.Size);
  271. FHTMLParser := THTMLParser.Create(Buffer);
  272. FHTMLParser.OnFoundTag := @FoundTag;
  273. FHTMLParser.OnFoundText := @FoundText;
  274. FHTMLParser.Exec;
  275. FHTMLParser.Free;
  276. end;
  277. procedure TChmSiteMap.LoadFromStream(AStream: TStream);
  278. var
  279. Buffer: String;
  280. begin
  281. if Assigned(FHTMLParser) then FHTMLParser.Free;
  282. SetLength(Buffer, AStream.Size-AStream.Position);
  283. if AStream.Read(Buffer[1], AStream.Size-AStream.Position) > 0 then begin;
  284. FHTMLParser := THTMLParser.Create(Buffer);
  285. FHTMLParser.OnFoundTag := @FoundTag;
  286. FHTMLParser.OnFoundText := @FoundText;
  287. FHTMLParser.Exec;
  288. FHTMLParser.Free;
  289. end;
  290. end;
  291. procedure TChmSiteMap.SaveToStream(AStream: TStream);
  292. var
  293. Indent: Integer;
  294. procedure WriteString(AString: String);
  295. var
  296. I: Integer;
  297. begin
  298. for I := 0 to Indent-1 do AStream.WriteByte(Byte(' '));
  299. AStream.Write(AString[1], Length(AString));
  300. AStream.WriteByte(10);
  301. end;
  302. procedure WriteParam(AName: String; AValue: String);
  303. begin
  304. WriteString('<param name="'+AName+'" value="'+AValue+'">');
  305. end;
  306. procedure WriteEntries(AItems: TChmSiteMapItems);
  307. var
  308. I : Integer;
  309. Item: TChmSiteMapItem;
  310. begin
  311. for I := 0 to AItems.Count-1 do begin
  312. Item := AItems.Item[I];
  313. WriteString('<LI> <OBJECT type="text/sitemap">');
  314. Inc(Indent, 8);
  315. if (SiteMapType = stIndex) and (Item.Children.Count > 0) then
  316. WriteParam('Keyword', Item.Text);
  317. //if Item.KeyWord <> '' then WriteParam('Keyword', Item.KeyWord);
  318. if Item.Text <> '' then WriteParam('Name', Item.Text);
  319. if (Item.Local <> '') or (SiteMapType = stIndex) then WriteParam('Local', Item.Local);
  320. if Item.URL <> '' then WriteParam('URL', Item.URL);
  321. if (SiteMapType = stIndex) and (Item.SeeAlso <> '') then WriteParam('See Also', Item.SeeAlso);
  322. //if Item.FrameName <> '' then WriteParam('FrameName', Item.FrameName);
  323. //if Item.WindowName <> '' then WriteParam('WindowName', Item.WindowName);
  324. if Item.Comment <> '' then WriteParam('Comment', Item.Comment);
  325. if (SiteMapType = stTOC) and (Item.IncreaseImageIndex) then WriteParam('New', 'yes'); // is this a correct value?
  326. if (SiteMapType = stTOC) and (Item.ImageNumber <> -1) then WriteParam('ImageNumber', IntToStr(Item.ImageNumber));
  327. Dec(Indent, 3);
  328. WriteString('</OBJECT>');
  329. Dec(Indent, 5);
  330. // Now Sub Entries
  331. if Item.Children.Count > 0 then begin
  332. WriteString('<UL>');
  333. Inc(Indent, 8);
  334. WriteEntries(Item.Children);
  335. Dec(Indent, 8);
  336. WriteString('</UL>');
  337. end;
  338. end;
  339. end;
  340. begin
  341. Indent := 0;
  342. WriteString('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">');
  343. WriteString('<HTML>');
  344. WriteString('<HEAD>');
  345. WriteString('<meta name="GENERATOR" content="Microsoft&reg; HTML Help Workshop 4.1">'); // Should we change this?
  346. WriteString('<!-- Sitemap 1.0 -->');
  347. WriteString('</HEAD><BODY>');
  348. // Site Properties
  349. WriteString('<OBJECT type="text/site properties">');
  350. Inc(Indent, 8);
  351. if SiteMapType = stTOC then begin
  352. if FrameName <> '' then WriteParam('FrameName', FrameName);
  353. if WindowName <> '' then WriteParam('WindowName', WindowName);
  354. if ImageList <> '' then WriteParam('ImageList', ImageList);
  355. if ImageWidth > 0 then WriteParam('Image Width', IntToStr(ImageWidth));
  356. if BackgroundColor <> 0 then WriteParam('Background', hexStr(BackgroundColor, 4));
  357. if ForegroundColor <> 0 then WriteParam('Foreground', hexStr(ForegroundColor, 4));
  358. if ExWindowStyles <> 0 then WriteParam('ExWindow Styles', hexStr(ExWindowStyles, 4));
  359. if WindowStyles <> 0 then WriteParam('Window Styles', hexStr(WindowStyles, 4));
  360. if UseFolderImages then WriteParam('ImageType', 'Folder');
  361. end;
  362. // both TOC and Index have font
  363. if Font <> '' then
  364. WriteParam('Font', Font);
  365. Dec(Indent, 8);
  366. WriteString('</OBJECT>');
  367. // And now the items
  368. if Items.Count > 0 then begin
  369. WriteString('<UL>');
  370. Inc(Indent, 8);
  371. // WriteEntries
  372. WriteEntries(Items);
  373. Dec(Indent, 8);
  374. WriteString('</UL>');
  375. end;
  376. WriteString('</BODY></HTML>');
  377. AStream.Size := AStream.Position;
  378. end;
  379. { TChmSiteMapItem }
  380. procedure TChmSiteMapItem.SetChildren(const AValue: TChmSiteMapItems);
  381. begin
  382. if FChildren = AValue then exit;
  383. FChildren := AValue;
  384. end;
  385. constructor TChmSiteMapItem.Create(AOwner: TChmSiteMapItems);
  386. begin
  387. Inherited Create;
  388. FOwner := AOwner;
  389. FChildren := TChmSiteMapItems.Create(Owner.Owner, Self);
  390. end;
  391. destructor TChmSiteMapItem.Destroy;
  392. begin
  393. FChildren.Free;
  394. Inherited Destroy;
  395. end;
  396. { TChmSiteMapItems }
  397. function TChmSiteMapItems.GetItem(AIndex: Integer): TChmSiteMapItem;
  398. begin
  399. Result := TChmSiteMapItem(FList.Items[AIndex]);
  400. end;
  401. function TChmSiteMapItems.GetCount: Integer;
  402. begin
  403. Result := FList.Count;
  404. end;
  405. procedure TChmSiteMapItems.SetItem(AIndex: Integer; const AValue: TChmSiteMapItem);
  406. begin
  407. FList.Items[AIndex] := AValue;
  408. end;
  409. constructor TChmSiteMapItems.Create(AOwner: TChmSiteMap; AParentItem: TChmSiteMapItem);
  410. begin
  411. FList := TList.Create;
  412. FParentItem := AParentItem;
  413. FOwner := AOwner;
  414. end;
  415. destructor TChmSiteMapItems.Destroy;
  416. begin
  417. Clear;
  418. FList.Free;
  419. inherited Destroy;
  420. end;
  421. procedure TChmSiteMapItems.Delete(AIndex: Integer);
  422. begin
  423. Item[AIndex].Free;
  424. FList.Delete(AIndex);
  425. end;
  426. function TChmSiteMapItems.Add(AItem: TChmSiteMapItem): Integer;
  427. begin
  428. Result := FList.Add(AItem);
  429. end;
  430. function TChmSiteMapItems.NewItem: TChmSiteMapItem;
  431. begin
  432. Result := TChmSiteMapItem.Create(Self);
  433. Add(Result);
  434. end;
  435. function TChmSiteMapItems.Insert(AItem: TChmSiteMapItem; AIndex: Integer): Integer;
  436. begin
  437. Result := AIndex;
  438. FList.Insert(AIndex, AItem);
  439. end;
  440. procedure TChmSiteMapItems.Clear;
  441. var
  442. I: LongInt;
  443. begin
  444. for I := Count-1 downto 0 do Delete(I);
  445. end;
  446. procedure TChmSiteMapItems.Sort(Compare: TListSortCompare);
  447. begin
  448. FList.Sort(Compare);
  449. end;
  450. end.