Java 语法高亮器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1853419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Syntax Highlighter for Java
提问by AhmetB - Google
As you know there is a Syntax highlighter for PHP called GeSHiwhich supports a great number of Programming Languages or Code formats.
如您所知,PHP 有一个名为GeSHi的语法高亮器,它支持大量的编程语言或代码格式。
However, I couldn't find such a library for Java which supports programming languages that I need (ADA, ASP, BNF, Bash, Brainfwor, C, C++, C#, CSS, Cobol, ColdFusion, D, Fortran, Haskell, HTML, INI (Config), Java, JavaScript, Lisp, Make, Objective C, PASCAL, Perl, PHP, PLSQL, Prolog, Python, Ruby, Scheme, SQL, VB.NET, Verilog, VHDL, Visual Basic, XML.)
但是,我找不到这样一个支持我需要的编程语言的 Java 库(ADA、ASP、BNF、Bash、Brainfwor、C、C++、C#、CSS、Cobol、ColdFusion、D、Fortran、Haskell、HTML、 INI(配置)、Java、JavaScript、Lisp、Make、Objective C、PASCAL、Perl、PHP、PLSQL、Prolog、Python、Ruby、Scheme、SQL、VB.NET、Verilog、VHDL、Visual Basic、XML。)
Do you know one or should I prefer inefficient way which is retrieving the highlighted code from a remote PHP server via http transaction? Any ideas?
您是否知道一种或我应该更喜欢通过 http 事务从远程 PHP 服务器检索突出显示代码的低效方式?有任何想法吗?
Thanks.
谢谢。
回答by Bozho
Two related questions:
两个相关问题:
What code highlighting libs are there for Java?Where can I find a syntax highlighting library for Java?
Java 有哪些突出显示库的代码?在哪里可以找到 Java 的语法高亮库?
And one library I found: http://colorer.sourceforge.net/
我发现了一个图书馆:http: //colorer.sourceforge.net/
回答by Pascal Thivent
Have a look at JHighlighteror jEdit Syntax Package. All mentioned languages aren't supported out of the box. However, you have the sources, so I guess it should be possible to add language support.
看看JHighlighter或jEdit Syntax Package。并非所有提到的语言都是开箱即用的。但是,您有资源,所以我想应该可以添加语言支持。
Not a direct answer but, if client-side syntax highlighting is an option, the SyntaxHighlighterlibrary from Alex Gorbatchev is an awesome javascript library, supports lots of languagesand is highly extensible.
不是直接的答案,但是,如果客户端语法高亮是一个选项,来自 Alex Gorbatchev的SyntaxHighlighter库是一个很棒的 JavaScript 库,支持多种语言并且高度可扩展。
回答by orip
You could use Pygmentsthrough Jython. Won't be as fast as a Java solution, but much faster than interacting with a remote server.
您可以通过 Jython使用Pygments。不会像 Java 解决方案一样快,但比与远程服务器交互要快得多。
Barring that, you could run Geshi locally and pipe source code through it, that would also beat an HTTP round trip.
除此之外,您可以在本地运行 Geshi 并通过它管道源代码,这也将击败 HTTP 往返。
回答by Jmini
回答by Jmini
There is a port of prettify.jsfor Java: java-prettify.
有一个用于 Java的prettify.js端口:java-prettify。
It can be used to produce HTML (computed in Java), as I discussed here: Use the java-prettify parser to create HTML
它可用于生成 HTML(用 Java 计算),正如我在此处讨论的: 使用 java-prettify 解析器创建 HTML
回答by tekumara
jeditis a text editor with syntax highlighting support for some 170+ languages via "modes". It also allows you to specify your own syntaxes. You can use the StandaloneTextArea component in your own application as follows:
jedit是一个文本编辑器,具有通过“模式”对大约 170 多种语言的语法突出显示支持。它还允许您指定自己的语法。您可以在自己的应用程序中使用 StandaloneTextArea 组件,如下所示:
- Extract source (eg: jedit4.3source.tar.bz2 to d:\source\jedit)
Use ant to copy all the textarea files to ..\textarea eg:
D:\Source\jedit\jEdit> ant prepare-textArea
However, it misses the file BufferUndoListener.java. Copy this manually by executing
D:\Source\jedit\jEdit> copy org\gjt\sp\jedit\buffer\BufferUndoListener.java ..\textarea\src\org\gjt\sp\jedit\buffer\
In Eclipse create a Java Project from existing source in the directory D:\Source\jedit\textarea
- Navigate to org.gjt.sp.jedit.textarea.StandaloneTextArea.java
- Change the line
- 提取源代码(例如:jedit4.3source.tar.bz2 到 d:\source\jedit)
使用 ant 将所有 textarea 文件复制到 ..\textarea 例如:
D:\Source\jedit\jEdit> ant prepare-textArea
但是,它错过了文件 BufferUndoListener.java。通过执行手动复制
D:\Source\jedit\jEdit> 复制 org\gjt\sp\jedit\buffer\BufferUndoListener.java ..\textarea\src\org\gjt\sp\jedit\buffer\
在 Eclipse 中,从目录 D:\Source\jedit\textarea 中的现有源创建一个 Java 项目
- 导航到 org.gjt.sp.jedit.textarea.StandaloneTextArea.java
- 换线
mode.setProperty("file","modes/xml.xml");to
mode.setProperty("file","src/modes/xml.xml");
mode.setProperty("file","modes/xml.xml");到
mode.setProperty("file","src/modes/xml.xml");
- Run. Copy and paste an XML into the editor and see the syntax highlighting is working.
- 跑。将 XML 复制并粘贴到编辑器中,然后查看语法突出显示是否有效。
回答by Erlend V
A couple of highlighters that work really well are:
一些非常有效的荧光笔是:
Both are easy to implement client side, and support a wide variety of languages. They also have a handsome range of css themes that make the code look like it does in your favorite editor.
两者都很容易实现客户端,并支持多种语言。它们还有一系列漂亮的 css 主题,使代码看起来就像在您最喜欢的编辑器中一样。

