manage_meta 0.0.13 → 0.0.14

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.
@@ -1,21 +1,23 @@
1
- ManageMeta
1
+ ManageMeta - Version: 0.0.14
2
2
  ============
3
3
 
4
- ManageMeta is yet another meta tag manager for Rails 3.x. Its features are: recognizes tags
5
- should render as HTTP-EQUIV; supports the Google 'canonical' link; is extensible; is non-intrusive
4
+ ManageMeta is yet another meta tag manager for Rails 3.x. Its features
5
+ are: recognizes tags should render as HTTP-EQUIV; supports the Google
6
+ 'canonical' link; is extensible; is non-intrusive
6
7
 
7
- NOTE: ManageMeta works by `include`ing itself into ActionController::Base via an `initializer`
8
- which is enclosed in ManageMeta::Railtie. This works for Rails 3.0.x and 3.1.x. Don't know if it
9
- works in Rails 2.x
8
+ NOTE: ManageMeta works by `include`ing itself into
9
+ ActionController::Base via an `initializer` which is enclosed in
10
+ ManageMeta::Railtie. This works for Rails 3.0.x and 3.1.x. Don't know
11
+ if it works in Rails 2.x
10
12
 
11
- What's New in Release 0.0.13?
13
+ What's New in Release 0.0.14?
12
14
  ------------
13
15
 
14
16
  Support for Facebook Open Graph.
15
17
 
16
- It's a bit clunky, but it will work and can be used to use ManageMeta as a prerequisite
17
- for an Open Graph metadata handling module. I may do that 'one of these days', but until
18
- then, see sketch below.
18
+ It's a bit clunky, but it will work and can be used to use ManageMeta
19
+ as a prerequisite for an Open Graph metadata handling module. I may do
20
+ that 'one of these days', but until then, see sketch below.
19
21
 
20
22
  How to use it
21
23
  -----------
@@ -28,37 +30,55 @@ or
28
30
 
29
31
  `gem "manage_meta", :git => "git://github.com/mikehoward/manage_meta.git"`
30
32
 
31
- Then, in your controller actions, call *add_meta()* for each meta tag you want
32
- to define.
33
+ Then, in your controller actions, call *add_meta()* for each meta tag
34
+ you want to define.
33
35
 
34
36
  `add_meta :author, 'Fred Fink'`
35
37
 
36
- If there are meta tags you don't want to define, you can use *del_meta()* to remove them.
37
- At present there are only two automatically defined: 'robots' and 'generator'. You may
38
- also redefine them by using *add_meta()* to give them new values.
38
+ If there are meta tags you don't want to define, you can use
39
+ *del_meta()* to remove them. At present there are only two
40
+ automatically defined: 'robots' and 'generator'. You may also redefine
41
+ them by using *add_meta()* to give them new values.
39
42
 
40
- If there is a meta tag which requires a currently unsupported format, you may add the
41
- format using *add_meta_format()*, and then add the tag using *add_meta()*.
43
+ If there is a meta tag which requires a currently unsupported format,
44
+ you may add the format using *add_meta_format()*, and then add the tag
45
+ using *add_meta()*.
42
46
 
43
47
  Finally, edit app/views/layouts/application.html.erb - or equivalent - to insert
44
48
 
45
49
  `<%= render_meta %>`
46
50
 
47
- into the _head_ section of your HTML output. This will render and return all of the
48
- defined meta tags.
51
+ into the _head_ section of your HTML output. This will render and
52
+ return all of the defined meta tags.
53
+
54
+ NOTE: `manage_meta` now creates `charset` meta tags for HTML5 and (if
55
+ desired) HTML4.01 and XHTML output. These tags should be placed early
56
+ in the <head> section of your page.
57
+
58
+ `manage_meta` does this by inserting the appropriate meta tag at the
59
+ head of the rendered metadata. Consequently, you should put the `<%=
60
+ render_meta %>` statement IMMEDIATELY after the <head> tag if (1) your
61
+ document is an HTML5 OR (2) it is HTML 4 or XHTML and you
62
+ `add_meta(:content_type, ...)`.
49
63
 
