Class: Octopress::CodeHighlighter::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/octopress-code-highlighter/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
14
# File 'lib/octopress-code-highlighter/renderer.rb', line 6

def initialize(code, options = {})
  @code    = code
  @options = options.delete_if { |k,v| v.nil? }
  @options = DEFAULTS.merge(@options)
  @aliases = stringify_keys(@options[:aliases] || {})
  @lang = @options[:lang]
  @options[:title] ||= ' ' if @options[:url]
  @renderer = select_renderer
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



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

def code
  @code
end

#langObject (readonly)

Returns the value of attribute lang.



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

def lang
  @lang
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#captionObject



95
96
97
98
99
100
101
102
103
# File 'lib/octopress-code-highlighter/renderer.rb', line 95

def caption
  if options[:title]
    figcaption  = "<figcaption class='code-highlight-caption'><span class='code-highlight-caption-title'>#{options[:title]}</span>"
    figcaption += "<a class='code-highlight-caption-link' href='#{options[:url]}'>#{(options[:link_text] || 'link').strip}</a>" if options[:url]
    figcaption += "</figcaption>"
  else
    ''
  end
end

#encode_liquid(code) ⇒ Object



141
142
143
144
# File 'lib/octopress-code-highlighter/renderer.rb', line 141

def encode_liquid(code)
  code.gsub(/{{/, '&#x7b;&#x7b;')
    .gsub(/{%/, '&#x7b;&#x25;')
end

#get_range(code, start, endline) ⇒ Object

Public:



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/octopress-code-highlighter/renderer.rb', line 129

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



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/octopress-code-highlighter/renderer.rb', line 30

def highlight
  if cache = Cache.read_cache(code, options)
    cache
  else
    rendered_code = render
    rendered_code = encode_liquid(rendered_code)
    rendered_code = tableize_code(rendered_code)
    rendered_code = "<figure class='code-highlight-figure'>#{caption}#{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

#plain?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/octopress-code-highlighter/renderer.rb', line 44

def plain?
  options[:lang].empty? || options[:lang] == 'plain'
end

#renderObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/octopress-code-highlighter/renderer.rb', line 48

def render
  case @renderer
  when 'pygments'
    code = render_pygments
  when 'rouge'
    code = render_rouge
  else
    code = render_plain
  end

  if code =~ /<pre.+?>(.+)<\/pre>/m
    $1.strip
  else
    code
  end
end

#render_plainObject



65
66
67
# File 'lib/octopress-code-highlighter/renderer.rb', line 65

def render_plain
  @code.gsub('<','&lt;') 
end

#render_pygmentsObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/octopress-code-highlighter/renderer.rb', line 69

def render_pygments
  if lexer = Pygments::Lexer.find(lang) || Pygments::Lexer.find(@aliases[lang])
    begin
      lexer.highlight @code, {
        formatter: 'html',
        options: {
          encoding: 'utf-8'
        }
      }
    rescue MentosError => e
      raise e
    end
  else
    render_plain
  end
end

#render_rougeObject



86
87
88
89
90
91
92
93
# File 'lib/octopress-code-highlighter/renderer.rb', line 86

def render_rouge
  if lexer = Rouge::Lexer.find(lang) || Rouge::Lexer.find(@aliases[lang])
    formatter = ::Rouge::Formatters::HTML.new()
    formatter.format(lexer.lex(@code))
  else
    render_plain
  end
end

#select_rendererObject



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/octopress-code-highlighter/renderer.rb', line 16

def select_renderer
  begin
    require 'rouge'
    return 'rouge' if defined?(Rouge)
  rescue LoadError; end
  begin
    require 'pygments.rb'
    return 'pygments' if defined?(Pygments)
  rescue LoadError
    puts "To highlight code, install the gem pygments.rb or rouge.".red
    return 'plain'
  end
end

#tableize_code(code) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/octopress-code-highlighter/renderer.rb', line 105

def tableize_code(code)
  start = options[:start]
  lines = options[:linenos]
  marks = options[:marks]

  table = "<div class='code-highlight'>"
  table += "<pre class='code-highlight-pre'>"
  code.lines.each_with_index do |line,index|
    classes = 'code-highlight-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='code-highlight-line'>#{line}</div></div>"
  end
  table +="</pre></div>"
end