whtml.pp 9.2 KB

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