50
64
  What it Adds
51
65
  ------------
52
66
 
53
- ManageMeta defines seven (7) methods and three (3) instance variables into all classes
54
- derived from ApplicationController
67
+ ManageMeta defines seven (9) methods and three (6) instance variables
68
+ into all classes derived from ApplicationController
55
69
 
56
70
  The public methods are:
57
71
 
58
72
  * `add_meta()` - adds a meta tag
59
73
  * `del_meta()` - which deletes a meta tag
60
74
  * `add_meta_format()` - which adds a meta tag format
61
- * `render_meta()` - which returns a string rendering all currently defined meta tags.
75
+ * `render_meta()` - which returns a string rendering all currently
76
+ * defined meta tags.
77
+ * `manage_meta_set_emit_encoding(bool)` - sets @@manage_meta_emit_encoding.
78
+ (set to false if you want to include your encoding by hand) [default is `true`]
79
+ * `manage_meta_set_encoding()` - allows setting character encoding
80
+ * `manage_meta_set_html_version()` - allows setting html version to
81
+ html5, html4, or xhtml
62
82
 
63
83
  private methods:
64
84
 
@@ -83,6 +103,23 @@ The Details
83
103
 
84
104
  Here are the ugly details
85
105
 
106
+ ### charset meta tags ###
107
+
108
+ `charset` meta tags are ONLY emitted if the class variable
109
+ `@@manage_meta_set_encoding` is `true`. Inasmuch as `ManageMeta` is
110
+ included in ApplicationController::Base, this effects all controllers.
111
+
112
+ You control the value of this variable using
113
+ `manage_meta_set_emit_encoding()`. The default is `true`, so the
114
+ charset meta tags are emitted - subject to . . .
115
+
116
+ NOTE: If @@manage_meta_emit_encoding is `true`, character encoding
117
+ strings are
118
+
119
+ * always emitted for HTML 5 pages
120
+ * Only emitted for HTML 4.01 and XHTML pages IF a `:content_type` meta
121
+ tag is added via `add_meta()`
122
+
86
123
  ### Methods in Detail ###
87
124
 
88
125
  #### `add_meta()` ####
@@ -110,8 +147,8 @@ of the _content_ attribute of the meta tag.
110
147
 
111
148
  Three meta tag formats are defined automatically:
112
149
 
113
- * `:named => '<meta name="#{name}" content="#{content}" char-encoding="utf-8" />'`
114
- * `:http_equiv => '<meta http-equiv="#{name}" content="#{content}" char-encoding="utf-8" />'`
150
+ * `:named => '<meta name="#{name}" content="#{content}" charset="utf-8" />'`
151
+ * `:http_equiv => '<meta http-equiv="#{name}" content="#{content}" charset="utf-8" />'`
115
152
  * `:canonical => '<link rel="canonical" href="#{content}" />'`
116
153
 
117
154
  The _@manage_meta_name_to_format_ is populated with entries mapping known HTTP-EQUIV tags
@@ -146,11 +183,31 @@ simply goes through all the defined key, value pairs in @manage_meta_meta_hash a
146
183
  returns their associated format strings after replacing the `#{name}` and `#{content}`
147
184
  symbols with their values.
148
185
 
149
- `#{name}` is replaced with the meta tag key [as in `:content_type`] passed through
150
- `_manage_meta_sym_to_name()`.
186
+ `#{name}` is replaced with the meta tag key [as in `:content_type`]
187
+ passed through `_manage_meta_sym_to_name()`.
151
188
 
152
189
  `#{value}` is replaced by the value assigned in `@manage_meta_meta_hash`.
153
190
 
