Frage

I have a lot of defs and captures in a mako template. Rendering of template is passed to pdflatex so the output of mako must be just right. The printed text may contain apostrophes or ampersands and so on. And to disable them from turning to ' etc. I must use the filter 'n' in all expressions inside the template: ${text | n}

I have tried to add the <%page expression_filter="n" /> but it has no effect on the matter. Other filters like trim work well with page-tag, but disabling all filtering doesn't work.

Here's what I have

<%! from bs4 import BeautifulSoup %>


<%def name='html_tag_to_latex(tag, content)' filter="trim">
    % if tag == "p":
        ${content | n}\par{}
    % elif tag == "h1":
        \clearpage\section{${content | n}}
    % elif tag == "h2":
        \subsection{${content | n}}
    % else: 
        ${content | n}
</%def>

<%def name='html_to_latex(html_string)' filter="n,trim">
    <%def name='beautifulsoup_to_latex(item)' filter="n,trim">
        <% text = u'' %>
        % for child in item.contents:
            % try:
                <% child.contents %>
                <% subtext = capture(beautifulsoup_to_latex, item=child) %>
                % if subtext.strip():
                    <%
                    text += capture(
                        html_tag_to_latex, 
                        tag=child.name,
                        content=subtext.strip()
                    ) 
                    %>
                % endif
            % except AttributeError:
                <% text += unicode_to_latex(child.string) %> 
            % endtry
        % endfor
        ## Capture all the kids and then print out
        ${text | n} 
    </%def>
    <% soup = BeautifulSoup(html_string) %>
    ${beautifulsoup_to_latex(soup)}
</%def>

All n-filters for some reason need to be explicitly placed in side expressions. Adding the filter n in defs has no effect; filter="n,trim" or filter="n" does nothing. And this for whatever reason affects only apostrophes.

The unicode_to_latex-method does a dictionary checkup to transform unicodes to LaTeX-markup eg. & -> \&. It works fine, but mako turns it intoa \&amp;. Scandinavian letters äåö are displayed as is, so makos entity-filter is not in use.

Is the only solution really to add the | n to all expressions? It is the only one that works for some reason. Why can't I use the expression_filter?

Edit: Noted that the line ${beautifulsoup_to_latex(soup)} does not need to disable filterin. only inside the html_tag_to_latex and html_to_latex methods expressions need that.

War es hilfreich?

Lösung

Solved! I think.

I use Pyramid-framework version 1.4 with mako templating and it seems that deep inside mako_templating.py resides a line:

default_filters = sget('default_filters', 'h')

Which explains why the html filtering is always used as a default. Doesn't really explain why it overrides the page expression_filters, but seems close enough to answer my own question.

As of 1.5.something Pyramid has moved mako renderers to a differed package called pyramid_mako and it seems the same default settings are there too.

To override this one must set the mako.default_filters setting in the Pyramids .ini-file. This however screwed up all my exisiting templates, so I think I'll have to stick with using the | n flag with expressions within the templates.

This took some time to figure out. Hope this will help somebody else.

EDIT: Set mako.default_filters = unicode removes the need to use n-flag. Only using mako.default_filters = n messed things up.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top