Hi Norbert,

thanks for commenting. I had a bad feeling about creating new class editions while I implemented them, so I appreciate your comments and I'll revisit my implementation, because I agree with all of your input here. Especially extending the Visitor felt "wrong" or bad. DD can be helpful, but does not lend itself well to extensions, especially "personal" ones ;-)

I didn't expect you to accept my extensions to Mustache anyways, becaus Mustache should be Mustache.

But given that we now have multiple "standard" mechanisms for mixing text into templates in Smalltalk, and none fits very well for a lot of cases, I thought it could probably be a good starting point to look into Mustache as a powerful and nicely crafted starting point for more extendable implementations for inserting text. An  extension point that builds on top of your

parser := MustacheParser new.
parser delimiterExtensions at: $§ put: [ parser readLaTeXEscapedToken ]

suggestion would be nice, but is not needed.

I am just playing with ideas here.


Again: thanks a lot for your help and comments. Even if nobody else never needs or wants what I will implement here, this still helps me a lot in achieving what I need.



Joachim






Am 17.04.19 um 10:52 schrieb Norbert Hartl:
Joachim,

I put the comments inline.

Am 17.04.2019 um 10:34 schrieb jtuc...@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:jtuc...@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






--
-----------------------------------------------------------------------
Objektfabrik Joachim Tuchel          mailto:jtuc...@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



Reply via email to