test
Plain text
code posted
created at 21 Jun 04:57, updated at 23 Apr 15:23
Edit
|
Back
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
require 'rubygems'
require 'coderay'
# output as HTML div (using inline CSS styles)
puts CodeRay.scan('puts "Hello, world!"', :ruby).div
# ...with line numbers
puts CodeRay.scan("5.times do\n puts 'Hello, world!'\nend", :ruby).div(:line_numbers => :table)
# output as standalone HTML page (using CSS classes)
puts CodeRay.scan('puts "Hello, world!"', :ruby).page
# keep scanned tokens for later use
tokens = CodeRay.scan('{ "just": "an", "example": 42 }', :json)
# produce a token statistic
puts tokens.statistic
# count the tokens
tokens.count # => 26
# produce a HTML div, but with CSS classes
puts tokens.div(:css => :class)
# highlight a file (HTML div); guess the file type base on the extension
# puts CodeRay.highlight_file('foo.erb.html')
# get a new scanner for Python
python_scanner = CodeRay.scanner :python
# get a new encoder for terminal
terminal_encoder = CodeRay.encoder :term
# scanning into tokens
tokens = python_scanner.tokenize 'import this; # The Zen of Python'
# format the tokens
puts terminal_encoder.encode_tokens(tokens)
# re-using scanner and encoder
ruby_highlighter = CodeRay::Duo[:ruby, :div]
puts ruby_highlighter.encode('puts "Hello, world!"') |
1.2 KB in 2 ms with coderay