Class: Rack::Codehighlighter
- Inherits:
-
Object
- Object
- Rack::Codehighlighter
- Defined in:
- lib/codehighlighter-middleware.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, highlighter = :syntax, opts = {}) ⇒ Codehighlighter
constructor
A new instance of Codehighlighter.
Constructor Details
#initialize(app, highlighter = :syntax, opts = {}) ⇒ Codehighlighter
Returns a new instance of Codehighlighter.
8 9 10 11 12 |
# File 'lib/codehighlighter-middleware.rb', line 8 def initialize(app, highlighter = :syntax, opts = {}) @app = app @highlighter = highlighter @opts = opts end |
Instance Method Details
#call(env) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/codehighlighter-middleware.rb', line 13 def call(env) status, headers, response = @app.call(env) if headers['Content-Type'] != nil && headers['Content-Type'].include?("text/html") content = "" response.each { |part| content += part } doc = Hpricot(content) nodes = doc.search("//pre/code") nodes.each do |node| s = node.inner_html || "[++where is the code?++]" node.parent.swap(send(@highlighter, s)) end STDERR.puts "Highlighting code with: #{@highlighter}" body = doc.to_html size = body.respond_to?(:bytesize) ? body.bytesize : body.size headers['Content-Length'] = size.to_s [status, headers, [body]] else [status, headers, response] end end |