herbie 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,7 @@ require 'erubis'
4
4
  module Herbie
5
5
  module Helpers
6
6
  private
7
-
7
+
8
8
  def erb_with_output_buffer(buf = '')
9
9
  @_out_buf, old_buffer = buf, @_out_buf
10
10
  yield
@@ -20,6 +20,6 @@ module Herbie
20
20
  def erb_concat(text)
21
21
  @_out_buf << text unless @_out_buf.nil?
22
22
  end
23
-
23
+
24
24
  end
25
25
  end
@@ -19,7 +19,7 @@ module Herbie
19
19
  end
20
20
  end
21
21
  a.join(' ')
22
- end
22
+ end
23
23
 
24
24
  end
25
25
  end
@@ -4,20 +4,25 @@ module Herbie
4
4
  if block_given?
5
5
  erb_concat "<#{name}#{' ' + attributes(attrs) unless attrs.nil? || attrs.empty?}>\n#{capture_erb(&block)}\n</#{name}>"
6
6
  elsif !attrs[:content].nil?
7
- content = attrs.delete :content
8
- "<#{name}#{' ' + attributes(attrs) unless attrs.empty?}>#{content}</#{name}>"
7
+ case name
8
+ when :meta
9
+ "<#{name}#{' ' + attributes(attrs) unless attrs.empty?}>"
10
+ else
11
+ content = attrs.delete :content
12
+ "<#{name}#{' ' + attributes(attrs) unless attrs.empty?}>#{content}</#{name}>"
13
+ end
9
14
  else
10
15
  "<#{name}#{' ' + attributes(attrs) unless attrs.empty?}>"
11
16
  end
12
17
  end
13
-
18
+
14
19
  # work in progress
15
20
  def tags(name, collection, attrs={}, &block)
16
21
  cycle = attrs.delete :cycle
17
22
  result = ""
18
23
  collection.each_with_index do |item, i|
19
24
  a = attrs.dup
20
-
25
+
21
26
  unless cycle.nil?
22
27
  c = a.delete :class
23
28
  classes = []
@@ -26,7 +31,7 @@ module Herbie
26
31
  classes << cycle[:odd] if i.odd? && cycle.key?(:odd)
27
32
  a[:class] = classes.join " " unless classes.empty?
28
33
  end
29
-
34
+
30
35
  result += tag name, a do
31
36
  block.call(item)
32
37
  end
@@ -34,13 +39,13 @@ module Herbie
34
39
  end
35
40
  result
36
41
  end
37
-
42
+
38
43
  def script(source=nil, &block)
39
44
  attrs = {
40
45
  :type => "text/javascript",
41
46
  :charset => "utf-8"
42
47
  }
43
-
48
+
44
49
  if block_given?
45
50
  erb_concat "#{tag :script, attrs}\n#{capture_erb(&block)}\n</script>"
46
51
  else
@@ -49,23 +54,23 @@ module Herbie
49
54
  "#{tag :script, attrs}</script>"
50
55
  end
51
56
  end
52
-
57
+
53
58
  def style(href=nil, attrs={}, &block)
54
59
  default_attrs = {
55
60
  :rel => "stylesheet",
56
61
  :type => "text/css"
57
62
  }
58
-
63
+
59
64
  if block_given?
60
65
  default_attrs.delete :rel
61
66
  erb_concat "#{tag :style, default_attrs.merge(attrs)}\n#{capture_erb(&block)}\n</style>"
62
67
  else
