deface 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +11 -5
- data/Rakefile +0 -19
- data/deface.gemspec +2 -2
- data/lib/deface.rb +4 -0
- data/lib/deface/override.rb +29 -6
- data/lib/deface/railtie.rb +7 -0
- data/lib/deface/template_helper.rb +9 -0
- data/spec/deface/override_spec.rb +31 -0
- data/tasks/deface.rake +26 -0
- metadata +6 -4
data/README.markdown
CHANGED
@@ -41,10 +41,15 @@ Source
|
|
41
41
|
|
42
42
|
* <tt>:template</tt> - Relative path to a template
|
43
43
|
|
44
|
+
|
44
45
|
Optional
|
45
46
|
--------
|
46
47
|
* <tt>:name</tt> - Unique name for override so it can be identified and modified later. This needs to be unique within the same `:virtual_path`
|
47
48
|
|
49
|
+
* <tt>:disabled</tt> - When set to true the override will not be applied.
|
50
|
+
|
51
|
+
* <tt>:original</tt> - String containing original markup that is being overridden. If supplied Deface will log when the original markup changes, which helps highlight overrides that need attention when upgrading versions of the source application. Only really warranted for :replace overrides. NB: All whitespace is stripped before comparsion.
|
52
|
+
|
48
53
|
Examples
|
49
54
|
========
|
50
55
|
|
@@ -69,11 +74,12 @@ Inserts the contents of `shared/_comment.html.erb` after all instances of `div`
|
|
69
74
|
:insert_after => "div#comment_21",
|
70
75
|
:partial => "shared/comment")
|
71
76
|
|
72
|
-
Removes any
|
77
|
+
Removes any ERB block containing the string `helper_method` in the `posts/new.html.erb` template, will also log if markup being removed does not match `<%= helper_method %>`
|
73
78
|
|
74
79
|
Deface::Override.new(:virtual_path => "posts/new",
|
75
80
|
:name => "example-4",
|
76
|
-
:remove => "code[erb-loud]:contains('helper_method')"
|
81
|
+
:remove => "code[erb-loud]:contains('helper_method')",
|
82
|
+
:original => "<%= helper_method %>")
|
77
83
|
|
78
84
|
|
79
85
|
Implementation
|
@@ -107,12 +113,12 @@ Deface overrides have full access to all variables accessible to the view being
|
|
107
113
|
|
108
114
|
Caveats
|
109
115
|
======
|
116
|
+
Deface uses the amazing Nokogiri library (and in turn libxml) for parsing HTML / view files, in some circumstances either Deface's own pre-parser or libxml's will fail to correctly parse a template. You can avoid such issues by ensuring your templates contain valid HTML. Some other caveats include:
|
110
117
|
|
111
|
-
|
118
|
+
1. Ensure that your layout views include doctype, html, head and body tags in a single file, as Nokogiri will create such elements if it detects any of these tags have been incorrectly nested.
|
112
119
|
|
113
|
-
Parsing will fail and result in invalid output if ERB blocks are responsible for closing a HTML tag what was opened normally, i.e. don't do this:
|
120
|
+
2. Parsing will fail and result in invalid output if ERB blocks are responsible for closing a HTML tag what was opened normally, i.e. don't do this:
|
114
121
|
|
115
122
|
|
116
123
|
<div <%= ">" %>
|
117
124
|
|
118
|
-
|
data/Rakefile
CHANGED
@@ -22,22 +22,3 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
22
22
|
rdoc.rdoc_files.include('README')
|
23
23
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
24
24
|
end
|
25
|
-
|
26
|
-
begin
|
27
|
-
require "jeweler"
|
28
|
-
Jeweler::Tasks.new do |gem|
|
29
|
-
gem.name = "deface"
|
30
|
-
gem.summary = "Deface is a library that allows you to customize ERB views in Rails"
|
31
|
-
gem.description = "Deface is a library that allows you to customize ERB views in a Rails application without editing the underlying view."
|
32
|
-
gem.email = "brian@railsdog.com"
|
33
|
-
gem.homepage = "http://github.com/BDQ/Deface"
|
34
|
-
gem.authors = ["Brian Quinn"]
|
35
|
-
gem.files = Dir["*", "{lib}/**/*"]
|
36
|
-
gem.add_dependency("nokogiri", "~> 1.4.3")
|
37
|
-
gem.add_dependency("rails", "~> 3.0.0")
|
38
|
-
end
|
39
|
-
|
40
|
-
Jeweler::GemcutterTasks.new
|
41
|
-
rescue LoadError
|
42
|
-
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
43
|
-
end
|
data/deface.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{deface}
|
8
|
-
s.version = "0.5.
|
8
|
+
s.version = "0.5.1"
|
9
9
|
|
10
10
|
s.authors = ["Brian Quinn"]
|
11
11
|
s.description = %q{Deface is a library that allows you to customize ERB views in a Rails application without editing the underlying view.}
|
@@ -33,7 +33,7 @@ Gem::Specification.new do |s|
|
|
33
33
|
end
|
34
34
|
else
|
35
35
|
s.add_dependency(%q<nokogiri>, ["~> 1.4.3"])
|
36
|
-
s.add_dependency(%q<rails>, ["
|
36
|
+
s.add_dependency(%q<rails>, [">= 3.0.0"])
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
data/lib/deface.rb
CHANGED
data/lib/deface/override.rb
CHANGED
@@ -37,6 +37,10 @@ module Deface
|
|
37
37
|
# * <tt>:name</tt> - Unique name for override so it can be identified and modified later.
|
38
38
|
# This needs to be unique within the same :virtual_path
|
39
39
|
# * <tt>:disabled</tt> - When set to true the override will not be applied.
|
40
|
+
# * <tt>:original</tt> - String containing original markup that is being overridden.
|
41
|
+
# If supplied Deface will log when the original markup changes, which helps highlight overrides that need
|
42
|
+
# attention when upgrading versions of the source application. Only really warranted for :replace overrides.
|
43
|
+
# NB: All whitespace is stripped before comparsion.
|
40
44
|
|
41
45
|
|
42
46
|
def initialize(args)
|
@@ -77,6 +81,25 @@ module Deface
|
|
77
81
|
Deface::Parser.convert(source.clone)
|
78
82
|
end
|
79
83
|
|
84
|
+
def original_source
|
85
|
+
return nil unless @args[:original].present?
|
86
|
+
|
87
|
+
Deface::Parser.convert(@args[:original].clone)
|
88
|
+
end
|
89
|
+
|
90
|
+
# logs if original source has changed
|
91
|
+
def validate_original(match)
|
92
|
+
return true if self.original_source.nil?
|
93
|
+
|
94
|
+
valid = self.original_source.to_s.gsub(/\s/, '') == match.to_s.gsub(/\s/, '')
|
95
|
+
|
96
|
+
if !valid && defined?(Rails) == "constant"
|
97
|
+
Rails.logger.error "\e[1;32mDeface: [WARNING]\e[0m The original source for '#{self.name}' has changed, this override should be reviewed to ensure it's still valid."
|
98
|
+
end
|
99
|
+
|
100
|
+
valid
|
101
|
+
end
|
102
|
+
|
80
103
|
def disabled?
|
81
104
|
@args.key?(:disabled) ? @args[:disabled] : false
|
82
105
|
end
|
@@ -89,10 +112,10 @@ module Deface
|
|
89
112
|
#
|
90
113
|
def self.apply(source, details)
|
91
114
|
overrides = find(details)
|
92
|
-
@enable_logging ||=
|
115
|
+
@enable_logging ||= defined?(Rails) == "constant"
|
93
116
|
|
94
117
|
if @enable_logging && overrides.size > 0
|
95
|
-
Rails.logger.info "\e[1;32mDeface:\e[0m #{overrides.size} overrides found for #{details[:virtual_path]}"
|
118
|
+
Rails.logger.info "\e[1;32mDeface:\e[0m #{overrides.size} overrides found for '#{details[:virtual_path]}'"
|
96
119
|
end
|
97
120
|
|
98
121
|
unless overrides.empty?
|
@@ -100,9 +123,7 @@ module Deface
|
|
100
123
|
|
101
124
|
overrides.each do |override|
|
102
125
|
if override.disabled?
|
103
|
-
if @enable_logging
|
104
|
-
Rails.logger.info "\e[1;32mDeface:\e[0m #{override.name} is disabled"
|
105
|
-
end
|
126
|
+
Rails.logger.info("\e[1;32mDeface:\e[0m '#{override.name}' is disabled") if @enable_logging
|
106
127
|
next
|
107
128
|
end
|
108
129
|
|
@@ -112,10 +133,12 @@ module Deface
|
|
112
133
|
matches = doc.css(override.selector)
|
113
134
|
|
114
135
|
if @enable_logging
|
115
|
-
Rails.logger.info "\e[1;32mDeface:\e[0m #{override.name} matched #{matches.size} times with #{override.selector}"
|
136
|
+
Rails.logger.send(matches.size == 0 ? :error : :info, "\e[1;32mDeface:\e[0m '#{override.name}' matched #{matches.size} times with '#{override.selector}'")
|
116
137
|
end
|
117
138
|
|
118
139
|
matches.each do |match|
|
140
|
+
override.validate_original(match)
|
141
|
+
|
119
142
|
case override.action
|
120
143
|
when :remove
|
121
144
|
match.replace ""
|
@@ -17,5 +17,14 @@ module Deface
|
|
17
17
|
|
18
18
|
@lookup_context.find(name, prefix, partial).source
|
19
19
|
end
|
20
|
+
|
21
|
+
#gets source erb for an element
|
22
|
+
def element_source(template_source, selector)
|
23
|
+
doc = Deface::Parser.convert(template_source)
|
24
|
+
|
25
|
+
doc.css(selector).inject([]) do |result, match|
|
26
|
+
result << Deface::Parser.undo_erb_markup!(match.to_s.dup)
|
27
|
+
end
|
28
|
+
end
|
20
29
|
end
|
21
30
|
end
|
@@ -17,6 +17,37 @@ module Deface
|
|
17
17
|
@override.selector.should == "h1"
|
18
18
|
end
|
19
19
|
|
20
|
+
describe "#original_source" do
|
21
|
+
it "should return nil with not specified" do
|
22
|
+
@override.original_source.should be_nil
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should return parsed nokogiri document when present" do
|
26
|
+
@original = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :text => "<h1>Argh!</h1>", :original => "<p><%= something %></p>")
|
27
|
+
@original.original_source.should be_an_instance_of Nokogiri::HTML::DocumentFragment
|
28
|
+
@original.original_source.to_s.should == "<p><code erb-loud> something </code></p>"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "#validate_original" do
|
33
|
+
before(:each) do
|
34
|
+
@original = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :text => "<h1>Argh!</h1>", :original => "<p><%= something %></p>")
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should return true when :original is not present" do
|
38
|
+
@override.validate_original("").should be_true
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should return true when :original present, and input contains similar (ignoring whitespace)" do
|
42
|
+
@original.validate_original("<p><code erb-loud> something </code></p>").should be_true
|
43
|
+
@original.validate_original("<p><code erb-loud>something\n</code> </p>").should be_true
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should return false when :original present, and input contains different string" do
|
47
|
+
@original.validate_original("wrong").should be_false
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
20
51
|
describe "#find" do
|
21
52
|
it "should find by virtual_path" do
|
22
53
|
Deface::Override.find({:virtual_path => "posts/index"}).size.should == 1
|
data/tasks/deface.rake
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
namespace :deface do
|
2
|
+
desc 'Gets source of html element from template.'
|
3
|
+
task :get_source, :template_path, :selector, :needs => :environment do |t, args|
|
4
|
+
include Deface::TemplateHelper
|
5
|
+
|
6
|
+
begin
|
7
|
+
source = load_template_source(args[:template_path], false)
|
8
|
+
output = element_source(source, args[:selector])
|
9
|
+
rescue
|
10
|
+
puts "Failed to find tempalte/partial"
|
11
|
+
|
12
|
+
output = []
|
13
|
+
end
|
14
|
+
|
15
|
+
if output.empty?
|
16
|
+
puts "0 matches found"
|
17
|
+
else
|
18
|
+
puts "Querying '#{args[:template_path]}' for '#{args[:selector]}'"
|
19
|
+
output.each_with_index do |content, i|
|
20
|
+
puts "---------------- Match #{i+1} ----------------"
|
21
|
+
puts content
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deface
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 9
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 5
|
9
|
-
-
|
10
|
-
version: 0.5.
|
9
|
+
- 1
|
10
|
+
version: 0.5.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Brian Quinn
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-06-03 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -69,6 +69,7 @@ files:
|
|
69
69
|
- lib/deface/action_view_extensions.rb
|
70
70
|
- lib/deface/override.rb
|
71
71
|
- lib/deface/parser.rb
|
72
|
+
- lib/deface/railtie.rb
|
72
73
|
- lib/deface/template_helper.rb
|
73
74
|
- spec/assets/admin/posts/index.html.erb
|
74
75
|
- spec/assets/shared/_post.html.erb
|
@@ -78,6 +79,7 @@ files:
|
|
78
79
|
- spec/deface/template_helper_spec.rb
|
79
80
|
- spec/deface/template_spec.rb
|
80
81
|
- spec/spec_helper.rb
|
82
|
+
- tasks/deface.rake
|
81
83
|
has_rdoc: true
|
82
84
|
homepage: http://github.com/railsdog/deface
|
83
85
|
licenses: []
|