python - 使用 Pygments 过滤空格和换行符

标签 python pygments

我一直在尝试向我的 Django 站点添加语法高亮显示。问题是我得到了 &nbsp;<br />字符也被格式化。有没有办法保留这些字符?这是我正在使用的代码:

from BeautifulSoup  import BeautifulSoup
from django import template
from django.template.defaultfilters import stringfilter
import pygments
import pygments.formatters
import pygments.lexers


register = template.Library()

@register.filter
@stringfilter
def pygmentized(html):
    soup = BeautifulSoup(html)
    codeblocks = soup.findAll('code')
    for block in codeblocks:
        if block.has_key('class'):
            try:
                code = ''.join([unicode(item) for item in block.contents])
                lexer = pygments.lexers.get_lexer_by_name(block['class'], stripall=True)
                formatter = pygments.formatters.HtmlFormatter()
                code_hl = pygments.highlight(code, lexer, formatter)
                block.contents = [BeautifulSoup(code_hl)]
                block.name = 'code'
            except:
                raise
    return unicode(soup)

最佳答案

嗯,Petri 是对的,pre 用于代码块。在他指出我刚刚写了一个函数来清理第一个输出之前,它很乱,但也许只需要从最终输出中删除某些东西的人可能会发现它没问题:

from BeautifulSoup  import BeautifulSoup
from django import template
from django.template.defaultfilters import stringfilter
import pygments
import pygments.formatters
import pygments.lexers


register = template.Library()
wanted = {'br': '<br />', 'BR': '<BR />', 'nbsp': '&nbsp;', 'NBSP': '&NBSP;', '/&gt;': ''}

def uglyfilter(html):
    content = BeautifulSoup(html)
    for node in content.findAll('span'):
        data = ''.join(node.findAll(text=True))
        if wanted.has_key(data):
            node.replaceWith(wanted.get(data))
    return unicode(content)     


@register.filter
@stringfilter
def pygmentized(html):
    soup = BeautifulSoup(html)
    codeblocks = soup.findAll('pre')
    for block in codeblocks:
        if block.has_key('class'):
            try:
                code = ''.join([unicode(item) for item in block.contents])
                lexer = pygments.lexers.get_lexer_by_name(block['class'], stripall=True)
                formatter = pygments.formatters.HtmlFormatter()
                code_hl = pygments.highlight(code, lexer, formatter)
                clean = uglyfilter(code_hl)
                block.contents = [BeautifulSoup(clean)]
                block.name = 'pre'
            except:
                raise
    return unicode(soup)

关于python - 使用 Pygments 过滤空格和换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5385779/

相关文章:

python - Tensorflow卷积网络——如何计算维度(形状)?

python - numpy.square 返回稀疏矩阵的错误结果

jekyll - 在 Jekyll 中使用 pygments 时如何支持行号

python - 更改pythonpath的优先级

ruby - Windows 上的 Jekyll : Pygments not working

php - 在 PHP 中使用 Pygments(PHP 中的 Python)

python - 通过跨多索引级别分布来乘以系列

python - 在 systemctl 停止服务时正确退出 python 脚本

python - 使用 PyInstaller 制作的 .app 会立即关闭吗?

html - Sphinx 文档和悬停文本