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

« Newer Snippets
Older Snippets »
Showing 11-13 of 13 total

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

Syntax Highlighting with enscript

enscript --color --language=html -Eruby --output=dest.html file.rb


You may need to obtain ruby.st for Ruby syntax highlighting:

$ cd /usr/share/enscript/hl
$ sudo curl -O http://java.thn.htu.se/~toor/blog-ng/wp-content/ruby.st


Like any good UNIX program, you can also use pipes:

$ find . -name '*.rb' | xargs cat | enscript --color --language=html -Eruby --output=-

Python syntax coloring for series60

The new Text() control allows rich text which can be
used for syntax coloring
(screenshot)
http://photos22.flickr.com/30187174_8598b800ec_o.jpg
from appuifw import *
from pyfontify import fontify
import os, e32

src = ur"""
__version__ = "0.4"
import string, re, keyword  
commentPat = "#.*"  
# Build up a regular expression
pat = "q[^\q\n]*(\\\\[\000-\377][^\q\n]*)*q"
"""

color = { 'keyword': 0x0000ff,
          'string': 0xff00ff,
          'comment': 0x008000,
          'function': 0x008080,
          'class': 0x008080 }

t = Text()
pos = 0
for tag, start, end, sl in fontify(src):
    t.color = 0
    t.add(src[pos:start])
    t.color = color[tag]
    t.add(src[start:end])
    pos = end
t.color = 0
t.add(src[pos:])

app.body = t
e32.ao_sleep(10)

You first need to install the pyfontify module from here
http://linux.duke.edu/~mstenner/free-docs/diveintopython-3.9-1/py/pyfontify.py
« Newer Snippets
Older Snippets »
Showing 11-13 of 13 total