Re: backslashes get stripped from $ signs

Dieter Plaetinck <dieter-7FYLOmueu/[email protected]> Sat, 8 Jan 2011 22:56:58 +0100
Newsgroups gmane.comp.web.pyblosxom.user
Message-ID <[email protected]>
On Mon, 27 Dec 2010 11:46:48 +0100
Dieter Plaetinck <dieter-7FYLOmueu/[email protected]> wrote:

> 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

anyone an idea?  on a python commandline shell I can just do:
str.replace('\$', '\\$') 
but I have no idea why I can't get it to work in Pyblosxom/tools.py

Dieter

------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any company
that requires sensitive data to be transmitted over the Web.   Learn how to 
best implement a security strategy that keeps consumers' information secure 
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl