Class: Rack::Codehighlighter
- Inherits:
-
Object
- Object
- Rack::Codehighlighter
- Includes:
- Utils
- Defined in:
- lib/rack/codehighlighter.rb
Constant Summary collapse
- FORMAT =
%{%s - [%s] [%s] "%s %s%s %s" (%s) %d %d %0.4f\n}
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, highlighter = :censor, opts = {}) ⇒ Codehighlighter
constructor
A new instance of Codehighlighter.
Constructor Details
#initialize(app, highlighter = :censor, opts = {}) ⇒ Codehighlighter
Returns a new instance of Codehighlighter.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/rack/codehighlighter.rb', line 10 def initialize(app, highlighter = :censor, opts = {}) @app = app @highlighter = highlighter @opts = { :element => "pre", :pattern => /\A:::(\w+)\s*\n/, :reason => "[[-- ugly code removed --]]", :markdown => false } @opts.merge! opts end |
Instance Method Details
#call(env) ⇒ Object
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 47 48 49 50 51 52 53 |
# File 'lib/rack/codehighlighter.rb', line 22 def call(env) began_at = Time.now status, headers, response = @app.call(env) headers = HeaderHash.new(headers) if !STATUS_WITH_NO_ENTITY_BODY.include?(status) && !headers['transfer-encoding'] && headers['content-type'] && headers['content-type'].include?("text/html") content = "" response.each { |part| content += part } doc = Hpricot(content) nodes = doc.search(@opts[:element]) nodes.each do |node| s = node.inner_html || "[++where is the code?++]" if @opts[:markdown] node.parent.swap(send(@highlighter, s)) else node.swap(send(@highlighter, s)) end end body = doc.to_html headers['content-length'] = bytesize(body).to_s log(env, status, headers, began_at) if @opts[:logging] [status, headers, [body]] else [status, headers, response] end end |