Class: Rouge::Formatters::Terminal256

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

Overview

A formatter for 256-color terminals

Direct Known Subclasses

TerminalTruecolor

Defined Under Namespace

Classes: EscapeSequence, Unescape

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

Returns a new instance of Terminal256.

Parameters:

  • theme (Hash, Rouge::Theme) (defaults to: Themes::ThankfulEyes.new)

    the theme to render with.



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



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

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

#get_style(token) ⇒ Object



184
185
186
187
188
# File 'lib/rouge/formatters/terminal256.rb', line 184

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

  theme.get_own_style(token) || text_style
end

#make_escape_sequence(style) ⇒ Object



180
181
182
# File 'lib/rouge/formatters/terminal256.rb', line 180

def make_escape_sequence(style)
  EscapeSequence.new(style)
end

#stream(tokens, &b) ⇒ Object



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

def stream(tokens, &b)
  tokens.each do |tok, val|
    escape_sequence(tok).stream_value(val, &b)
  end
end

#text_styleObject



190
191
192
193
194
195
# File 'lib/rouge/formatters/terminal256.rb', line 190

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