Title / Description
Code 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!"')
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code