Class: Rouge::Formatters::Terminal256

Inherits:
Rouge::Formatter show all
Defined in:
lib/rouge/formatters/terminal256.rb

Overview

A formatter for 256-color terminals

Defined Under Namespace

Classes: EscapeSequence

Constant Summary

Constants inherited from Rouge::Formatter

Rouge::Formatter::REGISTRY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Rouge::Formatter

find, format, #format, #render, tag

Constructor Details

#initialize(opts = {}) ⇒ Terminal256

Returns a new instance of Terminal256.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :theme (Object) — default: default is thankful_eyes

    the theme to render with.



13
14
15
16
# File 'lib/rouge/formatters/terminal256.rb', line 13

def initialize(opts={})
  @theme = opts[:theme] || 'thankful_eyes'
  @theme = Theme.find(@theme) if @theme.is_a? String
end

Instance Attribute Details

#themeObject (readonly)



8
9
10
# File 'lib/rouge/formatters/terminal256.rb', line 8

def theme
  @theme
end

Instance Method Details

#escape_sequence(token) ⇒ Object

private



151
152
153
154
155
# File 'lib/rouge/formatters/terminal256.rb', line 151

def escape_sequence(token)
  @escape_sequences ||= {}
  @escape_sequences[token.name] ||=
    EscapeSequence.new(get_style(token))
end

#get_style(token) ⇒ Object



157
158
159
160
161
# File 'lib/rouge/formatters/terminal256.rb', line 157

def get_style(token)
  return text_style if token == Token::Tokens::Text

  theme.get_own_style(token) || text_style
end

#stream(tokens, &b) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/rouge/formatters/terminal256.rb', line 18

def stream(tokens, &b)
  tokens.each do |tok, val|
    escape = escape_sequence(tok)
    yield escape.style_string
    yield val
    yield escape.reset_string
  end
end

#text_styleObject



163
164
165
166
167
168
# File 'lib/rouge/formatters/terminal256.rb', line 163

def text_style
  style = theme.get_style(Token['Text'])
  # don't highlight text backgrounds
  style.delete :bg
  style
end