Class: RailsInfo::CodePresenter

Inherits:
Presenter show all
Defined in:
app/presenters/rails_info/code_presenter.rb

Instance Method Summary collapse

Methods inherited from Presenter

#subject=

Constructor Details

#initialize(subject, options = {}) ⇒ CodePresenter

Returns a new instance of CodePresenter.



2
3
4
5
6
7
8
9
10
11
# File 'app/presenters/rails_info/code_presenter.rb', line 2

def initialize(subject, options = {})
  super(subject, options)
  
  @code = options[:text]
  @line_number = options[:number]
  @line_numbers = options[:line_numbers]
  @highlighted_line_numbers = options[:highlighted_line_numbers] || []
  @highlighted_line_numbers = @highlighted_line_numbers.is_a?(Array) ? @highlighted_line_numbers : [@highlighted_line_numbers]
  @highlighted_number_name = options[:highlighted_number_name]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RailsInfo::Presenter

Instance Method Details

#tableObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/presenters/rails_info/code_presenter.rb', line 13

def table
  outer_html = ''
  
  unless @line_numbers.include?(@line_number)
    outer_html = (:p) do 
       :strong do 
        raw(
          "Line number #{@line_number.inspect} is either an empty line or not included (anymore).<br/>" +
          "So highlighting the nearest line."
        )
      end
    end
  end
  
  outer_html + (:table, cellspacing: 0, cellpadding: 0) do
    html, visible_line_number = '', 1
    
    @line_numbers.each do |line_number|
      html +=  :tr do
        tr_html = ''
        
        if @highlighted_line_numbers.include?(visible_line_number)
          #line_number_link = link_to line_number.to_s, "#top", name: @highlighted_number_name.parameterize
          tr_html =  :td, line_number, class: 'hll'
        else
          tr_html =  :td, line_number
        end
        
        if line_number == @line_numbers.first
          options = @highlighted_line_numbers ? {hl_lines: @highlighted_line_numbers } : {}
          
          # 1) This Ruby wrapped Python code can cause Ruby segmentation faults. 
          #    If you can abandon syntax highlighting deactivate 1) and use 2) instead
          code = ::Pygments.highlight(@code, lexer: 'ruby', options: options) rescue @code.gsub(/\n/, '<br/>').gsub(' ', '&nbsp;')

          # 2) 
          # code = @code.gsub(/\n/, '<br/>').gsub(' ', '&nbsp;')

          tr_html +=  :td, raw(code), rowspan: @line_numbers.length
        end
        
        raw tr_html
      end  
      
      visible_line_number += 1
    end  
    
    raw html
  end
end