meta-tags 1.2.4 → 1.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,5 +2,6 @@
2
2
  .DS_Store
3
3
  .bundle
4
4
  .rvmrc
5
+ Gemfile.lock
5
6
  doc
6
7
  pkg
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - ree
5
+ - jruby
6
+
7
+ notifications:
8
+ recipients:
9
+ - kpumuk@kpumuk.info
data/CHANGELOG.md ADDED
@@ -0,0 +1,49 @@
1
+ ## 1.2.4 (April 26, 2011)
2
+
3
+ Features:
4
+
5
+ - Added support for Open Graph meta tags
6
+
7
+ Bugfixes:
8
+
9
+ - Fixed bug with double HTML escaping in title separator
10
+ - Allow to set meta title without a separator
11
+
12
+ ## 1.2.2, 1.2.3 (June 10, 2010)
13
+
14
+ Bugfixes:
15
+
16
+ - Fixed action\_pack integration (welcome back `alias_method_chain`)
17
+ - Fixed bug when `@page_*` variables did not work
18
+
19
+ ## 1.2.1 (June 2, 2010)
20
+
21
+ Bugfixes:
22
+
23
+ - Fixed deprecation warning about `html_safe!`
24
+
25
+ ## 1.2.0 (May 31, 2010)
26
+
27
+ Bugfixes:
28
+
29
+ - Fixed bug when title is set through Array, and `:lowercase` is true
30
+ - Updated `display_meta_tags` to be compatible with rails_xss
31
+
32
+ ## 1.1.1 (November 21, 2009)
33
+
34
+ Features:
35
+
36
+ - Added support for canonical link element
37
+ - Added YARD documentation
38
+
39
+ ## 1.1.0 (November 5, 2009)
40
+
41
+ Features:
42
+
43
+ - Added ability to specify title as an Array of parts
44
+ - Added helper for `noindex`
45
+ - Added `nofollow` meta tag support
46
+
47
+ Bugfixes:
48
+
49
+ - Fixed Rails 2.3 deprecation warnings
data/MIT-LICENSE CHANGED
@@ -1,4 +1,6 @@
1
- Copyright (c) 2008 Dmytro Shteflyuk
1
+ Copyright (c) 2011 Dmytro Shteflyuk
2
+
3
+ MIT License
2
4
 
3
5
  Permission is hereby granted, free of charge, to any person obtaining
