class Rouge::Formatter

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

Constants

REGISTRY

@private

Public Class Methods

disable_escape!() click to toggle source
# File lib/rouge/formatter.rb, line 38
def self.disable_escape!
  @escape_enabled = false
  Thread.current[:'rouge/with-escape'] = false
end
enable_escape!() click to toggle source
# File lib/rouge/formatter.rb, line 34
def self.enable_escape!
  @escape_enabled = true
end
escape_enabled?() click to toggle source
# File lib/rouge/formatter.rb, line 30
def self.escape_enabled?
  !!(((defined? @escape_enabled) && @escape_enabled) || Thread.current[:'rouge/with-escape'])
end
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 44
def self.format(tokens, *a, &b)
  new(*a).format(tokens, &b)
end
new(opts={}) click to toggle source
# File lib/rouge/formatter.rb, line 48
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
with_escape() { || ... } click to toggle source
# File lib/rouge/formatter.rb, line 23
def self.with_escape
  Thread.current[:'rouge/with-escape'] = true
  yield
ensure
  Thread.current[:'rouge/with-escape'] = false
end

Public Instance Methods

escape?(tok) click to toggle source
# File lib/rouge/formatter.rb, line 52
def escape?(tok)
  tok == Token::Tokens::Escape
end
filter_escapes(tokens) { |Error, v| ... } click to toggle source
# File lib/rouge/formatter.rb, line 56
def filter_escapes(tokens)
  tokens.each do |t, v|
    if t == Token::Tokens::Escape
      yield Token::Tokens::Error, v
    else
      yield t, v
    end
  end
end
format(tokens, &b) click to toggle source

Format a token stream.

# File lib/rouge/formatter.rb, line 67
def format(tokens, &b)
  tokens = enum_for(:filter_escapes, tokens) unless Formatter.escape_enabled?

  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 79
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 86
def stream(tokens, &b)
  raise 'abstract'
end

Protected Instance Methods

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

  out = []
  tokens.each do |tok, val|
    val.scan %r/\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