找不到别名的Lexer

2024-04-27 05:08:04 发布

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

我试图使用Pygments和beautifulsoup作为我为googleappengine构建的博客软件的代码高亮解决方案。在

它的工作原理是我的HTML帖子将使用pre标记来识别代码。像这样:

<p>Check out this cool code example<p>    
<pre class="python">
        import this
        def demo():
            pass</pre>

BeautifulSoup捕获pre标记之间的部分并将其传递给Pygments。假设Pygments检查类值并应用正确的lexar。Pygments然后应用格式化并用格式化的文本替换原始文本。解决方案在SaltyCrane's Blog中有更详细的解释。在

错误

^{pr2}$

也许我只是没有正确导入模块?在

代码

from google.appengine.ext import db
import fix_path
import bs4
import pygments
from bs4 import BeautifulSoup
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter


    def formatter(p):
        soup = BeautifulSoup(p.otext)
        preblocks = soup.findAll('pre')
        for pre in preblocks:
            if pre.has_key('class'):
                code = ''.join([unicode(item) for item in pre.contents])
                code = unescape_html(code)
                lexer = lexers.get_lexer_by_name(pre['class'])
                formatter = formatters.HtmlFormatter()
                code_hl = highlight(code, lexer, formatter)
                pre.replaceWith(BeautifulSoup(code_hl))
            else:
                print "No Go"
        return unicode(soup)

回溯

Traceback (most recent call last):
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "C:\Users\john\webdev\workspace\bsby\views.py", line 45, in post
    p.ftext = utils.formatter(p)
  File "C:\Users\john\webdev\workspace\bsby\utils.py", line 18, in formatter
    lexer = lexers.get_lexer_by_name(pre['class'])
  File "C:\Users\john\webdev\workspace\bsby\lib\pygments\lexers\__init__.py", line 80, in get_lexer_by_name
    raise ClassNotFound('no lexer for alias %r found' % _alias)
ClassNotFound: no lexer for alias [u'python'] found

Tags: infrompyimportlibgooglelinecode
1条回答
网友
1楼 · 发布于 2024-04-27 05:08:04

是的,这就是问题所在。我没有导入运行脚本所需的库。这是正确的代码。在

import fix_path
import bs4
import pygments
from bs4 import BeautifulSoup
from pygments import highlight
from pygments import lexers
from pygments import formatters


    def formatter(p):
        soup = BeautifulSoup(p.otext)
        preblocks = soup.findAll('pre')
        for pre in preblocks:
            if pre.has_key('class'):
                code = ''.join([unicode(item) for item in pre.contents])
                lexer = lexers.get_lexer_by_name("python")
                formatter = formatters.HtmlFormatter()
                code_hl = highlight(code, lexer, formatter)
                pre.replaceWith(BeautifulSoup(code_hl))
                return unicode(soup)
            else:
                return null

这是另一个问题。。。。在

我想把雷克萨斯的名字像这样动态地拉。。。在

^{pr2}$

但是,当我这样做时,我得到以下错误。在

ClassNotFound: no lexer for alias [u'python'] found

如果我这么做

lexer = lexers.get_lexer_by_name("python")

它可以工作,但只适用于python。在

相关问题 更多 >