class Rouge::Formatter

A Formatter takes a token stream and formats it for human viewing.

Constants

REGISTRY

@private

Public Class Methods

find(tag) click to toggle source

Find a formatter class given a unique tag.

# File lib/rouge/formatter.rb, line 19
def self.find(tag)
  REGISTRY[tag]
end
format(tokens, *a, &b) click to toggle source

Format a token stream. Delegates to {#format}.

# File lib/rouge/formatter.rb, line 24
def self.format(tokens, *a, &b)
  new(*a).format(tokens, &b)
end
new(opts={}) click to toggle source
# File lib/rouge/formatter.rb, line 28
def initialize(opts={})
  # pass
end
tag(tag=nil) click to toggle source

Specify or get the unique tag for this formatter. This is used for specifying a formatter in `rougify`.

# File lib/rouge/formatter.rb, line 11
def self.tag(tag=nil)
  return @tag unless tag
  REGISTRY[tag] = self

  @tag = tag
end

Public Instance Methods

format(tokens, &b) click to toggle source

Format a token stream.

# File lib/rouge/formatter.rb, line 33
def format(tokens, &b)
  return stream(tokens, &b) if block_given?

  out = String.new('')
  stream(tokens) { |piece| out << piece }

  out
end
render(tokens) click to toggle source

@deprecated Use {#format} instead.

# File lib/rouge/formatter.rb, line 43
def render(tokens)
  warn 'Formatter#render is deprecated, use #format instead.'
  format(tokens)
end
stream(tokens, &b) click to toggle source

@abstract yield strings that, when concatenated, form the formatted output

# File lib/rouge/formatter.rb, line 50
def stream(tokens, &b)
  raise 'abstract'
end

Protected Instance Methods

token_lines(tokens) { |out| ... } click to toggle source
# File lib/rouge/formatter.rb, line 55
def token_lines(tokens, &b)
  return enum_for(:token_lines, tokens) unless block_given?

  out = []
  tokens.each do |tok, val|
    val.scan /\n|[^\n]+/ do |s|
      if s == "\n"
        yield out
        out = []
      else
        out << [tok, s]
      end
    end
  end

  # for inputs not ending in a newline
  yield out if out.any?
end