Class: Rouge::Rails::TemplateHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/rouge/rails/template_handler.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ TemplateHandler

Returns a new instance of TemplateHandler.



9
10
11
# File 'lib/rouge/rails/template_handler.rb', line 9

def initialize(template)
  @template = template
end

Instance Attribute Details

#templateObject

Returns the value of attribute template.



7
8
9
# File 'lib/rouge/rails/template_handler.rb', line 7

def template
  @template
end

Class Method Details

.call(template) ⇒ Object



3
4
5
# File 'lib/rouge/rails/template_handler.rb', line 3

def self.call(template)
  new(template).call
end

.erb_handlerObject



45
46
47
# File 'lib/rouge/rails/template_handler.rb', line 45

def self.erb_handler
  @erb_handler ||= ActionView::Template.handler_for_extension(:erb)
end

Instance Method Details

#callObject



13
14
15
16
17
18
19
# File 'lib/rouge/rails/template_handler.rb', line 13

def call
  %{
    lexer = #{lexer}
    formatter = Rouge::Formatters::HTML.new(css_class: "#{colorscheme}")
    formatter.format(lexer.lex(#{source})).html_safe
  }
end

#colorschemeObject



29
30
31
32
33
34
35
# File 'lib/rouge/rails/template_handler.rb', line 29

def colorscheme
  if template.locals.include?("colorscheme")
    "highlight \#{colorscheme}"
  else
    "highlight #{Rouge::Rails.configuration.default_colorscheme}"
  end
end

#default_lexerObject



41
42
43
# File 'lib/rouge/rails/template_handler.rb', line 41

def default_lexer
  "Rouge::Lexers::PlainText"
end

#erb_handlerObject



49
50
51
# File 'lib/rouge/rails/template_handler.rb', line 49

def erb_handler
  self.class.erb_handler
end

#lexerObject



21
22
23
24
25
26
27
# File 'lib/rouge/rails/template_handler.rb', line 21

def lexer
  if template.locals.include?("language")
    "Rouge::Lexer.find(language) || #{default_lexer}"
  else
    default_lexer
  end
end

#sourceObject



37
38
39
# File 'lib/rouge/rails/template_handler.rb', line 37

def source
  "begin; #{erb_handler.call(template)} end"
end