views:

616

answers:

8

I'm looking for an open source, cross platform (Windows & Linux at least) command line tool to take some code (C++, but multiple languages would be sweet), and spit out valid a XHTML representation of that code, with syntax highlighting included.

Ideally the XHTML should just wrap the code with <span> and <div> tags with different classes so I can supply the CSS code and change the colouration, but that's an optional extra.

Does anyone know of such an application?

+2  A: 

Vim can save any code it highlights to "colored" HTML (it runs on several platforms). There is GNU hightlight too. And tons of others.

PW
can you give an example how to do it in vim ?
Ilya
open your file in gvimthen <ESC> :syn on:TOhtmlthe vim screen split, one buffer for the html, one for your original file. Then you save the html rendered file and you're done.
PW
A: 

Not sure how helpful this will be, but my team uses doxygen to produce documentation, which happens to provide color syntax highlighting on our code views as well as a side bonus. Never really needed it, but it does it.

f4nt
+3  A: 

There is very good one, driven by XML, fast and opensource: http://sourceforge.net/projects/colorer/

Sergey Skoblikov
Being driven by XML is only a feature in the Java world... :-)
Martin Geisler
+2  A: 

I don't recall if GeSHi has a command-line program but even if it doesn't, it shouldn't be hard to whip one up. It does a great job of taking code and generating pretty, coloured HTML/XHTML, even with line numbers (or every X line numbers, even) and other helpful features.

Chris Charabaruk
A: 

If you're ok with using ruby, you want coderay.

Subimage
+1  A: 

Enscript looks like what you are asking for :

  1. spit HTML (or PS, or RTF) from ascii files
  2. It includes features for `pretty-printing' (language-sensitive code highlighting) in several programming languages.
shodanex
+6  A: 

I can recommend Pygments. It's easy to work with and supports a lot of languages. It does what you want, i.e., it wraps the code in <span> tags:

from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter

code = 'print "Hello World"'
print highlight(code, PythonLexer(), HtmlFormatter())

gives

<div class="highlight">
<pre><span class="k">print</span> <span class="s">&quot;Hello World&quot;</span></pre>
</div>

and you can then use one of the supplied style sheets of make your own.

You can also call it via it's pygmentize script. The script can format the output in different ways: HTML, LaTeX, ANSI color terminal output.

Martin Geisler
I ended up using pygments - it's simply brilliant! Thanks a lot for your suggestion.
Thomi
A: 

I'll add my own one to the list, it colors C# but could be adapted for C, C++ and Java. It produces the inline styles by default and a pre tag.

The source is there in C#, you'll need to grab mono/monodevelop and compile it as as a console app, so it's not shrink wrapped in that respect.

Chris S