Skip to content

Commit a7959cf

Browse files
committed
Add option to TREMLWriter to render one-line REML
Added optional "Formatted" paramter to TREMLWriter.Render method to given option to render REML all on one line with no indentation suitable for inclusion in .ini files. Default behaviour remains to render multi- line, indented REML. Some private methods received similar new parameters and changes.
1 parent 3fab0d3 commit a7959cf

File tree

1 file changed

+62
-49
lines changed

1 file changed

+62
-49
lines changed

Diff for: Src/UREMLDataIO.pas

+62-49
Original file line numberDiff line numberDiff line change
@@ -132,28 +132,45 @@ TREMLWriter = class(TNoPublicConstructObject)
132132
@param Text [in] Plain text to be converted.
133133
@return Converted text.
134134
}
135-
function RenderTag(const TagElem: IActiveTextActionElem): string;
136-
{Renders an active text action element as a REML tag.
137-
@param TagElem [in] Active text action element to be rendered.
138-
@return Required REML tag.
139-
}
140-
function RenderText(const TextElem: IActiveTextTextElem): string;
141-
{Renders an active text text element. Illegal characters are converted to
142-
REML character entities.
143-
@param TextElem [in] Active text text element.
144-
@return REML-safe text containing necessary character entities.
145-
}
135+
/// <summary>Renders an active text action element as a REML tag.</summary>
136+
/// <param name="TagElem"><c>IActiveTextActionElem</c> [in] Active text
137+
/// action element to be rendered.</param>
138+
/// <param name="Formatted"><c>Boolean</c> [in] Optional flag that
139+
/// determines if tag is to be rendered formatted as multiple, indented
140+
/// lines of REML code (True) or with no formatting (False).</param>
141+
/// <returns><c>string</c>. Required REML tag.</returns>
142+
function RenderTag(const TagElem: IActiveTextActionElem;
143+
const Formatted: Boolean): string;
144+
145+
/// <summary>Renders an active text text element. Illegal characters are
146+
/// converted to REML character entities.</summary>
147+
/// <param name="TextElem"><c>IActiveTextTextElem</c> [in] Active text text
148+
/// element to be rendered.</param>
149+
/// <param name="Indented"><c>Boolean</c> [in] Optional flag that
150+
/// determines if text is to be rendered indented (True) or not (False).
151+
/// </param>
152+
/// <returns><c>string</c>. REML-safe text containing necessary character
153+
/// entities.</returns>
154+
function RenderText(const TextElem: IActiveTextTextElem;
155+
const Indented: Boolean): string;
156+
146157
strict protected
147158
constructor InternalCreate;
148-
{Internal class constructor. Sets up object to render active text document
159+
{Internal constructor. Sets up object to render active text document
149160
as REML.
150161
}
151162
public
152-
class function Render(const ActiveText: IActiveText): string;
153-
{Renders REML representation of an active text object.
154-
@param ActiveText [in] Active text to be rendered.
155-
@return String containing REML markup.
156-
}
163+
/// <summary>Renders REML representation of an active text object.
164+
/// </summary>
165+
/// <param name="ActiveText"><c>IActiveText</c> [in] Active text to be
166+
/// rendered.</param>
167+
/// <param name="Formatted"><c>Boolean</c> [in] Optional flag that
168+
/// determines if REML is to be rendered formatted as multiple lines of
169+
/// indented text (True: the default) or as a single line of text with no
170+
/// formatting (False).</param>
171+
/// <returns><c>string</c> containing REML markup.</returns>
172+
class function Render(const ActiveText: IActiveText;
173+
const Formatted: Boolean = True): string;
157174
end;
158175

159176

@@ -719,11 +736,8 @@ constructor TREMLWriter.InternalCreate;
719736
inherited InternalCreate;
720737
end;
721738

722-
class function TREMLWriter.Render(const ActiveText: IActiveText): string;
723-
{Renders REML representation of an active text object.
724-
@param ActiveText [in] Active text to be rendered.
725-
@return String containing REML markup.
726-
}
739+
class function TREMLWriter.Render(const ActiveText: IActiveText;
740+
const Formatted: Boolean): string;
727741
var
728742
Elem: IActiveTextElem; // each element in active text object
729743
TextElem: IActiveTextTextElem; // an active text text element
@@ -744,30 +758,31 @@ class function TREMLWriter.Render(const ActiveText: IActiveText): string;
744758
for Elem in ActiveText do
745759
begin
746760
if Supports(Elem, IActiveTextTextElem, TextElem) then
747-
Text := Text + RW.RenderText(TextElem)
761+
Text := Text + RW.RenderText(TextElem, Formatted)
748762
else if Supports(Elem, IActiveTextActionElem, TagElem) then
749-
Text := Text + RW.RenderTag(TagElem);
763+
Text := Text + RW.RenderTag(TagElem, Formatted);
750764
end;
751-
SrcLines := TIStringList.Create(Text, EOL, False);
752-
DestLines := TIStringList.Create;
753-
for SrcLine in SrcLines do
765+
if Formatted then
754766
begin
755-
DestLine := StrTrimRight(SrcLine);
756-
if not StrIsEmpty(DestLine) then
757-
DestLines.Add(DestLine);
758-
end;
759-
Result := DestLines.GetText(EOL, False);
767+
SrcLines := TIStringList.Create(Text, EOL, False);
768+
DestLines := TIStringList.Create;
769+
for SrcLine in SrcLines do
770+
begin
771+
DestLine := StrTrimRight(SrcLine);
772+
if not StrIsEmpty(DestLine) then
773+
DestLines.Add(DestLine);
774+
end;
775+
Result := DestLines.GetText(EOL, False);
776+
end
777+
else
778+
Result := StrTrim(Text);
760779
finally
761780
RW.Free;
762781
end;
763782
end;
764783

765-
function TREMLWriter.RenderTag(
766-
const TagElem: IActiveTextActionElem): string;
767-
{Renders an active text action element as a REML tag.
768-
@param TagElem [in] Active text action element to be rendered.
769-
@return Required REML tag.
770-
}
784+
function TREMLWriter.RenderTag(const TagElem: IActiveTextActionElem;
785+
const Formatted: Boolean): string;
771786
var
772787
TagName: string; // name of tag
773788
ParamName: string; // name of any parameter
@@ -785,7 +800,8 @@ function TREMLWriter.RenderTag(
785800
if TActiveTextElemCaps.DisplayStyleOf(TagElem.Kind) = dsBlock then
786801
begin
787802
Dec(fLevel);
788-
Result := EOL + StrOfSpaces(IndentMult * fLevel) + Result + EOL;
803+
if Formatted then
804+
Result := EOL + StrOfSpaces(IndentMult * fLevel) + Result + EOL;
789805
fIsStartOfTextLine := True;
790806
end;
791807
end
@@ -815,15 +831,17 @@ function TREMLWriter.RenderTag(
815831
);
816832
if TActiveTextElemCaps.DisplayStyleOf(TagElem.Kind) = dsBlock then
817833
begin
818-
Result := EOL + StrOfSpaces(IndentMult * fLevel) + Result + EOL;
834+
if Formatted then
835+
Result := EOL + StrOfSpaces(IndentMult * fLevel) + Result + EOL;
819836
Inc(fLevel);
820837
fIsStartOfTextLine := True;
821838
end
822839
else if TActiveTextElemCaps.DisplayStyleOf(TagElem.Kind) = dsInline then
823840
begin
824841
if fIsStartOfTextLine then
825842
begin
826-
Result := StrOfSpaces(IndentMult * fLevel) + Result;
843+
if Formatted then
844+
Result := StrOfSpaces(IndentMult * fLevel) + Result;
827845
fIsStartOfTextLine := False;
828846
end;
829847
end;
@@ -839,15 +857,10 @@ function TREMLWriter.RenderTag(
839857
end;
840858
end;
841859

842-
function TREMLWriter.RenderText(
843-
const TextElem: IActiveTextTextElem): string;
844-
{Renders an active text text element. Illegal characters are converted to
845-
REML character entities.
846-
@param TextElem [in] Active text text element.
847-
@return REML-safe text containing necessary character entities.
848-
}
860+
function TREMLWriter.RenderText(const TextElem: IActiveTextTextElem;
861+
const Indented: Boolean): string;
849862
begin
850-
if fIsStartOfTextLine then
863+
if fIsStartOfTextLine and Indented then
851864
begin
852865
Result := StrOfSpaces(IndentMult * fLevel);
853866
fIsStartOfTextLine := False;

0 commit comments

Comments
 (0)