Class: Stencil::TermStyleDirective

Inherits:
Block show all
Defined in:
lib/stencil/directives/term-style.rb

Direct Known Subclasses

Bold, ForegroundColor, Inverse, Underline

Instance Method Summary collapse

Methods inherited from Block

#add, #ended, #inspect, #parsed

Methods inherited from Directive

#checked_render, create, #inspect, #inspect_args, #interpret, #parsed, #postrender, #pre_end, #prerender, register, #render_end, #setup_parameters

Constructor Details

#initialize(location, string) ⇒ TermStyleDirective

Returns a new instance of TermStyleDirective.



57
58
59
60
61
# File 'lib/stencil/directives/term-style.rb', line 57

def initialize(location, string)
  @old_style = nil
  @style = nil
  super
end

Instance Method Details

#code_for(kind, name) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/stencil/directives/term-style.rb', line 63

def code_for(kind, name)
  if kind.has_key?(name.to_s)
    "\e[#{kind[name.to_s]}m"
  else
    ""
  end
end

#escape_codes(new_options, old_options) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/stencil/directives/term-style.rb', line 71

def escape_codes(new_options, old_options)
  return "\e[0m" if new_options.empty?
  options = new_options.dup
  old_options.each_pair do |key, value|
    if options[key] == value
      options.delete(key)
    end
  end

  options.keys.inject("") do |codes, key|
    codes + code_for(Styles[key], options[key])
  end
end

#old_style(state) ⇒ Object



85
86
# File 'lib/stencil/directives/term-style.rb', line 85

def old_style(state)
end

#render(state) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/stencil/directives/term-style.rb', line 91

def render(state)
  @old_style = state.terminal_style
  @style = update_style(@old_style, state.data)
  state.terminal_style = @style
  result = 
    [escape_codes(@style, @old_style)] + 
    super + 
    [escape_codes(@old_style, @style)]
  state.terminal_style = @old_style
  result
end

#style(state) ⇒ Object



88
89
# File 'lib/stencil/directives/term-style.rb', line 88

def style(state)
end

#update_style(style, state) ⇒ Object



103
104
105
# File 'lib/stencil/directives/term-style.rb', line 103

def update_style(style, state)
  return style
end