Class: Rouge::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rouge/formatter.rb

Overview

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

Constant Summary collapse

REGISTRY =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Formatter

Returns a new instance of Formatter.



29
30
31
# File 'lib/rouge/formatter.rb', line 29

def initialize(opts={})
  # pass
end

Class Method Details

.find(tag) ⇒ Object

Find a formatter class given a unique tag.



20
21
22
# File 'lib/rouge/formatter.rb', line 20

def self.find(tag)
  REGISTRY[tag]
end

.format(tokens, *a, &b) ⇒ Object

Format a token stream. Delegates to #format.



25
26
27
# File 'lib/rouge/formatter.rb', line 25

def self.format(tokens, *a, &b)
  new(*a).format(tokens, &b)
end

.tag(tag = nil) ⇒ Object

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



12
13
14
15
16
17
# File 'lib/rouge/formatter.rb', line 12

def self.tag(tag=nil)
  return @tag unless tag
  REGISTRY[tag] = self

  @tag = tag
end

Instance Method Details

#format(tokens, &b) ⇒ Object

Format a token stream.



34
35
36
37
38
39
40
41
# File 'lib/rouge/formatter.rb', line 34

def format(tokens, &b)
  return stream(tokens, &b) if block_given?

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

  out
end

#render(tokens) ⇒ Object

Deprecated.

Use #format instead.



44
45
46
47
# File 'lib/rouge/formatter.rb', line 44

def render(tokens)
  warn 'Formatter#render is deprecated, use #format instead.'
  format(tokens)
end

#stream(tokens, &b) ⇒ Object

This method is abstract.

yield strings that, when concatenated, form the formatted output



51
52
53
# File 'lib/rouge/formatter.rb', line 51

def stream(tokens, &b)
  raise 'abstract'
end