gatchaman 0.1.0

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.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,3 @@
1
+ 2013-01-11 maruyama shinpei <shinpeim@maruyama-no-MacBook-Pro.local>
2
+
3
+ * release version 0.1.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gacchaman.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Shinpei Maruyama
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.
@@ -0,0 +1,21 @@
1
+ # Gatchaman
2
+
3
+ Gatchaman is a gem to replace src values in HTML documents with data URI scheme
4
+
5
+ *THE SOFTWARE IS IT'S IN ALPHA QUALITY. IT MAY CHANGE THE API WITHOUT NOTICE.*
6
+
7
+ ## Installation
8
+
9
+ $ gem install gatchaman
10
+
11
+ ## Usage
12
+
13
+ $ gatchan input_file [-r document_root] [-c current_directory]
14
+
15
+ ## Contributing
16
+
17
+ 1. Fork it
18
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
19
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
20
+ 4. Push to the branch (`git push origin my-new-feature`)
21
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+ require 'gatchaman'
4
+
5
+ def parse_options
6
+ opt = OptionParser.new
7
+ options = {:document_root => ".", :current_directory => nil}
8
+
9
+ opt.on('-r document_root') {|v| options[:document_root] = v}
10
+ opt.on('-c current_directory') {|v| options[:current_directory] = v}
11
+ opt.parse!(ARGV)
12
+
13
+ options
14
+ end
15
+
16
+ options = parse_options
17
+ input_file = ARGV.shift or raise "usage #{$0} input_file"
18
+
19
+ print Gatchaman.new(options[:document_root], options[:current_directory] || File.dirname(input_file)).
20
+ data_uri_schemize(IO.read(input_file))
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gatchaman/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "gatchaman"
8
+ gem.version = Gatchaman::VERSION
9
+ gem.authors = ["Shinpei Maruyama"]
10
+ gem.email = ["shinpeim@gmail.com"]
11
+ gem.description = %q{Gatchaman is a gem to replace src values in HTML documents with data URI scheme}
12
+ gem.summary = %q{Gatchaman is a gem to replace src values in HTML documents with data URI scheme}
13
+ gem.homepage = "https://github.com/Shinpeim/Gatchaman"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency "nokogiri", "~> 1.5.6"
21
+ gem.add_runtime_dependency "mime-types", "~> 1.19"
22
+ gem.add_development_dependency "rspec", "~> 2.12.0"
23
+ end
@@ -0,0 +1,42 @@
1
+ require "gatchaman/version"
2
+ require "nokogiri"
3
+ require "mime/types"
4
+ require "open-uri"
5
+ require "base64"
6
+ class Gatchaman
7
+ def initialize(document_root, current_dir)
8
+ @document_root = chomp_last_slash(document_root)
9
+ @current_dir = chomp_last_slash(current_dir)
10
+ end
11
+
12
+ def data_uri_schemize(html_string)
13
+ doc = Nokogiri::HTML::DocumentFragment.parse(html_string)
14
+ elements = doc.xpath("*[@src]")
15
+ elements.each do |elements|
16
+ elements[:src] = to_data_scheme(extract_path(elements[:src]))
17
+ end
18
+ doc.to_s
19
+ end
20
+
21
+ private
22
+ def chomp_last_slash(str)
23
+ str.gsub(/\/$/,"")
24
+ end
25
+ def extract_path(path)
26
+ if path =~ /^http/
27
+ return path
28
+ elsif path =~ /^\//
29
+ return @document_root + path
30
+ else
31
+ return @current_dir + "/" + path
32
+ end
33
+ end
34
+ def to_data_scheme(path)
35
+ mime = MIME::Types.type_for(path).first
36
+ image_binary = open(path, "r:ASCII-8BIT"){|f|
37
+ f.read(nil)
38
+ }
39
+ base64 = Base64.encode64(image_binary).gsub(/\s/,'')
40
+ "data:#{mime.to_s};base64,#{base64}"
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ class Gatchaman
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,44 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "spec_helper"
3
+ require "gatchaman"
4
+ require "base64"
5
+ describe Gatchaman do
6
+ describe "#data_uri_schemize" do
7
+ let(:document_root) {File.dirname(File.dirname(__FILE__))}
8
+ let(:current_dir) {File.dirname(__FILE__)}
9
+ let(:gatchaman){Gatchaman.new(document_root, current_dir)}
10
+ let(:base64_encoded_resouce){
11
+ "iVBORw0KGgoAAAANSUhEUgAAAAQAAAAGCAIAAABrW6giAAAACXBIWXMAA
12
+ BYlAAAWJQFJUiTwAAABy2lUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPH
13
+ g6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0
14
+ iWE1QIENvcmUgNS4xLjIiPgogICA8cmRmOlJERiB4bWxuczpyZGY9Imh0
15
+ dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiP
16
+ gogICAgICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIgogICAgIC
17
+ AgICAgICB4bWxuczpleGlmPSJodHRwOi8vbnMuYWRvYmUuY29tL2V4aWY
18
+ vMS4wLyI+CiAgICAgICAgIDxleGlmOkNvbG9yU3BhY2U+NjU1MzU8L2V4
19
+ aWY6Q29sb3JTcGFjZT4KICAgICAgICAgPGV4aWY6UGl4ZWxYRGltZW5za
20
+ W9uPjQ8L2V4aWY6UGl4ZWxYRGltZW5zaW9uPgogICAgICAgICA8ZXhpZj
21
+ pQaXhlbFlEaW1lbnNpb24+NjwvZXhpZjpQaXhlbFlEaW1lbnNpb24+CiA
22
+ gICAgIDwvcmRmOkRlc2NyaXB0aW9uPgogICA8L3JkZjpSREY+CjwveDp4
23
+ bXBtZXRhPgrjpclbAAAAFUlEQVQIHWOUlJRkgAEmGANEk8MBABhmAFcTc
24
+ YlpAAAAAElFTkSuQmCC".gsub(/[\s\n]/,'')
25
+ }
26
+
27
+ it "絶対urlのsrcをdata schemeで置き換えてくれること" do
28
+ gatchaman.should_receive(:open).with("http://example.com/img/test.png", "r:ASCII-8BIT").
29
+ and_return(Base64.decode64(base64_encoded_resouce))
30
+ gatchaman.data_uri_schemize("<img src='http://example.com/img/test.png'>").
31
+ should == "<img src=\"data:image/png;base64,#{base64_encoded_resouce}\">"
32
+ end
33
+
34
+ it "相対urlの相対パスsrcをdata schemeで置き換えてくれること" do
35
+ gatchaman.data_uri_schemize('<img src="resouces/test.png">').
36
+ should == "<img src=\"data:image/png;base64,#{base64_encoded_resouce}\">"
37
+ end
38
+
39
+ it "相対urlの絶対パスsrcをdata schemeで置き換えてくれること" do
40
+ gatchaman.data_uri_schemize('<img src="/spec/resouces/test.png">').
41
+ should == "<img src=\"data:image/png;base64,#{base64_encoded_resouce}\">"
42
+ end
43
+ end
44
+ end
Binary file
@@ -0,0 +1,3 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'gatchaman'
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gatchaman
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Shinpei Maruyama
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-11 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.6
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.6
30
+ - !ruby/object:Gem::Dependency
31
+ name: mime-types
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.19'
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: '1.19'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 2.12.0
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: 2.12.0
62
+ description: Gatchaman is a gem to replace src values in HTML documents with data
63
+ URI scheme
64
+ email:
65
+ - shinpeim@gmail.com
66
+ executables:
67
+ - gatchan
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - .gitignore
72
+ - ChangeLog
73
+ - Gemfile
74
+ - LICENSE.txt
75
+ - README.md
76
+ - Rakefile
77
+ - bin/gatchan
78
+ - gatchaman.gemspec
79
+ - lib/gatchaman.rb
80
+ - lib/gatchaman/version.rb
81
+ - spec/gatchaman_spec.rb
82
+ - spec/resouces/test.png
83
+ - spec/spec_helper.rb
84
+ homepage: https://github.com/Shinpeim/Gatchaman
85
+ licenses: []
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 1.8.24
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: Gatchaman is a gem to replace src values in HTML documents with data URI
108
+ scheme
109
+ test_files:
110
+ - spec/gatchaman_spec.rb
111
+ - spec/resouces/test.png
112
+ - spec/spec_helper.rb