2
0

chmsitemap.pas 16 KB

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