Menu

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

Download this file

137 lines (123 with data), 4.5 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
{
* 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 a class that create active text by parsing plain text mark-up.
}
unit CS.ActiveText.Parsers.PlainText;
interface
uses
// Project
CS.ActiveText;
type
/// <summary>Class that creates active text by parsing plain text.</summary>
/// <remarks>Text can optionally be parsed into active text paragraph
/// blocks.</remarks>
TActiveTextPlainTextParser = class(TInterfacedObject, IActiveTextParser)
public
type
/// <summary>Enumeration of options used to govern how the text parser
/// operates.</summary>
/// <remarks>
/// <para>- ptpSplitIntoParas - The text is split into zero or more
/// active text paragraph blocks.</para>
/// <para>- ptpIgnoreEmptyParas - Any empty paragraphs in the text are
/// ignored. Ignored if ptpSplitIntoParas is not also specified.</para>
/// <para>- ptpTrim - Trim the text (and any paragraphs) of leading and
/// trailing white space.</para>
/// <para>- ptpCompressWhiteSpace - Compresses all white space into a
/// single space. Where ptpSplitIntoParas is supplied this is done after
/// the text is split into paragraphs.</para>
/// </remarks>
TOption = (
ptpSplitIntoParas, ptpIgnoreEmptyParas, ptpTrim, ptpCompressWhiteSpace
);
/// <summary>Set of options used to govern how the text parser operates.
/// </summary>
TOptions = set of TOption;
strict private
var
/// <summary>String used to separate paragraphs.</summary>
/// <remarks>Used only when ptpSplitIntoParas is in Options.</remarks>
fParaSeparator: string;
/// <summary>Set of options that govern how the text parser operates.
/// </summary>
fOptions: TOptions;
public
/// <summary>Constructs new parser instance, customised per the given
/// parameters.</summary>
/// <param name="ParaSeparator">string [in] String used to delimit
/// paragraphs within the text being parsed. Ignored if ptpSplitIntoParas
/// option is not set, otherwise the value must not be the empty string.
/// </param>
/// <param name="Options">TOptions [in] Set of options governing how the
/// text is parsed.</param>
constructor Create(const ParaSeparator: string; const Options: TOptions);
/// <summary>Parses plain text into active text.</summary>
/// <param name="Text">string [in] Plain text to be parsed.</param>
/// <returns>IActiveText. Active text object created from Text.</returns>
/// <remarks>Method of IActiveTextParser.</remarks>
function Parse(const Text: string): IActiveText;
end;
implementation
uses
SysUtils,
UIStringList,
UStrUtils;
{ TActiveTextPlainTextParser }
constructor TActiveTextPlainTextParser.Create(const ParaSeparator: string;
const Options: TOptions);
begin
inherited Create;
fParaSeparator := ParaSeparator;
fOptions := Options;
end;
function TActiveTextPlainTextParser.Parse(const Text: string): IActiveText;
var
Paras: IStringList;
ProcessedText: string;
Para: string;
ProcessedPara: string;
begin
Result := TActiveTextFactory.CreateActiveText;
if ptpTrim in fOptions then
ProcessedText := StrTrim(Text)
else
ProcessedText := Text;
if ProcessedText = EmptyStr then
Exit;
if ptpSplitIntoParas in fOptions then
begin
Assert(fParaSeparator <> EmptyStr,
ClassName + '.Parse: paragraph separator is empty');
Paras := TIStringList.Create(
ProcessedText,
fParaSeparator,
not (ptpIgnoreEmptyParas in fOptions),
ptpTrim in fOptions
);
for Para in Paras do
begin
Result.AddElem(TActiveTextFactory.CreateActionElem(ekPara, fsOpen));
if ptpCompressWhiteSpace in fOptions then
ProcessedPara := StrCompressWhiteSpace(Para)
else
ProcessedPara := Para;
Result.AddElem(TActiveTextFactory.CreateTextElem(ProcessedPara));
Result.AddElem(TActiveTextFactory.CreateActionElem(ekPara, fsClose));
end;
end
else
begin
if ptpCompressWhiteSpace in fOptions then
ProcessedText := StrCompressWhiteSpace(ProcessedText);
Result.AddElem(TActiveTextFactory.CreateTextElem(ProcessedText));
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.