How to prevent Mustache from replacing & with & etc.?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to prevent Mustache from replacing & with & etc.?

jtuchel
Hi Pharoers,


I may be a bit wrong on this list. I try anyways.

I am using Mustache to fill text into a String which will then be handed
over to LaTeX. The Data I fill in comes from the Database.

Now the problem is this: if a String from the database contains an
ampersand (like in 'Katz & Maus'), Mustache will replace special chars
with HTML entities. That is fine in a web context, but LaTeX doesnt
actually work well with HTML entities. I could, of course, remove the
html entities after Mustache invested a lopt of work into creating them.
But it seems more logical to keep Mustache from being so eager to help.

BTW: I already took the hurdle of Mustache excaping $& fir tabular
environments (LaTeX uses the ampersand as a divider for table cells) by
using \catcode in our .tex template files. But now the String comes from
the database and so I am a bit lost...


So is there any best practice for this problem?


Joachim





Reply | Threaded
Open this post in threaded view
|

Re: How to prevent Mustache from replacing & with & etc.?

NorbertHartl
Hi,


> Am 16.04.2019 um 09:02 schrieb [hidden email]:
>
> Hi Pharoers,
>
>
> I may be a bit wrong on this list. I try anyways.
>
> I am using Mustache to fill text into a String which will then be handed over to LaTeX. The Data I fill in comes from the Database.
>
> Now the problem is this: if a String from the database contains an ampersand (like in 'Katz & Maus'), Mustache will replace special chars with HTML entities. That is fine in a web context, but LaTeX doesnt actually work well with HTML entities. I could, of course, remove the html entities after Mustache invested a lopt of work into creating them. But it seems more logical to keep Mustache from being so eager to help.

It is not logical because mustache is defined that way. So redefining does not improve it. But just use triple {{{ }}} to have strings not being escaped.

>
> BTW: I already took the hurdle of Mustache excaping $& fir tabular environments (LaTeX uses the ampersand as a divider for table cells) by using \catcode in our .tex template files. But now the String comes from the database and so I am a bit lost...
>
>
> So is there any best practice for this problem?
>
Yes, use it this way

'{{{ value }}}' asMustacheTemplate value: { #value -> '&' } asDictionary

Norbert


Reply | Threaded
Open this post in threaded view
|

Re: How to prevent Mustache from replacing & with & etc.?

jtuchel
Norbert,

thanks for answering. I had tried the {{{ }}} option. In case of LaTeX,
this is not so easy, however, because {{{ and }}} are completely valid
syntactical elements of LaTeX.

It seems like you can replace {{ and }} in Mustache, but not {{{ and
}}}. At least it didn't work when I tried. Using '{{{=### ###=}}} ' in
my MustacheTemplate would leave text like '###myObject.printString###'
unchanged in the resulting text instead of replacing it with the text
representation of myObject.

So what I tried last and what gave me quite some mileage on the way to a
sulotion was to use

\catcode to replace & with ; in my LaTeX file for the Column separator
problem
{{=## to make sure I can fill in my table rows with ; as column separator.

Thus my Mustache-Template looks like this (excerpt):

% redefine Mustache-Insertion syntax {{=## ##=}}
\catcode`\;=4 % redefine ; for & to allow non-escaped column-separators
in \tabular

...
\begin{tabular}{ccp{6.0cm}rrr}
##myObject.asLatexTableRows##  % This is where & is needed for LaTeX to
separate table columns, but Mustache would replace the with the html
entity &
\end{tabular}


This is a bit hacky, and as I wrote, also still presents problems,
because if any of the Strings being inserted using Mustache contains an
&, there will be an & in the .tex file, which will lead to a LaTeX
error.


So, essentially, what I am most likely looking for is a way to replace
{{{ and }}} in Mustache to make sure the $&'s will remain untouched in
my inserted text...
Sorry for asking the wrong question, I was so glad how far I'd gotten
with what I did that I didn't see the forest between all the trees.

So my real question is: how can I replace {{{ in Mustache with something
that will not irritate LaTeX?


Any ideas?


Joachim








Am 16.04.19 um 12:00 schrieb Norbert Hartl:

> Hi,
>
>
>> Am 16.04.2019 um 09:02 schrieb [hidden email]:
>>
>> Hi Pharoers,
>>
>>
>> I may be a bit wrong on this list. I try anyways.
>>
>> I am using Mustache to fill text into a String which will then be handed over to LaTeX. The Data I fill in comes from the Database.
>>
>> Now the problem is this: if a String from the database contains an ampersand (like in 'Katz & Maus'), Mustache will replace special chars with HTML entities. That is fine in a web context, but LaTeX doesnt actually work well with HTML entities. I could, of course, remove the html entities after Mustache invested a lopt of work into creating them. But it seems more logical to keep Mustache from being so eager to help.
> It is not logical because mustache is defined that way. So redefining does not improve it. But just use triple {{{ }}} to have strings not being escaped.
>
>> BTW: I already took the hurdle of Mustache excaping $& fir tabular environments (LaTeX uses the ampersand as a divider for table cells) by using \catcode in our .tex template files. But now the String comes from the database and so I am a bit lost...
>>
>>
>> So is there any best practice for this problem?
>>
> Yes, use it this way
>
> '{{{ value }}}' asMustacheTemplate value: { #value -> '&' } asDictionary
>
> Norbert
>
>
>

--
-----------------------------------------------------------------------
Objektfabrik Joachim Tuchel          mailto:[hidden email]
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 | Threaded
Open this post in threaded view
|

Re: How to prevent Mustache from replacing & with & etc.?

jtuchel

So writing my answer I came to the conclusion to visit https://ci.inria.fr/pharo-contribution/job/EnterprisePharoBook/lastSuccessfulBuild/artifact/book-result/Mustache/Mustache.html again.

BTW: Good work, thanks for both implementing and documenting Mustache this nicely!

And I found {{& which keeps Mustache from escaping. By combinng {{=## and {{& for the "insertion points" I can now render table data with $& as column seprators from Smalltalk. No need for the \catcode any more.

But this is not the end of the story. Now I get 'misplaced alignment tab character' errors from LaTeX whenever the text contains an &. Because LaTeX wants & to be escaped as \& ;-))))


So now I can transport my &'s to Latex without much trouble by using ##&, but LaTeX needs the & which are intended to be printed as & to be escaped, just not as html entities but tex entities ;-)

Does anybody have a solution for this already or should I try to use Mustache and bend it to my LaTeX needs...?


Joachim


P.S.: some problems look so easy at the beginning and open a wide universe of unmatching available solutions when you attack them. I cannot use bindWith: because of the % signs, expandMakrosWith: also doesn't work, and Mustache is not suitable out of the box. It's worth a laugh, but somehow it's not funny...





 
-- 
-----------------------------------------------------------------------
Objektfabrik Joachim Tuchel          [hidden email]
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 | Threaded
Open this post in threaded view
|

Re: How to prevent Mustache from replacing & with & etc.?

NorbertHartl
In reply to this post by jtuchel
Joachim,

> Am 17.04.2019 um 07:56 schrieb [hidden email]:
>
> Norbert,
>
> thanks for answering. I had tried the {{{ }}} option. In case of LaTeX, this is not so easy, however, because {{{ and }}} are completely valid syntactical elements of LaTeX.
>
> It seems like you can replace {{ and }} in Mustache, but not {{{ and }}}. At least it didn't work when I tried. Using '{{{=### ###=}}} ' in my MustacheTemplate would leave text like '###myObject.printString###' unchanged in the resulting text instead of replacing it with the text representation of myObject.
>
> So what I tried last and what gave me quite some mileage on the way to a sulotion was to use
>
> \catcode to replace & with ; in my LaTeX file for the Column separator problem
> {{=## to make sure I can fill in my table rows with ; as column separator.
>
> Thus my Mustache-Template looks like this (excerpt):
>
> % redefine Mustache-Insertion syntax {{=## ##=}}
> \catcode`\;=4 % redefine ; for & to allow non-escaped column-separators in \tabular
>
> ...
> \begin{tabular}{ccp{6.0cm}rrr}
> ##myObject.asLatexTableRows##  % This is where & is needed for LaTeX to separate table columns, but Mustache would replace the with the html entity &
> \end{tabular}
>
>
> This is a bit hacky, and as I wrote, also still presents problems, because if any of the Strings being inserted using Mustache contains an &, there will be an & in the .tex file, which will lead to a LaTeX error.
>
>
> So, essentially, what I am most likely looking for is a way to replace {{{ and }}} in Mustache to make sure the $&'s will remain untouched in my inserted text...
> Sorry for asking the wrong question, I was so glad how far I'd gotten with what I did that I didn't see the forest between all the trees.
>
> So my real question is: how can I replace {{{ in Mustache with something that will not irritate LaTeX?
>
>
> Any ideas?
>
Sure ;) In Mustache {{ and }} are the default delimiters. Every addtional character is a modifier. So {{{ is actuall a start delimiter {{ with a { as modifier. So what you can do is to change the delimiter but leave the { modifier like so

'{{=<% %>=}} <%{ value %>}' asMustacheTemplate value: { #value -> '&amp;' } asDictionary

So {{ is the start delimiter, = is the modifier to change the delimiters. We set them to <% and %>. If you want to have unescaped strings you keep using the { on the new delimiter which turns out to be <%{ and %>}.

Norbert

>
> Am 16.04.19 um 12:00 schrieb Norbert Hartl:
>> Hi,
>>
>>
>>> Am 16.04.2019 um 09:02 schrieb [hidden email]:
>>>
>>> Hi Pharoers,
>>>
>>>
>>> I may be a bit wrong on this list. I try anyways.
>>>
>>> I am using Mustache to fill text into a String which will then be handed over to LaTeX. The Data I fill in comes from the Database.
>>>
>>> Now the problem is this: if a String from the database contains an ampersand (like in 'Katz & Maus'), Mustache will replace special chars with HTML entities. That is fine in a web context, but LaTeX doesnt actually work well with HTML entities. I could, of course, remove the html entities after Mustache invested a lopt of work into creating them. But it seems more logical to keep Mustache from being so eager to help.
>> It is not logical because mustache is defined that way. So redefining does not improve it. But just use triple {{{ }}} to have strings not being escaped.
>>
>>> BTW: I already took the hurdle of Mustache excaping $& fir tabular environments (LaTeX uses the ampersand as a divider for table cells) by using \catcode in our .tex template files. But now the String comes from the database and so I am a bit lost...
>>>
>>>
>>> So is there any best practice for this problem?
>>>
>> Yes, use it this way
>>
>> '{{{ value }}}' asMustacheTemplate value: { #value -> '&amp;' } asDictionary
>>
>> Norbert
>>
>>
>>
>
> --
> -----------------------------------------------------------------------
> Objektfabrik Joachim Tuchel          mailto:[hidden email]
> 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 | Threaded
Open this post in threaded view
|

Re: How to prevent Mustache from replacing & with &amp; etc.?

NorbertHartl


Am 17.04.2019 um 08:31 schrieb Norbert Hartl <[hidden email]>:

Joachim,

Am 17.04.2019 um 07:56 schrieb [hidden email]:

Norbert,

thanks for answering. I had tried the {{{ }}} option. In case of LaTeX, this is not so easy, however, because {{{ and }}} are completely valid syntactical elements of LaTeX.

It seems like you can replace {{ and }} in Mustache, but not {{{ and }}}. At least it didn't work when I tried. Using '{{{=### ###=}}} ' in my MustacheTemplate would leave text like '###myObject.printString###' unchanged in the resulting text instead of replacing it with the text representation of myObject.

So what I tried last and what gave me quite some mileage on the way to a sulotion was to use

\catcode to replace & with ; in my LaTeX file for the Column separator problem
{{=## to make sure I can fill in my table rows with ; as column separator.

Thus my Mustache-Template looks like this (excerpt):

% redefine Mustache-Insertion syntax {{=## ##=}}
\catcode`\;=4 % redefine ; for & to allow non-escaped column-separators in \tabular

...
\begin{tabular}{ccp{6.0cm}rrr}
##myObject.asLatexTableRows##  % This is where & is needed for LaTeX to separate table columns, but Mustache would replace the with the html entity &amp;
\end{tabular}


This is a bit hacky, and as I wrote, also still presents problems, because if any of the Strings being inserted using Mustache contains an &, there will be an &amp; in the .tex file, which will lead to a LaTeX error.


So, essentially, what I am most likely looking for is a way to replace {{{ and }}} in Mustache to make sure the $&'s will remain untouched in my inserted text...
Sorry for asking the wrong question, I was so glad how far I'd gotten with what I did that I didn't see the forest between all the trees.

So my real question is: how can I replace {{{ in Mustache with something that will not irritate LaTeX?


Any ideas?

Sure ;) In Mustache {{ and }} are the default delimiters. Every addtional character is a modifier. So {{{ is actuall a start delimiter {{ with a { as modifier. So what you can do is to change the delimiter but leave the { modifier like so 

'{{=<% %>=}} <%{ value %>}' asMustacheTemplate value: { #value -> '&amp;' } asDictionary 

So {{ is the start delimiter, = is the modifier to change the delimiters. We set them to <% and %>. If you want to have unescaped strings you keep using the { on the new delimiter which turns out to be <%{ and %>}. 

I’m not sure the { should not be nested. Need to think about it. It might should be like <%{ }%>.

Norbert

Norbert

Am 16.04.19 um 12:00 schrieb Norbert Hartl:
Hi,


Am 16.04.2019 um 09:02 schrieb [hidden email]:

Hi Pharoers,


I may be a bit wrong on this list. I try anyways.

I am using Mustache to fill text into a String which will then be handed over to LaTeX. The Data I fill in comes from the Database.

Now the problem is this: if a String from the database contains an ampersand (like in 'Katz & Maus'), Mustache will replace special chars with HTML entities. That is fine in a web context, but LaTeX doesnt actually work well with HTML entities. I could, of course, remove the html entities after Mustache invested a lopt of work into creating them. But it seems more logical to keep Mustache from being so eager to help.
It is not logical because mustache is defined that way. So redefining does not improve it. But just use triple {{{ }}} to have strings not being escaped.

BTW: I already took the hurdle of Mustache excaping $& fir tabular environments (LaTeX uses the ampersand as a divider for table cells) by using \catcode in our .tex template files. But now the String comes from the database and so I am a bit lost...


So is there any best practice for this problem?

Yes, use it this way

'{{{ value }}}' asMustacheTemplate value: { #value -> '&amp;' } asDictionary

Norbert




-- 
-----------------------------------------------------------------------
Objektfabrik Joachim Tuchel          [hidden email]
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