whtml.pp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. {
  2. $Id$
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 1999-2000 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit whtml;
  12. {$ifdef fpc}
  13. {$mode objfpc}
  14. {$endif}
  15. interface
  16. uses wformat,Classes,SysUtils;
  17. Type
  18. THTMLWriter=Class(TFormattingWriter)
  19. Public
  20. Constructor Create (AStream : TStream); override;
  21. Procedure TagStart(Const Name, Attrs : String);
  22. Procedure TagEnd(Const Name : String);
  23. Function EscapeText (AText : String) : String; override;
  24. Procedure DocumentStart(Const Title : String); override;
  25. Procedure DocumentEnd; override;
  26. Procedure HeaderStart(Alevel : Integer); override;
  27. Procedure HeaderEnd(Alevel : Integer); override;
  28. Procedure ParagraphStart; override;
  29. Procedure ParagraphEnd; override;
  30. Procedure LineBreak; override;
  31. Procedure Rule; override;
  32. Procedure BoldStart; override;
  33. Procedure BoldEnd;override;
  34. Procedure ItalicStart;override;
  35. Procedure ItalicEnd;override;
  36. Procedure UnderlineStart;override;
  37. Procedure UnderlineEnd;override;
  38. Procedure PreformatStart; override;
  39. Procedure PreformatEnd; override;
  40. Procedure TableStart( NoCols: Integer; Border : Boolean); override;
  41. Procedure TableEnd; override;
  42. Procedure RowStart; override;
  43. Procedure RowEnd; override;
  44. Procedure CellStart; override;
  45. Procedure CellEnd; override;
  46. Procedure HeaderCellStart; override;
  47. Procedure HeaderCellEnd; override;
  48. Procedure ListStart(ListType : TListType); override;
  49. Procedure ListEnd(ListType : TListType); override;
  50. Procedure ListItemStart; override;
  51. Procedure ListItemEnd; override;
  52. Procedure DefinitionItem(Const Aname,AText : String); override;
  53. // Form support
  54. Procedure FormStart(Const Action,Method : String);
  55. Procedure FormEnd;
  56. Procedure EmitInput(Const Name,Value : String);
  57. Procedure EmitInput(Const Name,Value, Attrs : String);
  58. Procedure EmitPasswordInput(Const Name,Value : String);
  59. Procedure EmitCheckBox(Const Name,Value : String);
  60. Procedure EmitCheckBox(Const Name,Value : String; Checked : Boolean);
  61. Procedure EmitRadioButton(Const Name,Value : String);
  62. Procedure EmitRadioButton(Const Name,Value : String; Checked : Boolean);
  63. Procedure EmitArea(Const Name,Value : String; Rows,Cols : Integer);
  64. Procedure EmitComboBox(Const Name, Value : String; Items : TStrings; UseValues : Boolean);
  65. Procedure EmitComboBox(Const Name, Value : String; Items : TStrings);
  66. Procedure EmitButton(Const Name,ButtonType,Value : String);
  67. Procedure EmitSubmitButton(Const Name,Value : String);
  68. Procedure EmitResetButton(Const Name,Value : String);
  69. Procedure EmitHiddenVar(Const Name,Value: String);
  70. end;
  71. Const
  72. ListTags : Array[TListType] of string[2] = ('OL','UL','DL');
  73. implementation
  74. { THTMLWriter }
  75. procedure THTMLWriter.BoldEnd;
  76. begin
  77. TagEnd('B');
  78. end;
  79. procedure THTMLWriter.BoldStart;
  80. begin
  81. TagStart('B','');
  82. end;
  83. procedure THTMLWriter.CellEnd;
  84. begin
  85. TagEnd('TD');
  86. end;
  87. procedure THTMLWriter.CellStart;
  88. begin
  89. TagStart('TD','');
  90. end;
  91. constructor THTMLWriter.Create(AStream: TStream);
  92. begin
  93. inherited;
  94. end;
  95. procedure THTMLWriter.DefinitionItem(const Aname, AText: String);
  96. begin
  97. TagStart('DT','');
  98. Write(Aname);
  99. TagEnd('DT');
  100. TagStart('DD','');
  101. Write(AText);
  102. TagEnd('DD');
  103. end;
  104. procedure THTMLWriter.DocumentEnd;
  105. begin
  106. TagEnd('BODY');
  107. TagEnd('HTML');
  108. end;
  109. procedure THTMLWriter.DocumentStart(const Title: String);
  110. begin
  111. inherited;
  112. TagStart('HTML','');
  113. TagStart('TITLE','');
  114. Write(Title);
  115. TagEnd('TITLE');
  116. TagStart('BODY','');
  117. end;
  118. function THTMLWriter.EscapeText(AText: String): String;
  119. begin
  120. // replace by a more sensitive method.
  121. Result:=StringReplace(AText,'&','&amp',[rfReplaceAll]);
  122. Result:=StringReplace(Result,'<','&lt',[rfReplaceAll]);
  123. Result:=StringReplace(Result,'>','&gt',[rfReplaceAll]);
  124. Result:=StringReplace(Result,#10,'<BR>',[rfreplaceAll]);
  125. end;
  126. procedure THTMLWriter.HeaderCellEnd;
  127. begin
  128. TagEnd('TH');
  129. end;
  130. procedure THTMLWriter.HeaderCellStart;
  131. begin
  132. TagStart('TH','');
  133. end;
  134. procedure THTMLWriter.HeaderEnd(Alevel: Integer);
  135. begin
  136. TagEnd(Format('H%d',[ALevel]));
  137. end;
  138. procedure THTMLWriter.HeaderStart(Alevel: Integer);
  139. begin
  140. TagStart(Format('H%d',[ALevel]),'');
  141. end;
  142. procedure THTMLWriter.ItalicEnd;
  143. begin
  144. TagEnd('I');
  145. end;
  146. procedure THTMLWriter.ItalicStart;
  147. begin
  148. TagStart('I','');
  149. end;
  150. procedure THTMLWriter.LineBreak;
  151. begin
  152. TagStart('BR','');
  153. end;
  154. procedure THTMLWriter.ListEnd(ListType: TListType);
  155. begin
  156. TagEnd(ListTags[ListType]);
  157. end;
  158. procedure THTMLWriter.ListItemEnd;
  159. begin
  160. TagEnd('LI');
  161. end;
  162. procedure THTMLWriter.ListItemStart;
  163. begin
  164. TagStart('LI','');
  165. end;
  166. procedure THTMLWriter.ListStart(ListType: TListType);
  167. begin
  168. TagEnd(ListTags[ListType]);
  169. end;
  170. procedure THTMLWriter.ParagraphEnd;
  171. begin
  172. TagEnd('P')
  173. end;
  174. procedure THTMLWriter.ParagraphStart;
  175. begin
  176. TagStart('P','')
  177. end;
  178. procedure THTMLWriter.PreformatEnd;
  179. begin
  180. TagEnd('PRE')
  181. end;
  182. procedure THTMLWriter.PreformatStart;
  183. begin
  184. TagStart('PRE','');
  185. end;
  186. procedure THTMLWriter.RowEnd;
  187. begin
  188. TagEnd('TR')
  189. end;
  190. procedure THTMLWriter.RowStart;
  191. begin
  192. TagStart('TR','')
  193. end;
  194. procedure THTMLWriter.Rule;
  195. begin
  196. TagStart('HR','');
  197. end;
  198. procedure THTMLWriter.TableStart(NoCols: Integer; Border: Boolean);
  199. Var
  200. Attr : string;
  201. begin
  202. if Border then
  203. Attr:='BORDER=1'
  204. else
  205. Attr:='';
  206. TagStart('TABLE',Attr);
  207. end;
  208. procedure THTMLWriter.TableEnd;
  209. begin
  210. TagEnd('TABLE');
  211. end;
  212. procedure THTMLWriter.TagEnd(const Name : String);
  213. begin
  214. Dump('</'+Name+'>');
  215. end;
  216. procedure THTMLWriter.TagStart(const Name, Attrs: String);
  217. begin
  218. Dump('<'+Name);
  219. If Attrs<>'' then
  220. begin
  221. Dump(' ');
  222. Dump(Attrs);
  223. end;
  224. Dump('>');
  225. end;
  226. procedure THTMLWriter.UnderlineEnd;
  227. begin
  228. TagEnd('U');
  229. end;
  230. procedure THTMLWriter.UnderlineStart;
  231. begin
  232. TagStart('U','');
  233. end;
  234. // Form support.
  235. Procedure THTMLWriter.FormStart(Const Action,Method : String);
  236. Var
  237. A : String;
  238. begin
  239. A:='ACTION="'+Action+'"';
  240. If (Method<>'') then
  241. A:=A+' METHOD="'+Method+'"';
  242. TagStart('FORM',A);
  243. end;
  244. Procedure THTMLWriter.FormEnd;
  245. begin
  246. Tagend('FORM');
  247. end;
  248. Procedure THTMLWriter.EmitInput(Const Name,Value : String);
  249. begin
  250. EmitInput(Name,Value,'');
  251. end;
  252. Procedure THTMLWriter.EmitPasswordInput(Const Name,Value : String);
  253. begin
  254. EmitInput(Name,Value,'TYPE="password"');
  255. end;
  256. Procedure THTMLWriter.EmitInput(Const Name,Value, Attrs : String);
  257. Var
  258. A : String;
  259. begin
  260. A:='NAME="'+Name+'"';
  261. If (Value<>'') then
  262. A:=A+' VALUE="'+Value+'"';
  263. If (Attrs<>'') then
  264. A:=A+' '+Attrs;
  265. TagStart('INPUT',A);
  266. end;
  267. Procedure THTMLWriter.EmitCheckBox(Const Name,Value : String);
  268. begin
  269. EmitCheckBox(Name,Value,False);
  270. end;
  271. Procedure THTMLWriter.EmitCheckBox(Const Name,Value : String; Checked : Boolean);
  272. Var
  273. A : String;
  274. begin
  275. A:='NAME="'+Name+'" TYPE="checkbox" VALUE="'+Value+'"';
  276. If Checked then
  277. A:=A+' CHECKED=1';
  278. TagStart('INPUT',A);
  279. end;
  280. Procedure THTMLWriter.EmitRadioButton(Const Name,Value : String);
  281. begin
  282. EmitRadioButton(Name,Value,False);
  283. end;
  284. Procedure THTMLWriter.EmitRadioButton(Const Name,Value : String; Checked : Boolean);
  285. Var
  286. A : String;
  287. begin
  288. A:='NAME="'+Name+'" TYPE="checkbox" VALUE="'+Value+'"';
  289. If Checked then
  290. A:=A+' CHECKED=1';
  291. TagStart('INPUT',A);
  292. end;
  293. Procedure THTMLWriter.EmitArea(Const Name,Value : String; Rows,Cols : Integer);
  294. Var
  295. A : String;
  296. begin
  297. A:='NAME="'+Name+'"';
  298. If (Rows<>0) and (cols<>0) then
  299. A:=A+Format(' ROWS=%d COLS=%d',[Rows,Cols]);
  300. TagStart('TEXTAREA',A);
  301. Write(Value);
  302. TagEnd('TEXTAREA');
  303. end;
  304. Procedure THTMLWriter.EmitComboBox(Const Name, Value : String; Items : TStrings);
  305. begin
  306. EmitComboBox(Name,Value,Items,False);
  307. end;
  308. Procedure THTMLWriter.EmitComboBox(Const Name, Value : String; Items : TStrings; UseValues : Boolean);
  309. Var
  310. A,S,V : String;
  311. I,P : Integer;
  312. begin
  313. TagStart('SELECT','NAME='+Name+'"');
  314. A:='';
  315. For I:=0 to Items.Count-1 do
  316. begin
  317. S:=Items[I];
  318. If UseValues then
  319. begin
  320. P:=Pos('=',S);
  321. If P>0 then
  322. begin
  323. V:=Copy(S,1,P-1);
  324. Delete(S,1,P);
  325. A:='VALUE="'+Copy(S,1,P-1)+'"';
  326. end
  327. else
  328. begin
  329. A:='';
  330. V:=S;
  331. end;
  332. end;
  333. If (Value<>'') and (V=Value) then
  334. A:=A+' SELECTED';
  335. TagStart('OPTION',A);
  336. end;
  337. TagEnd('SELECT')
  338. end;
  339. Procedure THTMLWriter.EmitSubmitButton(Const Name,Value : String);
  340. begin
  341. EmitButton(Name,'submit',Value)
  342. end;
  343. Procedure THTMLWriter.EmitResetButton(Const Name,Value : String);
  344. begin
  345. EmitButton(Name,'reset',Value)
  346. end;
  347. Procedure THTMLWriter.EmitButton(Const Name,ButtonType,Value : String);
  348. Var
  349. A : String;
  350. begin
  351. A:='TYPE="'+ButtonType+'"';
  352. If (Value<>'') then
  353. A:=A+' VALUE="'+Value+'"';
  354. If (Name<>'') then
  355. A:=A+' NAME="'+Name+'"';
  356. TagStart('INPUT',A)
  357. end;
  358. Procedure THTMLWriter.EmitHiddenVar(Const Name,Value: String);
  359. Var
  360. A : String;
  361. begin
  362. A:='TYPE="hidden" NAME="'+Name+'" VALUE="'+Value+'"';
  363. TagStart('INPUT',A);
  364. end;
  365. end.
  366. {
  367. $Log$
  368. Revision 1.2 2003-10-03 08:42:22 michael
  369. + Form support.
  370. Revision 1.1 2003/10/01 20:49:29 michael
  371. + Initial implementation
  372. }