191
+ #### manage_meta_set_encoding( encoding ) ####
192
+
193
+ Sets the instance variable `@manage_meta_encoding` to `encoding`.
194
+ The default value is `utf-8`, so don't bother unless you're using
195
+ something else.
196
+
197
+ #### manage_meta_set_html_version( version ) ####
198
+
199
+ sets the instance variable `@manage_meta_html_version` to the symbol
200
+ associated with string `version`. Version is decoded using regular
201
+ expressions which match strings like html5, html 5, HTML 5, html 4,
202
+ etc.
203
+
204
+ The actual value is one of the symbols: :html5, :html4, or :xhtml.
205
+
206
+ An ArgumentError is raised if `version` does not match a known string.
207
+
208
+ The value of `@manage_meta_html_version` is used in prepending a
209
+ `charset` meta tag to string emitted by `render_meta`.
210
+
154
211
  ### Instance Variables in Detail ###
155
212
 
156
213
  All three hashes use symbols for keys.
@@ -179,12 +236,27 @@ such as `:content_length => :http_equiv`
179
236
 
180
237
  both keys and values are symbols
181
238
 
239
+ #### `@@manage_meta_emit_encoding` ####
240
+
241
+ Boolean which is `true` or `false`. Controls whether or not an
242
+ encoding meta tag is emitted
243
+
244
+ #### `@manage_meta_encoding` ####
245
+
246
+ String which is used in the `charset` meta tag which is emitted
247
+
248
+ #### `@manage_meta_html_version` ####
249
+
250
+ Symbol which represents the html version used in the page. Possible
251
+ values are `html4`, `html4`, and `xhtml`. Currently only used to
252
+ control what type of `charset` encoding meta tag to emit.
253
+
182
254
  A Facebook Open Graph Sketch
183
255
  -----------------------
184
256
 
185
257
  First define an appropriate format:
186
258
 
187
- `add_meta_format :property, '<meta property="#{name}" content="#{content}" char-encoding="utf-8">'`
259
+ `add_meta_format :property, '<meta property="#{name}" content="#{content}" charset="utf-8">'`
188
260
 
189
261
  Then, add each Open Graph meta tag you need, specifying both :format and :no_capitalize options:
190
262
 
data/Rakefile CHANGED
@@ -1,33 +1,28 @@
1
1
  require 'rake'
2
+ require './lib/manage_meta/version'
2
3
 
3
4
  # snarf gemspec and set version
4
- x = eval File.new('manage_meta.gemspec').read
5
- manage_meta_version = x.version.to_s
5
+ manage_meta_version = ManageMeta::VERSION.to_s
6
6
 
7
7
  task :default => :test
8
8
 
9
9
  desc "Run ManageMeta unit tests"
10
10
  task :test do
11
+ puts "Version: #{manage_meta_version}"
11
12
  require './test/manage_meta_test'
12
13
  end
13
14
 
14
- desc "run rdoc to create doc"
15
- task :doc do
16
- system 'rdoc'
15
+ desc "build README"
16
+ task :readme do
17
+ system "sed -e s/VERSION/#{manage_meta_version}/ README.markdown.src >README.markdown"
17
18
  end
18
19
 
19
20
  desc "build gem"
20
- task :gem do
21
+ task :gem => :readme do
21
22
  system 'gem build manage_meta.gemspec'
22
23
  end
23
24
 
24
- desc "tag as #{manage_meta_version}"
25
- task :tag do
26
- system "git tag #{manage_meta_version}"
27
- end
28
-
29
- desc "distribute to github and rubygems"
30
- task :distribute => [:tag, :gem] do
31
- system "gem push manage_meta-#{manage_meta_version}.gem"
32
- system "git push manage_meta"
25
+ desc "prep for distributions"
26
+ task :prep => :readme do
27
+ puts "push it by hand - in case you are on a branch or something"
33
28
  end
@@ -1,2 +1,3 @@
1
1
  require 'manage_meta/manage_meta'
