Menu

[r4761]: / branches / parsnip / Src / Main / CS.ActiveText.Renderers.HTML.pas  Maximize  Restore  History

Download this file

329 lines (281 with data), 9.7 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
{
* 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) 2009-2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Provides a class that renders active text as HTML.
}
unit CS.ActiveText.Renderers.HTML;
interface
uses
// Delphi
SysUtils,
Graphics,
Generics.Collections,
// Project
CS.ActiveText,
UBaseObjects,
UCSSBuilder,
UHTMLUtils;
type
TActiveTextHTMLRenderer = class(TInterfacedObject, IActiveTextRenderer)
public
type
TCSSStyles = class(TObject)
strict private
var
fWrapperClass: string;
fElemClassMap: array[TActiveTextActionElemKind] of string;
procedure SetElemClass(ElemKind: TActiveTextActionElemKind;
const Value: string); inline;
function GetElemClass(ElemKind: TActiveTextActionElemKind): string;
inline;
procedure Initialise;
public
constructor Create;
procedure Assign(const Other: TCSSStyles);
property WrapperClass: string read fWrapperClass write fWrapperClass;
property ElemClasses[Kind: TActiveTextActionElemKind]: string
read GetElemClass write SetElemClass;
end;
strict private
var
fCSSStyles: TCSSStyles;
fBuilder: TStringBuilder;
fInBlock: Boolean;
procedure SetCSSStyles(const AStyles: TCSSStyles);
function GetTagName(const Kind: TActiveTextActionElemKind): string;
function MakeOpeningTag(const Kind: TActiveTextActionElemKind;
Attrs: IHTMLAttributes = nil): string;
function MakeClosingTag(const Kind: TActiveTextActionElemKind): string;
public
constructor Create(const Builder: TStringBuilder);
destructor Destroy; override;
class function Render(ActiveText: IActiveText;
const CSSStyles: TCSSStyles = nil): string;
property CSSStyles: TCSSStyles read fCSSStyles write SetCSSStyles;
/// <summary>Called before active text is processed.</summary>
/// <remarks>Method of IActiveTextRenderer.</remarks>
procedure Initialise;
/// <summary>Called after last active text element has been processed.
/// </summary>
/// <remarks>Method of IActiveTextRenderer.</remarks>
procedure Finalise;
/// <summary>Called when plain text should be output.</summary>
/// <param name="AText">string. Text to be output.</param>
/// <remarks>Method of IActiveTextRenderer.</remarks>
procedure OutputText(const AText: string);
/// <summary>Called at the start of a new block of active text.</summary>
/// <param name="Kind">TActiveTextActionElemKind [in] Kind of block being
/// opened. This will be an active text element with DisplayStyle =
/// dsBlock.</param>
/// <remarks>Method of IActiveTextRenderer.</remarks>
procedure BeginBlock(const Kind: TActiveTextActionElemKind);
/// <summary>Called when the current active text block ends.</summary>
/// <param name="Kind">TActiveTextActionElemKind [in] Kind of block being
/// opened. This will be an active text element with DisplayStyle =
/// dsBlock.</param>
/// <remarks>Method of IActiveTextRenderer.</remarks>
procedure EndBlock(const Kind: TActiveTextActionElemKind);
/// <summary>Called at the start of a new active text styling element.
/// </summary>
/// <param name="Kind">TActiveTextActionElemKind [in] Kind of styling
/// element. The kind indicates the type of styling to be applied. This
/// will be an active text element with DisplayStyle = dsInline that is
/// not ekLink.</param>
/// <remarks>Method of IActiveTextRenderer.</remarks>
procedure BeginInlineStyle(const Kind: TActiveTextActionElemKind);
/// <summary>Called the current active text styling element ends.</summary>
/// <param name="Kind">TActiveTextActionElemKind [in] Kind of styling
/// element. The kind indicates the type of styling to be applied. This
/// will be an active text element with DisplayStyle = dsInline that is
/// not ekLink.</param>
/// <remarks>Method of IActiveTextRenderer.</remarks>
procedure EndInlineStyle(const Kind: TActiveTextActionElemKind);
/// <summary>Called at the start of a new active text link element.
/// </summary>
/// <param name="URL">string. URL to accessed from the link.</param>
/// <remarks>Method of IActiveTextRenderer.</remarks>
procedure BeginLink(const URL: string);
/// <summary>Called when the current active text link element ends.
/// </summary>
/// <param name="URL">string. URL to accessed from the link.</param>
/// <remarks>Method of IActiveTextRenderer.</remarks>
procedure EndLink(const URL: string);
end;
implementation
uses
// Project
UColours,
UCSSUtils,
UFontHelper,
UIStringList,
UStrUtils;
{ TActiveTextHTMLRenderer }
procedure TActiveTextHTMLRenderer.BeginBlock(
const Kind: TActiveTextActionElemKind);
begin
fBuilder.Append(MakeOpeningTag(Kind));
fInBlock := True;
end;
procedure TActiveTextHTMLRenderer.BeginInlineStyle(
const Kind: TActiveTextActionElemKind);
begin
if not fInBlock then
Exit;
fBuilder.Append(MakeOpeningTag(Kind));
end;
procedure TActiveTextHTMLRenderer.BeginLink(const URL: string);
begin
if not fInBlock then
Exit;
fBuilder.Append(MakeOpeningTag(ekLink, THTMLAttributes.Create('href', URL)));
end;
constructor TActiveTextHTMLRenderer.Create(const Builder: TStringBuilder);
begin
Assert(Assigned(Builder), ClassName + '.Create: Builder is nil');
inherited Create;
fCSSStyles := TCSSStyles.Create;
fBuilder := Builder;
end;
destructor TActiveTextHTMLRenderer.Destroy;
begin
fCSSStyles.Free;
inherited;
end;
procedure TActiveTextHTMLRenderer.EndBlock(
const Kind: TActiveTextActionElemKind);
begin
fInBlock := False;
fBuilder.Append(MakeClosingTag(Kind));
end;
procedure TActiveTextHTMLRenderer.EndInlineStyle(
const Kind: TActiveTextActionElemKind);
begin
if not fInBlock then
Exit;
fBuilder.Append(MakeClosingTag(Kind));
end;
procedure TActiveTextHTMLRenderer.EndLink(const URL: string);
begin
if not fInBlock then
Exit;
fBuilder.Append(MakeClosingTag(ekLink));
end;
procedure TActiveTextHTMLRenderer.Finalise;
begin
fBuilder.Append(THTML.ClosingTag('div'));
end;
function TActiveTextHTMLRenderer.GetTagName(
const Kind: TActiveTextActionElemKind): string;
const
Tags: array[TActiveTextActionElemKind] of string = (
'a', 'strong', 'em', 'var', 'p', 'span', 'h2', 'code'
);
begin
Result := Tags[Kind];
end;
procedure TActiveTextHTMLRenderer.Initialise;
var
WrapperClassAttr: IHTMLAttributes;
begin
if fCSSStyles.WrapperClass <> '' then
WrapperClassAttr := THTMLAttributes.Create('class', fCSSStyles.WrapperClass)
else
WrapperClassAttr := nil;
fBuilder.Append(THTML.OpeningTag('div', WrapperClassAttr));
end;
function TActiveTextHTMLRenderer.MakeClosingTag(
const Kind: TActiveTextActionElemKind): string;
begin
Result := THTML.ClosingTag(GetTagName(Kind));
end;
function TActiveTextHTMLRenderer.MakeOpeningTag(
const Kind: TActiveTextActionElemKind; Attrs: IHTMLAttributes): string;
var
AllAttrs: IHTMLAttributes;
begin
AllAttrs := THTMLAttributes.Create;
if fCSSStyles.ElemClasses[Kind] <> EmptyStr then
AllAttrs.Add('class', fCSSStyles.ElemClasses[Kind]);
AllAttrs.Append(Attrs);
Result := THTML.OpeningTag(GetTagName(Kind), AllAttrs);
end;
procedure TActiveTextHTMLRenderer.OutputText(const AText: string);
begin
if not fInBlock then
Exit;
fBuilder.Append(THTML.Entities(AText));
end;
class function TActiveTextHTMLRenderer.Render(ActiveText: IActiveText;
const CSSStyles: TCSSStyles): string;
var
SB: TStringBuilder;
Renderer: TActiveTextHTMLRenderer;
begin
SB := TStringBuilder.Create;
try
// Renderer will be freed automatically since it is reference counted and
// passed to ActiveText.Render via its IActiveTextRenderer interface.
Renderer := TActiveTextHTMLRenderer.Create(SB);
Renderer.CSSStyles := CSSStyles;
ActiveText.Render(Renderer);
Result := SB.ToString;
finally
SB.Free;
end;
end;
procedure TActiveTextHTMLRenderer.SetCSSStyles(const AStyles: TCSSStyles);
begin
fCSSStyles.Assign(AStyles)
end;
{ TActiveTextHTMLRenderer.TCSSStyles }
procedure TActiveTextHTMLRenderer.TCSSStyles.Assign(const Other: TCSSStyles);
var
Kind: TActiveTextActionElemKind;
begin
if Assigned(Other) then
begin
fWrapperClass := Other.fWrapperClass;
for Kind := Low(TActiveTextActionElemKind)
to High(TActiveTextActionElemKind) do
fElemClassMap[Kind] := Other.fElemClassMap[Kind];
end
else
Initialise;
end;
constructor TActiveTextHTMLRenderer.TCSSStyles.Create;
begin
inherited Create;
Initialise;
end;
function TActiveTextHTMLRenderer.TCSSStyles.GetElemClass(
ElemKind: TActiveTextActionElemKind): string;
begin
Result := fElemClassMap[ElemKind];
end;
procedure TActiveTextHTMLRenderer.TCSSStyles.Initialise;
const
DefaultWrapperClassName = 'active-text';
DefaultClasses: array[TActiveTextActionElemKind] of string = (
'external-link', '', '', '', '', 'warning', '', ''
);
var
ElemKind: TActiveTextActionElemKind;
begin
fWrapperClass := DefaultWrapperClassName;
for ElemKind := Low(TActiveTextActionElemKind)
to High(TActiveTextActionElemKind) do
SetElemClass(ElemKind, DefaultClasses[ElemKind]);
end;
procedure TActiveTextHTMLRenderer.TCSSStyles.SetElemClass(
ElemKind: TActiveTextActionElemKind; const Value: string);
begin
fElemClassMap[ElemKind] := Value;
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.