Module: TextTube::Coderay
- Extended by:
- Filterable
- Defined in:
- lib/texttube/filters/coderay.rb
Overview
a filter for Coderay
Class Method Summary collapse
-
.codify(str, lang) ⇒ Object
Run the Coderay scanner.
-
.html_unescape(a_string) ⇒ Object
Unescape the HTML as the Coderay scanner won’t work otherwise.
- .run(content, options = {}) ⇒ String
Methods included from Filterable
Class Method Details
.codify(str, lang) ⇒ Object
Run the Coderay scanner.
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('&', '&').gsub('<', '<').gsub('>', '>').gsub('"', '"') end |
.run(content, options = {}) ⇒ String
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, ={}) = {lang: :ruby } if .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") [:lang] = lines.shift.match(%r{::::(\w+)})[1].to_sym inner_html = lines.join("\n") end if ([:lang] == :skip) || (! .has_key? :lang ) code_block.inner_html = inner_html else code = Coderay.codify(Coderay.html_unescape(inner_html), [:lang]) code_block.inner_html = code code_block["class"] = "CodeRay" end end#block doc.to_s end |