# Copyright 2010 Savonix Corporation
# Author Albert L. Lash, IV
# License MIT
module Rack
class XSLView
class XSLViewError < StandardError ; end
def initialize(app, options)
@app = app
@options = {:myxsl => nil}.merge(options)
if @options[:myxsl].nil?
require 'rexml/document'
@xslt = XML::XSLT.new()
@xslt.xsl = REXML::Document.new ''
else
@xslt = @options[:myxsl]
end
end
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 <