Never been to DZone Snippets before?

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

About this user

Brad Phelan http://xtargets.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

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.

   1  
   2         def  vimsyn(text, filetype)
   3             synfile = Tempfile.new('synfile')
   4             synfile.close
   5             codefile = Tempfile.new('codefile')
   6             codefile << text
   7             codefile.close
   8  
   9             # filetype="matlab"
  10  
  11             expr = %Q%/usr/local/bin/vim -f -n -X -e -s -c \
  12                 "set filetype=#{filetpe}" \
  13                 -c "syntax on" \
  14                 -c "set background=dark" \
  15                 -c "let html_use_css=1" \
  16                 -c "run syntax/2html.vim" \
  17                 -c "wq! #{synfile.path}" -c "q" \ 
  18                 #{codefile.path}%
  19  
  20             `#{expr}`
  21             html = IO.readlines(synfile.path).join
  22             body = html.match(/<body.*<\/body>/m)[0]
  23             css  = html.match(/<style.*<\/style>/m)[0]
  24             css.gsub!(/pre/,'pre.code')
  25             css.gsub!(/^body.*$/,'')
  26             body.gsub!(/^<body/,'<div')
  27             body.gsub!(/<\/body>/,'<div>')                                        
  28             body.gsub!(/<pre>/,'<pre class=code>')
  29  
  30             return css+body
  31         end
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS