Re: backslashes get stripped from $ signs

Dieter Plaetinck <dieter-7FYLOmueu/[email protected]> Mon, 27 Dec 2010 11:46:48 +0100
Newsgroups gmane.comp.web.pyblosxom.user
Message-ID <[email protected]>
On Sun, 26 Dec 2010 20:41:16 -0500
will kahn-greene <[email protected]> wrote:

> Looks like a problem with the blosxom renderer.  Templates use $ for
> variables.  It allows you to escape the $ in a template with a \ so
> that you can have dollar signs show up in templates.
> 
> However, it looks like render_template totally does this wrong by
> changing \$ to $ after the variables have been expanded and affecting
> the variable expansion values.  Thus any \$ get converted to $ in the
> story body.
> 
> The code in the blosxom renderer that handles variable finding and
> substitution is pretty complex.  Seems like it might be a good idea to
> ditch that code, switch to using a real parser, and then we'd be able
> to trivially fix this issue, too.  We'd also be able to add some other
> features like if blocks.
> 
> I don't think that's something I want to do in 1.5, though.  This is
> definitely a 1.6 issue.

fair enough, once we have the new bugtracker in place we can start
gathering requirements to base the decision of the new template engine
 
> In the meantime, you could add a second \ or patch your copy of the
> renderer locally.

thanks, i attempted patching, but failed:

diff --git a/Pyblosxom/tools.py b/Pyblosxom/tools.py
index c560bef..f24b510 100644
--- a/Pyblosxom/tools.py
+++ b/Pyblosxom/tools.py
@@ -482,6 +482,10 @@ def parse(request, var_dict, template):
     :returns: the template string with template variables expanded.
     """
     encoding = request.config.get("blog_encoding", "utf-8")
+    # after the expansion, \$ in templates will be rendered as $, but this will get applied
+    # to the entire output.  To protect expanded content from this, we double escape first
+    if 'body' in var_dict.keys():
+        var_dict['body'] = var_dict['body'].replace(r'\$', '\\\$')
     replacer = Replacer(request, encoding, var_dict)
     return _VAR_REGEXP.sub(replacer.replace, template)
 

this kind of works, except that for some reason i need 3 backslashes in the replacement, and this will also change '$' into '\$',
which i don't want, obviously. changing the first param to '\\$' doesn't help either.  I'm not sure what the 'r' is for, but I saw it
in BlosxomRenderer:render_template(), and using it doesn't seem to change anything.

Dieter

------------------------------------------------------------------------------
Learn how Oracle Real Application Clusters (RAC) One Node allows customers
to consolidate database storage, standardize their database environment, and, 
should the need arise, upgrade to a full multi-node Oracle RAC database 
without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl