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
15
16
17
18
19
# 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)
  if defined? Octopress.config
    @aliases = Octopress.config['code_aliases']
  elsif defined? Ink
    @aliases = Ink.config['code_aliases']
  end
  @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



106
107
108
109
110
111
112
113
114
# File 'lib/octopress-code-highlighter/renderer.rb', line 106

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



152
153
154
155
# File 'lib/octopress-code-highlighter/renderer.rb', line 152

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

#get_range(code, start, endline) ⇒ Object

Public:



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/octopress-code-highlighter/renderer.rb', line 140

def get_range(code, start, endline)
  length    = code.lines.count
  start   ||= 1
  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).join("\n")
  end
  code
end

#highlightObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/octopress-code-highlighter/renderer.rb', line 41

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)


55
56
57
# File 'lib/octopress-code-highlighter/renderer.rb', line 55

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

#renderObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/octopress-code-highlighter/renderer.rb', line 59

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



76
77
78
# File 'lib/octopress-code-highlighter/renderer.rb', line 76

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

#render_pygmentsObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/octopress-code-highlighter/renderer.rb', line 80

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



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

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

#renderer_available?(which) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/octopress-code-highlighter/renderer.rb', line 37

def renderer_available?(which)
  Gem::Specification::find_all_by_name(which).any?
end

#select_rendererObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/octopress-code-highlighter/renderer.rb', line 21

def select_renderer
  case true
    when renderer_available?('rouge')
      require 'rouge'
      return  'rouge'
    when renderer_available?('pygments.rb')
      require 'pygments'
      return  'pygments'
  else
    $stderr.puts 'No syntax highlighting:'.yellow
    $stderr.puts "\tInstall pygments.rb, rouge".yellow
  end
  
  'plain'
end

#tableize_code(code) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/octopress-code-highlighter/renderer.rb', line 116

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