gatchaman 0.1.1 → 0.2.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.
data/ChangeLog CHANGED
@@ -5,3 +5,8 @@
5
5
  2013-01-11 maruyama shinpei <shinpeim@gmail.com>
6
6
 
7
7
  * bin/gatchan prettify error message
8
+
9
+ 2013-01-14 maruyama shinepi <shinpeim@gmail.com>
10
+
11
+ * RADME.md add usage of Gatchaman class
12
+ * relaese version 0.1.1
data/README.md CHANGED
@@ -10,11 +10,11 @@ Gatchaman is a gem to replace src values in HTML documents with data URI scheme
10
10
 
11
11
  ## Usage
12
12
 
13
- $ gatchan input_file [-r document_root] [-c current_directory]
13
+ $ gatchan input_file [-r document_root] [-c current_directory] [--expand-js] [--expand-css]
14
14
 
15
15
  or
16
16
 
17
- gatchaman = Gatchaman.new(document_root, current_directory)
17
+ gatchaman = Gatchaman.new(document_root: document_root, current_dir: current_directory, expand_js: true, expand_css: true)
18
18
  gatchaman.data_uri_schemize(html_string)
19
19
 
20
20
  ## Contributing
@@ -24,3 +24,7 @@ or
24
24
  3. Commit your changes (`git commit -am 'Add some feature'`)
25
25
  4. Push to the branch (`git push origin my-new-feature`)
26
26
  5. Create new Pull Request
27
+
28
+ ## Contributers
29
+
30
+ wneko [js and css expand options]
@@ -8,13 +8,15 @@ def parse_options
8
8
 
9
9
  opt.on('-r document_root') {|v| options[:document_root] = v}
10
10
  opt.on('-c current_directory') {|v| options[:current_directory] = v}
11
+ opt.on('--expand-js') { options[:expand_js] = true}
12
+ opt.on('--expand-css') { options[:expand_css] = true}
11
13
  opt.parse!(ARGV)
12
14
 
13
15
  options
14
16
  end
15
17
 
16
18
  def help
17
- warn "usage: #{$0} input_file [-r document_root] [-c current_directory]"
19
+ warn "usage: #{$0} input_file [-r document_root] [-c current_directory] [--expand-js] [--expand-css]"
18
20
  exit 1
19
21
  end
20
22
 
@@ -4,17 +4,30 @@ require "mime/types"
4
4
  require "open-uri"
5
5
  require "base64"
6
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)
7
+ SCHEMIZE_TARGET_ELEMENTS = [
8
+ :img,
9
+ :audio,
10
+ :video
11
+ ].freeze
12
+
13
+ def initialize(options = {})
14
+ @document_root = chomp_last_slash(options[:document_root])
15
+ @current_dir = chomp_last_slash(options[:current_dir])
16
+
17
+ @expand_js = options[:expand_js] || false
18
+ @expand_css = options[:expand_css] || false
10
19
  end
11
20
 
12
21
  def data_uri_schemize(html_string)
13
22
  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]))
23
+
24
+ doc.css(SCHEMIZE_TARGET_ELEMENTS.join(',')).each do |element|
25
+ element[:src] = to_data_scheme(extract_path(element[:src]))
17
26
  end
27
+
28
+ expand_js(doc) if @expand_js
29
+ expand_css(doc) if @expand_css
30
+
18
31
  doc.to_s
19
32
  end
20
33
 
@@ -39,4 +52,40 @@ class Gatchaman
39
52
  base64 = Base64.encode64(image_binary).gsub(/\s/,'')
40
53
  "data:#{mime.to_s};base64,#{base64}"
41
54
  end
55
+ def inner_html_from_file(filename)
56
+ ["", open(extract_path(filename)).read.chomp, ""].join("\n")
57
+ end
58
+ def expand_js(doc)
59
+ doc.css('script').each do |element|
60
+ expand_js_element(element)
61
+ end
62
+ doc
63
+ end
64
+ def expand_css(doc)
65
+ doc.css('link[rel=stylesheet]').each do |element|
66
+ expand_css_element(element)
67
+ end
68
+ doc
69
+ end
70
+ def expand_js_element(element)
71
+ return if element[:src].nil? or element[:src].size == 0
72
+ element.inner_html += inner_html_from_file(element[:src])
73
+ [:src, :charset].each {|attr| element.delete(attr.to_s)}
74
+ end
75
+ def expand_css_element(element)
76
+ element.name = "style"
77
+ content = inner_html_from_file(element[:href])
78
+ content = expand_css_url(content)
79
+ element.inner_html = content
80
+ element.attributes.keys.each do |attr_name|
81
+ next if [:media, :type].include? attr_name.to_sym
82
+ element.delete(attr_name)
83
+ end
84
+ end
85
+ def expand_css_url(css_content)
86
+ css_content.gsub(/\burl\b\(([^)]+)\)/) do
87
+ data_scheme = to_data_scheme(extract_path($1))
88
+ "url(#{data_scheme})"
89
+ end
90
+ end
42
91
  end
