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

Class Method Details

.find(tag) ⇒ Object

Find a formatter class given a unique tag.


17
18
19
# File 'lib/rouge/formatter.rb', line 17

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

.format(tokens, opts = {}, &b) ⇒ Object

Format a token stream. Delegates to #format.


22
23
24
# File 'lib/rouge/formatter.rb', line 22

def self.format(tokens, opts={}, &b)
  new(opts).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`.


9
10
11
12
13
14
# File 'lib/rouge/formatter.rb', line 9

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.


27
28
29
30
31
32
33
34
# File 'lib/rouge/formatter.rb', line 27

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.


37
38
39
40
# File 'lib/rouge/formatter.rb', line 37

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


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

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