DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Using VIM As A Syntax Highlighting Engine From Ruby
You can use VIM as a syntax highlighting engine for code fragments in your blogs etc. The below code snippet works sort of, but it is slow for me. Not sure at the moment whether it is VIM or something else. Perhapps others can try it out and suggest speed or other improvments.
def vimsyn(text, filetype)
synfile = Tempfile.new('synfile')
synfile.close
codefile = Tempfile.new('codefile')
codefile << text
codefile.close
# filetype="matlab"
expr = %Q%/usr/local/bin/vim -f -n -X -e -s -c \
"set filetype=#{filetpe}" \
-c "syntax on" \
-c "set background=dark" \
-c "let html_use_css=1" \
-c "run syntax/2html.vim" \
-c "wq! #{synfile.path}" -c "q" \
#{codefile.path}%
`#{expr}`
html = IO.readlines(synfile.path).join
body = html.match(/<body.*<\/body>/m)[0]
css = html.match(/<style.*<\/style>/m)[0]
css.gsub!(/pre/,'pre.code')
css.gsub!(/^body.*$/,'')
body.gsub!(/^<body/,'<div')
body.gsub!(/<\/body>/,'<div>')
body.gsub!(/<pre>/,'<pre class=code>')
return css+body
end






Comments
Snippets Manager replied on Wed, 2006/10/25 - 11:41pm
irb(main):008:0> vimsyn(open('vimsyn.rb').readlines, 'ruby') sh: line 2: /tmp/codefile.5090.1: Permission denied => "\npre.code { color: #ffffff; background-color: #000000; }\n\n\n\n\n"use "system()" instead of "`#{expr}`" works on my environment.