scene7ize 0.2.4 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,3 +15,5 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ scene7ize.sublime-project
19
+ scene7ize.sublime-workspace
data/README.md CHANGED
@@ -10,9 +10,10 @@ Automatically assumes JPGs have been optimized, default quality is set to 100.
10
10
 
11
11
  ## Usage
12
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.
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
+ - Fourth argument (optional) is a base path. If provided, images will be reference from it - otherwise they will be referenced relative to the input file.
16
17
 
17
18
  To run in "safe mode", specify an output file:
18
19
 
data/lib/scene7ize/cli.rb CHANGED
@@ -27,9 +27,14 @@ module Scene7ize
27
27
  :type => :string,
28
28
  :aliases => '-o',
29
29
  :banner => 'write to an output file instead of overwriting the input file'
30
+
31
+ method_option :base_path,
32
+ :type => :string,
33
+ :aliases => '-b',
34
+ :banner => 'sets a base path to reference the image, otherwise references relative to input file'
30
35
  def parse
31
36
 
32
- Scene7ize.parse_file(options[:url_prefix], options[:input_file], options[:output_file])
37
+ Scene7ize.parse_file(options[:url_prefix], options[:input_file], options[:output_file], options[:base_path])
33
38
 
34
39
  end
35
40
 
@@ -1,3 +1,3 @@
1
1
  module Scene7ize
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.6"
3
3
  end
data/lib/scene7ize.rb CHANGED
@@ -3,12 +3,18 @@ require 'mini_magick'
3
3
  require 'uri'
4
4
 
5
5
  module Scene7ize
6
- DEFAULT_REGEX = /(?<=['"\())])(?<dir_and_basename>((?!['"\)]).)*)\.(?<ext>gif|jpg|jpeg|png)(?=['"\)])/i
6
+ DEFAULT_REGEX = /(?<=[\('"])(?<dir_and_basename>((?!['"\)]).)*)\.(?<ext>gif|jpg|jpeg|png)(?=['"\)])/i
7
7
 
8
8
  class << self; attr_accessor :scene7prefix; end
9
9
 
10
10
  def self.scene7url_from(image_filename)
11
11
  # TODO: error handling
12
+
13
+ if !File.exists?(image_filename)
14
+ puts "ERROR: Could not open #{image_filename}. Leaving unchanged."
15
+ return false
16
+ end
17
+
12
18
  image = MiniMagick::Image.open(image_filename)
13
19
 
14
20
  suffix = case image[:format].downcase
@@ -31,20 +37,24 @@ module Scene7ize
31
37
 
32
38
  def self.replace(content)
33
39
  replacement = content.gsub(DEFAULT_REGEX) do |match|
34
- image_filename = "#{$~[:dir_and_basename]}.#{$~[:ext]}"
40
+ original_filename = "#{$~[:dir_and_basename]}.#{$~[:ext]}"
41
+
42
+ puts "matching #{original_filename}"
35
43
 
36
44
  # reconstruct image path relative to input file and open
37
- image_filename = File.join(@input_file_path, image_filename)
38
- self.scene7url_from(image_filename)
45
+ image_filename = File.join(@base_path || @input_file_path, original_filename)
46
+
47
+ self.scene7url_from(image_filename) || original_filename
39
48
  end
40
49
  end
41
50
 
42
51
 
43
- def self.parse_file(scene7prefix, input_file, output_file = nil)
52
+ def self.parse_file(scene7prefix, input_file, output_file = nil, base_path = nil)
44
53
  @scene7prefix = scene7prefix
45
54
 
46
55
  file_content = File.read(input_file)
47
56
  @input_file_path = File.dirname(input_file)
57
+ @base_path = File.path(base_path) if base_path
48
58
 
49
59
  replacement = replace(file_content)
50
60
 
data/scene7ize.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |gem|
10
10
  gem.email = ["brad@pixelwavedesign.com"]
11
11
  gem.description = %q{...}
12
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 = "https://github.com/codercarson/scene7ize"
13
+ gem.homepage = "https://github.com/codecarson/scene7ize"
14
14
 
15
15
  gem.files = `git ls-files`.split($/)
16
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -22,6 +22,8 @@ describe Scene7ize do
22
22
  @output_data = <<-CONTENTS
