class Octopress::Pygments::Renderer

Attributes

code[RW]
lang[RW]
options[RW]

Public Class Methods

new(code, options = {}) click to toggle source
# File lib/octopress-pygments/renderer.rb, line 6
def initialize(code, options = {})
  @code    = code
  @options = options.delete_if { |k,v| v.nil? }
  @options = DEFAULTS.merge(@options)
  @aliases = @options[:aliases]
  @aliases = (@aliases ? stringify_keys(@aliases) : {})
  @lang    = determine_lang(@options[:lang])
end

Public Instance Methods

captionize(caption, url, link_text) click to toggle source
# File lib/octopress-pygments/renderer.rb, line 60
def captionize (caption, url, link_text)
  figcaption  = "<figcaption class='pygments-code-caption'><span class='pygments-code-caption-title'>#{caption}</span>"
  figcaption += "<a class='pygments-code-caption-link' href='#{url}'>#{(link_text || 'link').strip}</a>" if url
  figcaption += "</figcaption>"
end
determine_lang(lang) click to toggle source
# File lib/octopress-pygments/renderer.rb, line 34
def determine_lang(lang)
  if lang == ''
    lang = 'plain'
  elsif ::Pygments::Lexer.find lang 
    lang
  elsif !@aliases[lang].nil? and ::Pygments::Lexer.find @aliases[lang]
    @aliases[lang]
  else
    'plain'
  end
end
encode_liquid(code) click to toggle source
# File lib/octopress-pygments/renderer.rb, line 101
def encode_liquid(code)
  code.gsub(/{{/, '&#x7b;&#x7b;')
    .gsub(/{%/, '&#x7b;&#x25;')
end
get_range(code, start, endline) click to toggle source

Public:

# File lib/octopress-pygments/renderer.rb, line 89
def get_range(code, start, endline)
  length    = code.lines.count
  start
  endline ||= length
  if start > 1 or endline < length
    raise "#{filepath} is #{length} lines long, cannot begin at line #{start}" if start > length
    raise "#{filepath} is #{length} lines long, cannot read beyond line #{endline}" if endline > length
    code = code.split(/\n/).slice(start - 1, endline + 1 - start).join("\n")
  end
  code
end
highlight() click to toggle source
# File lib/octopress-pygments/renderer.rb, line 15
def highlight
  @options[:title] ||= ' ' if @options[:url]
  if cache = Cache.read_cache(code, @options)
    cache
  else
    if @lang == 'plain'
      rendered_code = code.to_s.gsub('<','&lt;')
    else
      rendered_code = render_pygments(code, @lang).match(/<pre>(.+)<\/pre>/m)[1].gsub(/ *$/, '') #strip out divs <div class="highlight">
    end
    rendered_code = tableize_code(rendered_code, @lang, {linenos: @options[:linenos], start: @options[:start], marks: @options[:marks]})
    title = captionize(@options[:title], @options[:url], @options[:link_text]) if @options[:title]
    rendered_code = "<figure class='pygments-code-figure'>#{title}#{rendered_code}</figure>"
    rendered_code = "{% raw %}#{rendered_code}{% endraw %}" if @options[:escape]
    Cache.write_to_cache(rendered_code, @options) unless @options[:no_cache]
    rendered_code
  end
end
render_pygments(code, lang) click to toggle source
# File lib/octopress-pygments/renderer.rb, line 46
def render_pygments(code, lang)
  highlighted_code = ::Pygments.highlight(
    code,
    {
      lexer:     lang,
      formatter: 'html',
      options: {
        encoding: 'utf-8'
      }
    }
  )
  encode_liquid(highlighted_code).to_s
end
tableize_code(code, lang, options = {}) click to toggle source
# File lib/octopress-pygments/renderer.rb, line 66
def tableize_code (code, lang, options = {})
  start = options[:start]
  lines = options[:linenos]
  marks = options[:marks]
  table = "<div class='pygments-code'>"
  table += "<pre class='pygments-code-pre #{lang}'>"
  code.lines.each_with_index do |line,index|
    classes = 'pygments-code-row'
    classes += lines ? ' numbered' : ' unnumbered'
    if marks.include? index + start
      classes += ' marked-line'
      classes += ' start-marked-line' unless marks.include? index - 1 + start
      classes += ' end-marked-line' unless marks.include? index + 1 + start
    end
    line = line.strip.empty? ? ' ' : line
    table += "<div data-line='#{index + start}' class='#{classes}'><div class='pygments-code-line'>#{line}</div></div>"
  end
  table +="</pre></div>"
end

Private Instance Methods

stringify_keys(hash) click to toggle source
# File lib/octopress-pygments/renderer.rb, line 108
def stringify_keys(hash)
  hash.inject({}){|result, (key, value)|
    new_key = case key
              when String then key.to_s
              else key
              end
  new_value = case value
              when Hash then stringify_keys(value)
              else value
              end
  result[new_key] = new_value
  result
  }
end