html_package 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/hip +4 -0
- data/lib/html_package/cli.rb +15 -0
- data/lib/html_package/loader.rb +37 -0
- data/lib/html_package/package.rb +69 -0
- data/lib/html_package.rb +5 -0
- metadata +52 -0
data/bin/hip
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "thor"
|
2
|
+
require "html_package"
|
3
|
+
module HTMLPackage
|
4
|
+
class CLI < Thor
|
5
|
+
map "-i" => :install
|
6
|
+
|
7
|
+
desc "install", "installs a package"
|
8
|
+
method_option "file", type: "string", required: true, banner: "(path or url to package file)"
|
9
|
+
method_option "out", type: "string", required: true, banner: "(path to dir to install to)"
|
10
|
+
def install
|
11
|
+
package = HTMLPackage::Package.open options[:file]
|
12
|
+
HTMLPackage::Loader.new(package, options[:out]).load
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "pathname"
|
2
|
+
|
3
|
+
module HTMLPackage
|
4
|
+
class Loader
|
5
|
+
|
6
|
+
def initialize(package, out_dir)
|
7
|
+
@package = package
|
8
|
+
@out_dir = Pathname.new(out_dir).expand_path
|
9
|
+
end
|
10
|
+
|
11
|
+
def load
|
12
|
+
threads = @package.all_files.map do |uri|
|
13
|
+
puts "loading: #{uri.blue}"
|
14
|
+
Thread.new do
|
15
|
+
begin
|
16
|
+
content = open(uri).read
|
17
|
+
outfile = @out_dir + Pathname.new(uri).basename
|
18
|
+
|
19
|
+
begin
|
20
|
+
File.open(outfile, "r") { |file| file.write content }
|
21
|
+
rescue Exception => e
|
22
|
+
puts "failed: File.write #{outfile.to_s.red}"
|
23
|
+
puts " #{e.class.name.bold} #{e.message.red.bold}"
|
24
|
+
puts
|
25
|
+
end
|
26
|
+
rescue OpenURI::HTTPError => e
|
27
|
+
puts "failed: GET #{uri.red}"
|
28
|
+
puts " #{e.class.name.bold} #{e.message.red.bold}"
|
29
|
+
puts
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
threads.map(&:join)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require "open-uri"
|
2
|
+
require "nokogiri"
|
3
|
+
require "colored"
|
4
|
+
|
5
|
+
module HTMLPackage
|
6
|
+
class Package
|
7
|
+
JAVASCRIPT = "text/javascript"
|
8
|
+
PACKAGE = "text/package+html"
|
9
|
+
SPADE = "text/spade+javascript"
|
10
|
+
|
11
|
+
PACKAGES = "[type='#{PACKAGE}']"
|
12
|
+
SCRIPTS = "[type='#{JAVASCRIPT}']"
|
13
|
+
SPADES = "[type='#{SPADE}']"
|
14
|
+
|
15
|
+
SELF = "[rel=self]"
|
16
|
+
DEPENDENCY = "[rel=dependency]"
|
17
|
+
|
18
|
+
def self.open(uri, resource_type=SPADES)
|
19
|
+
puts "opening package: #{uri.green}"
|
20
|
+
new Nokogiri::HTML(Kernel.open uri), resource_type
|
21
|
+
rescue OpenURI::HTTPError => e
|
22
|
+
puts "failed: GET #{uri.red}"
|
23
|
+
puts " #{e.class.name.bold} #{e.message.red.bold}"
|
24
|
+
puts
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize(document, resource_type)
|
28
|
+
@document = document
|
29
|
+
@resource_type = resource_type
|
30
|
+
end
|
31
|
+
|
32
|
+
def own_files
|
33
|
+
links(SELF, @resource_type).map{|link| link[:href]}
|
34
|
+
end
|
35
|
+
|
36
|
+
def dependency_packages
|
37
|
+
@dependency_packages ||= begin
|
38
|
+
links(DEPENDENCY, PACKAGES).map do |package_link|
|
39
|
+
href = package_link[:href]
|
40
|
+
puts "dependency: #{href.blue}"
|
41
|
+
HTMLPackage::Package.open(href, @resource_type)
|
42
|
+
end
|
43
|
+
end.compact
|
44
|
+
end
|
45
|
+
|
46
|
+
def dependency_files
|
47
|
+
links = []
|
48
|
+
links += direct_links = links(DEPENDENCY, @resource_type).map{|link| link[:href]}
|
49
|
+
|
50
|
+
dependency_packages.each do |package|
|
51
|
+
links += package.all_files
|
52
|
+
end
|
53
|
+
|
54
|
+
links
|
55
|
+
end
|
56
|
+
|
57
|
+
def all_files
|
58
|
+
@all_files ||= ( own_files + dependency_files ).uniq
|
59
|
+
end
|
60
|
+
|
61
|
+
def links(*properties)
|
62
|
+
find ["link"].concat(properties).join
|
63
|
+
end
|
64
|
+
|
65
|
+
def find(selector)
|
66
|
+
@document.css(selector)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/html_package.rb
ADDED
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: html_package
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Collin Miller
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-30 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: Uses a special text/package+html content type to load packages of javascript
|
15
|
+
files.
|
16
|
+
email:
|
17
|
+
- collintmiller@gmail.com
|
18
|
+
executables:
|
19
|
+
- hip
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- bin/hip
|
24
|
+
- lib/html_package/cli.rb
|
25
|
+
- lib/html_package/loader.rb
|
26
|
+
- lib/html_package/package.rb
|
27
|
+
- lib/html_package.rb
|
28
|
+
homepage: http://github.com/collintmiller/html_package
|
29
|
+
licenses: []
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.3.6
|
46
|
+
requirements: []
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 1.8.10
|
49
|
+
signing_key:
|
50
|
+
specification_version: 3
|
51
|
+
summary: A package loader that reads html files.
|
52
|
+
test_files: []
|