Class: Rack::XSLView
- Inherits:
-
Object
- Object
- Rack::XSLView
- Defined in:
- lib/rack-xslview.rb
Defined Under Namespace
Classes: XSLViewError
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options) ⇒ XSLView
constructor
A new instance of XSLView.
Constructor Details
#initialize(app, options) ⇒ XSLView
Returns a new instance of XSLView.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/rack-xslview.rb', line 9 def initialize(app, ) @app = app @options = {:myxsl => nil}.merge() if @options[:myxsl].nil? require 'rexml/document' @xslt = XML::XSLT.new() @xslt.xsl = REXML::Document.new '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"><xsl:import href="http://github.com/docunext/1bb02b59/raw/master/standard.html.xsl"/><xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="no" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" indent="yes"/></xsl:stylesheet>' else @xslt = @options[:myxsl] end 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rack-xslview.rb', line 21 def call(env) # No matter what, @app will be called status, headers, body = @app.call(env) original_response = Array[status, headers, body] exluded_status = Array[204, 301, 302, 304] return original_response if exluded_status.include?(status) || body.nil? return original_response unless headers["Content-Type"].to_s.match(/(ht|x)ml/) # If setup includes paths to exclude from xslt processing, check them checknoxsl(env) if @options[:noxsl] # Obtain entire request body, ensuring sure it can be processed myxml = getResponse(body) # One more check for an empty respone return original_response if myxml.empty? # Should XSL file be reloaded? if @options[:reload] == true @xslt = XML::XSLT.new() @xslt.xsl = REXML::Document.new @options[:xslfile] end unless @options[:tidy].nil? require 'tidy_ffi' input_xml = myxml.include?('http://www.w3.org/1999/xhtml') ? 0 : 1 output = myxml.include?('http://www.w3.org/1999/xhtml') ? 'output_xhtml' : 'output_xml' @options[:tidy][:input_xml] = input_xml @options[:tidy][output.to_sym] = 1 nuxml = TidyFFI::Tidy.new(myxml, @options[:tidy]).clean nuxml = '' if nuxml == '' input_xml = myxml.include?('http://www.w3.org/1999/xhtml') ? '' : '-xml' cmd = %Q~echo | tidy -asxml #{input_xml} -q --doctype "omit" --show-warnings 0 --numeric-entities 1 --drop-proprietary-attributes 1 --preserve-entities 0 --input-encoding "utf8" --char-encoding "utf8" --output-encoding "utf8" --alt-text "" --tidy-mark 0 --logical-emphasis 1 <<THISISITOKOK\n#{myxml}\nTHISISITOKOK }~ nuxml = `#{cmd}` myxml = nuxml unless nuxml == '' end end @xslt.xml = myxml # If setup includes env vars to pass through as params, do so unless @options[:passenv].nil? @myhash = {} @options[:passenv].each { |envkey| # Does env var exist? @myhash[envkey] = "#{env[envkey]}" if env[envkey] } @xslt.parameters = @myhash unless @myhash.empty? end # Perform the transformation newbody = Array.[](@xslt.serve) # If we've made it this far, we can alter the headers headers.delete('Content-Length') headers['Content-Length'] = newbody[0].length.to_s # Content type override? unless @options[:content_type].nil? headers["Content-Type"] = @options[:content_type] end [status, headers, newbody] rescue XSLViewError # TODO Log: "Rack XSLView not processed" if env['RACK_ENV'] == 'development' original_response end |