inliner 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +12 -0
- data/bin/inliner +24 -0
- data/inliner.gemspec +24 -0
- data/lib/inliner/version.rb +3 -0
- data/lib/inliner.rb +134 -0
- metadata +165 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Mark Sonnabaum
|
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,12 @@
|
|
1
|
+
# Inliner
|
2
|
+
|
3
|
+
A simple tool for inlining assets. Useful for creating maintenance pages and probably some other stuff. Code for inlining initially borrowed from [rack-pagespeed](https://github.com/juliocesar/rack-pagespeed).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
$ gem install inliner
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
$ inliner http://www.example.com/blah > inlined_example.html
|
12
|
+
|
data/bin/inliner
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
$:.unshift File.expand_path("../../lib", __FILE__)
|
3
|
+
|
4
|
+
require 'optparse'
|
5
|
+
require 'methadone'
|
6
|
+
require 'inliner'
|
7
|
+
|
8
|
+
class App
|
9
|
+
include Methadone::Main
|
10
|
+
include Methadone::CLILogging
|
11
|
+
|
12
|
+
main do |url|
|
13
|
+
d = Inliner::Inline.new(url)
|
14
|
+
puts d.inline
|
15
|
+
end
|
16
|
+
|
17
|
+
version Inliner::VERSION
|
18
|
+
|
19
|
+
arg :url
|
20
|
+
|
21
|
+
use_log_level_option
|
22
|
+
|
23
|
+
go!
|
24
|
+
end
|
data/inliner.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/inliner/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Mark Sonnabaum"]
|
6
|
+
gem.email = ["mark@sonnabaum.com"]
|
7
|
+
gem.summary = %q{Inlines assets from a URL.}
|
8
|
+
gem.description = gem.summary
|
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 = "inliner"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Inliner::VERSION
|
17
|
+
gem.add_development_dependency('rdoc')
|
18
|
+
gem.add_development_dependency('aruba')
|
19
|
+
gem.add_development_dependency('rake','~> 0.9.2')
|
20
|
+
gem.add_development_dependency('nokogiri','~> 1.5.2')
|
21
|
+
gem.add_development_dependency('rack','~> 1.4.1')
|
22
|
+
gem.add_development_dependency('em-http-request', '~> 1.0.2')
|
23
|
+
gem.add_dependency('methadone', '~>1.0.0.rc4')
|
24
|
+
end
|
data/lib/inliner.rb
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
require 'uri'
|
3
|
+
require 'net/http'
|
4
|
+
require 'rack'
|
5
|
+
require "inliner/version"
|
6
|
+
require 'em-http-request'
|
7
|
+
require 'pp'
|
8
|
+
|
9
|
+
module Inliner
|
10
|
+
class Inline
|
11
|
+
def initialize(url)
|
12
|
+
unless validate_url(url)
|
13
|
+
raise ArgumentError, "#{url} is not a valid URL!"
|
14
|
+
end
|
15
|
+
|
16
|
+
@assets = {}
|
17
|
+
@url = URI.parse(url)
|
18
|
+
EM.run do
|
19
|
+
http = EM::HttpRequest.new(@url, :connect_timeout => 5, :inactivity_timeout => 10).get :redirects => 1
|
20
|
+
http.callback {
|
21
|
+
@doc = Nokogiri::HTML(http.response)
|
22
|
+
EM.stop
|
23
|
+
}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def validate_url(url)
|
28
|
+
!(url =~ URI::regexp).nil?
|
29
|
+
end
|
30
|
+
|
31
|
+
def inline_asset(node)
|
32
|
+
contents = asset_contents(asset_uri(node))
|
33
|
+
return if !contents
|
34
|
+
node.before inline_node(contents, node)
|
35
|
+
node.remove
|
36
|
+
end
|
37
|
+
|
38
|
+
def inline_node(contents, node)
|
39
|
+
case node.name
|
40
|
+
when 'img'
|
41
|
+
inline = node.clone
|
42
|
+
inline['src'] = create_data_uri(asset_uri(node), contents)
|
43
|
+
inline['alt'] = node['alt'] if node['alt']
|
44
|
+
when 'link'
|
45
|
+
inline = Nokogiri::XML::Node.new 'style', @doc
|
46
|
+
inline.content = inline_css_assets(contents)
|
47
|
+
else
|
48
|
+
inline = Nokogiri::XML::Node.new node.name, @doc
|
49
|
+
inline.content = contents
|
50
|
+
end
|
51
|
+
inline
|
52
|
+
end
|
53
|
+
|
54
|
+
def inline_css_assets(contents)
|
55
|
+
css_urls = Hash[contents.scan(/url\([\'"]?(.+?)[\'"]?\)/).map { |u|
|
56
|
+
uri = @url + u[0]
|
57
|
+
[u[0], uri.to_s]
|
58
|
+
}]
|
59
|
+
|
60
|
+
unless css_urls.empty?
|
61
|
+
get_urls(css_urls.values)
|
62
|
+
css_urls.each do |path, full|
|
63
|
+
data_uri = create_data_uri(path, asset_contents(full))
|
64
|
+
contents = contents.gsub(path, data_uri)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
contents
|
68
|
+
end
|
69
|
+
|
70
|
+
def create_data_uri(path, contents)
|
71
|
+
mime = Rack::Mime.mime_type(File.extname(path))
|
72
|
+
"data:#{Rack::Mime.mime_type(File.extname(path))};base64,#{[contents].pack('m').gsub(/\n/, "")}"
|
73
|
+
end
|
74
|
+
|
75
|
+
def to_html
|
76
|
+
return @doc.to_s
|
77
|
+
end
|
78
|
+
|
79
|
+
def get_urls(urls)
|
80
|
+
assets = {}
|
81
|
+
EventMachine.run do
|
82
|
+
multi = EventMachine::MultiRequest.new
|
83
|
+
urls.uniq.each_with_index do |url, idx|
|
84
|
+
begin
|
85
|
+
http = EventMachine::HttpRequest.new(url, :connect_timeout => 1)
|
86
|
+
req = http.get :redirects => 1
|
87
|
+
rescue URI::InvalidURIError(e)
|
88
|
+
p e.backtrace
|
89
|
+
end
|
90
|
+
multi.add url, req
|
91
|
+
end
|
92
|
+
|
93
|
+
multi.callback do
|
94
|
+
multi.responses[:callback].each do |idx, r|
|
95
|
+
#p idx
|
96
|
+
assets[idx] = r.response
|
97
|
+
end
|
98
|
+
EventMachine.stop
|
99
|
+
end
|
100
|
+
end
|
101
|
+
assets
|
102
|
+
end
|
103
|
+
|
104
|
+
def inline
|
105
|
+
assets = @doc.css('img') + @doc.css('link[rel="stylesheet"][href]') + @doc.css('script[src]')
|
106
|
+
urls = assets.map {|node| asset_uri(node)}
|
107
|
+
|
108
|
+
@assets = get_urls urls
|
109
|
+
assets.each do |node|
|
110
|
+
inline_asset node
|
111
|
+
end
|
112
|
+
to_html
|
113
|
+
end
|
114
|
+
|
115
|
+
def asset_uri(node)
|
116
|
+
path = case node.name
|
117
|
+
when 'script'
|
118
|
+
node['src']
|
119
|
+
when 'img'
|
120
|
+
node['src']
|
121
|
+
when 'link'
|
122
|
+
node['href']
|
123
|
+
end
|
124
|
+
return false unless path
|
125
|
+
|
126
|
+
uri = @url + path
|
127
|
+
uri.to_s
|
128
|
+
end
|
129
|
+
|
130
|
+
def asset_contents(uri)
|
131
|
+
@assets[uri] || Net::HTTP.get(URI.parse(uri))
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
metadata
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: inliner
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mark Sonnabaum
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-09 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rdoc
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: aruba
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 0.9.2
|
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.9.2
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: nokogiri
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 1.5.2
|
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: 1.5.2
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rack
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 1.4.1
|
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.4.1
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: em-http-request
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ~>
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 1.0.2
|
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: 1.0.2
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: methadone
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ~>
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 1.0.0.rc4
|
118
|
+
type: :runtime
|
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.0.rc4
|
126
|
+
description: Inlines assets from a URL.
|
127
|
+
email:
|
128
|
+
- mark@sonnabaum.com
|
129
|
+
executables:
|
130
|
+
- inliner
|
131
|
+
extensions: []
|
132
|
+
extra_rdoc_files: []
|
133
|
+
files:
|
134
|
+
- Gemfile
|
135
|
+
- LICENSE
|
136
|
+
- README.md
|
137
|
+
- bin/inliner
|
138
|
+
- inliner.gemspec
|
139
|
+
- lib/inliner.rb
|
140
|
+
- lib/inliner/version.rb
|
141
|
+
homepage: ''
|
142
|
+
licenses: []
|
143
|
+
post_install_message:
|
144
|
+
rdoc_options: []
|
145
|
+
require_paths:
|
146
|
+
- lib
|
147
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
148
|
+
none: false
|
149
|
+
requirements:
|
150
|
+
- - ! '>='
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
154
|
+
none: false
|
155
|
+
requirements:
|
156
|
+
- - ! '>='
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: '0'
|
159
|
+
requirements: []
|
160
|
+
rubyforge_project:
|
161
|
+
rubygems_version: 1.8.23
|
162
|
+
signing_key:
|
163
|
+
specification_version: 3
|
164
|
+
summary: Inlines assets from a URL.
|
165
|
+
test_files: []
|