如何避免evoqu中的htmlescaping

2024-04-29 11:25:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着让我的evoque模板有点颜色代码, 但是我得到的html已经被lt gt的转义了

我读到应该有一个被引用的类 但我没能找到evoque.引用包装

我的目标是不逃过模板中的html,而是“真实的”。在

from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from evoque.domain import Domain
import os


tmpl="""

$begin{code}
    ${codyfy(evoque(name=label), lang=label.split()[0][1:])}
$end{code}

$begin{c 0}
    int main(void){printf("hello world");return 0;}
$end{c 0}

$begin{python 0}
    print "hello world"
$end{python 0}

$evoque{#code, label="#c 0"}
$evoque{#code, label="#python 0"}
"""
td = Domain(os.path.abspath("."))
def codyfy(src,lang="python"):
    return highlight(src,get_lexer_by_name(lang, stripall=True),HtmlFormatter())

td.set_on_globals('codyfy',codyfy)
td.set_template("testtmpl", src=tmpl, from_string=True)
t = td.get_template("testtmpl")
print t.evoque()

Tags: namefromimportsrc模板langgetpygments
3条回答

你用raw=True试过了吗?参见:

我以前没有使用过Qpy,但也许这条注释会有所帮助:

Defining custom quoted-no-more classes

[...] It is also highly recommended to download and install the Qpy unicode templating utility that provides the qpy.xml Quoted-No-More class for automatic input escaping. [...]

是的-我们是平行的,艾尔斯和我。 答案在这里-把它剪成上面:

from qpy import xml
def codyfy(src,lang="python"):
    return xml(highlight(src,get_lexer_by_name(lang, stripall=True),HtmlFormatter()))

xml()显然是一种使后续逃逸器停止工作的有效负载。在

在qpy.xml文件()是Qpy包在superfast C和其他python版本中提供的XML(和HTML)的引用no-more类。Evoque在quoting=“xml”时查找此特定类(即等效于:quoting=qpy.xml文件)用于加载或呈现模板。在

但是任何被引用的自定义类型都不能被指定为引用参数的值。这个evoque.引用包提供了一个基本的不带引号的类型,以及一些具体的示例,使您的自定义quoted no more类型的定义更容易。然而,如前所述evoque引用尚不可用,它在变更日志中有所提及,但对于尚未发布的Evoque版本(请参阅变更日志页面)。在

如果您通过codyfy()传递一整组模板的输出,那么您可能还希望考虑将其指定为一个筛选器,可以在每个模板上指定,也可以将其指定为集合的默认筛选器。使用过滤器的一个例子是: http://evoque.gizmojo.org/howto/markdown/

实际上,使用过滤器是一种更好的方法,因为它更通用:
a) 不用硬接线qpy.xml文件()在codyfy()内调用,并且
b) 能够在所有模板中使用与filter完全相同的codyfy()函数,即使这些模板指定的引用不是qpy.xml文件. 在

相关问题 更多 >