Class: Octopress::Pygments::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/octopress-pygments/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, options = {}) ⇒ Renderer

Returns a new instance of Renderer.



6
7
8
9
10
11
12
13
# 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

Instance Attribute Details

#codeObject

Returns the value of attribute code.



4
5
6
# File 'lib/octopress-pygments/renderer.rb', line 4

def code
  @code
end

#langObject

Returns the value of attribute lang.



4
5
6
# File 'lib/octopress-pygments/renderer.rb', line 4

def lang
  @lang
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/octopress-pygments/renderer.rb', line 4

def options
  @options
end

Instance Method Details

#captionize(caption, url, link_text) ⇒ Object



60
61
62
63
64
# 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) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# 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) ⇒ Object



101
102
103
104
# 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) ⇒ Object

Public:



89
90
91
92
93
94
95
96
97
98
99
# 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

#highlightObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# 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 = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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