class GHI::Formatting::Colors::Pygmentizer

Public Class Methods

new() click to toggle source
# File lib/ghi/formatting/colors.rb, line 324
def initialize
  @style = GHI.config('ghi.highlight.style') || 'monokai'
end

Public Instance Methods

highlight(code_block) click to toggle source
# File lib/ghi/formatting/colors.rb, line 328
def highlight(code_block)
  begin
    indent = code_block['indent']
    lang   = code_block['lang']
    code   = code_block['code']

    if lang != ""
      output = pygmentize(lang, code)
    else
      output = code
    end
    with_indentation(output, indent)
  rescue
    code_block
  end
end

Private Instance Methods

pygmentize(lang, code) click to toggle source
# File lib/ghi/formatting/colors.rb, line 347
def pygmentize(lang, code)
  Pygments.highlight(unescape(code), :formatter => '256', :lexer => lang,
                     :options => { :style => @style })
end
unescape(str) click to toggle source
# File lib/ghi/formatting/colors.rb, line 352
def unescape(str)
  str.gsub(/\e\[[^m]*m/, '')
end
with_indentation(string, indent) click to toggle source
# File lib/ghi/formatting/colors.rb, line 356
def with_indentation(string, indent)
  string.each_line.map do |line|
    "#{indent}#{line}"
  end.join
end