From b6809d606c06b919b12e6b4a3b7d3f50ea2bf022 Mon Sep 17 00:00:00 2001 From: Norbert Hartl Date: Wed, 28 Sep 2022 11:35:59 +0200 Subject: [PATCH] Added storeString to MustachePart so that the template model can put serializied properly --- .../MustacheCompositePart.class.st | 13 +++- .../MustacheHtmlEscapedToken.class.st | 7 ++- .../MustacheInvertedSection.class.st | 10 ++- .../Mustache-Core/MustachePart.class.st | 20 +++++- .../Mustache-Core/MustachePartial.class.st | 13 +++- .../Mustache-Core/MustacheSection.class.st | 16 ++++- .../MustacheStringChunk.class.st | 12 +++- .../Mustache-Core/MustacheToken.class.st | 19 +++++- .../Mustache-Tests/MustacheTests.class.st | 62 ++++++++++++++++++- 9 files changed, 163 insertions(+), 9 deletions(-) diff --git a/repository/Mustache-Core/MustacheCompositePart.class.st b/repository/Mustache-Core/MustacheCompositePart.class.st index 13904f3..a58639d 100644 --- a/repository/Mustache-Core/MustacheCompositePart.class.st +++ b/repository/Mustache-Core/MustacheCompositePart.class.st @@ -4,7 +4,7 @@ Class { #instVars : [ 'parts' ], - #category : 'Mustache-Core' + #category : #'Mustache-Core' } { #category : #adding } @@ -22,3 +22,14 @@ MustacheCompositePart >> initialize [ MustacheCompositePart >> parts [ ^ parts ] + +{ #category : #writing } +MustacheCompositePart >> storeOn: aWriteStream [ + parts do: [ :part | + part storeOn: aWriteStream ] +] + +{ #category : #accessing } +MustacheCompositePart >> storeSize [ + ^ parts sum: #storeSize +] diff --git a/repository/Mustache-Core/MustacheHtmlEscapedToken.class.st b/repository/Mustache-Core/MustacheHtmlEscapedToken.class.st index 8c3a4bf..1ca075b 100644 --- a/repository/Mustache-Core/MustacheHtmlEscapedToken.class.st +++ b/repository/Mustache-Core/MustacheHtmlEscapedToken.class.st @@ -1,7 +1,7 @@ Class { #name : #MustacheHtmlEscapedToken, #superclass : #MustacheToken, - #category : 'Mustache-Core' + #category : #'Mustache-Core' } { #category : #visiting } @@ -9,6 +9,11 @@ MustacheHtmlEscapedToken >> accept: aVisitor [ aVisitor visitHtmlEscapedToken: self ] +{ #category : #writing } +MustacheHtmlEscapedToken >> storeOn: aWriteStream [ + aWriteStream << '{{' << token << '}}' +] + { #category : #resolving } MustacheHtmlEscapedToken >> valueInContext: anObject [ ^ (super valueInContext: anObject) asHTMLString diff --git a/repository/Mustache-Core/MustacheInvertedSection.class.st b/repository/Mustache-Core/MustacheInvertedSection.class.st index e5cd088..f48a6a8 100644 --- a/repository/Mustache-Core/MustacheInvertedSection.class.st +++ b/repository/Mustache-Core/MustacheInvertedSection.class.st @@ -1,10 +1,18 @@ Class { #name : #MustacheInvertedSection, #superclass : #MustacheSection, - #category : 'Mustache-Core' + #category : #'Mustache-Core' } { #category : #testing } MustacheInvertedSection >> isInverted [ ^ true ] + +{ #category : #writing } +MustacheInvertedSection >> storeOn: aWriteStream [ + aWriteStream << '{{^' << selector << '}}'. + parts do: [ :part | + part storeOn: aWriteStream ]. + aWriteStream << '{{/' << selector << '}}' +] diff --git a/repository/Mustache-Core/MustachePart.class.st b/repository/Mustache-Core/MustachePart.class.st index 19dd218..ca5c92f 100644 --- a/repository/Mustache-Core/MustachePart.class.st +++ b/repository/Mustache-Core/MustachePart.class.st @@ -6,10 +6,28 @@ For public access have a look at MustacheTemplate Class { #name : #MustachePart, #superclass : #Object, - #category : 'Mustache-Core' + #category : #'Mustache-Core' } { #category : #resolving } MustachePart >> lookup: aToken inContext: anObject [ ^ anObject mustacheLookup: aToken ] + +{ #category : #writing } +MustachePart >> printString [ + ^ self storeString +] + +{ #category : #writing } +MustachePart >> storeSize [ + self shouldBeImplemented +] + +{ #category : #writing } +MustachePart >> storeString [ + ^ String + new: self storeSize + streamContents: [ :stream | + self storeOn: stream ] +] diff --git a/repository/Mustache-Core/MustachePartial.class.st b/repository/Mustache-Core/MustachePartial.class.st index f7febc1..97d4302 100644 --- a/repository/Mustache-Core/MustachePartial.class.st +++ b/repository/Mustache-Core/MustachePartial.class.st @@ -4,7 +4,7 @@ Class { #instVars : [ 'name' ], - #category : 'Mustache-Core' + #category : #'Mustache-Core' } { #category : #'instance creation' } @@ -27,3 +27,14 @@ MustachePartial >> name [ MustachePartial >> name: aString [ name := aString ] + +{ #category : #writing } +MustachePartial >> storeOn: aStream [ + aStream << '{{>' << name << '}}' +] + +{ #category : #accessing } +MustachePartial >> storeSize [ + "the size is the name of the partial plus 5 markup chararcters {{>}}" + ^ name size + 5 +] diff --git a/repository/Mustache-Core/MustacheSection.class.st b/repository/Mustache-Core/MustacheSection.class.st index 402bbec..f527c28 100644 --- a/repository/Mustache-Core/MustacheSection.class.st +++ b/repository/Mustache-Core/MustacheSection.class.st @@ -4,7 +4,7 @@ Class { #instVars : [ 'selector' ], - #category : 'Mustache-Core' + #category : #'Mustache-Core' } { #category : #'instance creation' } @@ -43,6 +43,20 @@ MustacheSection >> selector: aString [ selector := aString ] +{ #category : #writing } +MustacheSection >> storeOn: aWriteStream [ + aWriteStream << '{{#' << selector << '}}'. + super storeOn: aWriteStream. + aWriteStream << '{{/' << selector << '}}' +] + +{ #category : #accessing } +MustacheSection >> storeSize [ + "the size of a section is the size of its content plus two times + the selector size plus 5 extra markup characters {{#}} and {{/}}" + ^ super storeSize + (2 * (selector size + 5)) +] + { #category : #accessing } MustacheSection >> valueInContext: anObject [ ^ self lookupInContext: anObject diff --git a/repository/Mustache-Core/MustacheStringChunk.class.st b/repository/Mustache-Core/MustacheStringChunk.class.st index 10e2ae3..a01cc43 100644 --- a/repository/Mustache-Core/MustacheStringChunk.class.st +++ b/repository/Mustache-Core/MustacheStringChunk.class.st @@ -4,7 +4,7 @@ Class { #instVars : [ 'string' ], - #category : 'Mustache-Core' + #category : #'Mustache-Core' } { #category : #'instance creation' } @@ -18,6 +18,16 @@ MustacheStringChunk >> accept: aVisitor [ aVisitor visitStringChunk: self ] +{ #category : #writing } +MustacheStringChunk >> storeOn: aWriteStream [ + aWriteStream nextPutAll: string +] + +{ #category : #accessing } +MustacheStringChunk >> storeSize [ + ^ string size +] + { #category : #accessing } MustacheStringChunk >> string [ ^ string diff --git a/repository/Mustache-Core/MustacheToken.class.st b/repository/Mustache-Core/MustacheToken.class.st index 503c0f1..60eda6f 100644 --- a/repository/Mustache-Core/MustacheToken.class.st +++ b/repository/Mustache-Core/MustacheToken.class.st @@ -4,7 +4,7 @@ Class { #instVars : [ 'token' ], - #category : 'Mustache-Core' + #category : #'Mustache-Core' } { #category : #'instance creation' } @@ -23,6 +23,23 @@ MustacheToken >> lookupInContext: anObject [ ^ self lookup: token inContext: anObject ] +{ #category : #writing } +MustacheToken >> storeOn: aWriteStream [ + aWriteStream << '{{{' << token << '}}}' +] + +{ #category : #accessing } +MustacheToken >> storeSize [ + "size of the token plus 4 markup characters {{}}" + ^ token size + 4 +] + +{ #category : #accessing } +MustacheToken >> token [ + + ^ token +] + { #category : #accessing } MustacheToken >> token: aString [ token := aString diff --git a/repository/Mustache-Tests/MustacheTests.class.st b/repository/Mustache-Tests/MustacheTests.class.st index 6fe7170..f9f95a8 100644 --- a/repository/Mustache-Tests/MustacheTests.class.st +++ b/repository/Mustache-Tests/MustacheTests.class.st @@ -1,7 +1,7 @@ Class { #name : #MustacheTests, #superclass : #TestCase, - #category : 'Mustache-Tests' + #category : #'Mustache-Tests' } { #category : #tests } @@ -448,3 +448,63 @@ MustacheTests >> testUnescapedToken [ self assert: result = 'This is a test for &.'. ] + +{ #category : #tests } +MustacheTests >> testWriteComment [ + | template | + "right now we just ignore comments and therefor they do not appear when + the template is serialized" + template := MustacheTemplate on: 'before {{!name}} after'. + + self assert: template storeString equals: 'before after'. +] + +{ #category : #tests } +MustacheTests >> testWriteInvertedSection [ + | template | + template := MustacheTemplate on: '{{^wrapped}} {{name}} is awesome {{/wrapped}}'. + + self assert: template storeString equals: '{{^wrapped}} {{name}} is awesome {{/wrapped}}'. +] + +{ #category : #tests } +MustacheTests >> testWriteMultipleTokens [ + | template | + template := MustacheTemplate on: '1 = {{ one }}, 2 = {{ two }}, and so on'. + self assert: template storeString equals: '1 = {{one}}, 2 = {{two}}, and so on' +] + +{ #category : #tests } +MustacheTests >> testWritePartial [ + | template | + "right now we just ignore comments and therefor they do not appear when + the template is serialized" + template := MustacheTemplate on: 'before {{>name}} after'. + + self assert: template storeString equals: 'before {{>name}} after'. +] + +{ #category : #tests } +MustacheTests >> testWriteSection [ + | template | + template := MustacheTemplate on: '{{#wrapped}} {{name}} is awesome {{/wrapped}}'. + + self assert: template storeString equals: '{{#wrapped}} {{name}} is awesome {{/wrapped}}'. +] + +{ #category : #tests } +MustacheTests >> testWriteUnescapedToken [ + | template | + template := MustacheTemplate on: 'before {{{name}}} after'. + + self assert: template storeString equals: 'before {{{name}}} after'. +] + +{ #category : #tests } +MustacheTests >> testWriteUnescapedTokenAmpersand [ + | template | + template := MustacheTemplate on: 'before {{&name}} after'. + "at the moment there is no special handling to remember which unescape + option it was. So the result will be the canonical one" + self assert: template storeString equals: 'before {{{name}}} after'. +]