chmsitemap.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. FHTMLParser:=nil;
  253. FItems := TChmSiteMapItems.Create(Self, nil); ;
  254. end;
  255. destructor TChmSiteMap.Destroy;
  256. begin
  257. if Assigned(FHTMLParser) then FHTMLParser.Free;
  258. FItems.Free;
  259. Inherited Destroy;
  260. end;
  261. procedure TChmSiteMap.LoadFromFile(AFileName: String);
  262. var
  263. Buffer: String;
  264. TmpStream: TMemoryStream;
  265. begin
  266. if Assigned(FHTMLParser) then FHTMLParser.Free;
  267. TmpStream := TMemoryStream.Create;
  268. TmpStream.LoadFromFile(AFileName);
  269. SetLength(Buffer, TmpStream.Size);
  270. TmpStream.Position := 0;
  271. TmpStream.Read(Buffer[1], TmpStream.Size);
  272. FHTMLParser := THTMLParser.Create(Buffer);
  273. FHTMLParser.OnFoundTag := @FoundTag;
  274. FHTMLParser.OnFoundText := @FoundText;
  275. FHTMLParser.Exec;
  276. FreeAndNil(FHTMLParser);
  277. end;
  278. procedure TChmSiteMap.LoadFromStream(AStream: TStream);
  279. var
  280. Buffer: String;
  281. begin
  282. if Assigned(FHTMLParser) then FHTMLParser.Free;
  283. SetLength(Buffer, AStream.Size-AStream.Position);
  284. if AStream.Read(Buffer[1], AStream.Size-AStream.Position) > 0 then begin;
  285. FHTMLParser := THTMLParser.Create(Buffer);
  286. FHTMLParser.OnFoundTag := @FoundTag;
  287. FHTMLParser.OnFoundText := @FoundText;
  288. FHTMLParser.Exec;
  289. FreeAndNil(FHTMLParser);
  290. end;
  291. end;
  292. procedure TChmSiteMap.SaveToStream(AStream: TStream);
  293. var
  294. Indent: Integer;
  295. procedure WriteString(AString: String);
  296. var
  297. I: Integer;
  298. begin
  299. for I := 0 to Indent-1 do AStream.WriteByte(Byte(' '));
  300. AStream.Write(AString[1], Length(AString));
  301. AStream.WriteByte(10);
  302. end;
  303. procedure WriteParam(AName: String; AValue: String);
  304. begin
  305. WriteString('<param name="'+AName+'" value="'+AValue+'">');
  306. end;
  307. procedure WriteEntries(AItems: TChmSiteMapItems);
  308. var
  309. I : Integer;
  310. Item: TChmSiteMapItem;
  311. begin
  312. for I := 0 to AItems.Count-1 do begin
  313. Item := AItems.Item[I];
  314. WriteString('<LI> <OBJECT type="text/sitemap">');
  315. Inc(Indent, 8);
  316. if (SiteMapType = stIndex) and (Item.Children.Count > 0) then
  317. WriteParam('Keyword', Item.Text);
  318. //if Item.KeyWord <> '' then WriteParam('Keyword', Item.KeyWord);
  319. if Item.Text <> '' then WriteParam('Name', Item.Text);
  320. if (Item.Local <> '') or (SiteMapType = stIndex) then WriteParam('Local', Item.Local);
  321. if Item.URL <> '' then WriteParam('URL', Item.URL);
  322. if (SiteMapType = stIndex) and (Item.SeeAlso <> '') then WriteParam('See Also', Item.SeeAlso);
  323. //if Item.FrameName <> '' then WriteParam('FrameName', Item.FrameName);
  324. //if Item.WindowName <> '' then WriteParam('WindowName', Item.WindowName);
  325. if Item.Comment <> '' then WriteParam('Comment', Item.Comment);
  326. if (SiteMapType = stTOC) and (Item.IncreaseImageIndex) then WriteParam('New', 'yes'); // is this a correct value?
  327. if (SiteMapType = stTOC) and (Item.ImageNumber <> -1) then WriteParam('ImageNumber', IntToStr(Item.ImageNumber));
  328. Dec(Indent, 3);
  329. WriteString('</OBJECT>');
  330. Dec(Indent, 5);
  331. // Now Sub Entries
  332. if Item.Children.Count > 0 then begin
  333. WriteString('<UL>');
  334. Inc(Indent, 8);
  335. WriteEntries(Item.Children);
  336. Dec(Indent, 8);
  337. WriteString('</UL>');
  338. end;
  339. end;
  340. end;
  341. begin
  342. Indent := 0;
  343. WriteString('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">');
  344. WriteString('<HTML>');
  345. WriteString('<HEAD>');
  346. WriteString('<meta name="GENERATOR" content="Microsoft&reg; HTML Help Workshop 4.1">'); // Should we change this?
  347. WriteString('<!-- Sitemap 1.0 -->');
  348. WriteString('</HEAD><BODY>');
  349. // Site Properties
  350. WriteString('<OBJECT type="text/site properties">');
  351. Inc(Indent, 8);
  352. if SiteMapType = stTOC then begin
  353. if FrameName <> '' then WriteParam('FrameName', FrameName);
  354. if WindowName <> '' then WriteParam('WindowName', WindowName);
  355. if ImageList <> '' then WriteParam('ImageList', ImageList);
  356. if ImageWidth > 0 then WriteParam('Image Width', IntToStr(ImageWidth));
  357. if BackgroundColor <> 0 then WriteParam('Background', hexStr(BackgroundColor, 4));
  358. if ForegroundColor <> 0 then WriteParam('Foreground', hexStr(ForegroundColor, 4));
  359. if ExWindowStyles <> 0 then WriteParam('ExWindow Styles', hexStr(ExWindowStyles, 4));
  360. if WindowStyles <> 0 then WriteParam('Window Styles', hexStr(WindowStyles, 4));
  361. if UseFolderImages then WriteParam('ImageType', 'Folder');
  362. end;
  363. // both TOC and Index have font
  364. if Font <> '' then
  365. WriteParam('Font', Font);
  366. Dec(Indent, 8);
  367. WriteString('</OBJECT>');
  368. // And now the items
  369. if Items.Count > 0 then begin
  370. WriteString('<UL>');
  371. Inc(Indent, 8);
  372. // WriteEntries
  373. WriteEntries(Items);
  374. Dec(Indent, 8);
  375. WriteString('</UL>');
  376. end;
  377. WriteString('</BODY></HTML>');
  378. AStream.Size := AStream.Position;
  379. end;
  380. { TChmSiteMapItem }
  381. procedure TChmSiteMapItem.SetChildren(const AValue: TChmSiteMapItems);
  382. begin
  383. if FChildren = AValue then exit;
  384. FChildren := AValue;
  385. end;
  386. constructor TChmSiteMapItem.Create(AOwner: TChmSiteMapItems);
  387. begin
  388. Inherited Create;
  389. FOwner := AOwner;
  390. FChildren := TChmSiteMapItems.Create(Owner.Owner, Self);
  391. end;
  392. destructor TChmSiteMapItem.Destroy;
  393. begin
  394. FChildren.Free;
  395. Inherited Destroy;
  396. end;
  397. { TChmSiteMapItems }
  398. function TChmSiteMapItems.GetItem(AIndex: Integer): TChmSiteMapItem;
  399. begin
  400. Result := TChmSiteMapItem(FList.Items[AIndex]);
  401. end;
  402. function TChmSiteMapItems.GetCount: Integer;
  403. begin
  404. Result := FList.Count;
  405. end;
  406. procedure TChmSiteMapItems.SetItem(AIndex: Integer; const AValue: TChmSiteMapItem);
  407. begin
  408. FList.Items[AIndex] := AValue;
  409. end;
  410. constructor TChmSiteMapItems.Create(AOwner: TChmSiteMap; AParentItem: TChmSiteMapItem);
  411. begin
  412. FList := TList.Create;
  413. FParentItem := AParentItem;
  414. FOwner := AOwner;
  415. end;
  416. destructor TChmSiteMapItems.Destroy;
  417. begin
  418. Clear;
  419. FList.Free;
  420. inherited Destroy;
  421. end;
  422. procedure TChmSiteMapItems.Delete(AIndex: Integer);
  423. begin
  424. Item[AIndex].Free;
  425. FList.Delete(AIndex);
  426. end;
  427. function TChmSiteMapItems.Add(AItem: TChmSiteMapItem): Integer;
  428. begin
  429. Result := FList.Add(AItem);
  430. end;
  431. function TChmSiteMapItems.NewItem: TChmSiteMapItem;
  432. begin
  433. Result := TChmSiteMapItem.Create(Self);
  434. Add(Result);
  435. end;
  436. function TChmSiteMapItems.Insert(AItem: TChmSiteMapItem; AIndex: Integer): Integer;
  437. begin
  438. Result := AIndex;
  439. FList.Insert(AIndex, AItem);
  440. end;
  441. procedure TChmSiteMapItems.Clear;
  442. var
  443. I: LongInt;
  444. begin
  445. for I := Count-1 downto 0 do Delete(I);
  446. end;
  447. procedure TChmSiteMapItems.Sort(Compare: TListSortCompare);
  448. begin
  449. FList.Sort(Compare);
  450. end;
  451. end.