Skip to content

Commit

Permalink
Merge pull request #9 from noha/add-writing-back-to-string
Browse files Browse the repository at this point in the history
Added storeString to MustachePart so that the template model can put …
  • Loading branch information
noha authored Sep 28, 2022
2 parents d791333 + b6809d6 commit 23a0dfd
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 9 deletions.
13 changes: 12 additions & 1 deletion repository/Mustache-Core/MustacheCompositePart.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Class {
#instVars : [
'parts'
],
#category : 'Mustache-Core'
#category : #'Mustache-Core'
}

{ #category : #adding }
Expand All @@ -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
]
7 changes: 6 additions & 1 deletion repository/Mustache-Core/MustacheHtmlEscapedToken.class.st
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
Class {
#name : #MustacheHtmlEscapedToken,
#superclass : #MustacheToken,
#category : 'Mustache-Core'
#category : #'Mustache-Core'
}

{ #category : #visiting }
MustacheHtmlEscapedToken >> accept: aVisitor [
aVisitor visitHtmlEscapedToken: self
]

{ #category : #writing }
MustacheHtmlEscapedToken >> storeOn: aWriteStream [
aWriteStream << '{{' << token << '}}'
]

{ #category : #resolving }
MustacheHtmlEscapedToken >> valueInContext: anObject [
^ (super valueInContext: anObject) asHTMLString
Expand Down
10 changes: 9 additions & 1 deletion repository/Mustache-Core/MustacheInvertedSection.class.st
Original file line number Diff line number Diff line change
@@ -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 << '}}'
]
20 changes: 19 additions & 1 deletion repository/Mustache-Core/MustachePart.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
]
13 changes: 12 additions & 1 deletion repository/Mustache-Core/MustachePartial.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Class {
#instVars : [
'name'
],
#category : 'Mustache-Core'
#category : #'Mustache-Core'
}

{ #category : #'instance creation' }
Expand All @@ -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
]
16 changes: 15 additions & 1 deletion repository/Mustache-Core/MustacheSection.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Class {
#instVars : [
'selector'
],
#category : 'Mustache-Core'
#category : #'Mustache-Core'
}

{ #category : #'instance creation' }
Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion repository/Mustache-Core/MustacheStringChunk.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Class {
#instVars : [
'string'
],
#category : 'Mustache-Core'
#category : #'Mustache-Core'
}

{ #category : #'instance creation' }
Expand All @@ -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
Expand Down
19 changes: 18 additions & 1 deletion repository/Mustache-Core/MustacheToken.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Class {
#instVars : [
'token'
],
#category : 'Mustache-Core'
#category : #'Mustache-Core'
}

{ #category : #'instance creation' }
Expand All @@ -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
Expand Down
62 changes: 61 additions & 1 deletion repository/Mustache-Tests/MustacheTests.class.st
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Class {
#name : #MustacheTests,
#superclass : #TestCase,
#category : 'Mustache-Tests'
#category : #'Mustache-Tests'
}

{ #category : #tests }
Expand Down Expand Up @@ -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'.
]

0 comments on commit 23a0dfd

Please sign in to comment.