4
6
  a copy of this software and associated documentation files (the
@@ -17,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
19
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
20
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
21
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,248 @@
1
+ # MetaTags: a gem to make your Rails application SEO-friendly
2
+
3
+ [![Travis-CI build status](https://secure.travis-ci.org/kpumuk/meta-tags.png)](http://travis-ci.org/kpumuk/meta-tags)
4
+
5
+ Search Engine Optimization (SEO) plugin for Ruby on Rails applications.
6
+
7
+ ## Rails 3
8
+
9
+ MetaTags master branch now fully supports Rails 3 and is backward
10
+ compatible.
11
+
12
+ ## Installation
13
+
14
+ Add the "meta-tags" gem to your `Gemfile`.
15
+
16
+ gem 'meta-tags', :require => 'meta_tags'
17
+
18
+ And run `bundle install` command.
19
+
20
+ ## SEO Basics and MetaTags
21
+
22
+ ### Titles
23
+
24
+ Page titles are very important for Search engines. The titles in the
25
+ browser are displayed in the title bar. The search engines would look at
26
+ the this title bar to determine what the page is all about.
27
+
28
+ set_meta_tags :title => 'Member Login'
29
+ # <title>Some Page Title</title>
30
+ set_meta_tags :site => 'Site Title', :title => 'Member Login'
31
+ # <title>Site Title | Page Title</title>
32
+ set_meta_tags :site => 'Site Title', :title => 'Member Login', :reverse => true
33
+ # <title>Page Title | Site Title</title>
34
+
35
+ Recommended title tag length: up to <b>70 characters</b>, <b>10 words</b>.
36
+
37
+ ### Description
38
+
39
+ Description tags are called meta tags as they are not displayed by the
40
+ browsers as that of titles. But these descriptions may be displayed by
41
+ some search engines. They are used to describe the contents of a page in
42
+ 2 or 3 sentences.
43
+
44
+ set_meta_tags :description => "All text about keywords, other keywords"
45
+ # <meta name="description" content="All text about keywords, other keywords" />
46
+
47
+ Recommended description tag length: up to <b>160 characters</b>.
48
+
49
+ ### Keywords
50
+
51
+ Meta keywords tag are used to place your keywords that you think a
52
+ surfer would search in Search engines. Repeating keywords unnecessarily
53
+ would be considered spam and you may get permanently banned from SERP's
54
+
55
+ set_meta_tags :keywords => %w[keyword1 Keyword2 KeyWord3]
56
+ # <meta name="keywords" content="keyword1, keyword2, keyword3" />
57
+
58
+ Recommended keywords tag length: up to <b>255 characters</b>, <b>20 words</b>.
59
+
60
+ ### Noindex
61
+
62
+ By using the noindex meta tag, you can signal to search engines to not
63
+ include specific pages in their indexes.
64
+
65
+ set_meta_tags :noindex => true
66
+ # <meta name="robots" content="noindex" />
67
+ set_meta_tags :noindex => 'googlebot'
68
+ # <meta name="googlebot" content="noindex" />
69
+
70
+ This is useful for pages like login, password reset, privacy policy, etc.
71
+
72
+ Further reading:
73
+
74
+ * [Blocking Google](http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=93708)
75
+ * [Using meta tags to block access to your site](http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=93710)
76
+
77
+ ### Nofollow
78
+
79
+ Nofollow meta tag tells a search engine not to follow the links on a specific
80
+ page. It's entirely likely that a robot might find the same links on some
81
+ other page without a nofollow (perhaps on some other site), and so
82
+ still arrives at your undesired page.
83
+
84
+ set_meta_tags :nofollow => true
85
+ # <meta name="robots" content="nofollow" />
86
+ set_meta_tags :nofollow => 'googlebot'
87
+ # <meta name="googlebot" content="nofollow" />
88
+
89
+ Further reading:
90
+
91
+ * [About rel="nofollow"](http://www.google.com/support/webmasters/bin/answer.py?answer=96569)
92
+ * [Meta tags](http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=79812)
93
+
94
+ ### Canonical URL
95
+
96
+ Canonical link element tells a search engine what is the canonical or main URL
97
+ for a content which have multiple URLs. The search engine will always return
98
+ that URL, and link popularity and authority will be applied to that URL.
99
+
100
+ set_meta_tags :canonical => "http://yoursite.com/canonical/url"
101
+ # <link rel="canonical" href="http://yoursite.com/canonical/url" />
102
+
103
+ Further reading:
104
+
105
+ * [About rel="canonical"](http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=139394)
106
+ * [Canonicalization](http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=139066)
107
+
108
+ ### Open Graph
109
+
110
+ To turn your web pages into graph objects, you'll need to add Open Graph
111
+ protocol `<meta>` tags to your webpages. The tags allow you to specify
112
+ structured information about your web pages. The more information you provide, the more opportunities your web pages can be surfaced within Facebook today
113
+ and in the future. Here's an example for a movie page:
114
+
115
+ set_meta_tags :open_graph => {
116
+ :title => 'The Rock',
117
+ :type => :movie,
118
+ :url => 'http://www.imdb.com/title/tt0117500/',
119
+ :image => 'http://ia.media-imdb.com/rock.jpg'
120
+ }
121
+ # <meta property="og:title" content="The Rock"/>
122
+ # <meta property="og:type" content="movie"/>
123
+ # <meta property="og:url" content="http://www.imdb.com/title/tt0117500/"/>
124
+ # <meta property="og:image" content="http://ia.media-imdb.com/rock.jpg"/>
125
+
126
+ Further reading:
127
+
128
+ * [Open Graph protocol](http://developers.facebook.com/docs/opengraph/)
129
+
130
+ ## MetaTags Usage
131
+
132
+ First, add this code to your main layout:
133
+
134
+ <head>
135
+ <%= display_meta_tags :site => 'My website' %>
136
+ </head>
137
+
138
+ Then, to set the page title, add this to each of your views (see below for other options):
139
+
140
+ <h1><%= title 'My page title' %></h1>
141
+
142
+ When views are rendered, the page title will be included in the right spots:
143
+
144
+ <head>
145
+ <title>My website | My page title</title>
146
+ </head>
147
+ <body>
148
+ <h1>My page title</h1>
149
+ </body>
150
+
151
+ You can find allowed options for `display_meta_tags` method below.
152
+
153
+ ### Using MetaTags in controller
154
+
155
+ You can define following instance variables:
156
+
157
+ @page_title = 'Member Login'
158
+ @page_description = 'Member login page.'
159
+ @page_keywords = 'Site, Login, Members'
160
+
161
+ Also you could use `set_meta_tags` method to define all meta tags simultaneously:
162
+
163
+ set_meta_tags :title => 'Member Login',
164
+ :description => 'Member login page.',
165
+ :keywords => 'Site, Login, Members'
166
+
167
+ You can find allowed options for `set_meta_tags` method below.
168
+
169
+ ### Using MetaTags in view
170
+
171
+ To set meta tags you can use following methods:
172
+
173
+ <% title 'Member Login' %>
174
+ <% description 'Member login page.' %>
175
+ <% keywords 'Member login page.' %>
176
+
177
+ Also there is `set_meta_tags` method exists:
178
+
179
+ <% set_meta_tags :title => 'Member Login',
180
+ :description => 'Member login page.',
181
+ :keywords => 'Site, Login, Members' %>
182
+
183
+ The `title` method returns title itself, so you can use it to show the title
184
+ somewhere on the page:
185
+
186
+ <h1><%= title 'Member Login' %></h1>
187
+
188
+ If you want to set the title and display another text, use this:
189
+
190
+ <h1><%= title 'Member Login', 'Here you can login to the site:' %></h1>
191
+
192
+ ### Allowed options for `display_meta_tags` and `set_meta_tags` methods
193
+
194
+ Use these options to customize the title format:
195
+
196
+ * `:site` — site title;
197
+ * `:title` — page title;
198
+ * `:description` — page description;
199
+ * `:keywords` — page keywords;
200
+ * `:prefix` — text between site name and separator;
201
+ * `:separator` — text used to separate website name from page title;
202
+ * `:suffix` — text between separator and page title;
203
+ * `:lowercase` — when true, the page name will be lowercase;
204
+ * `:reverse` — when true, the page and site names will be reversed;
205
+ * `:noindex` — add noindex meta tag; when true, 'robots' will be used, otherwise the string will be used;
206
+ * `:nofollow` — add nofollow meta tag; when true, 'robots' will be used, otherwise the string will be used;
207
+ * `:canonical` — add canonical link tag;
208
+ * `:open_graph` — add Open Graph tags (Hash).
209
+
210
+ And here are a few examples to give you ideas.
211
+
212
+ <%= display_meta_tags :separator => "&mdash;".html_safe %>
213
+ <%= display_meta_tags :prefix => false, :separator => ":" %>
214
+ <%= display_meta_tags :lowercase => true %>
215
+ <%= display_meta_tags :reverse => true, :prefix => false %>
216
+ <%= display_meta_tags :open_graph => { :title => 'The Rock', :type => 'movie' } %>
217
+
218
+ ### Allowed values
219
+
220
+ You can specify `:title` as a string or array:
221
+
222
+ set_meta_tags :title => ['part1', 'part2'], :site => 'site'
223
+ # site | part1 | part2
224
+ set_meta_tags :title => ['part1', 'part2'], :reverse => true, :site => 'site'
225
+ # part2 | part1 | site
226
+
227
+ Keywords can be passed as string of comma-separated values, or as an array:
228
+
229
+ set_meta_tags :keywords => ['tag1', 'tag2']
230
+ # tag1, tag2
231
+
232
+ Description is a string (HTML will be stripped from output string).
233
+
234
+ ## Alternatives
235
+
236
+ There are several plugins influenced me to create this one:
237
+
238
+ * [Headliner](https://github.com/mokolabs/headliner)
239
+ * [meta\_on_rals](https://github.com/ashchan/meta_on_rails)
240
+
241
+ ## Credits
242
+
243
+ * [Dmytro Shteflyuk](https://github.com/kpumuk) (author)
244
+ * [Morgan Roderick](https://github.com/mroderick) (contributor)
245
+ * [Jesse Clark](https://github.com/jesseclark) (contributor)
246
+ * Sergio Cambra (contributor)
247
+ * Kristoffer Renholm (contributor)
248
+ * [Jürg Lehni](https://github.com/lehni) (contributor)
data/Rakefile CHANGED
@@ -1,16 +1,18 @@
1
1
  require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
3
 
4
- begin
5
- require 'yard'
6
- YARD::Rake::YardocTask.new(:yard) do |t|
7
- t.options = ['--title', 'MetaTags Documentation']
8
- if ENV['PRIVATE']
9
- t.options.concat ['--protected', '--private']
10
- else
11
- t.options.concat ['--protected', '--no-private']
12
- end
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :test => :spec
8
+ task :default => :spec
9
+
10
+ require 'yard'
11
+ YARD::Rake::YardocTask.new(:yard) do |t|
12
+ t.options = ['--title', 'MetaTags Documentation']
13
+ if ENV['PRIVATE']
14
+ t.options.concat ['--protected', '--private']
15
+ else
16
+ t.options.concat ['--protected', '--no-private']
13
17
  end
14
- rescue LoadError
15
- puts 'Yard not available. Install it with: sudo gem install yard'
16
18
  end
@@ -32,10 +32,17 @@ module MetaTags
32
32
  #
33
33
  # See <tt>MetaTags.set_meta_tags</tt> for details.
34
34
  def set_meta_tags(meta_tags)
35
- @meta_tags ||= {}
36
- @meta_tags.merge!(meta_tags || {})
35
+ meta_tags ||= {}
36
+ meta_tags[:open_graph] = meta_tags.delete(:og) if meta_tags.key?(:og)
37
+ self.meta_tags.deep_merge!(meta_tags || {})
37
38
  end
38
39
  protected :set_meta_tags
40
+
41
+ # Get meta tags for the page.
42
+ def meta_tags
43
+ @meta_tags ||= {}
44
+ end
45
+ protected :meta_tags
39
46
  end
40
47
  end
41
48
  end
@@ -1,3 +1,3 @@
1
1
  module MetaTags
2
- VERSION = '1.2.4'
2
+ VERSION = '1.2.5'
3
3
  end
@@ -2,6 +2,11 @@ module MetaTags
2
2
  # Contains methods to use in views and helpers.
3
3
  #
4
4
  module ViewHelper
5
+ # Get meta tags for the page.
6
+ def meta_tags
7
+ @meta_tags ||= {}
8
+ end
9
+
5
10
  # Set meta tags for the page.
6
11
  #
7
12
  # Method could be used several times, and all options passed will
@@ -21,8 +26,7 @@ module MetaTags
21
26
  # @see #display_meta_tags
22
27
  #
23
28
  def set_meta_tags(meta_tags = {})
24
- @meta_tags ||= {}
25
- @meta_tags.merge!(meta_tags || {})
29
+ self.meta_tags.deep_merge! normalize_open_graph(meta_tags)
26
30
  end
27
31
 
28
32
  # Set the page title and return it back.
@@ -145,42 +149,12 @@ module MetaTags
145
149
  # </head>
146
150
  #
147
151
  def display_meta_tags(default = {})
148
- meta_tags = (default || {}).merge(@meta_tags || {})
149
-
150
- # Prefix (leading space)
151
- prefix = meta_tags[:prefix] === false ? '' : (meta_tags[:prefix] || ' ')
152
-
153
- # Separator
154
- separator = meta_tags[:separator] === false ? '' : (meta_tags[:separator] || '|')
155
-
156
- # Suffix (trailing space)
157
- suffix = meta_tags[:suffix] === false ? '' : (meta_tags[:suffix] || ' ')
158
-
159
- # Special case: if separator is hidden, do not display suffix/prefix
160
- if meta_tags[:separator] == false
161
- prefix = suffix = ''
162
- end
163
-
164
- # Title
165
- title = meta_tags[:title]
166
- if meta_tags[:lowercase] === true and !title.blank?
167
- title = Array(title).map { |t| t.downcase }
168
- end
152
+ meta_tags = normalize_open_graph(default).deep_merge!(self.meta_tags)
169
153
 
170
154
  result = []
171
155
 
172
156
  # title
173
- if title.blank?
174
- result << content_tag(:title, meta_tags[:site])
175
- else
176
- title = normalize_title(title).unshift(h(meta_tags[:site]))
177
- title.reverse! if meta_tags[:reverse] === true
178
- sep = h(prefix) + h(separator) + h(suffix)
179
- title = title.join(sep)
180
- # We escaped every chunk of the title, so the whole title should be HTML safe
181
- title = title.html_safe if title.respond_to?(:html_safe)
182
- result << content_tag(:title, title)
183
- end
157
+ result << content_tag(:title, build_full_title(meta_tags))
184
158
 
185
159
  # description
186
160
  description = normalize_description(meta_tags[:description])
@@ -203,8 +177,7 @@ module MetaTags
203
177
  end
204
178
 
205
179
  # Open Graph
206
- open_graph = meta_tags[:open_graph] || meta_tags[:og] || {}
207
- open_graph.each do |property, content|
180
+ (meta_tags[:open_graph] || {}).each do |property, content|
208
181
  result << tag(:meta, :property => "og:#{property}", :content => content)
209
182
  end
210
183
 
@@ -226,14 +199,56 @@ module MetaTags
226
199
  end
227
200
 
228
201
  def normalize_description(description)
229
- return '' unless description
202
+ return '' if description.blank?
230
203
  truncate(strip_tags(description).gsub(/\s+/, ' '), :length => 200)
231
204
  end
232
205
 
233
206
  def normalize_keywords(keywords)
234
- return '' unless keywords
207
+ return '' if keywords.blank?
235
208
  keywords = keywords.flatten.join(', ') if Array === keywords
236
209
  strip_tags(keywords).mb_chars.downcase
237
210
  end
211
+
212
+ def normalize_open_graph(meta_tags)
213
+ meta_tags ||= {}
214
+ meta_tags[:open_graph] = meta_tags.delete(:og) if meta_tags.key?(:og)
215
+ meta_tags
216
+ end
217
+
218
+ def build_full_title(meta_tags)
219
+ # Prefix (leading space)
220
+ prefix = meta_tags[:prefix] === false ? '' : (meta_tags[:prefix] || ' ')
221
+
222
+ # Separator
223
+ separator = meta_tags[:separator] === false ? '' : (meta_tags[:separator] || '|')
224
+
225
+ # Suffix (trailing space)
226
+ suffix = meta_tags[:suffix] === false ? '' : (meta_tags[:suffix] || ' ')
227
+
228
+ # Special case: if separator is hidden, do not display suffix/prefix
229
+ if meta_tags[:separator] == false
230
+ prefix = suffix = ''
231
+ end
232
+
233
+ # Title
234
+ title = meta_tags[:title]
235
+ if meta_tags[:lowercase] === true and !title.blank?
236
+ title = Array(title).map { |t| t.downcase }
237
+ end
238
+
239
+ # title
240
+ if title.blank?
241
+ meta_tags[:site]
242
+ else
243
+ title = normalize_title(title)
244
+ title.unshift(h(meta_tags[:site])) unless meta_tags[:site].blank?
245
+ title.reverse! if meta_tags[:reverse] === true
246
+ sep = h(prefix) + h(separator) + h(suffix)
247
+ title = title.join(sep)
248
+ # We escaped every chunk of the title, so the whole title should be HTML safe
249
+ title = title.html_safe if title.respond_to?(:html_safe)
250
+ title
251
+ end
252
+ end
238
253
  end
239
254
  end