Syntax highlighting with vim & Ruby
This snippet will convert source code files to syntax-colored html files.
usage: vim-syntax-highlighting.rb src.rb src.html
1 2 3 require 'tempfile' 4 5 $VERBOSE=nil 6 STDERR.reopen(Tempfile::new($$)) unless STDIN.tty? 7 8 fin, fout, _ = ARGV 9 fin = ((fin.nil? or fin == '-') ? STDIN : open(fin)) 10 fout = ((fout.nil? or fout == '-') ? STDOUT : open(fout,'w+')) 11 12 ts = Tempfile::new($$), Tempfile::new($$) 13 ts[0].write fin.read 14 ts.each{|t| t.close} 15 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 ) 16 #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 ) 17 system command 18 ts.each{|t| t.open; t.rewind} 19 fout.write(ts[1].read) 20 ts.each{|t| t.close!} 21