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

Instance Method Summary collapse

Methods inherited from Rouge::Formatter

disable_escape!, enable_escape!, #escape?, escape_enabled?, #filter_escapes, find, #format, format, #render, tag, #token_lines, with_escape

Constructor Details

#initialize(theme = Themes::ThankfulEyes.new) ⇒ Terminal256


15
16
17
18
19
20
21
22
23
# File 'lib/rouge/formatters/terminal256.rb', line 15

def initialize(theme = Themes::ThankfulEyes.new)
  if theme.is_a?(Rouge::Theme)
    @theme = theme
  elsif theme.is_a?(Hash)
    @theme = theme[:theme] || Themes::ThankfulEyes.new
  else
    raise ArgumentError, "invalid theme: #{theme.inspect}"
  end
end

Instance Method Details

#escape_sequence(token) ⇒ Object

private


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

def escape_sequence(token)
  return '' if escape?(token)
  @escape_sequences ||= {}
  @escape_sequences[token.qualname] ||=
    EscapeSequence.new(get_style(token))
end

#get_style(token) ⇒ Object


168
169
170
171
172
# File 'lib/rouge/formatters/terminal256.rb', line 168

def get_style(token)
  return text_style if token.ancestors.include? Token::Tokens::Text

  theme.get_own_style(token) || text_style
end

#stream(tokens, &b) ⇒ Object


25
26
27
28
29
30
31
32
# File 'lib/rouge/formatters/terminal256.rb', line 25

def stream(tokens, &b)
  tokens.each do |tok, val|
    escape = escape_sequence(tok)
    yield escape.style_string
    yield val.gsub("\n", "#{escape.reset_string}\n#{escape.style_string}")
    yield escape.reset_string
  end
end

#text_styleObject


174
175
176
177
178
179
# File 'lib/rouge/formatters/terminal256.rb', line 174

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