rack-relativize 0.0.1
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/LICENSE +22 -0
- data/README.md +33 -0
- data/Rakefile +2 -0
- data/lib/rack/relativize.rb +87 -0
- data/lib/rack/relativize/version.rb +5 -0
- data/rack-relativize.gemspec +17 -0
- metadata +73 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Takashi Kato
|
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,33 @@
|
|
1
|
+
# Rack::Relativize
|
2
|
+
|
3
|
+
rack-relativize relativize path of html ( href, src attribute) and css ( url(..) ).
|
4
|
+
|
5
|
+
This middleware is port of Nanoc::Filters::RelativizePaths.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'rack-relativize'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install rack-relativize
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
config.ru
|
24
|
+
|
25
|
+
use Rack::Relativize
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
1. Fork it
|
30
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
31
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
32
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
33
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'rack/relativize/version'
|
3
|
+
|
4
|
+
class Rack::Relativize
|
5
|
+
|
6
|
+
def initialize(app)
|
7
|
+
@app = app
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(env)
|
11
|
+
status, headers, enumerable_body = original_response = @app.call(env.dup)
|
12
|
+
|
13
|
+
if headers["Content-Type"].to_s.match(/(ht|x)ml/) # FIXME: Use another pattern
|
14
|
+
type = :html
|
15
|
+
elsif headers["Content-Type"].to_s.match(/css/)
|
16
|
+
type = :css
|
17
|
+
else
|
18
|
+
return original_response
|
19
|
+
end
|
20
|
+
|
21
|
+
path = env['PATH_INFO']
|
22
|
+
raise "PATH_INFO is Empty" if path == ""
|
23
|
+
|
24
|
+
content = join_body(enumerable_body)
|
25
|
+
|
26
|
+
case type
|
27
|
+
when :html
|
28
|
+
processed_body = content.gsub(/(<[^>]+\s+(src|href))=(['"]?)(\/.*?)\3([ >])/) do
|
29
|
+
$1 + '=' + $3 + relative_path_to(path, $4) + $3 + $5
|
30
|
+
end
|
31
|
+
when :css
|
32
|
+
processed_body = content.gsub(/url\((['"]?)(\/.*?)\1\)/) do
|
33
|
+
'url(' + $1 + relative_path_to(path, $2) + $1 + ')'
|
34
|
+
end
|
35
|
+
else
|
36
|
+
raise RuntimeError.new(
|
37
|
+
"The relativize_paths needs to know the type of content to " +
|
38
|
+
"process. Pass :type => :html for HTML or :type => :css for CSS."
|
39
|
+
)
|
40
|
+
end
|
41
|
+
processed_headers = headers.merge({
|
42
|
+
"Content-Length" => processed_body.size.to_s
|
43
|
+
})
|
44
|
+
[status, processed_headers, [processed_body]]
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def relative_path_to(src, target)
|
50
|
+
require 'pathname'
|
51
|
+
|
52
|
+
# Find path
|
53
|
+
if target.is_a?(String)
|
54
|
+
path = target
|
55
|
+
else
|
56
|
+
path = target.path
|
57
|
+
raise RuntimeError, "Cannot get the relative path to #{target.inspect} because this target is not outputted (its routing rule returns nil)" if path.nil?
|
58
|
+
end
|
59
|
+
|
60
|
+
# Get source and destination paths
|
61
|
+
dst_path = Pathname.new(path)
|
62
|
+
raise RuntimeError, "Cannot get the relative path to #{path} because the current item representation, #{src.inspect}, is not outputted (its routing rule returns nil)" if src == nil
|
63
|
+
src_path = Pathname.new(src)
|
64
|
+
|
65
|
+
# Calculate the relative path (method depends on whether destination is
|
66
|
+
# a directory or not).
|
67
|
+
if src_path.to_s[-1,1] != '/'
|
68
|
+
relative_path = dst_path.relative_path_from(src_path.dirname).to_s
|
69
|
+
else
|
70
|
+
relative_path = dst_path.relative_path_from(src_path).to_s
|
71
|
+
end
|
72
|
+
|
73
|
+
# Add trailing slash if necessary
|
74
|
+
if dst_path.to_s[-1,1] == '/'
|
75
|
+
relative_path << '/'
|
76
|
+
end
|
77
|
+
|
78
|
+
# Done
|
79
|
+
relative_path
|
80
|
+
end
|
81
|
+
|
82
|
+
def join_body(enumerable_body)
|
83
|
+
parts = []
|
84
|
+
enumerable_body.each { |part| parts << part }
|
85
|
+
return parts.join("")
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/rack/relativize/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["tohosaku"]
|
6
|
+
gem.email = ["ny@cosmichorror.org"]
|
7
|
+
gem.description = %q{rack-relativize relativize path of html ( href, src attribute) and css ( url(..) ). }
|
8
|
+
gem.summary = %q{rack-relativize relativize path of html ( href, src attribute) and css ( url(..) ). }
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "rack-relativize"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Rack::Relativize::VERSION
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-relativize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- tohosaku
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-08-16 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: "rack-relativize relativize path of html ( href, src attribute) and css ( url(..) ). "
|
22
|
+
email:
|
23
|
+
- ny@cosmichorror.org
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- Gemfile
|
33
|
+
- LICENSE
|
34
|
+
- README.md
|
35
|
+
- Rakefile
|
36
|
+
- lib/rack/relativize.rb
|
37
|
+
- lib/rack/relativize/version.rb
|
38
|
+
- rack-relativize.gemspec
|
39
|
+
homepage: ""
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
hash: 3
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.8.17
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: rack-relativize relativize path of html ( href, src attribute) and css ( url(..) ).
|
72
|
+
test_files: []
|
73
|
+
|