我有很多defs和捕獲mako模板。模板渲染被傳遞給pdflatex,所以mako的輸出必須正確。打印的文本可能包含撇號或與符號等。並禁用他們轉向'
等。我必須在模板內的所有表達式中使用過濾器'n':${text | n}
如何使用捕獲和defs禁用mako模板中的所有過濾?
我試圖添加<%page expression_filter="n" />
,但它對此事沒有任何影響。其他篩選器如trim
與page-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把它變成\&
。斯堪的納維亞語字母äåö
按原樣顯示,所以makos entity
-filter未使用。
將| n
添加到所有表達式是唯一的解決方案嗎?這是唯一一個出於某種原因工作的人。爲什麼我不能使用expression_filter?
編輯: 注意到行${beautifulsoup_to_latex(soup)}
不需要禁用filterin。表達式只需要在html_tag_to_latex
和html_to_latex
之內。