63
68
  href = "/stylesheets/#{href}" unless href.match(/^\/{1,2}|^http:\/\/|^https:\/\//)
64
69
  attrs = default_attrs.merge({:href => href}.merge(attrs))
65
- "#{tag :link, attrs}"
70
+ "#{tag :link, attrs}"
66
71
  end
67
72
  end
68
-
73
+
69
74
  def link_to(href, text=nil, attrs={}, &block)
70
75
  attrs = {:href => href}.merge(attrs)
71
76
  if block_given?
@@ -74,10 +79,10 @@ module Herbie
74
79
  "#{tag :a, attrs}#{text ||= attrs[:href]}</a>"
75
80
  end
76
81
  end
77
-
82
+
78
83
  def content_for(name, content=nil, &block)
79
84
  @captured_content ||= {}
80
-
85
+
81
86
  if content || block_given?
82
87
  if @captured_content.key? name
83
88
  @capured_content[name] += content || capture_erb(&block)
@@ -88,6 +93,6 @@ module Herbie
88
93
  @captured_content[name]
89
94
  end
90
95
  end
91
-
96
+
92
97
  end
93
98
  end
data/spec/herbie_spec.rb CHANGED
@@ -3,7 +3,7 @@ require_relative "../lib/herbie.rb"
3
3
 
4
4
  describe Herbie::Helpers do
5
5
  include Herbie::Helpers
6
-
6
+
7
7
  describe "tag helpers" do
8
8
  it "should be able to output a simple tag" do
9
9
  attrs = {
@@ -37,6 +37,15 @@ describe Herbie::Helpers do
37
37
 
38
38
  tag(:input, attrs).should == "<input type=\"#{attrs[:type]}\" name=\"#{attrs[:name]}\" value=\"#{attrs[:value]}\" checked>"
39
39
  end
40
+
41
+ it "should output the content parameter as an attribute on meta tags" do
42
+ attrs = {
43
+ :name => "viewport",
44
+ :content => "width=device-width, user-scalable=yes, initial-scale=1.0"
45
+ }
46
+
47
+ tag(:meta, attrs).should == "<meta name=\"#{attrs[:name]}\" content=\"#{attrs[:content]}\">"
48
+ end
40
49
 
41
50
  it "should output tags mixed with ERB" do
42
51
  pending "Need a mechanism for capturing erb output within a passed block"
@@ -74,7 +83,7 @@ HTML
74
83
  <li><a href="/contactus" title="Get in touch with us">Contact Us</a></li>
75
84
  <li><a href="/help" title="Can't find what you need? Get help here">Help</a></li>
76
85
  MARKUP
77
-
86
+
78
87
  navigation_items = [
79
88
  {:text => "Home", :href => "/", :title => "Back to the homepage"},
80
89
  {:text => "Shop", :href => "/shop", :title => "View all our products"},
@@ -87,7 +96,7 @@ MARKUP
87
96
  end
88
97
  result.should == markup
89
98
  end
90
-
99
+
91
100
  it "should allow an arbitrary class to be added to alternating elements within the collection" do
92
101
  pending "Need a mechanism for capturing erb output within a passed block"
93
102
  markup = <<-MARKUP
@@ -105,55 +114,55 @@ MARKUP
105
114
  result.should == markup
106
115
  end
107
116
  end
108
-
117
+
109
118
  describe "script helpers" do
110
119
  it "should prefix the path to the script with ‘/javascripts’ if the script path is relative" do
111
120
  script("script.js").should == "<script type=\"text/javascript\" charset=\"utf-8\" src=\"/javascripts/script.js\"></script>"
112
121
  end
113
-
122
+
114
123
  it "should output a script with an absolute path to the script if the path provided was absolute" do
115
124
  script("/path/to/script.js").should == "<script type=\"text/javascript\" charset=\"utf-8\" src=\"/path/to/script.js\"></script>"
116
125
  end
117
-
126
+
118
127
  it "should output a script with an absolute path to the script if the path provided begins with http://, https:// or //" do
119
128
  script("http://code.jquery.com/jquery.js").should == "<script type=\"text/javascript\" charset=\"utf-8\" src=\"http://code.jquery.com/jquery.js\"></script>" and
120
129
  script("https://code.jquery.com/jquery.js").should == "<script type=\"text/javascript\" charset=\"utf-8\" src=\"https://code.jquery.com/jquery.js\"></script>" and
121
130
  script("//code.jquery.com/jquery.js").should == "<script type=\"text/javascript\" charset=\"utf-8\" src=\"//code.jquery.com/jquery.js\"></script>"
122
131
  end
123
-
132
+
124
133
  it "should output a script element with arbitrary javascript content provided by a block" do
125
134
  pending "Need a mechanism for capturing erb output within a passed block"
126
135
  script_block = Proc.new { "function hello_world(){ console.log(\"hello world!\"); }" }
127
136
  script(&script_block).should == "<script type=\"text/javascript\" charset=\"utf-8\">\n#{capture_erb(&script_block)}\n</script>"
128
137
  end
129
-
138
+
130
139
  it "should output a jQuery script include with the specified options" do
131
140
  pending "NYI"
132
-
141
+
133
142
  jquery(:version => "1.5.0", :min => false, :cdn => :google).should == "<script type=\"text/javascript\" charset=\"utf-8\" src=\"\"></script>"
134
143
  end
135
144
  end
136
-
145
+
137
146
  describe "stylesheet helpers" do
138
147
  it "should prefix the path to the script with ‘/stylesheets’ if the stylesheet path is relative" do
139
148
  style("foo.css").should == "<link rel=\"stylesheet\" type=\"text/css\" href=\"/stylesheets/foo.css\">"
140
149
  end
141
-
150
+
142
151
  it "should output a link element with the absolute path to the stylesheet if the path provided was absolute" do
143
152
  style("/style/foo.css").should == "<link rel=\"stylesheet\" type=\"text/css\" href=\"/style/foo.css\">"
144
153
  end
145
-
154
+
146
155
  it "should output a link element with the absolute path to the stylesheet if the path provided starts with http://, https:// or //" do
147
156
  style("http://foo.com/style/bar.css").should == "<link rel=\"stylesheet\" type=\"text/css\" href=\"http://foo.com/style/bar.css\">" and
148
157
  style("https://foo.com/style/bar.css").should == "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://foo.com/style/bar.css\">" and
149
158
  style("//foo.com/style/bar.css").should == "<link rel=\"stylesheet\" type=\"text/css\" href=\"//foo.com/style/bar.css\">"
150
159
  end
151
-
160
+
152
161
  it "should output a link element with appropriate media query attribute if provided" do
153
162
  media = "screen and (min-width:500px)"
154
163
  style("/style/foo.css", :media => media).should == "<link rel=\"stylesheet\" type=\"text/css\" href=\"/style/foo.css\" media=\"#{media}\">"
155
164
  end
156
-
165
+
157
166
  it "should output a style element with arbitrary content provided by a block" do
158
167
  pending "Need a mechanism for capturing erb output within a passed block"
159
168
  media = "screen and (min-width:500px)"
@@ -161,19 +170,19 @@ MARKUP
161
170
  style({:media => media}, &css_block).should == "<style type=\"text/css\" media=\"#{media}\">\n#{capture_erb(&css_block)}\n</style>"
162
171
  end
163
172
  end
164
-
173
+
165
174
  describe "link helpers" do
166
175
  it "should be able to output a simple link" do
167
176
  href = "http://www.foo.com/"
168
177
  link_to(href).should == "<a href=\"#{href}\">#{href}</a>"
169
178
  end
170
-
179
+
171
180
  it "should be able to output a link with href and text" do
172
181
  href = "http://www.foo.com/"
173
182
  text = "Visit foo.com"
174
183
  link_to(href, text).should == "<a href=\"#{href}\">#{text}</a>"
175
184
  end
176
-
185
+
177
186
  it "should be able to output links with arbitrary attributes" do
178
187
  href = "http://www.foo.com/"
179
188
  text = "Visit foo.com"
@@ -183,7 +192,7 @@ MARKUP
183
192
  }
184
193
  link_to(href, text, attrs).should == "<a href=\"#{href}\" class=\"#{attrs[:class]}\" target=\"#{attrs[:target]}\">#{text}</a>"
185
194
  end
186
-
195
+
187
196
  it "should be able to output a link enclosing arbitrary markup provided by a block" do
188
197
  pending "Need a mechanism for capturing erb output within a passed block"
189
198
  href = "http://www.foo.com/"
@@ -195,7 +204,7 @@ MARKUP
195
204
  link_to(href, text, attrs, &markup_block).should == "<a href=\"#{href}\" class=\"#{attrs[:class]}\">#{capture_erb(&markup_block)}</a>"
196
205
  end
197
206
  end
198
-
207
+
199
208
  describe "content helpers" do
200
209
  it "should accept a named block of content which can then be displayed elsewhere later" do
201
210
  pending "Need a mechanism for capturing erb output within a passed block"
@@ -204,7 +213,7 @@ MARKUP
204
213
  "<script type=\"text/javascript\" charset=\"utf-8\" src=\"/application.js\">"
205
214
  # Tilt['erb'].new { "%><%= script 'application.js' %><%" }
206
215
  end
207
-
216
+
208
217
  content_for(:script).should == "<script type=\"text/javascript\" charset=\"utf-8\" src=\"/application.js\">"
209
218
  end
210
219
  end
metadata CHANGED
@@ -1,105 +1,116 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: herbie
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.6
4
5
  prerelease:
5
- version: 0.1.5
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Ben Darlow
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-12-08 00:00:00 +00:00
14
- default_executable:
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
12
+ date: 2013-05-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
17
15
  name: tilt
18
- prerelease: false
19
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
20
17
  none: false
21
- requirements:
22
- - - ">="
23
- - !ruby/object:Gem::Version
24
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
25
22
  type: :runtime
26
- version_requirements: *id001
27
- - !ruby/object:Gem::Dependency
28
- name: erubis
29
23
  prerelease: false
30
- requirement: &id002 !ruby/object:Gem::Requirement
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: erubis
32
+ requirement: !ruby/object:Gem::Requirement
31
33
  none: false
32
- requirements:
33
- - - ">="
34
- - !ruby/object:Gem::Version
35
- version: "0"
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
36
38
  type: :runtime
37
- version_requirements: *id002
38
- - !ruby/object:Gem::Dependency
39
- name: rspec
40
39
  prerelease: false
41
- requirement: &id003 !ruby/object:Gem::Requirement
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: rspec
48
+ requirement: !ruby/object:Gem::Requirement
42
49
  none: false
43
- requirements:
50
+ requirements:
44
51
  - - ~>
45
- - !ruby/object:Gem::Version
46
- version: "2.6"
52
+ - !ruby/object:Gem::Version
53
+ version: '2.6'
47
54
  type: :development
48
- version_requirements: *id003
49
- - !ruby/object:Gem::Dependency
50
- name: colored
51
55
  prerelease: false
52
- requirement: &id004 !ruby/object:Gem::Requirement
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.6'
62
+ - !ruby/object:Gem::Dependency
63
+ name: colored
64
+ requirement: !ruby/object:Gem::Requirement
53
65
  none: false
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- version: "0"
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
58
70
  type: :development
59
- version_requirements: *id004
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
60
78
  description: Lovable HTML view helpers for use with ERB.
61
79
  email: ben@kapowaz.net
62
80
  executables: []
63
-
64
81
  extensions: []
65
-
66
82
  extra_rdoc_files: []
67
-
68
- files:
83
+ files:
69
84
  - lib/herbie/erb_helpers.rb
70
85
  - lib/herbie/generic_helpers.rb
71
86
  - lib/herbie/html_helpers.rb
72
87
  - lib/herbie.rb
73
88
  - spec/herbie_spec.rb
74
89
  - spec/spec_helper.rb
75
- has_rdoc: true
76
90
  homepage: http://github.com/kapowaz/herbie
77
91
  licenses: []
78
-
79
92
  post_install_message:
80
93
  rdoc_options: []
81
-
82
- require_paths:
94
+ require_paths:
83
95
  - lib
84
- required_ruby_version: !ruby/object:Gem::Requirement
96
+ required_ruby_version: !ruby/object:Gem::Requirement
85
97
  none: false
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: 1.9.2
90
- required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: 1.9.3
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
103
  none: false
92
- requirements:
93
- - - ">="
94
- - !ruby/object:Gem::Version
95
- version: "0"
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
96
108
  requirements: []
97
-
98
109
  rubyforge_project:
99
- rubygems_version: 1.6.2
110
+ rubygems_version: 1.8.23
100
111
  signing_key:
101
112
  specification_version: 3
102
113
  summary: herbie
103
- test_files:
114
+ test_files:
104
115
  - spec/herbie_spec.rb
105
116
  - spec/spec_helper.rb