gpt4 book ai didi

python - 找不到别名的词法分析器

转载 作者:太空宇宙 更新时间:2023-11-04 06:36:10 28 4
gpt4 key购买 nike

我正在尝试使用 Pygments 和 Beautiful Soup 作为我为 Google App Engine 构建的博客软件的代码突出显示解决方案。

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

<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 中有更详细的解释。 .

错误

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

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

代码

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

最佳答案

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

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

这是另一个问题....

我想像这样动态拉取 lexar 名称...

lexer = lexers.get_lexer_by_name(pre['class'])

但是,当我这样做时,出现以下错误。

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

如果我这样做

lexer = lexers.get_lexer_by_name("python")

它有效,但仅适用于 python。

关于python - 找不到别名的词法分析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10735534/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com