Menu

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

Download this file

236 lines (204 with data), 7.1 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
{
* 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 plain text.
}
unit CS.ActiveText.Renderers.PlainText;
interface
uses
// Delphi
SysUtils,
// Project
CS.ActiveText,
UIStringList;
type
TActiveTextPlainTextRenderer = class(TInterfacedObject, IActiveTextRenderer)
public
type
TOption = (
ptrIgnoreEmptyBlocks, ptrTrimLines, ptrIgnoreInterBlockText,
ptrIncludeURLs
);
TOptions = set of TOption;
strict private
const
URLOpenParen = '(';
URLCloseParan = ')';
var
fBuilder: TStringBuilder;
fBlockSeparator: string;
fOptions: TOptions;
fIsFirstBlock: Boolean;
fInBlock: Boolean;
fParaBuilder: TStringBuilder;
fParas: IStringList;
procedure OutputParagraph;
public
constructor Create(const Builder: TStringBuilder;
const BlockSeparator: string; const Options: TOptions);
destructor Destroy; override;
class function Render(ActiveText: IActiveText; const BlockSeparator: string;
const Options: TOptions): 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
UStrUtils;
{ TActiveTextPlainTextRenderer }
procedure TActiveTextPlainTextRenderer.BeginBlock(
const Kind: TActiveTextActionElemKind);
begin
if fParaBuilder.Length > 0 then
begin
if not fInBlock and not (ptrIgnoreInterBlockText in fOptions) then
OutputParagraph // this clears fParaBuilder
else
fParaBuilder.Clear;
end;
fInBlock := True;
end;
procedure TActiveTextPlainTextRenderer.BeginInlineStyle(
const Kind: TActiveTextActionElemKind);
begin
// do nothing
end;
procedure TActiveTextPlainTextRenderer.BeginLink(const URL: string);
begin
// do nothing
end;
constructor TActiveTextPlainTextRenderer.Create(const Builder: TStringBuilder;
const BlockSeparator: string; const Options: TOptions);
begin
Assert(Assigned(Builder), ClassName + '.Create: Builder is nil');
Assert(StrIsBlank(BlockSeparator),
ClassName + '.Create: BlockSeparator contains non-whitespace characters');
inherited Create;
fBuilder := Builder;
fBlockSeparator := BlockSeparator;
fOptions := Options;
fParas := TIStringList.Create;
fParaBuilder := TStringBuilder.Create;
end;
destructor TActiveTextPlainTextRenderer.Destroy;
begin
fParaBuilder.Free;
inherited;
end;
procedure TActiveTextPlainTextRenderer.EndBlock(
const Kind: TActiveTextActionElemKind);
begin
OutputParagraph;
fInBlock := False;
end;
procedure TActiveTextPlainTextRenderer.EndInlineStyle(
const Kind: TActiveTextActionElemKind);
begin
// do nothing
end;
procedure TActiveTextPlainTextRenderer.EndLink(const URL: string);
begin
if ptrIncludeURLs in fOptions then
fParaBuilder.Append(' ' + URLOpenParen + URL + URLCloseParan);
end;
procedure TActiveTextPlainTextRenderer.Finalise;
begin
if fInBlock
or (
(fParaBuilder.Length > 0) and not (ptrIgnoreInterBlockText in fOptions)
) then
OutputParagraph;
fBuilder.Append(
fParas.GetText(fBlockSeparator, not (ptrIgnoreEmptyBlocks in fOptions))
)
end;
procedure TActiveTextPlainTextRenderer.Initialise;
begin
fIsFirstBlock := True;
fInBlock := False;
fParas.Clear;
fParaBuilder.Clear;
end;
procedure TActiveTextPlainTextRenderer.OutputParagraph;
begin
if ptrTrimLines in fOptions then
fParas.Add(StrTrim(fParaBuilder.ToString))
else
fParas.Add(fParaBuilder.ToString);
fParaBuilder.Clear;
end;
procedure TActiveTextPlainTextRenderer.OutputText(const AText: string);
begin
fParaBuilder.Append(AText);
end;
class function TActiveTextPlainTextRenderer.Render(ActiveText: IActiveText;
const BlockSeparator: string; const Options: TOptions): string;
var
Builder: TStringBuilder;
Renderer: TActiveTextPlainTextRenderer;
begin
Builder := TStringBuilder.Create;
try
Renderer := Create(Builder, BlockSeparator, Options);
ActiveText.Render(Renderer);
Result := Builder.ToString;
finally
Builder.Free;
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.