views:

70

answers:

3

Hi.

I'm looking for a command line tool (or Perl module or VIM script or whatever) that will take some input files (such as XML or JavaScript files) and format them in HTML. I specifically want my output not to contain stuff like <span style="color: red"> or <font color=red> according to a particular colour scheme, rather it should use CSS class names to mark up the different syntactic parts of the file.

For example, if I had this file as input:

function f(x) {
  return x + 1;
}

the kind of output I would like is:

<pre><span class=keyword>function</span> <span class=ident>f</span><span class=punc>{</span>
  <span class=keyword>return</span> <span class=ident>x</span> <span class=op>+</span> <span class=numliteral>1</span><span class=punc>;</span>
<span class=punc>}</span></pre>

Does anyone know of such a tool?

Something like VIM's 2html.vim script, but outputting class="" attributes with the syntax highlight group names (like "Constant", "Identifier", "Statement", etc.) would be ideal.

Thanks,

Cameron

+1  A: 

You can feed a file into GeSHi using PHP on the command line (or cURL your own local server or some other hack)

http://qbnz.com/highlighter/geshi-doc.html#basic-usage

davidosomething
Thanks, that looks promosing. I'm just installing PHP now to give it a try.
heycam
OK, this is working well for me, thanks!
heycam
+1  A: 

There is buf2html.vim. Unfortunately, it uses non-semantic class names: See http://intrepid.perlmonk.org/apropos.vim/buf2html/current/myself.html

Sinan Ünür
Indeed, I neglected to mention in my question that I came across buf2html and gave it a go. Probably it shouldn't be hard to modify the script to use the VIM syntax highlight group name instead of the "cNNN" class names, but I'm not familiar with VIM scripting unfortunately.
heycam
+1  A: 

I think this is exacly what Vim's :TOhtml does if you

:let html_use_css = 1

Original:

function f(x) {
    return x + 1;
}

output:

<pre>
<span class="Identifier">function</span> f(<span class="">x</span><span class="javaScriptParens">)</span><span class=""> </span><span class="Identifier">{</span>
  <span class="Statement">return</span><span class=""> x + </span>1<span class="">;</span>
<span class="Identifier">}</span>
</pre>
kemp
That looks good too, thanks! I'll keep it in mind if buf2html doesn't work out for me.
heycam