Module: Rouge::Plugins::Redcarpet

Defined in:
lib/rouge/plugins/redcarpet.rb

Instance Method Summary collapse

Instance Method Details

#block_code(code, language) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rouge/plugins/redcarpet.rb', line 14

def block_code(code, language)
  name, opts = language ? language.split('?', 2) : [nil, '']

  # parse the options hash from a cgi-style string
  opts = CGI.parse(opts || '').map do |k, vals|
    [ k.to_sym, vals.empty? ? true : vals[0] ]
  end

  opts = Hash[opts]

  lexer_class = case name
  when 'guess', nil
    lexer = Lexer.guess(:source => code, :mimetype => opts[:mimetype])
  when String
    Lexer.find(name)
  end || Lexers::Text

  lexer = lexer_class.new(opts)
  formatter = Formatters::HTML.new(:css_class => "highlight #{lexer_class.tag}")

  Rouge.highlight(code, lexer, formatter)
end