Menu

[r3204]: / branches / experimental / Src / TrunkSrc / UHTMLBuilder.pas  Maximize  Restore  History

Download this file

274 lines (217 with data), 6.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2007-2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements a class used to create content of an XHTML strict document.
}
unit UHTMLBuilder;
interface
uses
// Delphi
SysUtils,
// Project
UHTMLUtils;
type
/// <summary>
/// Class used to create content of a XHTML strict document.
/// </summary>
THTMLBuilder = class(TObject)
strict private
var
/// <summary>Value of CSS property.</summary>
fCSS: string;
/// <summary>Value of Title property.</summary>
fTitle: string;
/// <summary>Used to create inner HTML of document body.</summary>
fBodyInner: TStringBuilder;
/// <summary>Creates an HTML attributes object containing class attribute
/// that references given CSS class name.</summary>
function MakeClassAttr(const ClassName: string): IHTMLAttributes;
/// <summary>Generates inline &lt;style&gt; tag containing CSS.</summary>
/// <remarks>Returns empty string if there is no CSS.</remarks>
function InlineStyleSheet: string;
/// <summary>Builds document's compound &lt;head&gt; tag and its sub-tags.
/// </summary>
function HeadTag: string;
/// <summary>Builds document's compound &lt;body&gt; tag and its content.
/// </summary>
function BodyTag: string;
/// <summary>Builds document's compound &lt;html&gt; tag and all its sub
/// tags and content.</summary>
function HTMLTag: string;
/// <summary>Getter for Title property.</summary>
/// <remarks>Returns default title if title is empty string.</remarks>
function GetTitle: string;
public
/// <summary>Object constructor. Initialises object with empty body.
/// </summary>
constructor Create;
/// <summary>Object destructor. Tears down object.</summary>
destructor Destroy; override;
/// <summary>Appends an opening &lt;pre&gt; tag with specified class to
/// document body.</summary>
procedure OpenPre(const ClassName: string);
/// <summary>Appends a closing &lt;/pre&gt; tag to document body.</summary>
procedure ClosePre;
/// <summary>Appends an opening &lt;span&gt; tag with specified class to
/// document body.</summary>
procedure OpenSpan(const ClassName: string);
/// <summary>Appends a closing &lt;/span&gt; tag to document body.
/// </summary>
procedure CloseSpan;
/// <summary>Appends given text to document body. Text is converted to
/// valid XHTML character entities if necessary.</summary>
procedure AddText(const Text: string);
/// <summary>Appends a new line to document body.</summary>
procedure NewLine;
/// <summary>Returns document fragment containing inner body XHTML.
/// </summary>
/// <remarks>Fragment does not include &lt;body&gt; tags.</remarks>
function HTMLFragment: string;
/// <summary>Returns complete XHTML document.</summary>
function HTMLDocument: string;
/// <summary>XHTML Document title.</summary>
/// <remarks>Default value used if Title is empty string.</remarks>
property Title: string read GetTitle write fTitle;
/// <summary>CSS used for document's inline cascading style sheet.
/// </summary>
property CSS: string read fCSS write fCSS;
end;
implementation
uses
// Project
UConsts;
const
// XHTML document elements
// XML processor instruction
cXMLProcInstruction = '<?xml version="1.0"?>';
// XML document type
cDocType = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" '
+ '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
// Various tag names
cHTMLTag = 'html';
cHeadTag = 'head';
cTitleTag = 'title';
cStyleTag = 'style';
cBodyTag = 'body';
cPreTag = 'pre';
cSpanTag = 'span';
resourcestring
// Default document title used if none provided
sUntitled = 'Untitled';
{ THTMLBuilder }
procedure THTMLBuilder.AddText(const Text: string);
begin
fBodyInner.Append(THTML.Entities(Text));
end;
function THTMLBuilder.BodyTag: string;
begin
Result := THTML.CompoundTag(cBodyTag, EOL + HTMLFragment + EOL);
end;
procedure THTMLBuilder.ClosePre;
begin
fBodyInner.Append(THTML.ClosingTag(cPreTag));
end;
procedure THTMLBuilder.CloseSpan;
begin
fBodyInner.Append(THTML.ClosingTag(cSpanTag));
end;
constructor THTMLBuilder.Create;
begin
inherited Create;
fBodyInner := TStringBuilder.Create;
end;
destructor THTMLBuilder.Destroy;
begin
fBodyInner.Free;
inherited;
end;
function THTMLBuilder.GetTitle: string;
begin
if fTitle <> '' then
Result := fTitle
else
Result := sUntitled;
end;
function THTMLBuilder.HeadTag: string;
begin
Result := THTML.CompoundTag(
cHeadTag,
EOL
+ THTML.CompoundTag(cTitleTag, THTML.Entities(Title))
+ EOL
+ InlineStyleSheet
);
end;
function THTMLBuilder.HTMLDocument: string;
begin
Result := cXMLProcInstruction
+ EOL
+ cDocType
+ EOL
+ HTMLTag
+ EOL;
end;
function THTMLBuilder.HTMLFragment: string;
begin
Result := fBodyInner.ToString;
end;
function THTMLBuilder.HTMLTag: string;
// ---------------------------------------------------------------------------
/// <summary>Builds object describing attributes of &lt;html&gt; tag.
/// </summary>
function HTMLAttrs: IHTMLAttributes;
begin
Result := THTMLAttributes.Create(
[THTMLAttribute.Create('xmlns', 'http://www.w3.org/1999/xhtml'),
THTMLAttribute.Create('xml:lang', 'en'),
THTMLAttribute.Create('lang', 'en')]
);
end;
// ---------------------------------------------------------------------------
begin
Result := THTML.CompoundTag(
cHTMLTag,
HTMLAttrs,
EOL + HeadTag + EOL + BodyTag + EOL
);
end;
function THTMLBuilder.InlineStyleSheet: string;
var
Attrs: IHTMLAttributes; // style tag's attributes
begin
if fCSS <> '' then
begin
Attrs := THTMLAttributes.Create('type', 'text/css');
Result := EOL
+ THTML.CompoundTag(cStyleTag, Attrs, EOL + fCSS + EOL)
+ EOL;
end
else
Result := '';
end;
function THTMLBuilder.MakeClassAttr(const ClassName: string): IHTMLAttributes;
begin
Result := THTMLAttributes.Create;
if ClassName <> '' then
Result.Add('class', ClassName);
end;
procedure THTMLBuilder.NewLine;
begin
fBodyInner.AppendLine;
end;
procedure THTMLBuilder.OpenPre(const ClassName: string);
begin
fBodyInner.Append(THTML.OpeningTag(cPreTag, MakeClassAttr(ClassName)));
end;
procedure THTMLBuilder.OpenSpan(const ClassName: string);
begin
fBodyInner.Append(THTML.OpeningTag(cSpanTag, MakeClassAttr(ClassName)));
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.