views:

412

answers:

4

Hello friends,

As you know there is a Syntax highlighter for PHP called GeSHi which supports a great number of Programming Languages or Code formats.

However, I couldn't find such a library for Java which supports programming languages that I need (ADA, ASP, BNF, Bash, Brainfuck, 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.)

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?

Thanks.

+3  A: 

Have a look at JHighlighter or 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.

Not a direct answer but, if client-side syntax highlighting is an option, the SyntaxHighlighter library from Alex Gorbatchev is an awesome javascript library, supports lots of languages and is highly extensible.

Pascal Thivent
Nice links. Thanks.
Ahmet Alp Balkan
I prefer the SyntaxHighlighter - works great, keeps the work out of server, lowers the traffic, leaves the code intact for search engines.
Ondra Žižka
@Ondra I totally agree with you. Actually, SyntaxHighlighter is so great that it became a kind of de facto standard on the web nowadays.
Pascal Thivent
+4  A: 

Two related questions:

http://stackoverflow.com/questions/221570/what-code-highlighting-libs-are-there-for-java http://stackoverflow.com/questions/864688/where-can-i-find-a-syntax-highlighting-library-for-java

And one library I found: http://colorer.sourceforge.net/

Bozho
All these have a small number of languages. :( Thanks anyway.
Ahmet Alp Balkan
the last link claims: Colorer-take5 supports more than hundred programming languages, scripts and markups. I haven't checked though
Bozho
+1  A: 

You could use Pygments through Jython. Won't be as fast as a Java solution, but much faster than interacting with a remote server.

Barring that, you could run Geshi locally and pipe source code through it, that would also beat an HTTP round trip.

orip
Nice solution. thanks.
Ahmet Alp Balkan
A: 

jedit is 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:

  • 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
mode.setProperty("file","modes/xml.xml");

to

mode.setProperty("file","src/modes/xml.xml");
  • Run. Copy and paste an XML into the editor and see the syntax highlighting is working.
tukushan
that is a nice idea. thanks.
Ahmet Alp Balkan