deface 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +21 -16
- data/Rakefile +2 -0
- data/VERSION +1 -1
- data/deface.gemspec +4 -2
- data/lib/deface/action_view_extensions.rb +2 -2
- data/lib/deface/override.rb +10 -25
- data/lib/deface/parser.rb +23 -73
- data/spec/deface/override_spec.rb +4 -15
- data/spec/deface/parser_spec.rb +12 -12
- metadata +6 -6
data/README.markdown
CHANGED
@@ -11,7 +11,7 @@ Deface temporarily converts ERB files into a pseudo HTML markup that can be pars
|
|
11
11
|
|
12
12
|
becomes
|
13
13
|
|
14
|
-
<erb-loud> some ruby code </
|
14
|
+
<code erb-loud> some ruby code </code>
|
15
15
|
|
16
16
|
and
|
17
17
|
|
@@ -19,7 +19,7 @@ and
|
|
19
19
|
|
20
20
|
becomes
|
21
21
|
|
22
|
-
<erb-silent> other ruby code </
|
22
|
+
<code erb-silent> other ruby code </code>
|
23
23
|
|
24
24
|
Deface overrides have full access to all variables accessible to the view being customized.
|
25
25
|
|
@@ -30,9 +30,7 @@ A new instance of the Deface::Override class is initialized for each customizati
|
|
30
30
|
|
31
31
|
Target
|
32
32
|
------
|
33
|
-
* <tt>:
|
34
|
-
|
35
|
-
* <tt>:virtual_path</tt> - The controller and action name where the override should take effect eg: *"controller/action"* or *"posts/index"* will apply to all layouts, templates and partials that are used in the rendering of the specified action.
|
33
|
+
* <tt>:virtual_path</tt> - The template / partial / layout where the override should take effect eg: *"shared/_person"*, *"admin/posts/new"* this will apply to all controller actions that use the specified template.
|
36
34
|
|
37
35
|
Action
|
38
36
|
------
|
@@ -54,27 +52,34 @@ Source
|
|
54
52
|
|
55
53
|
Optional
|
56
54
|
--------
|
57
|
-
* <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`
|
55
|
+
* <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`
|
58
56
|
|
59
57
|
Examples
|
60
58
|
========
|
61
59
|
|
62
60
|
Replaces all instances of _h1_ in the `posts/_form.html.erb` partial with `<h1>New Post</h1>`
|
63
61
|
|
64
|
-
|
62
|
+
Deface::Override.new(:virtual_path => "posts/_form",
|
63
|
+
:name => "example-1",
|
64
|
+
:replace => "h1",
|
65
|
+
:text => "<h1>New Post</h1>")
|
65
66
|
|
66
|
-
Inserts `<%= link_to "List Comments", comments_url(post) %>` before all instances of `p` with css class `comment` in
|
67
|
+
Inserts `<%= link_to "List Comments", comments_url(post) %>` before all instances of `p` with css class `comment` in `posts/index.html.erb`
|
67
68
|
|
68
|
-
|
69
|
+
Deface::Override.new(:virtual_path => "posts/index",
|
70
|
+
:name => "example-2",
|
71
|
+
:insert_before => "p.comment",
|
72
|
+
:text => "<%= link_to "List Comments", comments_url(post) %>")
|
69
73
|
|
70
|
-
Inserts the contents of `shared/_comment.html.erb` after all instances of `div` with an id of `comment_21` in
|
74
|
+
Inserts the contents of `shared/_comment.html.erb` after all instances of `div` with an id of `comment_21` in `posts/show.html.erb`
|
71
75
|
|
72
|
-
|
76
|
+
Deface::Override.new(:virtual_path => "posts/show",
|
77
|
+
:name => "example-3",
|
78
|
+
:insert_after => "div#comment_21",
|
79
|
+
:partial => "shared/comment")
|
73
80
|
|
74
81
|
Removes any instance of `<%= helper_method %>` in the `posts/new.html.erb" template:
|
75
82
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
83
|
+
Deface::Override.new(:virtual_path => "posts/new",
|
84
|
+
:name => "example-4",
|
85
|
+
:remove => "code[erb-loud]:contains('helper_method')" )
|
data/Rakefile
CHANGED
@@ -28,7 +28,9 @@ begin
|
|
28
28
|
Jeweler::Tasks.new do |gem|
|
29
29
|
gem.name = "deface"
|
30
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."
|
31
32
|
gem.email = "brian@railsdog.com"
|
33
|
+
gem.homepage = "http://github.com/BDQ/Deface"
|
32
34
|
gem.authors = ["Brian Quinn"]
|
33
35
|
gem.files = Dir["*", "{lib}/**/*"]
|
34
36
|
gem.add_dependency("nokogiri", "~> 1.4.3")
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/deface.gemspec
CHANGED
@@ -5,11 +5,12 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{deface}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Brian Quinn"]
|
12
|
-
s.date = %q{2010-08-
|
12
|
+
s.date = %q{2010-08-31}
|
13
|
+
s.description = %q{Deface is a library that allows you to customize ERB views in a Rails application without editing the underlying view.}
|
13
14
|
s.email = %q{brian@railsdog.com}
|
14
15
|
s.extra_rdoc_files = [
|
15
16
|
"README.markdown"
|
@@ -26,6 +27,7 @@ Gem::Specification.new do |s|
|
|
26
27
|
"lib/deface/override.rb",
|
27
28
|
"lib/deface/parser.rb"
|
28
29
|
]
|
30
|
+
s.homepage = %q{http://github.com/BDQ/Deface}
|
29
31
|
s.rdoc_options = ["--charset=UTF-8"]
|
30
32
|
s.require_paths = ["lib"]
|
31
33
|
s.rubygems_version = %q{1.3.7}
|
@@ -2,7 +2,7 @@ ActionView::Template.class_eval do
|
|
2
2
|
alias_method :rails_initialize, :initialize
|
3
3
|
|
4
4
|
def initialize(source, identifier, handler, details)
|
5
|
-
overrides = Deface::Override.find(details
|
5
|
+
overrides = Deface::Override.find(details)
|
6
6
|
|
7
7
|
unless overrides.empty?
|
8
8
|
doc = Deface::Parser.convert_fragment(source)
|
@@ -26,7 +26,7 @@ ActionView::Template.class_eval do
|
|
26
26
|
|
27
27
|
source = doc.to_s
|
28
28
|
|
29
|
-
Deface::Parser.undo_erb_markup(source)
|
29
|
+
Deface::Parser.undo_erb_markup!(source)
|
30
30
|
end
|
31
31
|
|
32
32
|
rails_initialize(source, identifier, handler, details)
|
data/lib/deface/override.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
module Deface
|
2
2
|
class Override
|
3
|
-
cattr_accessor :
|
3
|
+
cattr_accessor :all, :actions
|
4
4
|
attr_accessor :args
|
5
5
|
|
6
|
-
@@
|
7
|
-
@@file ||= {}
|
6
|
+
@@all ||= {}
|
8
7
|
@@actions = [:remove, :replace, :insert_after, :insert_before]
|
9
8
|
|
10
9
|
# Initializes new override, you must supply only one Target, Action & Source
|
@@ -12,13 +11,9 @@ module Deface
|
|
12
11
|
#
|
13
12
|
# ==== Target
|
14
13
|
#
|
15
|
-
# * <tt>:
|
14
|
+
# * <tt>:virtual_path</tt> - The path of the template / partial where
|
16
15
|
# the override should take effect eg: "shared/_person", "admin/posts/new"
|
17
16
|
# this will apply to all controller actions that use the specified template
|
18
|
-
# * <tt>:virtual_path</tt> - The controller and action name where
|
19
|
-
# the override should take effect eg: "controller/action" or "posts/index"
|
20
|
-
# will apply to all layouts, templates and partials that are used in the
|
21
|
-
# rendering of the specified action.
|
22
17
|
#
|
23
18
|
# ==== Action
|
24
19
|
#
|
@@ -36,22 +31,15 @@ module Deface
|
|
36
31
|
# ==== Optional
|
37
32
|
#
|
38
33
|
# * <tt>:name</tt> - Unique name for override so it can be identified and modified later.
|
39
|
-
# This needs to be unique within the same :virtual_path
|
34
|
+
# This needs to be unique within the same :virtual_path
|
40
35
|
|
41
36
|
def initialize(args)
|
42
37
|
@args = args
|
43
38
|
|
44
|
-
|
45
|
-
key = args[:virtual_path].to_sym
|
39
|
+
key = args[:virtual_path].to_sym
|
46
40
|
|
47
|
-
|
48
|
-
|
49
|
-
elsif args.key?(:file_path)
|
50
|
-
key = args[:file_path]
|
51
|
-
|
52
|
-
@@file[key] ||= {}
|
53
|
-
@@file[key][args[:name].to_s.parameterize] = self
|
54
|
-
end
|
41
|
+
@@all[key] ||= {}
|
42
|
+
@@all[key][args[:name].to_s.parameterize] = self
|
55
43
|
end
|
56
44
|
|
57
45
|
def selector
|
@@ -71,19 +59,16 @@ module Deface
|
|
71
59
|
@args[:text]
|
72
60
|
end
|
73
61
|
|
74
|
-
Deface::Parser
|
62
|
+
Deface::Parser.erb_markup!(erb)
|
75
63
|
end
|
76
64
|
|
77
65
|
def self.find(details)
|
78
|
-
return [] unless
|
66
|
+
return [] unless @@all
|
79
67
|
|
80
68
|
result = []
|
81
69
|
|
82
70
|
virtual_path = details[:virtual_path]
|
83
|
-
result << @@
|
84
|
-
|
85
|
-
file_path = details[:file_path]
|
86
|
-
result << @@file.map { |key,overrides| overrides.try(:values) if file_path =~ /#{key}/ } if file_path
|
71
|
+
result << @@all[virtual_path.to_sym].try(:values)
|
87
72
|
|
88
73
|
result.flatten.compact
|
89
74
|
end
|
data/lib/deface/parser.rb
CHANGED
@@ -3,88 +3,38 @@ require 'erubis'
|
|
3
3
|
|
4
4
|
module Deface
|
5
5
|
class Parser
|
6
|
+
# converts erb to markup
|
7
|
+
#
|
8
|
+
def self.erb_markup!(source)
|
9
|
+
replacements = [ {"<%=" => "<code erb-loud>"},
|
10
|
+
{"<%" => "<code erb-silent>"},
|
11
|
+
{"%>" => "</code>"} ]
|
12
|
+
|
13
|
+
replacements.each{ |h| h.each { |replace, with| source.gsub! replace, with } }
|
14
|
+
source
|
15
|
+
end
|
16
|
+
|
6
17
|
# undoes ERB markup generated by Deface::Parser::ERB
|
7
18
|
#
|
8
|
-
def self.undo_erb_markup(source)
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
19
|
+
def self.undo_erb_markup!(source)
|
20
|
+
replacements = [ {"<code erb-silent>" => '<%'},
|
21
|
+
{"<code erb-loud>" => '<%='},
|
22
|
+
{"</code>" => '%>'},
|
23
|
+
{/(<|<)code erb-silent(>|>)/ => '<%'},
|
24
|
+
{/(<|<)code erb-loud(>|>)/ => '<%='},
|
25
|
+
{/(<|<)\/code(>|>)/ => '%>'},
|
26
|
+
{/(?!=<%=?)>(?=.*%>)/ => '>'},
|
27
|
+
{/(?!=<%=?)<(?=.*%>)/ => '<'}]
|
28
|
+
|
29
|
+
replacements.each{ |h| h.each { |replace, with| source.gsub! replace, with } }
|
13
30
|
source
|
14
31
|
end
|
15
32
|
|
16
33
|
|
17
34
|
def self.convert_fragment(source)
|
18
|
-
|
35
|
+
erb_markup!(source)
|
19
36
|
Nokogiri::HTML::DocumentFragment.parse(source)
|
20
37
|
end
|
21
38
|
|
22
|
-
# A class for converting ERB code into a format that's easier
|
23
|
-
# for css & xpath querying4
|
24
|
-
|
25
|
-
# the erb tags are converted to html tags in the following way.
|
26
|
-
# `<% ... %>` is converted into `<erb-silent> ... </erb-silent>`.
|
27
|
-
# `<%= ... %>` is converted into `<erb-loud> ... </erb-loud>`.
|
28
|
-
#
|
29
|
-
class ERB < Erubis::Basic::Engine
|
30
|
-
# Compiles an ERB template into a HTML document containing `erb-` tags.
|
31
|
-
# 100% borrowed from HTML2HAML .. thank you team HAML.
|
32
|
-
#
|
33
|
-
# @param template [String] The ERB template
|
34
|
-
# @return [String] The output document
|
35
|
-
def self.compile(template)
|
36
|
-
new(template).src
|
37
|
-
end
|
38
|
-
|
39
|
-
# not supported (yet?)
|
40
|
-
def escaped_expr(code)
|
41
|
-
raise "Not Supported"
|
42
|
-
end
|
43
|
-
|
44
|
-
# The conversion has no preamble.
|
45
|
-
def add_preamble(src); end
|
46
|
-
|
47
|
-
# The conversion has no postamble.
|
48
|
-
def add_postamble(src); end
|
49
|
-
|
50
|
-
# Concatenates the text onto the source buffer.
|
51
|
-
#
|
52
|
-
# @param src [String] The source buffer
|
53
|
-
# @param text [String] The raw text to add to the buffer
|
54
|
-
def add_text(src, text)
|
55
|
-
src << text
|
56
|
-
end
|
57
|
-
|
58
|
-
# Concatenates a silent Ruby statement onto the source buffer.
|
59
|
-
# This uses the `<erb-silent>` tag
|
60
|
-
#
|
61
|
-
# @param src [String] The source buffer
|
62
|
-
# @param code [String] The Ruby statement to add to the buffer
|
63
|
-
def add_stmt(src, code)
|
64
|
-
src << whitespace(code) << "<erb-silent>" << code.strip << "</erb-silent>"
|
65
|
-
end
|
66
|
-
|
67
|
-
# Concatenates a Ruby expression that's printed to the document
|
68
|
-
# onto the source buffer.
|
69
|
-
# This uses the `<erb-silent>` tag
|
70
|
-
#
|
71
|
-
# @param src [String] The source buffer
|
72
|
-
# @param code [String] The Ruby expression to add to the buffer
|
73
|
-
def add_expr_literal(src, code)
|
74
|
-
src << whitespace(code) << "<erb-loud>" << code.strip << "</erb-loud>"
|
75
|
-
end
|
76
|
-
|
77
|
-
# not supported (yet?)
|
78
|
-
def add_expr_debug(src, code)
|
79
|
-
raise "Not Supported"
|
80
|
-
end
|
81
|
-
|
82
|
-
private
|
83
|
-
def whitespace(code)
|
84
|
-
arr = code.scan /^(\s*)/
|
85
|
-
res = arr.flatten.first
|
86
|
-
res.length==1 ? "" : res
|
87
|
-
end
|
88
|
-
end
|
89
39
|
end
|
90
40
|
end
|
@@ -4,7 +4,6 @@ module Deface
|
|
4
4
|
describe Override do
|
5
5
|
before(:each) do
|
6
6
|
@override = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :text => "<h1>Argh!</h1>")
|
7
|
-
@override2 = Deface::Override.new(:file_path => "posts/index", :name => "Posts#index", :replace => "h1", :text => "<h1>Argh!</h1>")
|
8
7
|
end
|
9
8
|
|
10
9
|
it "should return correct action" do
|
@@ -22,22 +21,12 @@ module Deface
|
|
22
21
|
Deface::Override.find({:virtual_path => "posts/index"}).size.should == 1
|
23
22
|
end
|
24
23
|
|
25
|
-
it "should find by file_path" do
|
26
|
-
Deface::Override.find({:file_path => "/full/path/to/posts/index.html.erb"}).size.should == 1
|
27
|
-
end
|
28
|
-
|
29
24
|
describe "#new" do
|
30
25
|
|
31
|
-
it "should increase
|
26
|
+
it "should increase all#size by 1" do
|
32
27
|
expect {
|
33
28
|
Deface::Override.new(:virtual_path => "posts/new", :name => "Posts#new", :replace => "h1", :text => "<h1>argh!</h1>")
|
34
|
-
}.to change{Deface::Override.
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should increase file#size by 1" do
|
38
|
-
expect {
|
39
|
-
Deface::Override.new(:file_path => "posts/new", :name => "Posts#new", :replace => "h1", :text => "<h1>argh!</h1>")
|
40
|
-
}.to change{Deface::Override.file.size}.by(1)
|
29
|
+
}.to change{Deface::Override.all.size}.by(1)
|
41
30
|
end
|
42
31
|
end
|
43
32
|
|
@@ -87,10 +76,10 @@ module Deface
|
|
87
76
|
@replacement = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :text => "<h1>Arrrr!</h1>")
|
88
77
|
end
|
89
78
|
|
90
|
-
it "should not increase
|
79
|
+
it "should not increase all#size by 1" do
|
91
80
|
expect {
|
92
81
|
Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :text => "<h1>Arrrr!</h1>")
|
93
|
-
}.to change{Deface::Override.
|
82
|
+
}.to change{Deface::Override.all.size}.by(0)
|
94
83
|
|
95
84
|
end
|
96
85
|
|
data/spec/deface/parser_spec.rb
CHANGED
@@ -9,37 +9,37 @@ module Deface
|
|
9
9
|
end
|
10
10
|
|
11
11
|
it "should convert <% ... %>" do
|
12
|
-
Deface::Parser.convert_fragment("<% method_name %>").to_s.should == "<erb-silent>method_name</
|
12
|
+
Deface::Parser.convert_fragment("<% method_name %>").to_s.should == "<code erb-silent> method_name </code>"
|
13
13
|
end
|
14
14
|
|
15
15
|
it "should convert <%= ... %>" do
|
16
|
-
Deface::Parser.convert_fragment("<%= method_name %>").to_s.should == "<erb-loud>method_name</
|
16
|
+
Deface::Parser.convert_fragment("<%= method_name %>").to_s.should == "<code erb-loud> method_name </code>"
|
17
17
|
end
|
18
18
|
|
19
19
|
it "should convert nested <% ... %>" do
|
20
|
-
Deface::Parser.convert_fragment("<p id=\"<% method_name %>\"></p>").to_s.should == "<p id=\"<erb-silent>method_name</
|
20
|
+
Deface::Parser.convert_fragment("<p id=\"<% method_name %>\"></p>").to_s.should == "<p id=\"<code erb-silent> method_name </code>\"></p>"
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should convert nested <%= ... %>" do
|
24
|
-
Deface::Parser.convert_fragment("<p id=\"<%= method_name %>\"></p>").to_s.should == "<p id=\"<erb-loud>method_name</
|
24
|
+
Deface::Parser.convert_fragment("<p id=\"<%= method_name %>\"></p>").to_s.should == "<p id=\"<code erb-loud> method_name </code>\"></p>"
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
describe "#undo_erb_markup" do
|
29
|
-
it "should revert <erb-silent>" do
|
30
|
-
Deface::Parser.undo_erb_markup("<erb-silent>method_name</
|
29
|
+
it "should revert <code erb-silent>" do
|
30
|
+
Deface::Parser.undo_erb_markup!("<code erb-silent> method_name </code>").should == "<% method_name %>"
|
31
31
|
end
|
32
32
|
|
33
|
-
it "should revert <erb-loud>" do
|
34
|
-
Deface::Parser.undo_erb_markup("<erb-loud>method_name</
|
33
|
+
it "should revert <code erb-loud>" do
|
34
|
+
Deface::Parser.undo_erb_markup!("<code erb-loud> method_name </code>").should == "<%= method_name %>"
|
35
35
|
end
|
36
36
|
|
37
|
-
it "should revert nested <erb-silent>" do
|
38
|
-
Deface::Parser.undo_erb_markup("<
|
37
|
+
it "should revert nested <code erb-silent>" do
|
38
|
+
Deface::Parser.undo_erb_markup!("<p id=\"<code erb-silent> method_name > 1 </code>\"></p>").should == "<p id=\"<% method_name > 1 %>\"></p>"
|
39
39
|
end
|
40
40
|
|
41
|
-
it "should revert nested <erb-loud>" do
|
42
|
-
Deface::Parser.undo_erb_markup("<
|
41
|
+
it "should revert nested <code erb-loud>" do
|
42
|
+
Deface::Parser.undo_erb_markup!("<p id=\"<code erb-loud> method_name < 2 </code>\"></p>").should == "<p id=\"<%= method_name < 2 %>\"></p>"
|
43
43
|
end
|
44
44
|
|
45
45
|
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: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
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: 2010-08-
|
18
|
+
date: 2010-08-31 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -50,7 +50,7 @@ dependencies:
|
|
50
50
|
version: 3.0.0
|
51
51
|
type: :runtime
|
52
52
|
version_requirements: *id002
|
53
|
-
description:
|
53
|
+
description: Deface is a library that allows you to customize ERB views in a Rails application without editing the underlying view.
|
54
54
|
email: brian@railsdog.com
|
55
55
|
executables: []
|
56
56
|
|
@@ -74,7 +74,7 @@ files:
|
|
74
74
|
- spec/deface/template_spec.rb
|
75
75
|
- spec/spec_helper.rb
|
76
76
|
has_rdoc: true
|
77
|
-
homepage:
|
77
|
+
homepage: http://github.com/BDQ/Deface
|
78
78
|
licenses: []
|
79
79
|
|
80
80
|
post_install_message:
|