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.



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

def initialize(opts={})
  # pass
end

Class Method Details

.disable_escape!Object



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

def self.disable_escape!
  @escape_enabled = false
  Thread.current[:'rouge/with-escape'] = false
end

.enable_escape!Object



35
36
37
# File 'lib/rouge/formatter.rb', line 35

def self.enable_escape!
  @escape_enabled = true
end

.escape_enabled?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/rouge/formatter.rb', line 31

def self.escape_enabled?
  !!(((defined? @escape_enabled) && @escape_enabled) || Thread.current[:'rouge/with-escape'])
end

.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, *args, **kwargs, &b) ⇒ Object

Format a token stream. Delegates to #format.



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

def self.format(tokens, *args, **kwargs, &b)
  new(*args, **kwargs).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

.with_escapeObject



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

def self.with_escape
  Thread.current[:'rouge/with-escape'] = true
  yield
ensure
  Thread.current[:'rouge/with-escape'] = false
end

Instance Method Details

#escape?(tok) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/rouge/formatter.rb', line 53

def escape?(tok)
  tok == Token::Tokens::Escape
end

#filter_escapes(tokens) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/rouge/formatter.rb', line 57

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) ⇒ Object

Format a token stream.



68
69
70
71
72
73
74
75
76
77
# File 'lib/rouge/formatter.rb', line 68

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) ⇒ Object

Deprecated.

Use #format instead.



80
81
82
83
# File 'lib/rouge/formatter.rb', line 80

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



87
88
89
# File 'lib/rouge/formatter.rb', line 87

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