alexander 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/Guardfile +5 -0
- data/LICENSE +22 -0
- data/README.md +59 -0
- data/Rakefile +2 -0
- data/alexander.gemspec +28 -0
- data/lib/alexander/version.rb +3 -0
- data/lib/alexander/xsl_processor.rb +74 -0
- data/lib/alexander.rb +2 -0
- data/spec/alexander/xsl_processor_spec.rb +99 -0
- metadata +203 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Ronie Uliana
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# Alexander
|
2
|
+
|
3
|
+
A Rack middleware to process XML through XSLT to generate HTML.
|
4
|
+
|
5
|
+
The process occur only:
|
6
|
+
|
7
|
+
1. If the file served is a XML (`mime-type: "application/xml"`);
|
8
|
+
2. **and** the XML has a stylesheet processing instruction (`<?xml-stylesheet type="text/xsl" href="/teste.xsl"?>`);
|
9
|
+
3. **and** the browser (`HTTP_USER_AGENT` header) has **no** support for XSLT processing.
|
10
|
+
|
11
|
+
If *any* of these conditions is *false*, Alexander will do nothing.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
gem 'alexander'
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install alexander
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
Add to your Rack stack:
|
30
|
+
|
31
|
+
use Alexander::XslProcessor
|
32
|
+
|
33
|
+
## Browsers with XSLT processing support:
|
34
|
+
|
35
|
+
* Chrome >= 1.0
|
36
|
+
* Firefox >= 3.0
|
37
|
+
* Safari >= 3.0
|
38
|
+
* Internet Explorer >= 6.0
|
39
|
+
* Opera >= 9.0
|
40
|
+
|
41
|
+
## TODO
|
42
|
+
* Better error handling:
|
43
|
+
* Invalid XML.
|
44
|
+
* Invalid XSL.
|
45
|
+
* XSL page not found.
|
46
|
+
* URL parameter to force XSLT processing.
|
47
|
+
* Config parameter to force XSLT processing.
|
48
|
+
* `Content-Type` control, make it able to produce things other than HTML.
|
49
|
+
* Examples of use in README.
|
50
|
+
* Sinatra
|
51
|
+
* Rails
|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
1. Fork it
|
56
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
57
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
58
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
59
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/alexander.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/alexander/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Ronie Uliana"]
|
6
|
+
gem.email = ["ronie.uliana@gmail.com"]
|
7
|
+
gem.description = %q{A Rack middleware to compile XML+XSLT into HTML for unsupported browsers only}
|
8
|
+
gem.summary = %q{Named after the cocktail "Alexander". It will pre-process XML into HTML (using XSLT) only for browsers that are can't do it. Otherwise, it will just sit and watch.}
|
9
|
+
gem.homepage = "http://github.com/ruliana/alexander"
|
10
|
+
|
11
|
+
gem.add_dependency("nokogiri", "~>1.5")
|
12
|
+
gem.add_dependency("useragent", "~>0.4")
|
13
|
+
|
14
|
+
gem.add_development_dependency("reloader", "~>0.1")
|
15
|
+
gem.add_development_dependency("minitest", "~>2.12")
|
16
|
+
gem.add_development_dependency("minitest-matchers", "~>1.2")
|
17
|
+
gem.add_development_dependency("minitest-colorize", "~>0.0.4")
|
18
|
+
gem.add_development_dependency("guard", "~>1.0")
|
19
|
+
gem.add_development_dependency("guard-minitest", "~>0.5")
|
20
|
+
gem.add_development_dependency("simplecov", "~>0.6")
|
21
|
+
|
22
|
+
gem.files = `git ls-files`.split($\)
|
23
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
24
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
25
|
+
gem.name = "alexander"
|
26
|
+
gem.require_paths = ["lib"]
|
27
|
+
gem.version = Alexander::VERSION
|
28
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'rack'
|
2
|
+
require 'useragent'
|
3
|
+
require 'nokogiri'
|
4
|
+
|
5
|
+
module Alexander
|
6
|
+
Browser = Struct.new(:browser, :version)
|
7
|
+
|
8
|
+
XSLT_ENABLE_BROWSERS = [
|
9
|
+
Browser.new("Chrome", "1.0"),
|
10
|
+
Browser.new("Firefox", "3.0"),
|
11
|
+
Browser.new("Internet Explorer", "6.0"),
|
12
|
+
Browser.new("Opera", "9.0"),
|
13
|
+
Browser.new("Safari", "3.0")
|
14
|
+
]
|
15
|
+
|
16
|
+
class XslProcessor
|
17
|
+
def initialize(app)
|
18
|
+
@app = app
|
19
|
+
end
|
20
|
+
|
21
|
+
def call(env)
|
22
|
+
status, headers, body = @app.call(env)
|
23
|
+
|
24
|
+
return [status, headers, body] unless xml?(headers)
|
25
|
+
return [status, headers, body] if xlst_enable_browser?(env)
|
26
|
+
|
27
|
+
html_response = to_html(env, body)
|
28
|
+
return [status, headers, body] unless html_response
|
29
|
+
|
30
|
+
headers["Content-type"] = "text/html"
|
31
|
+
Rack::Response.new([html_response], status, headers).finish
|
32
|
+
end
|
33
|
+
|
34
|
+
def xml?(headers)
|
35
|
+
headers["Content-Type"] =~ /\bapplication\/xml\b/
|
36
|
+
end
|
37
|
+
|
38
|
+
def xlst_enable_browser?(env)
|
39
|
+
return false unless env && env["HTTP_USER_AGENT"]
|
40
|
+
user_agent = UserAgent.parse(env["HTTP_USER_AGENT"])
|
41
|
+
XSLT_ENABLE_BROWSERS.detect { |browser| user_agent >= browser }
|
42
|
+
end
|
43
|
+
|
44
|
+
def to_html(env, body)
|
45
|
+
xml = body_to_string(body)
|
46
|
+
xslt_request = detect_xslt_processing_instruction(xml)
|
47
|
+
return unless xslt_request
|
48
|
+
|
49
|
+
ask_xslt = env.dup
|
50
|
+
ask_xslt["PATH_INFO"] = xslt_request
|
51
|
+
ask_xslt["REQUEST_PATH"] = xslt_request
|
52
|
+
ask_xslt["REQUEST_URI"] = xslt_request
|
53
|
+
ask_xslt["QUERY_STRING"] = ""
|
54
|
+
status, headers, xslt = @app.call(ask_xslt)
|
55
|
+
return unless status == 200
|
56
|
+
|
57
|
+
xml_parsed = Nokogiri::XML(xml)
|
58
|
+
xsl_parsed = Nokogiri::XSLT(body_to_string(xslt))
|
59
|
+
xsl_parsed.transform(xml_parsed).to_s
|
60
|
+
end
|
61
|
+
|
62
|
+
def detect_xslt_processing_instruction(xml)
|
63
|
+
match = xml.match(/<\?xml-stylesheet.*href="([^"]+)"/)
|
64
|
+
return match[1] if match
|
65
|
+
end
|
66
|
+
|
67
|
+
def body_to_string(body)
|
68
|
+
result = ""
|
69
|
+
body.each { |it| result << it }
|
70
|
+
body.close if body.respond_to? :close
|
71
|
+
result
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/lib/alexander.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start
|
3
|
+
|
4
|
+
require 'minitest/autorun'
|
5
|
+
require 'minitest/colorize'
|
6
|
+
|
7
|
+
require_relative '../../lib/alexander'
|
8
|
+
|
9
|
+
class DummyApp
|
10
|
+
attr_accessor :xml, :xsl
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
self.xml = Rack::MockResponse.new(
|
14
|
+
200, {"Content-Type" => "application/xml;charset=utf-8"}, [<<-XML
|
15
|
+
<?xml version="1.0" encoding="utf-8"?>
|
16
|
+
<?xml-stylesheet type="text/xsl" href="teste.xsl"?>
|
17
|
+
<root />
|
18
|
+
XML
|
19
|
+
])
|
20
|
+
self.xsl = Rack::MockResponse.new(
|
21
|
+
200, {"Content-type" => "application/xslt+xml"}, [<<-XSL
|
22
|
+
<?xml version="1.0" encoding="utf-8"?>
|
23
|
+
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|
24
|
+
<xsl:template match="/">
|
25
|
+
<html><body></body></html>
|
26
|
+
</xsl:template>
|
27
|
+
</xsl:stylesheet>
|
28
|
+
XSL
|
29
|
+
])
|
30
|
+
end
|
31
|
+
|
32
|
+
def call(env)
|
33
|
+
if env["REQUEST_PATH"] =~ /.*\.xsl/
|
34
|
+
xsl
|
35
|
+
else
|
36
|
+
xml
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
CHROME_18 = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19 (KHTML, like Gecko) Ubuntu/11.10 Chromium/18.0.1025.151 Chrome/18.0.1025.151 Safari/535.19"
|
42
|
+
CURL = "curl/7.21.6 (x86_64-pc-linux-gnu) libcurl/7.21.6 OpenSSL/1.0.0e zlib/1.2.3.4 libidn/1.22 librtmp/2.3"
|
43
|
+
|
44
|
+
describe Alexander::XslProcessor do
|
45
|
+
|
46
|
+
def env_with_chrome
|
47
|
+
{"HTTP_USER_AGENT" => CHROME_18}
|
48
|
+
end
|
49
|
+
|
50
|
+
def env_with_curl
|
51
|
+
{"HTTP_USER_AGENT" => CURL}
|
52
|
+
end
|
53
|
+
|
54
|
+
before do
|
55
|
+
@dummy_app = DummyApp.new
|
56
|
+
@filter = Alexander::XslProcessor.new(@dummy_app)
|
57
|
+
end
|
58
|
+
|
59
|
+
def app
|
60
|
+
@filter
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "when response is NOT XML" do
|
64
|
+
it "should pass the response as is" do
|
65
|
+
@dummy_app.xml = Rack::MockResponse.new(200, {"Content-type" => "text/html"}, ["<html></html>"])
|
66
|
+
status, headers,response = @filter.call(env_with_chrome)
|
67
|
+
response.body.must_equal @dummy_app.xml.body
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "when response is XML" do
|
72
|
+
describe "when request came from a XSLT enable browser" do
|
73
|
+
it "should let response as is" do
|
74
|
+
status, headers, response = @filter.call(env_with_chrome)
|
75
|
+
response.body.must_equal @dummy_app.xml.body
|
76
|
+
end
|
77
|
+
end
|
78
|
+
describe "when request came from a XSLT NOT enable browser" do
|
79
|
+
it "should parse XML to HTML" do
|
80
|
+
status, headers, response = @filter.call(env_with_curl)
|
81
|
+
status.must_equal 200
|
82
|
+
headers["Content-type"].must_equal "text/html"
|
83
|
+
response.body.must_equal ["<html><body></body></html>\n"]
|
84
|
+
end
|
85
|
+
end
|
86
|
+
describe "when response is a XML without stylesheet" do
|
87
|
+
it "should let response as is" do
|
88
|
+
@dummy_app.xml = Rack::MockResponse.new(200, {"Content-type" => "application/xml"}, [<<-XML
|
89
|
+
<?xml version="1.0" encoding="utf-8"?>
|
90
|
+
<root />
|
91
|
+
XML
|
92
|
+
])
|
93
|
+
status, header, response = @filter.call(env_with_curl)
|
94
|
+
response.body.must_equal @dummy_app.xml.body
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
metadata
ADDED
@@ -0,0 +1,203 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: alexander
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ronie Uliana
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: nokogiri
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.5'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.5'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: useragent
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0.4'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0.4'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: reloader
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0.1'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.1'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: minitest
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '2.12'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '2.12'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: minitest-matchers
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '1.2'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '1.2'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: minitest-colorize
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ~>
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 0.0.4
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ~>
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 0.0.4
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: guard
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ~>
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '1.0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ~>
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '1.0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: guard-minitest
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ~>
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0.5'
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ~>
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0.5'
|
142
|
+
- !ruby/object:Gem::Dependency
|
143
|
+
name: simplecov
|
144
|
+
requirement: !ruby/object:Gem::Requirement
|
145
|
+
none: false
|
146
|
+
requirements:
|
147
|
+
- - ~>
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0.6'
|
150
|
+
type: :development
|
151
|
+
prerelease: false
|
152
|
+
version_requirements: !ruby/object:Gem::Requirement
|
153
|
+
none: false
|
154
|
+
requirements:
|
155
|
+
- - ~>
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: '0.6'
|
158
|
+
description: A Rack middleware to compile XML+XSLT into HTML for unsupported browsers
|
159
|
+
only
|
160
|
+
email:
|
161
|
+
- ronie.uliana@gmail.com
|
162
|
+
executables: []
|
163
|
+
extensions: []
|
164
|
+
extra_rdoc_files: []
|
165
|
+
files:
|
166
|
+
- .gitignore
|
167
|
+
- Gemfile
|
168
|
+
- Guardfile
|
169
|
+
- LICENSE
|
170
|
+
- README.md
|
171
|
+
- Rakefile
|
172
|
+
- alexander.gemspec
|
173
|
+
- lib/alexander.rb
|
174
|
+
- lib/alexander/version.rb
|
175
|
+
- lib/alexander/xsl_processor.rb
|
176
|
+
- spec/alexander/xsl_processor_spec.rb
|
177
|
+
homepage: http://github.com/ruliana/alexander
|
178
|
+
licenses: []
|
179
|
+
post_install_message:
|
180
|
+
rdoc_options: []
|
181
|
+
require_paths:
|
182
|
+
- lib
|
183
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
184
|
+
none: false
|
185
|
+
requirements:
|
186
|
+
- - ! '>='
|
187
|
+
- !ruby/object:Gem::Version
|
188
|
+
version: '0'
|
189
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
190
|
+
none: false
|
191
|
+
requirements:
|
192
|
+
- - ! '>='
|
193
|
+
- !ruby/object:Gem::Version
|
194
|
+
version: '0'
|
195
|
+
requirements: []
|
196
|
+
rubyforge_project:
|
197
|
+
rubygems_version: 1.8.21
|
198
|
+
signing_key:
|
199
|
+
specification_version: 3
|
200
|
+
summary: Named after the cocktail "Alexander". It will pre-process XML into HTML (using
|
201
|
+
XSLT) only for browsers that are can't do it. Otherwise, it will just sit and watch.
|
202
|
+
test_files:
|
203
|
+
- spec/alexander/xsl_processor_spec.rb
|