Joachim, I put the comments inline.
Am 17.04.2019 um 10:34 schrieb jtuchel@objektfabrik.de:
So I think I found a solution for my problem which seems to work. Thanks for listening and your input of ideas.
What I did was to follow this idea:
I am thinking of introducing a modifier, say § to switch the html entity encoding for another one which I of course have to implement on my own. The end result would be:
{{ myObject }} to encoe with HTML entities for all things (X)HTML {{& myObject }} to not encode at all (e.g. when I need & as & in the resulting String) {{§ myObject }} for an individually added encoding (tbd - for my LaTeX needs)
It turns out is was easy to implement a prototype of this (20 minutes?)
There is only one tiny hole in this implementation: If I want to fill in a table body with Text that includes an &. I need to think about it later. Maybe I'll have to come back to the catcode thingie and choose some unlikely character for column separation and use my newly implemented {{§ encoding...
All that's needed to implement the above idea were these changes (VA Smalltalk Fileout Format):
!Character publicMethods !
asLaTeXString "substitute characters like & into LaTex compliant elements" "For protoytpe just encode &"
(Dictionary with: $& -> '\&') keysAndValuesDo: [:k :v | self = k ifTrue: [^v]]. ^String with: self! !
!String publicMethods !
asLaTeXString "substitute the < & > into LaTeX compliant elements" "'<&>' asLaTeXString" ^ self class new: self size streamContents: [ :s| self do: [:c | s nextPutAll: c asLaTeXString ]]! !
MustacheToken subclass: #MustacheLaTeXEscapedToken instanceVariableNames: '' classVariableNames: '' poolDictionaries: ''!
!MustacheLaTeXEscapedToken publicMethods !
accept: aVisitor
aVisitor visitLaTeXEscapedToken: self!
valueInContext: anObject
^(super valueInContext: anObject)asLaTeXString ! !
This is the way it is designed so you can use your own token/visitor pair to extend it.
!MustacheParser publicMethods !
buildDelimiterExtensions
^Dictionary new at: $# put: [self startSection: #MustacheSection abrAsClass]; at: $/ put: [self endSection]; at: ${ put: [self readDefaultUnescapedToken]; at: $& put: [self readUnescapedToken]; at: $!! put: [self readComment]; at: $^ put: [self startSection: #MustacheInvertedSection abrAsClass]; at: $= put: [self readChangeDelimiter]; at: $> put: [self readPartial]; at: $§ put: [self readLaTeXEscapedToken]; yourself!
I donât think you need to change the parser to do that. parser := MustacheParser new. parser delimiterExtensions at: $§ put: [ parser readLaTeXEscapedToken ]. should do as well. If you have more specific needs I would subclass the parser
!MustacheVisitor publicMethods !
visitLaTeXEscapedToken: aToken ! !
!MustacheWriteVisitor publicMethods !
visitLaTeXEscapedToken: aToken
self addString: (aToken valueInContext: context)! !
I would make a visitor subclass for that. The problem is that the write visitor class name is hardcoded. We need to make that a setting. Then you could provide your own visitor. This way you can make it a real extension to mustache.
So what do people think about this? Is this a worthwhile extension? Do you see problems on the horizon that I cannot see?
I donât think it is a good extension. Mustache is implemented along the documentation and putting every use case in the code is not good. Changing the code so you can apply your changes or provide an extension package is needed of course. Norbert
Joachim
--
----------------------------------------------------------------------- Objektfabrik Joachim Tuchel mailto:jtuchel@objektfabrik.de Fliederweg 1 http://www.objektfabrik.de D-71640 Ludwigsburg http://joachimtuchel.wordpress.com Telefon: +49 7141 56 10 86 0 Fax: +49 7141 56 10 86 1