Class: Rack::Codehighlighter
- Inherits:
-
Object
- Object
- Rack::Codehighlighter
- Includes:
- Utils
- Defined in:
- lib/codehighlighter-middleware.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 = :coderay, opts = {}) ⇒ Codehighlighter
constructor
A new instance of Codehighlighter.
Constructor Details
#initialize(app, highlighter = :coderay, opts = {}) ⇒ Codehighlighter
Returns a new instance of Codehighlighter.
12 13 14 15 16 17 18 19 |
# File 'lib/codehighlighter-middleware.rb', line 12 def initialize(app, highlighter = :coderay, opts = {}) @app = app @highlighter = highlighter @opts = { :element => "//pre/code", :pattern => /\A:::(\w+)\s*\n/ } @opts.merge! opts end |
Instance Method Details
#call(env) ⇒ Object
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 47 48 |
# File 'lib/codehighlighter-middleware.rb', line 21 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?++]" node.parent.swap(send(@highlighter, s)) end body = doc.to_html headers['content-length'] = body.bytesize.to_s log(env, status, headers, began_at) if @opts[:logging] [status, headers, [body]] else [status, headers, response] end end |