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



113
114
115
116
117
118
119
120
121
# File 'lib/octopress-code-highlighter/renderer.rb', line 113

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

#escape_characters(code) ⇒ Object



159
160
161
# File 'lib/octopress-code-highlighter/renderer.rb', line 159

def escape_characters(code)
  code.gsub(/{/, '&#x7b;').gsub(/}/, '&#x7d;')
end

#get_range(code, start, endline) ⇒ Object

Public:



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/octopress-code-highlighter/renderer.rb', line 147

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
54
55
56
57
# 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 = escape_characters(rendered_code)
    rendered_code = tableize_code(rendered_code)
    classnames = 'code-highlight-figure'
    if options[:class]
      classnames << ' ' + options[:class]
    end
    rendered_code = "<figure class='#{classnames}'>#{caption}#{rendered_code}</figure>"
    rendered_code = "{% raw %}#{rendered_code}{% endraw %}" if options[:escape]
    Cache.write_to_cache(rendered_code, code, options) unless options[:no_cache]
    rendered_code
  end
end

#plain?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/octopress-code-highlighter/renderer.rb', line 59

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

#renderObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/octopress-code-highlighter/renderer.rb', line 63

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

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

  # Force wrapping after tags
  code.gsub(/\n<\/span>/, "</span>\n")
end

#render_plainObject



81
82
83
# File 'lib/octopress-code-highlighter/renderer.rb', line 81

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

#render_pygmentsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/octopress-code-highlighter/renderer.rb', line 85

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

#render_rougeObject



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

def render_rouge
  if lexer = Rouge::Lexer.find(lang) || Rouge::Lexer.find(@aliases[lang])
    formatter = ::Rouge::Formatters::HTML.new(wrap: false)
    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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/octopress-code-highlighter/renderer.rb', line 123

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