Over a million developers have joined DZone.

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(//m)[0]
           css  = html.match(//m)[0]
           css.gsub!(/pre/,'pre.code')
           css.gsub!(/^body.*$/,'')
           body.gsub!(/^/,'
') body.gsub!(/
/,'
')

           return css+body
       end
Topics:

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

{{ parent.tldr }}

{{ parent.urlSource.name }}