scene7ize 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in scene7ize.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Brad Carson
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,38 @@
1
+ # Scene7ize
2
+
3
+ Converts local image paths in an HTML or CSS file to Scene7 paths containing dimensions and format info. Will make working with Scene7 a little less unbearable.
4
+
5
+ Automatically assumes JPGs have been optimized, default quality is set to 100.
6
+
7
+ ## Installation
8
+
9
+ $ gem install scene7ize
10
+
11
+ ## Usage
12
+
13
+ The first argument is the Scene7 URL prefix.
14
+ The second argument is an input file.
15
+ Third argument (optional) is an output file.
16
+
17
+ To run in "safe mode", specify an output file:
18
+
19
+ $ scene7ize http://s7.example.com index.html output.html
20
+
21
+ Or, omit the output file to replace contents of an input file.
22
+
23
+ $ scene7ize http://s7.example.com index.html
24
+
25
+ ## TODO
26
+
27
+ 1. Code documentation
28
+ 2. Better error handling and documentation for arguments
29
+ 3. Accept a default parameter to set JPEG quality
30
+ 4. Create a way to reverse the Scene7ize process and restore original paths (likely by searching path for image files)
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
data/bin/scene7ize ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'scene7ize'
4
+ require 'scene7ize/cli'
5
+
6
+ Scene7ize::CLI.start
@@ -0,0 +1,55 @@
1
+ require 'thor'
2
+
3
+ module Scene7ize
4
+
5
+ # Facade for Scene7ize command line interface managed by [Thor](https://github.com/wycats/thor).
6
+ # This is the main interface to Scene7ize that is called by the Scene7ize binary `bin/scene7ize`.
7
+ # Do not put any logic in here, create a class and delegate instead.
8
+ class CLI < Thor
9
+
10
+ require 'scene7ize'
11
+ require 'scene7ize/version'
12
+
13
+ desc 'parse', 'Parses an input file, replacing image URLs with Scene7 ones'
14
+ method_option :url_prefix,
15
+ :type => :string,
16
+ :aliases => '-u',
17
+ :banner => 'the Scene7 URL prefix',
18
+ :required => true
19
+
20
+ method_option :input_file,
21
+ :type => :string,
22
+ :aliases => '-i',
23
+ :banner => 'the input file to process',
24
+ :required => true
25
+
26
+ method_option :output_file,
27
+ :type => :string,
28
+ :aliases => '-o',
29
+ :banner => 'write to an output file instead of overwriting the input file'
30
+ def parse
31
+
32
+ Scene7ize.parse_file(options[:url_prefix], options[:input_file], options[:output_file])
33
+
34
+ end
35
+
36
+
37
+ desc 'version', 'Show the Guard version'
38
+ map %w(-v --version) => :version
39
+
40
+ # Shows the current version of Scene7ize.
41
+ #
42
+ # @see Scene7ize::VERSION
43
+ #
44
+ def version
45
+ puts "Scene7ize version #{ ::Scene7ize::VERSION }"
46
+ end
47
+
48
+ private
49
+
50
+ def method_missing(meth, *args)
51
+ help
52
+ end
53
+ end
54
+
55
+ end
@@ -0,0 +1,3 @@
1
+ module Scene7ize
2
+ VERSION = "0.2.0"
3
+ end
data/lib/scene7ize.rb ADDED
@@ -0,0 +1,48 @@
1
+ require "scene7ize/version"
2
+ require 'mini_magick'
3
+ require 'uri'
4
+
5
+ module Scene7ize
6
+ DEFAULT_REGEX = /(?<=['"])(?<dir_and_basename>((?!['"]).)*)\.(?<ext>gif|jpg|jpeg|png)(?=['"])/im
7
+
8
+
9
+ def self.scene7url_from(scene7prefix, image_filename)
10
+ # TODO: error handling
11
+ image = MiniMagick::Image.open(image_filename)
12
+
13
+ suffix = case image[:format].downcase
14
+ when 'jpg', 'jpeg'
15
+ '&qlt=100'
16
+ when 'png'
17
+ '&fmt=png-alpha'
18
+ when 'gif'
19
+ '&fmt=gif-alpha'
20
+ else
21
+ ""
22
+ end
23
+
24
+ basename = File.basename(image_filename, File.extname(image_filename))
25
+
26
+ # TODO: error handle URI::InvalidURIError
27
+ URI.join(scene7prefix, "#{basename}?wid=#{image[:width]}&hei=#{image[:height]}#{suffix}").to_s
28
+ end
29
+
30
+
31
+ def self.parse_file(scene7prefix, input_file, output_file = nil)
32
+ file_content = File.read(input_file)
33
+ input_file_path = File.dirname(input_file)
34
+
35
+ replacement = file_content.gsub!(DEFAULT_REGEX) do |match|
36
+ image_filename = "#{$~[:dir_and_basename]}.#{$~[:ext]}"
37
+
38
+ # reconstruct image path relative to input file and open
39
+ image_filename = File.join(input_file_path, image_filename)
40
+ self.scene7url_from(scene7prefix, image_filename)
41
+ end
42
+
43
+ output_file = input_file if output_file.nil?
44
+ File.open(output_file, "w") { |file| file.puts replacement }
45
+
46
+ end
47
+
48
+ end
data/scene7ize.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'scene7ize/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "scene7ize"
8
+ gem.version = Scene7ize::VERSION
9
+ gem.authors = ["Brad Carson"]
10
+ gem.email = ["brad@pixelwavedesign.com"]
11
+ gem.description = %q{...}
12
+ gem.summary = %q{Converts local image paths in an HTML or CSS file to Scene7 paths containing dimensions and format data}
13
+ gem.homepage = ""
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_development_dependency 'rake'
21
+ gem.add_development_dependency 'rspec'
22
+
23
+ gem.add_dependency 'thor'
24
+ gem.add_dependency 'mini_magick'
25
+
26
+ end
@@ -0,0 +1,117 @@
1
+ require 'spec_helper'
2
+
3
+ describe Scene7ize do
4
+
5
+ context "#parse_file" do
6
+
7
+ before(:each) do
8
+ @input_filename = 'index.html'
9
+ @output_filename = 'output.html'
10
+ @image = double("Image", :width => 10, :height => 15, :format => 'jpg')
11
+ @s7_url_prefix = 'http://s7.example.com'
12
+ @s7_url = "#{@s7_url_prefix}/test-image?wid=#{@image.width}&hei=#{@image.height}&qlt=100"
13
+
14
+ # I suspect that in this test we really don't even care about the file contents and could
15
+ # omit this from the test since we are really just ensuring a different output file is
16
+ # being written?
17
+
18
+ @input_data = <<-CONTENTS
19
+ <p id="jpg"><a href="test-image.jpg"><img src="test-image.jpg" alt="Test jpg"></a></p>
20
+ CONTENTS
21
+
22
+ @output_data = <<-CONTENTS
23
+ <p id="jpg"><a href="#{@s7_url}"><img src="#{@s7_url}" alt="Test jpg"></a></p>
24
+ CONTENTS
25
+ end
26
+
27
+ it "should write to an output file when one is given" do
28
+ File.stub(:read).with(@input_filename) { @input_data }
29
+ Scene7ize.stub(:scene7url_from)
30
+ File.should_not_receive(:open).with(@input_filename)
31
+ File.should_receive(:open).with(@output_filename, 'w').and_return(@output_data)
32
+ Scene7ize.parse_file(@s7_url_prefix, @input_filename, @output_filename)
33
+ end
34
+
35
+ it "should overwrite input file when an output is not specified" do
36
+ File.stub(:read).with(@input_filename) { @input_data }
37
+ Scene7ize.stub(:scene7url_from)
38
+ File.should_not_receive(:open).with(@output_filename)
39
+ File.should_receive(:open).with(@input_filename, 'w').and_return(@output_data)
40
+ Scene7ize.parse_file(@s7_url_prefix, @input_filename)
41
+ end
42
+
43
+ it "should display an error given an invalid input file" do
44
+ expect { Scene7ize.parse_file(@s7_url_prefix, 'file-that-doesnt-exist.ext') }.to raise_error
45
+ end
46
+
47
+ end
48
+
49
+
50
+
51
+ context "#scene7url_from" do
52
+
53
+ context "given a supported image" do
54
+ before(:each) do
55
+ @image_filename = 'test.jpg'
56
+ @image = { :width => 10, :height => 15, :format => 'jpg' }
57
+
58
+ MiniMagick::Image.should_receive(:open).and_return(@image)
59
+ @scene7url = Scene7ize.scene7url_from("http://example.com/scene7/", @image_filename)
60
+ end
61
+
62
+ it "should contain a specified prefix"
63
+
64
+ it "should create a valid scene7 url" do
65
+ @scene7url.should == "http://example.com/scene7/test?wid=10&hei=15&qlt=100"
66
+ end
67
+
68
+ it "should contain a valid width parameter" do
69
+ @scene7url.should match /wid=10/
70
+ end
71
+
72
+ it "should contain a valid height parameter" do
73
+ @scene7url.should match /hei=15/
74
+ end
75
+
76
+ it "should contain a quality format parameter" do
77
+ @scene7url.should match /qlt=100/
78
+ end
79
+
80
+ it "should not contain a file extension" do
81
+ @scene7url.should_not match /\.(jpg|png|jpeg|gif)\?/
82
+ end
83
+ end
84
+
85
+ it "given a PNG, should contain a valid format parameter" do
86
+ @image_filename = 'test.png'
87
+ @image = { :width => 10, :height => 15, :format => 'png' }
88
+
89
+ MiniMagick::Image.should_receive(:open).and_return(@image)
90
+ @scene7url = Scene7ize.scene7url_from("http://example.com/scene7/", @image_filename)
91
+
92
+ @scene7url.should match /fmt=png-alpha/
93
+ @scene7url.should_not match /fmt=gif-alpha/
94
+ @scene7url.should_not match /qlt=\d{1,}/
95
+ end
96
+
97
+ it "given a GIF, should contain a valid format parameter" do
98
+ @image_filename = 'test.gif'
99
+ @image = { :width => 10, :height => 15, :format => 'gif' }
100
+
101
+ MiniMagick::Image.should_receive(:open).and_return(@image)
102
+ @scene7url = Scene7ize.scene7url_from("http://example.com/scene7/", @image_filename)
103
+
104
+ @scene7url.should match /fmt=gif-alpha/
105
+ @scene7url.should_not match /fmt=png-alpha/
106
+ @scene7url.should_not match /qlt=\d{1,}/
107
+ end
108
+
109
+ it "should throw an error if not a valid image file" do
110
+ @image_filename = 'test.png'
111
+
112
+ expect { Scene7ize.scene7url_from("http://example.com/scene7/", @image_filename) }.to raise_error
113
+ end
114
+
115
+ end
116
+
117
+ end
@@ -0,0 +1,2 @@
1
+ require 'scene7ize'
2
+
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scene7ize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brad Carson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
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: rspec
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: thor
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: mini_magick
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: ! '...'
79
+ email:
80
+ - brad@pixelwavedesign.com
81
+ executables:
82
+ - scene7ize
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - bin/scene7ize
92
+ - lib/scene7ize.rb
93
+ - lib/scene7ize/cli.rb
94
+ - lib/scene7ize/version.rb
95
+ - scene7ize.gemspec
96
+ - spec/scene7ize_spec.rb
97
+ - spec/spec_helper.rb
98
+ homepage: ''
99
+ licenses: []
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 1.8.23
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: Converts local image paths in an HTML or CSS file to Scene7 paths containing
122
+ dimensions and format data
123
+ test_files:
124
+ - spec/scene7ize_spec.rb
125
+ - spec/spec_helper.rb