2013-09-24 26 views
1

我有很多defs和捕獲mako模板。模板渲染被傳遞給pdflatex,所以mako的輸出必須正確。打印的文本可能包含撇號或與符號等。並禁用他們轉向'等。我必須在模板內的所有表達式中使用過濾器'n':${text | n}如何使用捕獲和defs禁用mako模板中的所有過濾?

我試圖添加<%page expression_filter="n" />,但它對此事沒有任何影響。其他篩選器如trimpage-tag配合良好,但禁用所有篩選不起作用。

這裏就是我有

<%! 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> 

所有正過濾器由於某種原因需要顯式地放在一邊表達式。在defs中添加過濾器n不起作用; filter="n,trim"filter="n"什麼都不做。無論出於何種原因,這隻會影響撇號。

unicode_to_latex -method進行字典檢查以將unicode轉換爲LaTeX標記,例如。 & - >\&。它工作正常,但mako把它變成\&amp;。斯堪的納維亞語字母äåö按原樣顯示,所以makos entity -filter未使用。

| n添加到所有表達式是唯一的解決方案嗎?這是唯一一個出於某種原因工作的人。爲什麼我不能使用expression_filter?

編輯: 注意到行${beautifulsoup_to_latex(soup)}不需要禁用filterin。表達式只需要在html_tag_to_latexhtml_to_latex之內。

回答

1

解決!我認爲。

我用金字塔框架1.4版本與灰鯖模板,似乎內心深處mako_templating.py所在的行:

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

這解釋了爲什麼HTML過濾通常被用作默認。並沒有真正解釋爲什麼它覆蓋了頁面expression_filters,但似乎足夠接近回答我自己的問題。

從1.5something Pyramid已經將mako渲染器移動到名爲pyramid_mako的不同包中,並且它似乎也具有相同的默認設置。

要覆蓋此項,必須在金字塔.ini-文件中設置mako.default_filters設置。然而,這樣搞砸了我現有的所有模板,所以我想我必須堅持在模板中使用帶有表達式的| n標誌。

這花了一些時間弄清楚。希望這會幫助別人。

編輯:設置mako.default_filters = unicode消除了使用n標誌的需要。只有使用mako.default_filters = n搞砸了。

相關問題