我应该将 Pygments 与 Scala 应用程序一起使用吗?

发布于 2024-09-29 05:50:26 字数 2285 浏览 0 评论 0原文

我正在考虑构建一个 Scala Web 应用程序,其中包含许多我想强调的编程语言的代码片段。看起来最好、最流行的语法荧光笔之一是 Pygments,一个 Python 工具。我下载了 Jython 并能够首先加载它,然后从我的 Scala REPL 中加载 Pygments。然而,所有的间接都非常丑陋,而且看起来相当慢(但一旦所有内容都编译完毕,可能会更快?)。

我的(清理后的)REPL 会话,用于说明:

scala> :cp /usr/local/Cellar/jython/2.5.1/libexec/jython.jar

scala> import org.python.util.PythonInterpreter;

scala> val interp = new PythonInterpreter()        
*sys-package-mgr*: processing new jar, '/usr/local/Cellar/scala/2.8.0/libexec/lib/jline.jar'
*sys-package-mgr*: processing new jar, '/usr/local/Cellar/scala/2.8.0/libexec/lib/scala-compiler.jar'
*sys-package-mgr*: processing new jar, '/usr/local/Cellar/scala/2.8.0/libexec/lib/scala-dbc.jar'
*sys-package-mgr*: processing new jar, '/usr/local/Cellar/scala/2.8.0/libexec/lib/scala-library.jar'
*sys-package-mgr*: processing new jar, '/usr/local/Cellar/scala/2.8.0/libexec/lib/scala-swing.jar'
*sys-package-mgr*: processing new jar, '/usr/local/Cellar/scala/2.8.0/libexec/lib/scalap.jar'
interp: org.python.util.PythonInterpreter = org.python.util.PythonInterpreter@111de95a

scala> interp.exec("import sys")

scala> interp.exec("sys.path.append('/Library/Python/2.6/site-packages')")

scala> interp.exec("from pygments import highlight")

scala> interp.exec("from pygments.lexers import PythonLexer")

scala> interp.exec("from pygments.formatters import HtmlFormatter")

scala> interp.exec("html = highlight(code, PythonLexer(), HtmlFormatter())")          

scala> val html = interp.get("html").toString
html: java.lang.String = 
<div class="highlight"><pre><span class="k">print</span> <span class="s">&quot;Hello World&quot;</span>
</pre></div>


scala>val xhtml =  XML.loadString(html)
xhtml: scala.xml.Elem = 
<div class="highlight"><pre><span class="k">print</span> <span class="s">&quot;Hello World&quot;</span>
</pre></div>

假设我选择使用 Pygments,您是否建议采用 Jython 路线(是比 interp.exec() 更好的调用 Python 代码的方法? )或设置一个单独的、简单的、Python 本机的 Web 服务,运行 Pygments 供我的 Scala 代码调用?当然,如果有质量相当且支持语言广度相当的库并且更容易在 Scala 中使用,我会洗耳恭听。

I am looking at building a Scala web app that will have lots of code snippets in many programming languages that I would like to hightlight. It looks like one of the best, most popular syntax highlighters is Pygments, a Python tool. I downloaded Jython and was able to load first it and then Pygments from within my Scala REPL. However, all the indirection is pretty ugly and it seems rather slow (but maybe faster once everything's compiled?).

My (cleaned-up) REPL session, for illustration:

scala> :cp /usr/local/Cellar/jython/2.5.1/libexec/jython.jar

scala> import org.python.util.PythonInterpreter;

scala> val interp = new PythonInterpreter()        
*sys-package-mgr*: processing new jar, '/usr/local/Cellar/scala/2.8.0/libexec/lib/jline.jar'
*sys-package-mgr*: processing new jar, '/usr/local/Cellar/scala/2.8.0/libexec/lib/scala-compiler.jar'
*sys-package-mgr*: processing new jar, '/usr/local/Cellar/scala/2.8.0/libexec/lib/scala-dbc.jar'
*sys-package-mgr*: processing new jar, '/usr/local/Cellar/scala/2.8.0/libexec/lib/scala-library.jar'
*sys-package-mgr*: processing new jar, '/usr/local/Cellar/scala/2.8.0/libexec/lib/scala-swing.jar'
*sys-package-mgr*: processing new jar, '/usr/local/Cellar/scala/2.8.0/libexec/lib/scalap.jar'
interp: org.python.util.PythonInterpreter = org.python.util.PythonInterpreter@111de95a

scala> interp.exec("import sys")

scala> interp.exec("sys.path.append('/Library/Python/2.6/site-packages')")

scala> interp.exec("from pygments import highlight")

scala> interp.exec("from pygments.lexers import PythonLexer")

scala> interp.exec("from pygments.formatters import HtmlFormatter")

scala> interp.exec("html = highlight(code, PythonLexer(), HtmlFormatter())")          

scala> val html = interp.get("html").toString
html: java.lang.String = 
<div class="highlight"><pre><span class="k">print</span> <span class="s">"Hello World"</span>
</pre></div>


scala>val xhtml =  XML.loadString(html)
xhtml: scala.xml.Elem = 
<div class="highlight"><pre><span class="k">print</span> <span class="s">"Hello World"</span>
</pre></div>

Assuming that I choose to use Pygments, would you suggest going the Jython route (is a better way to call Python code than interp.exec()?) or setting up a separate, simple, Python-native web service running Pygments for my Scala code to call? Of course, if there are libraries of comparable quality and breadth of supported languages that are easier to use from Scala, I'm all ears.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸里的那抹悲凉 2024-10-06 05:50:26

Pygments 是一个相当不错的语法荧光笔,如果您已经费尽心思弄清楚如何从 Scala 代码中运行它,那么您总是可以将混乱隐藏在一两个函数后面。仅仅因为它在 REPL 会话中速度不是很快,并不一定意味着它会成为问题 - JVM 在应用其许多优化之前会等待一段时间,无论如何,您需要突出显示多少代码?如果动态突出显示代码很慢,但内容变化不大,您可以只缓存渲染的 HTML。

我不知道 Scala 或 Java 中有什么好的语法突出显示工具可供您使用,但是 JavaScript 中有许多可用的语法突出显示工具,您可以将它们包含在您的站点中。这种方法的好处之一是,您不必使用任何服务器端资源来突出显示代码,您可以依靠站点的每个访问者提供突出显示他们查看的代码所需的额外计算能力。

http://alexgorbatchev.com/SyntaxHighlighter/ 是一种广泛使用的用于语法高亮的 JS 库。

Pygments is a pretty nice syntax highlighter and if you've already gone to the trouble of working out how to run it from Scala code you can always hide the mess behind a function or two. Just because it's not very fast in your REPL session doesn't necessarily mean it will be a problem - the JVM waits a while before applying many of its optimizations, and anyway how much code do you need to highlight? If dynamically highlighting the code is slow, but the content isn't changing much you can just cache the rendered HTML.

I'm not aware of a good syntax highlighting tool in Scala or Java which you could use, but there are a number of syntax highlighters available in JavaScript that you could include in your site. One benefit of that approach is that you don't have to use any server-side resources to highlight the code, you can rely on every visitor to your site to provide the extra compute power needed to highlight the code they view.

http://alexgorbatchev.com/SyntaxHighlighter/ is one widely used JS library for syntax highlighting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文