- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 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/
我最近开始使用 Scala 编程。我正在寻找免费的 Scala 分析器。从该语言的官方网站阅读后,我找到了 YourKit ,但该程序不是免费的。 谷歌搜索“scala profiler”没有给我任何
是否有一个程序可以让我获得有关脚本的详细信息:具体来说,我希望能够跟踪其内存占用情况并查看内存中有多少对象。 Firebug 有一个分析器,似乎可以提供计时信息,但我对内存管理更感兴趣。 最佳答案 G
Erlang 有没有好的代码分析器/分析器?我需要一些可以为我的代码构建调用图的东西。 最佳答案 对于静态代码分析,您有 Xref和 Dialyzer ,对于分析,您可以使用 cprof、fprof
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
什么是“self”和“total”列? “总计”列加起来不等于 100%(高得多),看起来像 self 一样。我怀疑 self 是非累积的,而 total 是。因此,如果 methodA 调用 met
在 Perl 中,有一个非常好的分析器,称为 NYTProf。 在其报告中包含 flame graph ,这样就很容易找到程序的瓶颈 是否有一个等效的 java profiler 可以生成相同的报告?
我想知道是否有办法在c++程序中查看特定部分的调用路径。我在开源代码中工作,其中包含许多库。我尝试按照最终在模板中的路径中的代码进行操作。那么,是否有这样的库或分析器来显示代码特定部分的调用路径? 谢
我正在寻找支持每个请求分析统计的分析器,最好是沿着程序流(而不是通常的线程调用堆栈)。所以基本上是每个请求的探查器调用堆栈 + 顺序调用 View ,如下所示: doGet
如何让 symfony profiler 在 phpstorm 中工作?它应该在状态栏中,但是当我打开那个窗口时,它总是说:“这里什么都没有”。缓存目录在配置中正确定义,symfony 插件本身工作正
人们在 Unix 中用来解析/分析日志文件的流行工具是什么?进行计数,查找唯一性,选择/复制具有特定模式的特定行。请提供一些工具或一些关键字。因为我相信以前肯定有类似的问题,但我对关键字一无所知。谢谢
我设置了一个过滤器来计算执行的查询数量,并在超过某个限制时将一些内容写入我的数据库。 它在我的开发环境中运行良好,但是当我在生产环境中测试它时,我的数据库不再返回分析器。我认为这是一个为开发环境启用数
你找到这样的工具并成功使用了吗? 最佳答案 我也在寻找 AS 的分析器,但我想要一个与 FlashDevelop 和 Flex SDK 配合使用的免费软件/开源解决方案。我没有找到。所以我写了一个简单
我需要扩展 SonarQube,以便我可以向其中添加新的分析器。我尝试使用 xpath 为 java 文件添加新规则。我想知道如何在 SonarQube 中添加新语言的代码分析器,例如对于 .meta
我可以在C#(嵌套)中使用elasticsearch 2.3.0版本 我想将分析与索引一起使用, 但是索引设置不会改变,我也不知道为什么。 这是我的代码: private void b
我有一个正在wireshark中查看的自定义协议(protocol)。我认为如果wireshark可以帮我剖析它,这样我就不必解码十六进制了,这会很有用。尽管我在程序日志中执行此操作,但wiresha
我正在考虑使用 Elasticsearch 来提供我们网站的搜索功能。 我一直在试验它,但无法启用 Porterstem 分析器(以便搜索战斗匹配战斗和战斗)。 这是我输入的摘要。 curl
我正在尝试运行一个基于java的java分析器来找出我的专用机器上的java应用程序的资源消耗情况。我尝试使用的分析器称为 Warmroast。 运行时出现以下错误。 java -jar warmro
我没有从我的分析器获得预期结果,并且想测试标记化过程。 此问题的答案:How to use a Lucene Analyzer to tokenize a String? List result =
我在 Lucene 中有特殊的分析需求,但我想继续使用 StandardAnalyzer 机制的部分内容。 特别是,我想要字符串 “-苹果--胡萝卜-番茄?” 被标记为: “-苹果-”2.“-胡萝卜-
有什么好的 JSF 分析器推荐吗? 我正在使用 Tomcat 6 JSF2、 hibernate 和 Spring 最佳答案 如果您使用的是 Eclipse 或 Netbeans 之类的 IDE,那么
我是一名优秀的程序员,十分优秀!