Skip to content

Commit

Permalink
Merge pull request #23 from sempare/ignorenlreview
Browse files Browse the repository at this point in the history
#22 document ignorenl
  • Loading branch information
Sempare Limited authored Nov 5, 2020
2 parents b3528bd + b1290d6 commit 7fb0a32
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 7 deletions.
25 changes: 25 additions & 0 deletions docs/statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Copyright (c) 2020 [Sempare Limited](http://www.sempare.ltd)
7. [with](#with)
8. [template](#template)
9. [require](#require)
10. [ignorenl](#ignorenl)

### print

Expand Down Expand Up @@ -253,3 +254,27 @@ An exeption is thrown when the

_require_ can take multiple parameters - in which case the input must match one of the types listed.

### ignorenl

The purpose of an _ignorenl_ block is to allow for template designers to space out content for easy maintenance, but to allow for the output to be more compact when required.

```
<table>
<% ignorenl %>
<tr>
<td>Col1</td>
<td>Col2</td>
</tr>
<% end %>
</table>
```

This would yield something like

```
<table>
<tr><td>Col1</td><td>Col2</td></tr>
</table>
```
7 changes: 2 additions & 5 deletions src/Sempare.Template.Evaluate.pas
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ implementation
{$WARN WIDECHAR_REDUCED OFF}

const
WHITESPACE: set of char = [#9, ' '];
NEWLINE: set of char = [#10, #13];
WHITESPACE: set of char = [#9, ' ', #13];

{$WARN WIDECHAR_REDUCED ON}

Expand All @@ -152,9 +151,7 @@ function IsWhitespace(const AChar: char): boolean; inline;

function IsNewline(const AChar: char): boolean; inline;
begin
{$WARN WIDECHAR_REDUCED OFF}
exit(AChar in NEWLINE);
{$WARN WIDECHAR_REDUCED ON}
exit(AChar = #10);
end;

{ TEvaluationTemplateVisitor }
Expand Down
35 changes: 34 additions & 1 deletion src/Sempare.Template.NewLineOption.Test.pas
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,47 @@ procedure TTestNewLineOption.TestRecurringSpaces;
var
s: TStringStream;
w: TNewLineStreamWriter;
s2:string;
begin
s := TStringStream.create;
w := TNewLineStreamWriter.create(s, TEncoding.ASCII, #10, []);
try
w.Write(' '#10#10' '#10#10);
finally
w.Free;
s2:=s.datastring;
Assert.AreEqual(' '#10#10' '#10#10, s2);
s.Free;
end;
s := TStringStream.create;
w := TNewLineStreamWriter.create(s, TEncoding.ASCII, #10, [eoTrimLines]);
try
w.Write(' '#10#10' '#10#10);
finally
w.Free;
s2:=s.datastring;
Assert.AreEqual(''#10#10''#10#10, s2);
s.Free;
end;

s := TStringStream.create;
w := TNewLineStreamWriter.create(s, TEncoding.ASCII, #10, []);
try
w.Write(' hello '#10#10' world ');
finally
w.Free;
s2:=s.datastring;
Assert.AreEqual(' hello '#10#10' world ', s2);
s.Free;
end;
s := TStringStream.create;
w := TNewLineStreamWriter.create(s, TEncoding.ASCII, #10, [eoTrimLines]);
try
w.Write(' hello '#10#10' world ');
finally
w.Free;
Assert.AreEqual('hello'#10#10'world', s.datastring);
s2:=s.datastring;
Assert.AreEqual('hello'#10#10'world', s2);
s.Free;
end;
end;
Expand Down
67 changes: 66 additions & 1 deletion src/Sempare.Template.Test.pas
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,21 @@ TTestTemplate = class
procedure TestRequire;
[Test]
procedure testPrint;

[Test]
procedure TestIgnoreNL;

[Test]
procedure TestIgnoreNL2;

end;

implementation

uses
System.SysUtils,
System.Generics.Collections,
Sempare.Template.Util,
Sempare.Template.Context,
Sempare.Template;

Expand Down Expand Up @@ -123,8 +132,64 @@ TRec = record

begin
end;

{$ENDIF}

procedure TTestTemplate.TestIgnoreNL;
var
LStringBuilder: TStringBuilder;
LString, LResult: string;
LPreserveGlobalNL: IPreserveValue<string>;
begin
LPreserveGlobalNL := Preserve.Value<string>(GNewLine, #10);
LStringBuilder := TStringBuilder.create;
try
LStringBuilder.append('hello ').append(#10);
LStringBuilder.append('world').append(#10);
LString := LStringBuilder.ToString;
finally
LStringBuilder.free;
end;

LResult := Template.Eval(LString);

Assert.AreEqual(LString, LResult);

LString := '<% ignorenl %>' + LString + '<%end%>';
LResult := Template.Eval(LString);
Assert.AreEqual('hello world', LResult);

end;

procedure TTestTemplate.TestIgnoreNL2;
var
LStringBuilder: TStringBuilder;
LString, LResult: string;
LPreserveGlobalNL: IPreserveValue<string>;
begin
LPreserveGlobalNL := Preserve.Value<string>(GNewLine, #10);
LStringBuilder := TStringBuilder.create;
try
LStringBuilder.append('<table>').append(#10);
LStringBuilder.append('<tr>').append(#10);
LStringBuilder.append('<td>col1</td>').append(#10);
LStringBuilder.append('<td>col2</td>').append(#10);
LStringBuilder.append('</tr>').append(#10);
LStringBuilder.append('</table>').append(#10);
LString := LStringBuilder.ToString;
finally
LStringBuilder.free;
end;

LResult := Template.Eval(LString);

Assert.AreEqual(LString, LResult);

LString := '<% ignorenl %>' + LString + '<%end%>';
LResult := Template.Eval(LString);
Assert.AreEqual('<table><tr><td>col1</td><td>col2</td></tr></table>', LResult);
end;

procedure TTestTemplate.TestNonMutation;
type
TRec = record
Expand Down Expand Up @@ -244,7 +309,7 @@ procedure TTestTemplate.TestUnderscoreIn;
L.AddRange(['1', '2', '3']);
Assert.AreEqual('123', Template.Eval('<% for v in _ %><% v %><% end %>', L));
finally
L.Free;
L.free;
end;
end;

Expand Down

0 comments on commit 7fb0a32

Please sign in to comment.