2
+ require 'manage_meta/version'
2
3
  require 'manage_meta/railtie' if defined? Rails
@@ -1,4 +1,9 @@
1
1
  module ManageMeta
2
+ LEGAL_HTML_VALUES = {
3
+ html4: /^html\s*4(.01)?$/i,
4
+ html5: /^html\s*5$/i,
5
+ xhtml: /^xhtml\s*(1(\.0)?)?$/i
6
+ }
2
7
  def self.included(mod)
3
8
  begin
4
9
  mod.send(:helper_method, :render_meta)
@@ -9,12 +14,15 @@ module ManageMeta
9
14
  #- initialize instance variables
10
15
  def _manage_meta_init
11
16
  return if @manage_meta_meta_hash.instance_of? Hash
17
+ @manage_meta_html_version = :html5
18
+ @manage_meta_encoding = 'utf-8'
19
+ @@manage_meta_emit_encoding = true
12
20
  @manage_meta_meta_hash = {}
13
21
  @manage_meta_options = {}
14
22
 
15
23
  @manage_meta_format_hash = {
16
- :named => '<meta name="#{name}" content="#{content}" char-encoding="utf-8" />',
17
- :http_equiv => '<meta http-equiv="#{name}" content="#{content}" char-encoding="utf-8" />',
24
+ :named => '<meta name="#{name}" content="#{content}" />',
25
+ :http_equiv => '<meta http-equiv="#{name}" content="#{content}" />',
18
26
  :canonical => '<link rel="canonical" href="#{content}" />',
19
27
  }
20
28
 
@@ -56,7 +64,7 @@ module ManageMeta
56
64
  # make sure name is a string
57
65
  name = _manage_meta_name_to_sym name
58
66
 
59
- # handle optional nonsense
67
+ # handle optional
60
68
  case
61
69
  when opt_value.is_a?(String)
62
70
  value = opt_value
@@ -113,16 +121,57 @@ module ManageMeta
113
121
  # render_meta
114
122
  #
115
123
  # returns a string consisting of all defined meta names in @manage_meta_meta_hash, formatted
116
- # using their name-specific formats and indented two spaces.
124
+ # using their name-specific formats and indented with two spaces.
117
125
  #--
118
126
  def render_meta
119
127
  _manage_meta_init
120
-
121
- ' ' + @manage_meta_meta_hash.map do |name, content|
128
+
129
+ leader = ' '
130
+ if @@manage_meta_emit_encoding
131
+ if @manage_meta_html_version == :html5
132
+ # insert charset meta tag immediately after <head>
133
+ leader += "<meta charset=\"#{@manage_meta_encoding}\">\n "
134
+ else # covers both xhtml and html 4.01
135
+ # augment content_type meta tag with charset encoding and move to top of meta tags
136
+ if (value = @manage_meta_meta_hash.delete(:content_type)) \
137
+ and value !~ /charset/i
138
+ leader += "<meta http-equiv=\"Content-type\" content=\"text/html; charset=#{@manage_meta_encoding}\" />\n "
139
+ end
140
+ end
141
+ end
142
+
143
+ leader + @manage_meta_meta_hash.map do |name, content|
122
144
  @manage_meta_format_hash[@manage_meta_name_to_format[name]].sub('#{name}', _manage_meta_sym_to_name(name)).sub('#{content}', content)
123
145
  end.join("\n ") + " \n"
124
146
  end
125
147
 
148
+ def manage_meta_set_emit_encoding bool
149
+ @@manage_meta_emit_encoding = !!bool
150
+ end
151
+
152
+ #++
153
+ # manage_meta_set_encoding encoding - sets character encoding for page
154
+ #--
155
+
156
+ def manage_meta_set_encoding encoding
157
+ @manage_meta_encoding = encoding
158
+ end
159
+
160
+ #++
161
+ # manage_meta_set_html_version version
162
+ #
163
+ #
164
+ #--
165
+ def manage_meta_set_html_version version = 'html5'
166
+ version_downcase = version.to_s.downcase.strip
167
+ LEGAL_HTML_VALUES.each do |html_key, regx|
168
+ next unless version_downcase =~ regx
169
+ @manage_meta_html_version = html_key
170
+ return
171
+ end
172
+ raise ArgumentError.new("Illegal html version: #{version}")
173
+ end
174
+
126
175
  private