23
23
  <p id="jpg"><a href="#{@s7_url}"><img src="#{@s7_url}" alt="Test jpg"></a></p>
24
24
  CONTENTS
25
+
26
+ File.stub(:exists?)
25
27
  end
26
28
 
27
29
  it "should write to an output file when one is given" do
@@ -44,6 +46,17 @@ describe Scene7ize do
44
46
  expect { Scene7ize.parse_file('file-that-doesnt-exist.ext') }.to raise_error
45
47
  end
46
48
 
49
+ it "should parse files relative to a base bath, if one is given" do
50
+ base_path = "/code/my_project_root"
51
+
52
+ File.stub(:read).with(@input_filename) { @input_data }
53
+ File.stub(:open) #.with(@output_filename, 'w').and_return(@output_data)
54
+
55
+ Scene7ize.should_receive(:scene7url_from).with(File.join(base_path, "test-image.jpg")).any_number_of_times
56
+
57
+ Scene7ize.parse_file(@s7_url_prefix, @input_filename, @output_filename, base_path)
58
+ end
59
+
47
60
  end
48
61
 
49
62
 
@@ -73,9 +86,11 @@ describe Scene7ize do
73
86
  @image_filename = 'test.jpg'
74
87
  @image = { :width => 10, :height => 15, :format => 'jpg' }
75
88
 
89
+ File.stub(:exists?).and_return(true)
76
90
  MiniMagick::Image.should_receive(:open).and_return(@image)
77
91
  Scene7ize.should_receive(:scene7prefix).and_return("http://s7.example.com/")
78
92
  @scene7url = Scene7ize.scene7url_from(@image_filename)
93
+
79
94
  end
80
95
 
81
96
  it "should contain a specified prefix"
@@ -105,6 +120,7 @@ describe Scene7ize do
105
120
  @image_filename = 'test.png'
106
121
  @image = { :width => 10, :height => 15, :format => 'png' }
107
122
 
123
+ File.stub(:exists?).and_return(true)
108
124
  MiniMagick::Image.should_receive(:open).and_return(@image)
109
125
  Scene7ize.should_receive(:scene7prefix).and_return("http://s7.example.com/")
110
126
  @scene7url = Scene7ize.scene7url_from(@image_filename)
@@ -118,6 +134,7 @@ describe Scene7ize do
118
134
  @image_filename = 'test.gif'
119
135
  @image = { :width => 10, :height => 15, :format => 'gif' }
120
136
 
137
+ File.stub(:exists?).and_return(true)
121
138
  MiniMagick::Image.should_receive(:open).and_return(@image)
122
139
  Scene7ize.should_receive(:scene7prefix).and_return("http://s7.example.com/")
123
140
  @scene7url = Scene7ize.scene7url_from(@image_filename)
@@ -127,11 +144,12 @@ describe Scene7ize do
127
144
  @scene7url.should_not match /qlt=\d{1,}/
128
145
  end
129
146
 
130
- it "should throw an error if not a valid image file" do
147
+ it "should return false if not a valid image file" do
131
148
  @image_filename = 'test.png'
132
149
  Scene7ize.stub(:scene7prefix).and_return("http://s7.example.com/")
133
150
 
134
- expect { Scene7ize.scene7url_from(@image_filename) }.to raise_error
151
+ scene7url = Scene7ize.scene7url_from(@image_filename)
152
+ scene7url.should == false
135
153
  end
136
154
 
137
155
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scene7ize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.6
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: 2012-11-21 00:00:00.000000000 Z
12
+ date: 2013-03-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -95,7 +95,7 @@ files:
95
95
  - scene7ize.gemspec
96
96
  - spec/scene7ize_spec.rb
97
97
  - spec/spec_helper.rb
98
- homepage: https://github.com/codercarson/scene7ize
98
+ homepage: https://github.com/codecarson/scene7ize
99
99
  licenses: []
100
100
  post_install_message:
101
101
  rdoc_options: []
@@ -115,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
115
  version: '0'
116
116
  requirements: []
117
117
  rubyforge_project:
118
- rubygems_version: 1.8.23
118
+ rubygems_version: 1.8.24
119
119
  signing_key:
120
120
  specification_version: 3
121
121
  summary: Converts local image paths in an HTML or CSS file to Scene7 paths containing