使用 Pygments 过滤空格和换行符
我一直在尝试向我的 django 站点添加语法突出显示。问题是我也对
和
字符进行了格式化。有没有办法保留这些字符?这是我正在使用的代码:
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)
I've been trying to add syntax highlighting to my django site. The problem is I'm getting the
and <br />
characters formatted as well. Is there a way to preserve these characterss? Here is the code I'm using:
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,Petri 是对的,pre 用于代码块。在他指出我刚刚编写了一个函数来清理第一个输出之前,它很混乱,但也许只需要从最终输出中删除某些内容的人可能会发现它没问题:
Well, Petri is right, pre is for code blocks. Before he pointed that out I just wrote a function to clean the first output, it's messy but maybe someone who just needs to remove certain things from the final output might find it ok: