From b1290d650d781ebe9787c9ef2a0624196f2909a6 Mon Sep 17 00:00:00 2001 From: Conrad Vermeulen Date: Thu, 5 Nov 2020 11:57:22 +0000 Subject: [PATCH] #22 document ignorenl --- docs/statements.md | 25 ++++++++ src/Sempare.Template.Evaluate.pas | 7 +-- src/Sempare.Template.NewLineOption.Test.pas | 35 ++++++++++- src/Sempare.Template.Test.pas | 67 ++++++++++++++++++++- 4 files changed, 127 insertions(+), 7 deletions(-) diff --git a/docs/statements.md b/docs/statements.md index 9e9e199..5d25d79 100644 --- a/docs/statements.md +++ b/docs/statements.md @@ -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 @@ -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. + +``` + + +<% ignorenl %> + + + + +<% end %> +
Col1Col2
+ +``` + +This would yield something like + +``` + + +
Col1Col2
+``` \ No newline at end of file diff --git a/src/Sempare.Template.Evaluate.pas b/src/Sempare.Template.Evaluate.pas index 4bf9bb2..7a35a79 100644 --- a/src/Sempare.Template.Evaluate.pas +++ b/src/Sempare.Template.Evaluate.pas @@ -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} @@ -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 } diff --git a/src/Sempare.Template.NewLineOption.Test.pas b/src/Sempare.Template.NewLineOption.Test.pas index 4b03603..12d284f 100644 --- a/src/Sempare.Template.NewLineOption.Test.pas +++ b/src/Sempare.Template.NewLineOption.Test.pas @@ -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; diff --git a/src/Sempare.Template.Test.pas b/src/Sempare.Template.Test.pas index cdd8fd2..56a980c 100644 --- a/src/Sempare.Template.Test.pas +++ b/src/Sempare.Template.Test.pas @@ -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; @@ -123,8 +132,64 @@ TRec = record begin end; + {$ENDIF} +procedure TTestTemplate.TestIgnoreNL; +var + LStringBuilder: TStringBuilder; + LString, LResult: string; + LPreserveGlobalNL: IPreserveValue; +begin + LPreserveGlobalNL := Preserve.Value(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; +begin + LPreserveGlobalNL := Preserve.Value(GNewLine, #10); + LStringBuilder := TStringBuilder.create; + try + LStringBuilder.append('').append(#10); + LStringBuilder.append('').append(#10); + LStringBuilder.append('').append(#10); + LStringBuilder.append('').append(#10); + LStringBuilder.append('').append(#10); + LStringBuilder.append('
col1col2
').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('
col1col2
', LResult); +end; + procedure TTestTemplate.TestNonMutation; type TRec = record @@ -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;