@@ -1,3 +1,3 @@
1
1
  class Gatchaman
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -6,7 +6,7 @@ describe Gatchaman do
6
6
  describe "#data_uri_schemize" do
7
7
  let(:document_root) {File.dirname(File.dirname(__FILE__))}
8
8
  let(:current_dir) {File.dirname(__FILE__)}
9
- let(:gatchaman){Gatchaman.new(document_root, current_dir)}
9
+ let(:gatchaman){Gatchaman.new(document_root: document_root, current_dir: current_dir, expand_js: true, expand_css: true)}
10
10
  let(:base64_encoded_resouce){
11
11
  "iVBORw0KGgoAAAANSUhEUgAAAAQAAAAGCAIAAABrW6giAAAACXBIWXMAA
12
12
  BYlAAAWJQFJUiTwAAABy2lUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPH
@@ -23,6 +23,8 @@ describe Gatchaman do
23
23
  bXBtZXRhPgrjpclbAAAAFUlEQVQIHWOUlJRkgAEmGANEk8MBABhmAFcTc
24
24
  YlpAAAAAElFTkSuQmCC".gsub(/[\s\n]/,'')
25
25
  }
26
+ let(:test_css_content) { open("#{current_dir}/resouces/test.css").read.chomp }
27
+ let(:test_js_content) { open("#{current_dir}/resouces/test.js").read.chomp }
26
28
 
27
29
  it "絶対urlのsrcをdata schemeで置き換えてくれること" do
28
30
  gatchaman.should_receive(:open).with("http://example.com/img/test.png", "r:ASCII-8BIT").
@@ -40,5 +42,20 @@ describe Gatchaman do
40
42
  gatchaman.data_uri_schemize('<img src="/spec/resouces/test.png">').
41
43
  should == "<img src=\"data:image/png;base64,#{base64_encoded_resouce}\">"
42
44
  end
45
+
46
+ it "cssを展開してくれること" do
47
+ gatchaman.data_uri_schemize('<link rel="stylesheet" type="text/css" media="screen" href="resouces/test.css">').
48
+ should == %[<style type="text/css" media="screen">\n#{test_css_content}\n</style>]
49
+ end
50
+
51
+ it "css内部のurl参照を展開してくれること" do
52
+ gatchaman.data_uri_schemize('<link rel="stylesheet" type="text/css" media="screen" href="resouces/test_2.css">').
53
+ include?(base64_encoded_resouce).should be_true
54
+ end
55
+
56
+ it "jsを展開してくれること" do
57
+ gatchaman.data_uri_schemize('<script src="resouces/test.js" type="text/javascript" charset="utf-8"></script>').
58
+ should == %[<script type="text/javascript">\n#{test_js_content}\n</script>]
59
+ end
43
60
  end
44
61
  end
@@ -0,0 +1,5 @@
1
+ /* Here is Comment */
2
+
3
+ body {
4
+ color: #f00;
5
+ }
@@ -0,0 +1,5 @@
1
+ // Here is Comment
2
+
3
+ !function() {
4
+ alert(1);
5
+ }()
@@ -0,0 +1,5 @@
1
+ /* Here is Comment */
2
+
3
+ body {
4
+ background: url(resouces/test.png);
5
+ }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gatchaman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-14 00:00:00.000000000 Z
12
+ date: 2013-02-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -79,7 +79,10 @@ files:
79
79
  - lib/gatchaman.rb
80
80
  - lib/gatchaman/version.rb
81
81
  - spec/gatchaman_spec.rb
82
+ - spec/resouces/test.css
83
+ - spec/resouces/test.js
82
84
  - spec/resouces/test.png
85
+ - spec/resouces/test_2.css
83
86
  - spec/spec_helper.rb
84
87
  homepage: https://github.com/Shinpeim/Gatchaman
85
88
  licenses: []
@@ -108,5 +111,8 @@ summary: Gatchaman is a gem to replace src values in HTML documents with data UR
108
111
  scheme
109
112
  test_files:
110
113
  - spec/gatchaman_spec.rb
114
+ - spec/resouces/test.css
115
+ - spec/resouces/test.js
111
116
  - spec/resouces/test.png
117
+ - spec/resouces/test_2.css
112
118
  - spec/spec_helper.rb