Menu

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

Download this file

301 lines (255 with data), 10.2 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
{
* 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) 2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Provides an object that can be passed to IActiveText.Render to render active
* text as REML code.
}
unit CS.ActiveText.Renderers.REML;
interface
uses
// Delphi
SysUtils,
// Project
CS.ActiveText,
UREMLDataIO;
type
/// <summary>Object that can be used to render active text as REML code.
/// </summary>
/// <remarks>Designed for use with the IActiveText.Render method.</remarks>
TActiveTextREMLRenderer = class(TInterfacedObject, IActiveTextRenderer)
strict private
var
/// <summary>Object used to construct string of REML.</summary>
fBuilder: TStringBuilder;
/// <summary>Any whitespace to be appended to the end of REML blocks.
/// </summary>
fBlockTerminator: string;
/// <summary>Converts the given plain text to REML compatible text.
/// </summary>
/// <remarks>Illegal REML characters are converted to the equivalent
/// character entity.</remarks>
function TextToREMLText(const Text: string): string;
/// <summary>Adds the REML tag to output that corresponds to the given kind
/// of active text element.</summary>
/// <param name="Kind">TActiveTextActionElemKind [in] Kind of active text
/// element for which REML tag is required.</param>
/// <param name="State">TREMLTagState [in] Required state of tag: opening
/// or closing.</param>
/// <param name="Attr">TREMLAttr [in] Attribute name/value pair to be
/// included in REML tag. Ignored if Attr is null or if State = rtsClose.
/// </param>
procedure EmitREMLTagFor(const Kind: TActiveTextActionElemKind;
const State: TREMLTagState; const Attr: TREMLAttr); overload;
/// <summary>Adds the REML tag to output that corresponds to the given kind
/// of active text element.</summary>
/// <param name="Kind">TActiveTextActionElemKind [in] Kind of active text
/// element for which REML tag is required.</param>
/// <param name="State">TREMLTagState [in] Required state of tag: opening
/// or closing.</param>
/// <remarks>This form of the method is for use when the required tag has
/// no attribute.</remarks>
procedure EmitREMLTagFor(const Kind: TActiveTextActionElemKind;
const State: TREMLTagState); overload;
public
/// <summary>Constructs a new renderer that stores REML in given string
/// builder object.</summary>
/// <param name="Builder">TStringBuilder [in] Object that receives
/// rendered REML code.</param>
/// <param name="BlockTerminator">string [in] Any text used to terminate
/// REML blocks. This can be either the empty string or any white space.
/// </param>
constructor Create(const Builder: TStringBuilder;
const BlockTerminator: string);
/// <summary>Renders active text as REML source code.</summary>
/// <param name="ActiveText">IActiveText [in] Active text to be rendered.
/// </param>
/// <param name="BlockTerminator">string [in] Any text used to terminate
/// REML blocks. This can be either the empty string or any white space.
/// </param>
/// <returns>string. REML representation of the active text.</returns>
/// <remarks>This is a helper method encapsulating a common use for this
/// renderer.</remarks>
class function Render(ActiveText: IActiveText;
const BlockTerminator: string): string;
/// <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
UConsts,
UExceptions,
UStrUtils;
{ TActiveTextREMLRenderer }
procedure TActiveTextREMLRenderer.BeginBlock(
const Kind: TActiveTextActionElemKind);
begin
EmitREMLTagFor(Kind, rtsOpen);
end;
procedure TActiveTextREMLRenderer.BeginInlineStyle(
const Kind: TActiveTextActionElemKind);
begin
EmitREMLTagFor(Kind, rtsOpen);
end;
procedure TActiveTextREMLRenderer.BeginLink(const URL: string);
var
ParamName: string;
begin
if not TREMLTags.LookupParamName(rtLink, ParamName) then
raise EBug.Create(
ClassName + '.BeginLink: No parameter name found for REML link tag'
);
EmitREMLTagFor(ekLink, rtsOpen, TREMLAttr.Create(ParamName, URL));
end;
constructor TActiveTextREMLRenderer.Create(const Builder: TStringBuilder;
const BlockTerminator: string);
begin
Assert(Assigned(Builder), ClassName + '.Create: Builder is nil');
Assert(StrIsBlank(BlockTerminator),
ClassName + '.Create: BlockTerminator contains non-whitespace characters');
inherited Create;
fBuilder := Builder;
fBlockTerminator := BlockTerminator;
end;
procedure TActiveTextREMLRenderer.EmitREMLTagFor(
const Kind: TActiveTextActionElemKind; const State: TREMLTagState);
begin
EmitREMLTagFor(Kind, State, TREMLAttr.CreateNull);
end;
procedure TActiveTextREMLRenderer.EmitREMLTagFor(
const Kind: TActiveTextActionElemKind; const State: TREMLTagState;
const Attr: TREMLAttr);
const
TagIDMap: array[TActiveTextActionElemKind] of TREMLTagID = (
rtLink, rtStrong, rtEm, rtVar, rtPara, rtWarning, rtHeading, rtMono
);
TagStartFmt: array[TREMLTagState] of string = ('<%s', '</%s');
var
TagName: string;
begin
if not TREMLTags.LookupTagName(TagIDMap[Kind], TagName) then
raise EBug.Create(ClassName + '.EmitClosingREMLTagFor: Invalid tag ID');
fBuilder.AppendFormat(TagStartFmt[State], [TagName]);
if (State = rtsOpen) and not Attr.IsNull then
begin
fBuilder.Append(' ');
fBuilder.Append(TextToREMLText(Attr.Key));
fBuilder.Append('=');
fBuilder.Append(DOUBLEQUOTE);
fBuilder.Append(TextToREMLText(Attr.Value));
fBuilder.Append(DOUBLEQUOTE);
end;
fBuilder.Append('>');
end;
procedure TActiveTextREMLRenderer.EndBlock(
const Kind: TActiveTextActionElemKind);
begin
EmitREMLTagFor(Kind, rtsClose);
if fBlockTerminator <> EmptyStr then
fBuilder.Append(fBlockTerminator);
end;
procedure TActiveTextREMLRenderer.EndInlineStyle(
const Kind: TActiveTextActionElemKind);
begin
EmitREMLTagFor(Kind, rtsClose);
end;
procedure TActiveTextREMLRenderer.EndLink(const URL: string);
begin
EmitREMLTagFor(ekLink, rtsClose);
end;
procedure TActiveTextREMLRenderer.Finalise;
begin
// Do nothing
end;
procedure TActiveTextREMLRenderer.Initialise;
begin
// Do nothing
end;
procedure TActiveTextREMLRenderer.OutputText(const AText: string);
begin
fBuilder.Append(TextToREMLText(AText));
end;
class function TActiveTextREMLRenderer.Render(ActiveText: IActiveText;
const BlockTerminator: string): string;
var
Builder: TStringBuilder;
Renderer: TActiveTextREMLRenderer;
begin
Builder := TStringBuilder.Create;
try
Renderer := Create(Builder, BlockTerminator);
ActiveText.Render(Renderer);
Result := Builder.ToString;
finally
Builder.Free;
end;
end;
function TActiveTextREMLRenderer.TextToREMLText(const Text: string): string;
var
Ch: Char; // each character in plain text
Entity: string; // stores each required entity
begin
Result := '';
for Ch in Text do
begin
Entity := TREMLEntities.MapToEntity(Ch);
if Entity = '' then
Result := Result + Ch
else
Result := Result + '&' + Entity + ';';
end;
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.