Module: TextTube::Coderay

Extended by:
Filterable
Defined in:
lib/texttube/filters/coderay.rb

Overview

a filter for Coderay

Class Method Summary collapse

Methods included from Filterable

filter_with, filters

Class Method Details

.codify(str, lang) ⇒ Object

Run the Coderay scanner.

Examples:

self.class.codify "x = 2", "ruby"

Parameters:



61
62
63
# File 'lib/texttube/filters/coderay.rb', line 61

def self.codify(str, lang) 
  CodeRay.scan(str, lang).html
end

.html_unescape(a_string) ⇒ Object

Unescape the HTML as the Coderay scanner won’t work otherwise.



50
51
52
53
# File 'lib/texttube/filters/coderay.rb', line 50

def self.html_unescape(a_string) 
  a_string.gsub('&amp;', '&').gsub('&lt;', '<').gsub('&gt;', 
  '>').gsub('&quot;', '"') 
end

.run(content, options = {}) ⇒ String

Parameters:

  • content (String)
  • options (Hash) (defaults to: {})

Returns:



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
# File 'lib/texttube/filters/coderay.rb', line 21

def self.run(content, options={})
  options = {lang: :ruby } if options.blank? 
  doc = Nokogiri::HTML::fragment(content) 

  code_blocks = doc.xpath("pre/code").map do |code_block| 
    #un-escape as Coderay will escape it again
    inner_html = code_block.inner_html
    
    # following the convention of Rack::Codehighlighter
    if inner_html.start_with?("::::")
      lines = inner_html.split("\n")
      options[:lang] = lines.shift.match(%r{::::(\w+)})[1].to_sym
      inner_html = lines.join("\n")
    end

    if (options[:lang] == :skip) || (! options.has_key? :lang )
      code_block.inner_html = inner_html
    else
      code = Coderay.codify(Coderay.html_unescape(inner_html), options[:lang]) 
      code_block.inner_html = code 
      code_block["class"] = "CodeRay"    
    end      
  end#block

  doc.to_s
end