127
176
  def _manage_meta_sym_to_name(sym)
128
177
  _manage_meta_init
@@ -153,4 +202,4 @@ module ManageMeta
153
202
  public :add_meta, :del_meta, :add_meta_format, :render_meta
154
203
  private :_manage_meta_init, :_manage_meta_sym_to_name, :_manage_meta_name_to_sym
155
204
 
156
- end
205
+ end
@@ -0,0 +1,3 @@
1
+ module ManageMeta
2
+ VERSION = '0.0.14'
3
+ end
@@ -189,4 +189,41 @@ class ManageMetaTest < Test::Unit::TestCase
189
189
  add_meta "og:title", 'The Grand Octopus', :format => :property, :no_capitalize => true
190
190
  assert_match '<meta property="og:title" content="The Grand Octopus">', render_meta, "og:title metadata should be correct"
191
191
  end
192
+
193
+ # test_set_encoding
194
+ def test_set_encoding
195
+ self.manage_meta_set_encoding 'foobar'
196
+ assert_equal 'foobar', self.instance_variable_get('@manage_meta_encoding')
197
+ end
198
+
199
+ # test html version
200
+ def test_set_html_version
201
+ self.send :_manage_meta_init
202
+ assert_equal :html5, self.instance_variable_get('@manage_meta_html_version')
203
+ ['html5/HTML 5', 'html5/HTML5',
204
+ 'html4/html 4', 'html4/ html 4.01', 'xhtml/ xhtml/xhtml 1.0',
205
+ 'xhtml/xhtml1' ].each do |str|
206
+ key, version = str.split('/')
207
+ self.manage_meta_set_html_version version
208
+ assert_equal key.to_sym, self.instance_variable_get('@manage_meta_html_version')
209
+ end
210
+ end
211
+
212
+ # test charset encoding in render_meta
213
+ def test_charset_encoding_in_render_meta
214
+ self.send :_manage_meta_init
215
+ assert_match /<meta charset="utf-8">/, self.render_meta
216
+
217
+ self.manage_meta_set_html_version 'html4'
218
+ self.add_meta :content_type, 'text/html'
219
+ assert_match /<meta http-equiv="Content-Type" content="text\/html; charset=utf-8"/i, self.render_meta
220
+ end
221
+
222
+ # test set emit encoding
223
+ def test_emit_encoding
224
+ self.send :_manage_meta_init
225
+ assert @@manage_meta_emit_encoding
226
+ self.manage_meta_set_emit_encoding false
227
+ assert_equal false, @@manage_meta_emit_encoding
228
+ end
192
229
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: manage_meta
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.0.14
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-01-09 00:00:00.000000000Z
12
+ date: 2012-04-11 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Provides (semi-)intellegent management of meta tags for Rails 3
15
15
  email: mike@clove.com
@@ -19,6 +19,7 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - lib/manage_meta/manage_meta.rb
21
21
  - lib/manage_meta/railtie.rb
22
+ - lib/manage_meta/version.rb
22
23
  - lib/manage_meta.rb
23
24
  - test/manage_meta_test.rb
24
25
  - MIT-LICENSE
@@ -44,7 +45,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
44
45
  version: '0'
45
46
  requirements: []
46
47
  rubyforge_project:
47
- rubygems_version: 1.8.6
48
+ rubygems_version: 1.8.11
48
49
  signing_key:
49
50
  specification_version: 3
50
51
  summary: ManageMeta - Yet Another Meta Tag manager for Rails 3