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.



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

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

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

Format a token stream. Delegates to #format.



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

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



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.



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

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.



39
40
41
42
# File 'lib/rouge/formatter.rb', line 39

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



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

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