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.


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

def initialize(opts={})
  # pass
end

Class Method Details

.find(tag) ⇒ Object

Find a formatter class given a unique tag.


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

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

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

Format a token stream. Delegates to #format.


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

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`.


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

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.


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

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

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

  out
end

#render(tokens) ⇒ Object

Deprecated.

Use #format instead.


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

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


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

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