123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 |
- {
- This file is part of the Free Component Library (FCL)
- Copyright (c) 1999-2000 by the Free Pascal development team
- See the file COPYING.FPC, included in this distribution,
- for details about the copyright.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- **********************************************************************}
- unit whtml;
- {$ifdef fpc}
- {$mode objfpc}
- {$endif}
- interface
- uses wformat,Classes,SysUtils;
- Type
- THTMLWriter=Class(TFormattingWriter)
- Public
- Constructor Create (AStream : TStream); override;
- Procedure TagStart(Const Name, Attrs : String);
- Procedure TagEnd(Const Name : String);
- Function EscapeText (AText : String) : String; override;
- Procedure DocumentStart(Const Title : String); override;
- Procedure DocumentEnd; override;
- Procedure HeaderStart(Alevel : Integer); override;
- Procedure HeaderEnd(Alevel : Integer); override;
- Procedure ParagraphStart; override;
- Procedure ParagraphEnd; override;
- Procedure LineBreak; override;
- Procedure Rule; override;
- Procedure BoldStart; override;
- Procedure BoldEnd;override;
- Procedure ItalicStart;override;
- Procedure ItalicEnd;override;
- Procedure UnderlineStart;override;
- Procedure UnderlineEnd;override;
- Procedure PreformatStart; override;
- Procedure PreformatEnd; override;
- Procedure TableStart( NoCols: Integer; Border : Boolean); override;
- Procedure TableEnd; override;
- Procedure RowStart; override;
- Procedure RowEnd; override;
- Procedure CellStart; override;
- Procedure CellEnd; override;
- Procedure HeaderCellStart; override;
- Procedure HeaderCellEnd; override;
- Procedure ListStart(ListType : TListType); override;
- Procedure ListEnd(ListType : TListType); override;
- Procedure ListItemStart; override;
- Procedure ListItemEnd; override;
- Procedure DefinitionItem(Const Aname,AText : String); override;
- // Form support
- Procedure FormStart(Const Action,Method : String);
- Procedure FormEnd;
- Procedure EmitInput(Const Name,Value : String);
- Procedure EmitInput(Const Name,Value, Attrs : String);
- Procedure EmitPasswordInput(Const Name,Value : String);
- Procedure EmitCheckBox(Const Name,Value : String);
- Procedure EmitCheckBox(Const Name,Value : String; Checked : Boolean);
- Procedure EmitRadioButton(Const Name,Value : String);
- Procedure EmitRadioButton(Const Name,Value : String; Checked : Boolean);
- Procedure EmitArea(Const Name,Value : String; Rows,Cols : Integer);
- Procedure EmitComboBox(Const Name, Value : String; Items : TStrings; UseValues : Boolean);
- Procedure EmitComboBox(Const Name, Value : String; Items : TStrings);
- Procedure EmitButton(Const Name,ButtonType,Value : String);
- Procedure EmitSubmitButton(Const Name,Value : String);
- Procedure EmitResetButton(Const Name,Value : String);
- Procedure EmitHiddenVar(Const Name,Value: String);
- end;
- Const
- ListTags : Array[TListType] of string[2] = ('OL','UL','DL');
- implementation
- { THTMLWriter }
- procedure THTMLWriter.BoldEnd;
- begin
- TagEnd('B');
- end;
- procedure THTMLWriter.BoldStart;
- begin
- TagStart('B','');
- end;
- procedure THTMLWriter.CellEnd;
- begin
- TagEnd('TD');
- end;
- procedure THTMLWriter.CellStart;
- begin
- TagStart('TD','');
- end;
- constructor THTMLWriter.Create(AStream: TStream);
- begin
- inherited;
- end;
- procedure THTMLWriter.DefinitionItem(const Aname, AText: String);
- begin
- TagStart('DT','');
- Write(Aname);
- TagEnd('DT');
- TagStart('DD','');
- Write(AText);
- TagEnd('DD');
- end;
- procedure THTMLWriter.DocumentEnd;
- begin
- TagEnd('BODY');
- TagEnd('HTML');
- end;
- procedure THTMLWriter.DocumentStart(const Title: String);
- begin
- inherited;
- TagStart('HTML','');
- TagStart('TITLE','');
- Write(Title);
- TagEnd('TITLE');
- TagStart('BODY','');
- end;
- function THTMLWriter.EscapeText(AText: String): String;
- begin
- // replace by a more sensitive method.
- Result:=StringReplace(AText,'&','&',[rfReplaceAll]);
- Result:=StringReplace(Result,'<','<',[rfReplaceAll]);
- Result:=StringReplace(Result,'>','>',[rfReplaceAll]);
- Result:=StringReplace(Result,#10,'<BR>',[rfreplaceAll]);
- end;
- procedure THTMLWriter.HeaderCellEnd;
- begin
- TagEnd('TH');
- end;
- procedure THTMLWriter.HeaderCellStart;
- begin
- TagStart('TH','');
- end;
- procedure THTMLWriter.HeaderEnd(Alevel: Integer);
- begin
- TagEnd(Format('H%d',[ALevel]));
- end;
- procedure THTMLWriter.HeaderStart(Alevel: Integer);
- begin
- TagStart(Format('H%d',[ALevel]),'');
- end;
- procedure THTMLWriter.ItalicEnd;
- begin
- TagEnd('I');
- end;
- procedure THTMLWriter.ItalicStart;
- begin
- TagStart('I','');
- end;
- procedure THTMLWriter.LineBreak;
- begin
- TagStart('BR','');
- end;
- procedure THTMLWriter.ListEnd(ListType: TListType);
- begin
- TagEnd(ListTags[ListType]);
- end;
- procedure THTMLWriter.ListItemEnd;
- begin
- TagEnd('LI');
- end;
- procedure THTMLWriter.ListItemStart;
- begin
- TagStart('LI','');
- end;
- procedure THTMLWriter.ListStart(ListType: TListType);
- begin
- TagStart(ListTags[ListType],'');
- end;
- procedure THTMLWriter.ParagraphEnd;
- begin
- TagEnd('P')
- end;
- procedure THTMLWriter.ParagraphStart;
- begin
- TagStart('P','')
- end;
- procedure THTMLWriter.PreformatEnd;
- begin
- TagEnd('PRE')
- end;
- procedure THTMLWriter.PreformatStart;
- begin
- TagStart('PRE','');
- end;
- procedure THTMLWriter.RowEnd;
- begin
- TagEnd('TR')
- end;
- procedure THTMLWriter.RowStart;
- begin
- TagStart('TR','')
- end;
- procedure THTMLWriter.Rule;
- begin
- TagStart('HR','');
- end;
- procedure THTMLWriter.TableStart(NoCols: Integer; Border: Boolean);
- Var
- Attr : string;
- begin
- if Border then
- Attr:='BORDER=1'
- else
- Attr:='';
- TagStart('TABLE',Attr);
- end;
- procedure THTMLWriter.TableEnd;
- begin
- TagEnd('TABLE');
- end;
- procedure THTMLWriter.TagEnd(const Name : String);
- begin
- Dump('</'+Name+'>');
- end;
- procedure THTMLWriter.TagStart(const Name, Attrs: String);
- begin
- Dump('<'+Name);
- If Attrs<>'' then
- begin
- Dump(' ');
- Dump(Attrs);
- end;
- Dump('>');
- end;
- procedure THTMLWriter.UnderlineEnd;
- begin
- TagEnd('U');
- end;
- procedure THTMLWriter.UnderlineStart;
- begin
- TagStart('U','');
- end;
- // Form support.
- Procedure THTMLWriter.FormStart(Const Action,Method : String);
- Var
- A : String;
- begin
- A:='ACTION="'+Action+'"';
- If (Method<>'') then
- A:=A+' METHOD="'+Method+'"';
- TagStart('FORM',A);
- end;
- Procedure THTMLWriter.FormEnd;
- begin
- Tagend('FORM');
- end;
- Procedure THTMLWriter.EmitInput(Const Name,Value : String);
- begin
- EmitInput(Name,Value,'');
- end;
- Procedure THTMLWriter.EmitPasswordInput(Const Name,Value : String);
- begin
- EmitInput(Name,Value,'TYPE="password"');
- end;
- Procedure THTMLWriter.EmitInput(Const Name,Value, Attrs : String);
- Var
- A : String;
- begin
- A:='NAME="'+Name+'"';
- If (Value<>'') then
- A:=A+' VALUE="'+Value+'"';
- If (Attrs<>'') then
- A:=A+' '+Attrs;
- TagStart('INPUT',A);
- end;
- Procedure THTMLWriter.EmitCheckBox(Const Name,Value : String);
- begin
- EmitCheckBox(Name,Value,False);
- end;
- Procedure THTMLWriter.EmitCheckBox(Const Name,Value : String; Checked : Boolean);
- Var
- A : String;
- begin
- A:='NAME="'+Name+'" TYPE="checkbox" VALUE="'+Value+'"';
- If Checked then
- A:=A+' CHECKED="checked"';
- TagStart('INPUT',A);
- end;
- Procedure THTMLWriter.EmitRadioButton(Const Name,Value : String);
- begin
- EmitRadioButton(Name,Value,False);
- end;
- Procedure THTMLWriter.EmitRadioButton(Const Name,Value : String; Checked : Boolean);
- Var
- A : String;
- begin
- A:='NAME="'+Name+'" TYPE="checkbox" VALUE="'+Value+'"';
- If Checked then
- A:=A+' CHECKED="checked"';
- TagStart('INPUT',A);
- end;
- Procedure THTMLWriter.EmitArea(Const Name,Value : String; Rows,Cols : Integer);
- Var
- A : String;
- begin
- A:='NAME="'+Name+'"';
- If (Rows<>0) and (cols<>0) then
- A:=A+Format(' ROWS=%d COLS=%d',[Rows,Cols]);
- TagStart('TEXTAREA',A);
- Write(Value);
- TagEnd('TEXTAREA');
- end;
- Procedure THTMLWriter.EmitComboBox(Const Name, Value : String; Items : TStrings);
- begin
- EmitComboBox(Name,Value,Items,False);
- end;
- Procedure THTMLWriter.EmitComboBox(Const Name, Value : String; Items : TStrings; UseValues : Boolean);
- Var
- A,S,V : String;
- I,P : Integer;
- begin
- TagStart('SELECT','NAME='+Name+'"');
- A:='';
- For I:=0 to Items.Count-1 do
- begin
- S:=Items[I];
- If UseValues then
- begin
- P:=Pos('=',S);
- If P>0 then
- begin
- V:=Copy(S,1,P-1);
- Delete(S,1,P);
- A:='VALUE="'+Copy(S,1,P-1)+'"';
- end
- else
- begin
- A:='';
- V:=S;
- end;
- end;
- If (Value<>'') and (V=Value) then
- A:=A+' SELECTED="selected"';
- TagStart('OPTION',A);
- end;
- TagEnd('SELECT')
- end;
- Procedure THTMLWriter.EmitSubmitButton(Const Name,Value : String);
- begin
- EmitButton(Name,'submit',Value)
- end;
- Procedure THTMLWriter.EmitResetButton(Const Name,Value : String);
- begin
- EmitButton(Name,'reset',Value)
- end;
- Procedure THTMLWriter.EmitButton(Const Name,ButtonType,Value : String);
- Var
- A : String;
- begin
- A:='TYPE="'+ButtonType+'"';
- If (Value<>'') then
- A:=A+' VALUE="'+Value+'"';
- If (Name<>'') then
- A:=A+' NAME="'+Name+'"';
- TagStart('INPUT',A)
- end;
- Procedure THTMLWriter.EmitHiddenVar(Const Name,Value: String);
- Var
- A : String;
- begin
- A:='TYPE="hidden" NAME="'+Name+'" VALUE="'+Value+'"';
- TagStart('INPUT',A);
- end;
- end.
|