snatch 0.0.10 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +15 -0
- data/VERSION +1 -1
- data/lib/snatch/clean/css.rb +56 -0
- data/lib/snatch/clean/html.rb +74 -0
- data/lib/snatch/clean.rb +2 -60
- data/lib/snatch.rb +9 -2
- data/snatch.gemspec +15 -6
- data/spec/snatch/clean/css_spec.rb +78 -0
- data/spec/snatch/clean/html_spec.rb +126 -0
- data/spec/snatch/clean_spec.rb +7 -86
- data/spec/snatch_spec.rb +0 -3
- data/spec/spec_helper.rb +8 -0
- data/spec/support/matchers/nokogiri.rb +36 -0
- metadata +11 -2
data/.autotest
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
begin
|
2
|
+
require 'autotest/growl'
|
3
|
+
require 'autotest/fsevent'
|
4
|
+
|
5
|
+
Autotest::Growl::show_modified_files = true
|
6
|
+
rescue LoadError
|
7
|
+
$stderr.puts "Failed to load Mac-specific autotest enhancements"
|
8
|
+
end
|
9
|
+
|
10
|
+
Autotest.add_hook :initialize do |autotest|
|
11
|
+
exceptions = %w[ .git .svn .hg .DS_Store ._* ]
|
12
|
+
exceptions.each do |exception|
|
13
|
+
autotest.add_exception(exception)
|
14
|
+
end
|
15
|
+
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
1.0.0
|
@@ -0,0 +1,56 @@
|
|
1
|
+
class Snatch
|
2
|
+
class Clean
|
3
|
+
class CSS
|
4
|
+
attr_accessor :doc, :working_directory
|
5
|
+
|
6
|
+
def initialize(doc, working_directory)
|
7
|
+
self.doc = doc
|
8
|
+
self.working_directory = working_directory
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.update(nokogiri_doc, working_directory = nil)
|
12
|
+
new(nokogiri_doc, working_directory).update
|
13
|
+
end
|
14
|
+
|
15
|
+
def update
|
16
|
+
doc.css('link[rel=stylesheet]').each do |stylesheet_node|
|
17
|
+
stylesheet_node['href'] = rewrite_href(stylesheet_node['href'])
|
18
|
+
end
|
19
|
+
doc
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def extract_path_components(href)
|
25
|
+
m = href.match(%r{^(.+)?stylesheet\.php\?cssid=(\d+)(?:&|&)mediatype=(\w+)})
|
26
|
+
m.nil? ? nil : m.to_a[1..-1].compact
|
27
|
+
end
|
28
|
+
|
29
|
+
def expand_paths(*paths)
|
30
|
+
paths.map { |path| File.expand_path(File.join(@working_directory, path)) }
|
31
|
+
end
|
32
|
+
|
33
|
+
def mv_stylesheet(php_path, css_path)
|
34
|
+
php_path, css_path = *expand_paths(php_path, css_path)
|
35
|
+
FileUtils.mv(php_path, css_path) if File.exist?(php_path)
|
36
|
+
end
|
37
|
+
|
38
|
+
def rewrite_href(href)
|
39
|
+
return href if href.empty?
|
40
|
+
css_path = href
|
41
|
+
matches = extract_path_components(href)
|
42
|
+
|
43
|
+
unless matches.nil?
|
44
|
+
path = matches.size == 3 ? matches.shift : nil
|
45
|
+
|
46
|
+
file_name = matches.join('-')
|
47
|
+
css_path = File.join(*[path, "#{file_name}.css"].compact)
|
48
|
+
|
49
|
+
mv_stylesheet(href, css_path)
|
50
|
+
end
|
51
|
+
|
52
|
+
css_path
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
class Snatch
|
2
|
+
class Clean
|
3
|
+
class HTML
|
4
|
+
module HrefFixMethods
|
5
|
+
def remove_index_html(a)
|
6
|
+
a['href'] = a['href'].sub(%r{index\.html?$}, '')
|
7
|
+
end
|
8
|
+
|
9
|
+
def replace_absolute(a)
|
10
|
+
a['href'] = a['href'].sub(%r{(https?)://#{MARKETING_SITE}/}, '/')
|
11
|
+
end
|
12
|
+
|
13
|
+
def encode_mailtos(a)
|
14
|
+
if a['href'] =~ /^mailto:(.*)/
|
15
|
+
a['href'] = 'mailto:' + url_encode($1)
|
16
|
+
a.inner_html = html_encode(a.inner_html)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def prepend_slash(a)
|
21
|
+
a['href'] = a['href'].sub(%r{^/?}, '/') unless a['href'].include?(':')
|
22
|
+
end
|
23
|
+
|
24
|
+
def append_slash(a)
|
25
|
+
slash_required = (%w(# : .) & a['href'].split(//)).empty?
|
26
|
+
a['href'] = a['href'].sub(%r{/?$}, '/') if slash_required
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
module SrcFixMethods
|
31
|
+
def replace_absolute(link)
|
32
|
+
link['src'] = link['src'].sub(%r{(https?)://#{MARKETING_SITE}/}, '/')
|
33
|
+
end
|
34
|
+
|
35
|
+
def rewrite_uploads(link)
|
36
|
+
link['src'] = link['src'].sub(%r{^#{UPLOADS_DIR}/}, "/#{UPLOADS_DIR}/")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
attr_accessor :doc, :working_directory
|
41
|
+
|
42
|
+
def initialize(doc, working_directory)
|
43
|
+
@doc = doc
|
44
|
+
@working_directory = working_directory
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.update(doc, working_directory)
|
48
|
+
new(doc, working_directory).update
|
49
|
+
end
|
50
|
+
|
51
|
+
def html_encode(string)
|
52
|
+
string.gsub(/./){ |char| "&#x#{char.unpack('U')[0].to_s(16)};" }
|
53
|
+
end
|
54
|
+
|
55
|
+
def url_encode(string)
|
56
|
+
string.gsub(/./) { |char| '%' + char.unpack('H2' * char.size).join('%').upcase }
|
57
|
+
end
|
58
|
+
|
59
|
+
def update
|
60
|
+
@doc.css('base, meta[generator]').each { |node| node.remove }
|
61
|
+
|
62
|
+
@doc.children.reject! { |child| child.comment? }
|
63
|
+
|
64
|
+
HrefFixMethods.instance_methods.each do |m|
|
65
|
+
@doc.css('a[href]').each { |a| send m, a }
|
66
|
+
end
|
67
|
+
|
68
|
+
SrcFixMethods.instance_methods.each do |m|
|
69
|
+
@doc.css('[src]').each { |a| send m, a }
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/lib/snatch/clean.rb
CHANGED
@@ -10,9 +10,6 @@ class Snatch
|
|
10
10
|
@doc = Nokogiri::XML(File.open(@file_name, 'r'))
|
11
11
|
end
|
12
12
|
|
13
|
-
# Convenience method for creating Snatch::Clean with HTML.
|
14
|
-
#
|
15
|
-
# Returns instance of Snatch::Clean
|
16
13
|
def self.process(html, working_directory = nil)
|
17
14
|
instance = new(html, working_directory)
|
18
15
|
instance.process
|
@@ -20,65 +17,10 @@ class Snatch
|
|
20
17
|
end
|
21
18
|
|
22
19
|
def process
|
23
|
-
@doc
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
@doc.css('a[href]').each do |anchor|
|
28
|
-
anchor['href'].sub!(%r{\.html?$}, '')
|
29
|
-
end
|
20
|
+
CSS.update(@doc, @working_directory)
|
21
|
+
HTML.update(@doc, @working_directory)
|
30
22
|
|
31
23
|
File.open(@file_name, 'w') { |f| f.write @doc.to_html }
|
32
24
|
end
|
33
|
-
|
34
|
-
private
|
35
|
-
def log(*messages)
|
36
|
-
width = messages.max(&:size).size
|
37
|
-
puts "\e[36;1m#{messages.map(&:inspect).join("\n")}\e[0m"
|
38
|
-
puts '=' * width.to_i
|
39
|
-
end
|
40
|
-
|
41
|
-
def rewrite_href(href)
|
42
|
-
return href unless href.present?
|
43
|
-
css_path = href
|
44
|
-
matches = extract_path_components(href)
|
45
|
-
|
46
|
-
if matches.present?
|
47
|
-
path = matches.size == 3 ? matches.shift : nil
|
48
|
-
|
49
|
-
file_name = matches.join('-')
|
50
|
-
css_path = File.join(*[path, "#{file_name}.css"].compact)
|
51
|
-
|
52
|
-
mv_stylesheet(href, css_path)
|
53
|
-
end
|
54
|
-
|
55
|
-
css_path
|
56
|
-
end
|
57
|
-
|
58
|
-
# Look for a match within our stylesheet link href. If it's there
|
59
|
-
# reject the original string from MatchData.
|
60
|
-
#
|
61
|
-
# Returns Array of matches or nil
|
62
|
-
def extract_path_components(href)
|
63
|
-
m = href.match(%r{^(.+)?stylesheet\.php\?cssid=(\d+)(?:&|&)mediatype=(\w+)})
|
64
|
-
m.present? ? m.to_a[1..-1].compact : nil
|
65
|
-
end
|
66
|
-
|
67
|
-
def remove_query_params(href)
|
68
|
-
href.sub(%r{\.php\?.*?$}, '.php')
|
69
|
-
end
|
70
|
-
|
71
|
-
# Convert any number of paths in to absolute paths prepending with
|
72
|
-
# the public path (e.g. /Users/jcf/git/static/public/#{path}).
|
73
|
-
#
|
74
|
-
# Returns an Array of expanded paths
|
75
|
-
def expand_paths(*paths)
|
76
|
-
paths.map { |path| File.expand_path(File.join(@working_directory, path)) }
|
77
|
-
end
|
78
|
-
|
79
|
-
def mv_stylesheet(php_path, css_path)
|
80
|
-
php_path, @css_path = *expand_paths(php_path, css_path)
|
81
|
-
FileUtils.mv(php_path, @css_path) if File.exist?(php_path)
|
82
|
-
end
|
83
25
|
end
|
84
26
|
end
|
data/lib/snatch.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
2
|
|
3
|
-
require "
|
3
|
+
require "extensions"
|
4
4
|
require 'snatch/clean'
|
5
|
+
require 'snatch/clean/html'
|
6
|
+
require 'snatch/clean/css'
|
5
7
|
|
6
8
|
class Snatch
|
9
|
+
RAILS_ROOT = Dir.pwd unless defined?(RAILS_ROOT)
|
7
10
|
RAILS_PUBLIC_ASSETS = [
|
8
11
|
'404.html',
|
9
12
|
'422.html',
|
@@ -19,6 +22,9 @@ class Snatch
|
|
19
22
|
].map { |file_name| File.expand_path("#{RAILS_ROOT}/public/#{file_name}") }
|
20
23
|
PUBLIC_PATH = File.expand_path("#{Dir.pwd}/public")
|
21
24
|
|
25
|
+
MARKETING_SITE = 'cms.alphasights-002.vm.brightbox.net'
|
26
|
+
UPLOADS_DIR = 'uploads'
|
27
|
+
|
22
28
|
def initialize(url = nil)
|
23
29
|
@url = url || 'www.google.com'
|
24
30
|
end
|
@@ -81,7 +87,8 @@ class Snatch
|
|
81
87
|
def remove_cms_files
|
82
88
|
glob_path = File.expand_path("#{RAILS_ROOT}/public") + '/*'
|
83
89
|
Pathname.glob(glob_path) do |pathname|
|
84
|
-
|
90
|
+
public_path = pathname.expand_path("#{RAILS_ROOT}/public").to_s
|
91
|
+
FileUtils.rm_rf(pathname.to_s) unless RAILS_PUBLIC_ASSETS.include?(public_path)
|
85
92
|
end
|
86
93
|
end
|
87
94
|
|
data/snatch.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{snatch}
|
8
|
-
s.version = "0.0
|
8
|
+
s.version = "1.0.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["James Conroy-Finn"]
|
12
|
-
s.date = %q{2010-02-
|
12
|
+
s.date = %q{2010-02-15}
|
13
13
|
s.description = %q{Simple site downloaded that wraps wget and converts PHP CSS files in to regular CSS files.}
|
14
14
|
s.email = %q{james@logi.cl}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -17,7 +17,8 @@ Gem::Specification.new do |s|
|
|
17
17
|
"README.rdoc"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
|
-
".
|
20
|
+
".autotest",
|
21
|
+
".document",
|
21
22
|
".gitignore",
|
22
23
|
"LICENSE",
|
23
24
|
"README.rdoc",
|
@@ -26,11 +27,16 @@ Gem::Specification.new do |s|
|
|
26
27
|
"lib/extensions.rb",
|
27
28
|
"lib/snatch.rb",
|
28
29
|
"lib/snatch/clean.rb",
|
30
|
+
"lib/snatch/clean/css.rb",
|
31
|
+
"lib/snatch/clean/html.rb",
|
29
32
|
"snatch.gemspec",
|
33
|
+
"spec/snatch/clean/css_spec.rb",
|
34
|
+
"spec/snatch/clean/html_spec.rb",
|
30
35
|
"spec/snatch/clean_spec.rb",
|
31
36
|
"spec/snatch_spec.rb",
|
32
37
|
"spec/spec.opts",
|
33
|
-
"spec/spec_helper.rb"
|
38
|
+
"spec/spec_helper.rb",
|
39
|
+
"spec/support/matchers/nokogiri.rb"
|
34
40
|
]
|
35
41
|
s.homepage = %q{http://github.com/jcf/snatch}
|
36
42
|
s.rdoc_options = ["--charset=UTF-8"]
|
@@ -38,9 +44,12 @@ Gem::Specification.new do |s|
|
|
38
44
|
s.rubygems_version = %q{1.3.5}
|
39
45
|
s.summary = %q{wget your site and replace any nasty PHP CSS files}
|
40
46
|
s.test_files = [
|
41
|
-
"spec/snatch/
|
47
|
+
"spec/snatch/clean/css_spec.rb",
|
48
|
+
"spec/snatch/clean/html_spec.rb",
|
49
|
+
"spec/snatch/clean_spec.rb",
|
42
50
|
"spec/snatch_spec.rb",
|
43
|
-
"spec/spec_helper.rb"
|
51
|
+
"spec/spec_helper.rb",
|
52
|
+
"spec/support/matchers/nokogiri.rb"
|
44
53
|
]
|
45
54
|
|
46
55
|
if s.respond_to? :specification_version then
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Snatch::Clean::CSS do
|
4
|
+
before(:each) do
|
5
|
+
File.stub!(:open)
|
6
|
+
@css = Snatch::Clean::CSS.new('nokogiri_doc', 'public')
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "rewrite_href" do
|
10
|
+
it "should do nothing without an href" do
|
11
|
+
@css.send(:rewrite_href, '').should == ''
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should move a top-level stylesheet file" do
|
15
|
+
href = 'stylesheet.php?cssid=12&mediatype=screen'
|
16
|
+
@css.should_receive(:mv_stylesheet).with(href, '12-screen.css')
|
17
|
+
@css.send(:rewrite_href, href)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should move a nested stylesheet file" do
|
21
|
+
href = '../stylesheet.php?cssid=12&mediatype=screen'
|
22
|
+
@css.should_receive(:mv_stylesheet).with(href, '../12-screen.css')
|
23
|
+
@css.send(:rewrite_href, href)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "extract_path_components" do
|
28
|
+
it "should find parents and values for cssid and mediatype" do
|
29
|
+
php_path = '/css/something/stylesheet.php?cssid=12&mediatype=screen'
|
30
|
+
path = @css.send(:extract_path_components, php_path)
|
31
|
+
path.to_a.should == ['/css/something/', '12', 'screen']
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should find relative path compontents and values for cssid and mediatype" do
|
35
|
+
php_path = '../../stylesheet.php?cssid=12&mediatype=screen'
|
36
|
+
path = @css.send(:extract_path_components, php_path)
|
37
|
+
path.to_a.should == ['../../', '12', 'screen']
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should find values for cssid and mediatype" do
|
41
|
+
php_path = 'stylesheet.php?cssid=12&mediatype=screen'
|
42
|
+
path = @css.send(:extract_path_components, php_path)
|
43
|
+
path.to_a.should == ['12', 'screen']
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "moving PHP files to CSS path" do
|
48
|
+
it "should expand multiple paths to include the public directory" do
|
49
|
+
public_path = Snatch::PUBLIC_PATH
|
50
|
+
expected = [
|
51
|
+
File.expand_path(File.join(public_path, 'a/b')),
|
52
|
+
File.expand_path(File.join(public_path, 'c/d'))
|
53
|
+
]
|
54
|
+
@css.send(:expand_paths, 'a/b', 'c/d').should == expected
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should assign multiple paths with a splat" do
|
58
|
+
public_path = Snatch::PUBLIC_PATH
|
59
|
+
expected = [
|
60
|
+
File.expand_path(File.join(public_path, 'a/b')),
|
61
|
+
File.expand_path(File.join(public_path, 'c/d')),
|
62
|
+
File.expand_path(File.join(public_path, 'e/f'))
|
63
|
+
]
|
64
|
+
a, b, c = *@css.send(:expand_paths, 'a/b', 'c/d', 'e/f')
|
65
|
+
a.should == expected.first
|
66
|
+
b.should == expected[1]
|
67
|
+
c.should == expected.last
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should expand absolute paths to include the public directory" do
|
71
|
+
public_path = Snatch::PUBLIC_PATH
|
72
|
+
expected = [
|
73
|
+
File.expand_path(File.join(public_path, '/a/b'))
|
74
|
+
]
|
75
|
+
@css.send(:expand_paths, '/a/b').should == expected
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Snatch::Clean::HTML do
|
5
|
+
before(:each) do
|
6
|
+
@html = Snatch::Clean::HTML.new('nokogiri_doc', 'public')
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "Removing crap" do
|
10
|
+
before do
|
11
|
+
@html.doc.should_receive(:css).with('a[href]').any_number_of_times.and_return([])
|
12
|
+
@html.doc.should_receive(:css).with('[src]').any_number_of_times.and_return([])
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should remove any base tags' do
|
16
|
+
mock_nodes = [(mock_node = mock('nokogiri_node'))]
|
17
|
+
@html.doc.should_receive(:css).with('base, meta[generator]').and_return(mock_nodes)
|
18
|
+
@html.doc.should_receive(:children).and_return([])
|
19
|
+
mock_nodes.should_receive(:each).and_yield(mock_node)
|
20
|
+
mock_node.should_receive(:remove)
|
21
|
+
@html.send(:update)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should remove comments and the generator meta tag' do
|
25
|
+
mock_nodes = [(mock_node = mock('nokogiri_node'))]
|
26
|
+
@html.doc.should_receive(:css).with('base, meta[generator]').and_return([])
|
27
|
+
@html.doc.should_receive(:children).twice.and_return(mock_nodes)
|
28
|
+
mock_node.should_receive(:comment?).and_return(true)
|
29
|
+
@html.send(:update)
|
30
|
+
@html.doc.children.should be_empty
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe Snatch::Clean::HTML::HrefFixMethods do
|
35
|
+
subject { mock.extend(Snatch::Clean::HTML::HrefFixMethods) }
|
36
|
+
|
37
|
+
it 'should remove a trailing index.html' do
|
38
|
+
fix_node(:remove_index_html, '<a href="/blah/index.html"></a>') do |node|
|
39
|
+
node.should have_href('/blah/')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should replace an absolute CMS URL with a domainless absolute URL' do
|
44
|
+
anchor = %Q{<a href="http://#{Snatch::MARKETING_SITE}/folder"></a>}
|
45
|
+
fix_node(:replace_absolute, anchor) do |node|
|
46
|
+
node.should have_href('/folder')
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should encode email addresses' do
|
51
|
+
anchor = %Q{<a href="mailto:blah@exåmplé.cøm">blah@exåmplé.cøm</a>}
|
52
|
+
subject.should_receive(:url_encode).and_return('url_encode')
|
53
|
+
subject.should_receive(:html_encode).and_return('html_encode')
|
54
|
+
fix_node(:encode_mailtos, anchor) do |node|
|
55
|
+
node.should have_href('mailto:url_encode')
|
56
|
+
node.text.should == 'html_encode'
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "leading slashes and colons" do
|
61
|
+
it 'should append a slash when there is no colon' do
|
62
|
+
fix_node(:prepend_slash, %Q{<a href="blah/file.txt"></a>}) do |node|
|
63
|
+
node.should have_href('/blah/file.txt')
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should not append a slash when there is a colon' do
|
68
|
+
fix_node(:prepend_slash, %Q{<a href="blah/file:wtf.txt"></a>}) do |node|
|
69
|
+
node.should have_href('blah/file:wtf.txt')
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'should not add two slashes' do
|
74
|
+
fix_node(:prepend_slash, %Q{<a href="/blah/file.txt"></a>}) do |node|
|
75
|
+
node.should have_href('/blah/file.txt')
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "trailing slash" do
|
81
|
+
it 'with a period' do
|
82
|
+
fix_node(:append_slash, %Q{<a href="file.extension"></a>}) do |node|
|
83
|
+
node.should have_href('file.extension')
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'with a hash' do
|
88
|
+
fix_node(:append_slash, %Q{<a href="blah#anchor"></a>"}) do |node|
|
89
|
+
node.should have_href('blah#anchor')
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'with a colon' do
|
94
|
+
# When would this ever happen?
|
95
|
+
fix_node(:append_slash, %Q{<a href="user:pass@domain.com"></a>}) do |node|
|
96
|
+
node.should have_href('user:pass@domain.com')
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'should not duplicate slashes' do
|
101
|
+
fix_node(:append_slash, %Q{<a href="folder/"></a>}) do |node|
|
102
|
+
node.should have_href('folder/')
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe Snatch::Clean::HTML::SrcFixMethods do
|
109
|
+
subject { mock.extend(Snatch::Clean::HTML::SrcFixMethods) }
|
110
|
+
|
111
|
+
it 'should replace an absolute CMS URL with a domainless absolute URL' do
|
112
|
+
link = %Q{<link src="http://#{Snatch::MARKETING_SITE}/folder"></a>}
|
113
|
+
fix_node(:replace_absolute, link) do |node|
|
114
|
+
node.should have_src('/folder')
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'should make upload URLs absolute and root' do
|
119
|
+
link = %Q{<link src="uploads/some-image.png"></link>}
|
120
|
+
fix_node(:rewrite_uploads, link) do |node|
|
121
|
+
node.should have_src('/uploads/some-image.png')
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
data/spec/snatch/clean_spec.rb
CHANGED
@@ -2,93 +2,14 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Snatch::Clean do
|
4
4
|
before(:each) do
|
5
|
-
File.stub!(:open)
|
5
|
+
File.stub!(:open).and_return('stream')
|
6
|
+
@clean = Snatch::Clean.new('file_name', 'public')
|
6
7
|
end
|
7
8
|
|
8
|
-
it
|
9
|
-
clean
|
10
|
-
|
11
|
-
|
12
|
-
clean.
|
13
|
-
end
|
14
|
-
|
15
|
-
describe "rewrite_href" do
|
16
|
-
before(:each) do
|
17
|
-
@clean = Snatch::Clean.new('file_name')
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should do nothing without an href" do
|
21
|
-
@clean.send(:rewrite_href, '').should == ''
|
22
|
-
end
|
23
|
-
|
24
|
-
it "should move a top-level stylesheet file" do
|
25
|
-
href = 'stylesheet.php?cssid=12&mediatype=screen'
|
26
|
-
@clean.should_receive(:mv_stylesheet).with(href, '12-screen.css')
|
27
|
-
@clean.send(:rewrite_href, href)
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should move a nested stylesheet file" do
|
31
|
-
href = '../stylesheet.php?cssid=12&mediatype=screen'
|
32
|
-
@clean.should_receive(:mv_stylesheet).with(href, '../12-screen.css')
|
33
|
-
@clean.send(:rewrite_href, href)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
describe "extract_path_components" do
|
38
|
-
it "should find parents and values for cssid and mediatype" do
|
39
|
-
clean = Snatch::Clean.new('file_name')
|
40
|
-
php_path = '/css/something/stylesheet.php?cssid=12&mediatype=screen'
|
41
|
-
path = clean.send(:extract_path_components, php_path)
|
42
|
-
path.to_a.should == ['/css/something/', '12', 'screen']
|
43
|
-
end
|
44
|
-
|
45
|
-
it "should find relative path compontents and values for cssid and mediatype" do
|
46
|
-
clean = Snatch::Clean.new('file_name')
|
47
|
-
php_path = '../../stylesheet.php?cssid=12&mediatype=screen'
|
48
|
-
path = clean.send(:extract_path_components, php_path)
|
49
|
-
path.to_a.should == ['../../', '12', 'screen']
|
50
|
-
end
|
51
|
-
|
52
|
-
it "should find values for cssid and mediatype" do
|
53
|
-
clean = Snatch::Clean.new('file_name')
|
54
|
-
php_path = 'stylesheet.php?cssid=12&mediatype=screen'
|
55
|
-
path = clean.send(:extract_path_components, php_path)
|
56
|
-
path.to_a.should == ['12', 'screen']
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
describe "moving PHP files to CSS path" do
|
61
|
-
it "should expand multiple paths to include the public directory" do
|
62
|
-
clean = Snatch::Clean.new('file_name')
|
63
|
-
public_path = Snatch::PUBLIC_PATH
|
64
|
-
expected = [
|
65
|
-
File.expand_path(File.join(public_path, 'a/b')),
|
66
|
-
File.expand_path(File.join(public_path, 'c/d'))
|
67
|
-
]
|
68
|
-
clean.send(:expand_paths, 'a/b', 'c/d').should == expected
|
69
|
-
end
|
70
|
-
|
71
|
-
it "should assign multiple paths with a splat" do
|
72
|
-
clean = Snatch::Clean.new('file_name')
|
73
|
-
public_path = Snatch::PUBLIC_PATH
|
74
|
-
expected = [
|
75
|
-
File.expand_path(File.join(public_path, 'a/b')),
|
76
|
-
File.expand_path(File.join(public_path, 'c/d')),
|
77
|
-
File.expand_path(File.join(public_path, 'e/f'))
|
78
|
-
]
|
79
|
-
a, b, c = *clean.send(:expand_paths, 'a/b', 'c/d', 'e/f')
|
80
|
-
a.should == expected.first
|
81
|
-
b.should == expected.second
|
82
|
-
c.should == expected.last
|
83
|
-
end
|
84
|
-
|
85
|
-
it "should expand absolute paths to include the public directory" do
|
86
|
-
clean = Snatch::Clean.new('file_name')
|
87
|
-
public_path = Snatch::PUBLIC_PATH
|
88
|
-
expected = [
|
89
|
-
File.expand_path(File.join(public_path, '/a/b'))
|
90
|
-
]
|
91
|
-
clean.send(:expand_paths, '/a/b').should == expected
|
92
|
-
end
|
9
|
+
it 'should update CSS and HTML' do
|
10
|
+
@clean.instance_variable_set(:@doc, 'nokogiri')
|
11
|
+
Snatch::Clean::CSS.should_receive(:update).with('nokogiri', 'public')
|
12
|
+
Snatch::Clean::HTML.should_receive(:update).with('nokogiri', 'public')
|
13
|
+
@clean.process
|
93
14
|
end
|
94
15
|
end
|
data/spec/snatch_spec.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -4,6 +4,14 @@ require 'snatch'
|
|
4
4
|
require 'spec'
|
5
5
|
require 'spec/autorun'
|
6
6
|
|
7
|
+
Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each { |f| require f }
|
8
|
+
|
7
9
|
Spec::Runner.configure do |config|
|
8
10
|
|
9
11
|
end
|
12
|
+
|
13
|
+
def fix_node(method, xhtml)
|
14
|
+
node = Nokogiri::XML(xhtml).children.first
|
15
|
+
subject.send method, node
|
16
|
+
yield node
|
17
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
Spec::Matchers.define :have_href do |expected|
|
2
|
+
match do |actual|
|
3
|
+
expected.should == actual['href']
|
4
|
+
end
|
5
|
+
|
6
|
+
failure_message_for_should do |actual|
|
7
|
+
"expected href to equal #{expected} but got #{actual['href']}"
|
8
|
+
end
|
9
|
+
|
10
|
+
failure_message_for_should_not do |actual|
|
11
|
+
"expected href to not equal #{expected}"
|
12
|
+
end
|
13
|
+
|
14
|
+
description do
|
15
|
+
"be equal to #{expected}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Spec::Matchers.define :have_src do |expected|
|
20
|
+
match do |actual|
|
21
|
+
expected.should == actual['src']
|
22
|
+
end
|
23
|
+
|
24
|
+
failure_message_for_should do |actual|
|
25
|
+
"expected src to equal #{expected} but got #{actual['src']}"
|
26
|
+
end
|
27
|
+
|
28
|
+
failure_message_for_should_not do |actual|
|
29
|
+
"expected src to not equal #{expected}"
|
30
|
+
end
|
31
|
+
|
32
|
+
description do
|
33
|
+
"be equal to #{expected}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: snatch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Conroy-Finn
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-02-
|
12
|
+
date: 2010-02-15 00:00:00 +00:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -42,6 +42,7 @@ extra_rdoc_files:
|
|
42
42
|
- LICENSE
|
43
43
|
- README.rdoc
|
44
44
|
files:
|
45
|
+
- .autotest
|
45
46
|
- .document
|
46
47
|
- .gitignore
|
47
48
|
- LICENSE
|
@@ -51,11 +52,16 @@ files:
|
|
51
52
|
- lib/extensions.rb
|
52
53
|
- lib/snatch.rb
|
53
54
|
- lib/snatch/clean.rb
|
55
|
+
- lib/snatch/clean/css.rb
|
56
|
+
- lib/snatch/clean/html.rb
|
54
57
|
- snatch.gemspec
|
58
|
+
- spec/snatch/clean/css_spec.rb
|
59
|
+
- spec/snatch/clean/html_spec.rb
|
55
60
|
- spec/snatch/clean_spec.rb
|
56
61
|
- spec/snatch_spec.rb
|
57
62
|
- spec/spec.opts
|
58
63
|
- spec/spec_helper.rb
|
64
|
+
- spec/support/matchers/nokogiri.rb
|
59
65
|
has_rdoc: true
|
60
66
|
homepage: http://github.com/jcf/snatch
|
61
67
|
licenses: []
|
@@ -85,6 +91,9 @@ signing_key:
|
|
85
91
|
specification_version: 3
|
86
92
|
summary: wget your site and replace any nasty PHP CSS files
|
87
93
|
test_files:
|
94
|
+
- spec/snatch/clean/css_spec.rb
|
95
|
+
- spec/snatch/clean/html_spec.rb
|
88
96
|
- spec/snatch/clean_spec.rb
|
89
97
|
- spec/snatch_spec.rb
|
90
98
|
- spec/spec_helper.rb
|
99
|
+
- spec/support/matchers/nokogiri.rb
|