Class: CodeRay::Duo

### Home Classes Methods

### In Files

- lib/coderay/duo.rb

### Parent

Object

### Methods

- ::[]
- ::new
- #call
- #encode
- #encoder
- #highlight
- #scanner

### Class/Module Index

Quicksearch

- CodeRay
- CodeRay::Duo
- CodeRay::Encoders
- CodeRay::Encoders::CommentFilter
- CodeRay::Encoders::Count
- CodeRay::Encoders::Debug
- CodeRay::Encoders::DebugLint
- CodeRay::Encoders::Div
- CodeRay::Encoders::Encoder
- CodeRay::Encoders::Filter
- CodeRay::Encoders::HTML
- CodeRay::Encoders::HTML::Output
- CodeRay::Encoders::JSON
- CodeRay::Encoders::LinesOfCode
- CodeRay::Encoders::Lint
- CodeRay::Encoders::Null
- CodeRay::Encoders::Page
- CodeRay::Encoders::Span
- CodeRay::Encoders::Statistic
- CodeRay::Encoders::Terminal
- CodeRay::Encoders::Text
- CodeRay::Encoders::TokenKindFilter
- CodeRay::Encoders::XML
- CodeRay::Encoders::YAML
- CodeRay::FileType
- CodeRay::ForRedCloth
- CodeRay::Plugin
- CodeRay::PluginHost
- CodeRay::Scanners
- CodeRay::Scanners::C
- CodeRay::Scanners::CPlusPlus
- CodeRay::Scanners::CSS
- CodeRay::Scanners::Clojure
- CodeRay::Scanners::Debug
- CodeRay::Scanners::Delphi
- CodeRay::Scanners::Diff
- CodeRay::Scanners::ERB
- CodeRay::Scanners::Go
- CodeRay::Scanners::Groovy
- CodeRay::Scanners::HAML
- CodeRay::Scanners::HTML
- CodeRay::Scanners::JSON
- CodeRay::Scanners::Java
- CodeRay::Scanners::JavaScript
- CodeRay::Scanners::Python
- CodeRay::Scanners::Raydebug
- CodeRay::Scanners::Ruby
- CodeRay::Scanners::SQL
- CodeRay::Scanners::Sass
- CodeRay::Scanners::Scanner
- CodeRay::Scanners::Taskpaper
- CodeRay::Scanners::Text
- CodeRay::Scanners::XML
- CodeRay::Scanners::YAML
- CodeRay::Styles
- CodeRay::Styles::Alpha
- CodeRay::Styles::Style
- CodeRay::Tokens
- CodeRay::TokensProxy
- CodeRay::WordList
- CodeRay::WordList::CaseIgnoring

No matching classes.

# CodeRay::Duo

# Duo

A Duo is a convenient way to use CodeRay. You just create a Duo, giving it a lang (language of the input code) and a format (desired output format), and call Duo#highlight with the code.

Duo makes it easy to re-use both scanner and encoder for a repetitive task. It also provides a very easy interface syntax:

    require 'coderay'
    CodeRay::Duo[:python, :div].highlight 'import this'

Until you want to do uncommon things with CodeRay, I recommend to use this method, since it takes care of everything.

### Attributes

format[RW]
lang[RW]
options[RW]

### Public Class Methods

[](lang = nil, format = nil, options = {}) click to toggle source

To allow calls like Duo[:ruby, :html].highlight.

Alias for: new
new(lang = nil, format = nil, options = {}) click to toggle source

Create a new Duo, holding a lang and a format to highlight code.

simple:

    CodeRay::Duo[:ruby, :html].highlight 'bla 42'

with options:

    CodeRay::Duo[:ruby, :html, :hint => :debug].highlight '????::??'

alternative syntax without options:

    CodeRay::Duo[:ruby => :statistic].encode 'class << self; end'

alternative syntax with options:

    CodeRay::Duo[{ :ruby => :statistic }, :do => :something].encode 'abc'

The options are forwarded to scanner and encoder (see CodeRay.get_scanner_options).

    # File lib/coderay/duo.rb, line 37
    def initialize lang = nil, format = nil, options = {}
      if format.nil? && lang.is_a?(Hash) && lang.size == 1
        @lang = lang.keys.first
        @format = lang[@lang]
      else
        @lang = lang
        @format = format
      end
      @options = options
    end

Also aliased as: []

### Public Instance Methods

call(code, options = {}) click to toggle source

Allows to use Duo like a proc object:

    CodeRay::Duo[:python => :yaml].call(code)

or, in Ruby 1.9 and later:

    CodeRay::Duo[:python => :yaml].(code)

Alias for: encode
encode(code, options = {}) click to toggle source

Tokenize and highlight the code using `scanner` and `encoder`.

    # File lib/coderay/duo.rb, line 64
    def encode code, options = {}
      options = @options.merge options
      encoder.encode(code, @lang, options)
    end

Also aliased as: highlight, call
encoder() click to toggle source

The encoder of the duo. Only created once.

    # File lib/coderay/duo.rb, line 59
    def encoder
      @encoder ||= CodeRay.encoder @format, @options
    end

highlight(code, options = {}) click to toggle source
Alias for: encode
scanner() click to toggle source

The scanner of the duo. Only created once.

    # File lib/coderay/duo.rb, line 54
    def scanner
      @scanner ||= CodeRay.scanner @lang, CodeRay.get_scanner_options(@options)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.