Over a million developers have joined DZone.

Syntax Highlighting With Vim & Ruby

·
From: http://www.ruby-forum.com/topic/64262

This snippet will convert source code files to syntax-colored html files.
usage:  vim-syntax-highlighting.rb src.rb src.html




require 'tempfile'

$VERBOSE=nil
STDERR.reopen(Tempfile::new($$)) unless STDIN.tty?

fin, fout, _ = ARGV
fin = ((fin.nil? or fin == '-') ? STDIN : open(fin))
fout = ((fout.nil? or fout == '-') ? STDOUT : open(fout,'w+'))

ts = Tempfile::new($$), Tempfile::new($$)
ts[0].write fin.read
ts.each{|t| t.close}
command = %Q( vim -f +'syn on' +'set filetype=ruby' +'set background=light' +'run! syntax/2html.vim' +'w! #{ ts[1].path }' +'qa!' - < #{ ts[0].path } > /dev/null 2>&1 )
#command = %Q( vim -f +'syn on' +'set filetype=c' +'set background=dark' +'run! syntax/2html.vim' +'w! #{ ts[1].path }' +'qa!' - < #{ ts[0].path } > /dev/null 2>&1 )
system command
ts.each{|t| t.open; t.rewind}
fout.write(ts[1].read)
ts.each{|t| t.close!}

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}