site_meta 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/History.txt +5 -0
  2. data/README.rdoc +71 -0
  3. data/init.rb +1 -0
  4. data/lib/site_meta.rb +204 -0
  5. data/rails/init.rb +3 -0
  6. metadata +91 -0
data/History.txt ADDED
@@ -0,0 +1,5 @@
1
+ == 0.2
2
+ * Added documentation
3
+ * meta_keywords now allows multiple arguments, strips spaces
4
+ == 0.1
5
+ First public release of helpers
data/README.rdoc ADDED
@@ -0,0 +1,71 @@
1
+ = site_meta
2
+
3
+ * http://bragi.github.com/site_meta
4
+
5
+ == DESCRIPTION:
6
+
7
+ Provides helpers for easy adding of html meta information to Rails applications.
8
+
9
+ == FEATURES:
10
+
11
+ * Easily add default description and keywords meta tags.
12
+ * Customize meta tags on per-view basis
13
+ * Add head title tag with breadcrubms
14
+ * Add page title
15
+
16
+ == PROBLEMS:
17
+
18
+ Use lighthouse to report problems/feature requests:
19
+
20
+ * http://bragi.lighthouseapp.com/projects/23211-site_meta
21
+
22
+ == SYNOPSIS:
23
+
24
+ In your layout (haml assumed):
25
+
26
+ %head
27
+ = meta_description("Default meta description")
28
+ = meta_keywords("some, default, keywords")
29
+ = head_title("Site name")
30
+ %body
31
+ %h1= page_title("Default page title")
32
+
33
+ In your view:
34
+
35
+ - set_meta_description(h(@item.description))
36
+ - set_meta_keywords(h(@item.tags.join(",")))
37
+ - set_head_title([h(@item.title), "Showing item"])
38
+ - set_page_title(h(@item.title))
39
+
40
+ == INSTALL:
41
+
42
+ * sudo gem install bragi-site_meta
43
+ * Add following line to your config/environment.rb
44
+ Rails::Initializer.run do |config|
45
+ config.gem 'bragi-site_meta'
46
+ end
47
+
48
+ == LICENSE:
49
+
50
+ (The MIT License)
51
+
52
+ Copyright (c) 2009 Lukasz Piestrzeniewicz
53
+
54
+ Permission is hereby granted, free of charge, to any person obtaining
55
+ a copy of this software and associated documentation files (the
56
+ 'Software'), to deal in the Software without restriction, including
57
+ without limitation the rights to use, copy, modify, merge, publish,
58
+ distribute, sublicense, and/or sell copies of the Software, and to
59
+ permit persons to whom the Software is furnished to do so, subject to
60
+ the following conditions:
61
+
62
+ The above copyright notice and this permission notice shall be
63
+ included in all copies or substantial portions of the Software.
64
+
65
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
66
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
67
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
68
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
69
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
70
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
71
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__)+'/rails/init.rb'
data/lib/site_meta.rb ADDED
@@ -0,0 +1,204 @@
1
+ # Provides helpers for easily adding metadata to your views and layouts.
2
+ #
3
+ # In your layout use helpers +head_title+, +meta_description+, +meta_keywords+
4
+ # with optional defaults. In each view you can override the defaults using
5
+ # helpers +set_head_title+, +set_meta_description+, +set_meta_keywords+.
6
+ #
7
+ # == Providing description meta
8
+ #
9
+ # In your layout use +meta_description+ in head section of HTML with optional
10
+ # default description:
11
+ #
12
+ # %head
13
+ # = meta_description("Site_meta is the easiest way to provide metadata in your Rails application")
14
+ #
15
+ # In each view you can override default setting using +set_meta_description+.
16
+ # In view app/views/pages/show.html.haml
17
+ #
18
+ # - set_meta_description(h(@page.description))
19
+ #
20
+ # == Providing keywords meta
21
+ #
22
+ # In your layout use +meta_keywords+ in head section of HTML with optional
23
+ # list of default keywords:
24
+ #
25
+ # %head
26
+ # = meta_keywords("rails,gem,plugin,ruby,meta,metadata,html")
27
+ #
28
+ # In each view you can override or add keywords using +set_meta_keywords+.
29
+ # In view app/views/pages/show.html.haml
30
+ #
31
+ # - set_meta_keywords(h(@page.tags.join(",")))
32
+ #
33
+ # which will add keywords to the default set or
34
+ #
35
+ # - set_meta_keywords("pricing,information", :replace)
36
+ #
37
+ # to replace the whole list.
38
+ #
39
+ # == Providing title
40
+ #
41
+ # In your layout use +head_title+ in head section and provide last element of
42
+ # breadcrumb trail (usually site name)
43
+ #
44
+ # %head
45
+ # = head_title("Site_meta")
46
+ #
47
+ # In each view you provide title using +set_head_title+, where you can provide
48
+ # more elements of breadcrumbs. In view app/views/pages/show.html.haml
49
+ #
50
+ # - set_head_title(h(@page.title))
51
+ #
52
+ # or in app/views/pages/edit.html.haml
53
+ #
54
+ # - set_head_title("Editing #{h(@page.title)}", "Pages")
55
+ #
56
+ # This will result in title set to:
57
+ #
58
+ # "Page title - Site_meta"
59
+ #
60
+ # in first case and:
61
+ #
62
+ # "Editing Page title - Pages - Site_meta"
63
+ #
64
+ # in second case
65
+ #
66
+ # == Providing title in HTML
67
+ #
68
+ # Title that is presented in the body part of HTML doesn't need the
69
+ # breadcrumbs and may differ from the title presented in HTML head part.
70
+ # Use +page_title+ helper in your layout:
71
+ #
72
+ # %body
73
+ # %h1= page_title
74
+ #
75
+ # In each view set the page title using +set_page_title+ helper. In view
76
+ # app/views/pages/show.html.haml:
77
+ #
78
+ # - set_page_title(h(@page.title))
79
+ #
80
+ # == Providing head and page title at the same time
81
+ #
82
+ # In most situations (like in case of show.html.haml mentioned above) you want
83
+ # to have the same title used as a page title and as a beginning of the
84
+ # breadcrumb trail. In that case use +set_head_and_page_title+ helper.
85
+ # In view app/views/pages/show.html.haml
86
+ #
87
+ # - set_head_and_page_title(h(@page.title), "Pages")
88
+ #
89
+ # This will set page title to first element and set proper trail in head.
90
+ #
91
+ # == Providing encoding
92
+ #
93
+ # To provide encoding information use +meta_content_type+ in your layout use
94
+ #
95
+ # %head
96
+ # = meta_content_type
97
+ #
98
+ # This will provide utf-8 charset by default.
99
+ module SiteMeta
100
+ VERSION = '0.2.0'
101
+
102
+ # Returns meta tag with appropriate encoding specified. +type+ may be any
103
+ # string, it's inserted verbatim. Popular encodings include "utf-8",
104
+ # "iso-8859-1" and others.
105
+ def meta_content_type(type=:utf)
106
+ type = "utf-8" if type == :utf
107
+ meta_tag("Content-Type", "text/html; charset=#{type}", "http-equiv")
108
+ end
109
+
110
+ # Returns string with title tag to use in html head. It uses +default_title+
111
+ # unless you provide breadcrumb elements using +set_head_title+ or
112
+ # +set_head_and_page_title+. Elements of title (with +default_title+
113
+ # being the last one) are joined using +separator+.
114
+ def head_title(default_title, separator=" - ")
115
+ title = [@head_title, default_title].flatten.compact.join(separator)
116
+ title_tag(title)
117
+ end
118
+
119
+ # Returns description meta tag with +default_description+ to use in html
120
+ # head. You can override it on per-view base using +set_meta_description+.
121
+ # Returns nil when +default_description+ is nil and you did not provide one
122
+ # using +set_meta_description+.
123
+ def meta_description(default_description=nil)
124
+ description = @meta_description || default_description
125
+ if description
126
+ meta_tag(:description, description)
127
+ end
128
+ end
129
+
130
+ # Returns keywords meta tag with +default_keywords+ to use in html head.
131
+ # You can override keywords on per-view base using +set_meta_keywords+.
132
+ # Optional +default_keywords+ may be a comma separated string or array of
133
+ # strings. Returns nil when +default_keywords+ is nil and none additional
134
+ # keywords were provided through +set_meta_keywords+.
135
+ def meta_keywords(default_keywords=[])
136
+ merge_mode = @meta_keywords_merge_mode || :merge
137
+ meta_keywords = split_keywords(@meta_keywords)
138
+ keywords = if merge_mode == :merge
139
+ default_keywords = split_keywords(default_keywords)
140
+ (meta_keywords + default_keywords).uniq
141
+ else
142
+ meta_keywords
143
+ end
144
+
145
+ unless keywords.empty?
146
+ meta_tag(:keywords, keywords.join(","))
147
+ end
148
+ end
149
+
150
+ # Returns page title set using +set_page_title+
151
+ def page_title
152
+ @page_title
153
+ end
154
+
155
+ # Sets head title breadcrumbs trail
156
+ def set_head_title(*title)
157
+ @head_title = title.flatten
158
+ end
159
+
160
+ # Sets head title breadcrumbs trail and page title to the leftmost element.
161
+ def set_head_and_page_title(*title)
162
+ title = [title].flatten.compact
163
+ set_head_title title
164
+ set_page_title title.first
165
+ end
166
+
167
+ # Sets page title to use with +page_title+
168
+ def set_page_title(title)
169
+ @page_title = title
170
+ end
171
+
172
+ # Sets meta +description+ to use with +meta_description+.
173
+ def set_meta_description(description)
174
+ @meta_description = description
175
+ end
176
+
177
+ # Sets +keywords+ to use with +meta_keywords+. Optional +merge_mode+ may be
178
+ # set to either :merge (default) or :replace. In the second mode provided
179
+ # keywords are not merged with defaults when output using +meta_keywords+.
180
+ def set_meta_keywords(keywords, merge_mode=:merge)
181
+ raise(ArgumentError, "Allowed merge modes are only :replace, :merge") unless [:replace,:merge].include?(merge_mode)
182
+ @meta_keywords = keywords
183
+ @meta_keywords_merge_mode = merge_mode
184
+ end
185
+
186
+ def split_keywords(keywords) #:nodoc:
187
+ return [] unless keywords
188
+ keywords = keywords.split(",") if keywords.is_a? String
189
+ keywords.flatten.map {|k| k.strip}
190
+ end
191
+
192
+ def meta_tag(name, content, key='name') #:nodoc:
193
+ if defined?(ActionView::Base)
194
+ tag 'meta', key => name, :content => content
195
+ else
196
+ "<meta #{key}=\"#{name}\" content=\"#{content}\" />"
197
+ end
198
+ end
199
+
200
+ def title_tag(content) #:nodoc:
201
+ "<title>#{content}</title>"
202
+ end
203
+
204
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ ActionView::Base.class_eval do
2
+ include SiteMeta
3
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: site_meta
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Lukasz Piestrzeniewicz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-08 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: actionpack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.0.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: newgem
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.0
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: hoe
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.8.0
44
+ version:
45
+ description: Provides helpers for easy adding of html meta information to Rails applications.
46
+ email:
47
+ - bragi@ragnarson.com
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ extra_rdoc_files:
53
+ - History.txt
54
+ - README.rdoc
55
+ files:
56
+ - History.txt
57
+ - init.rb
58
+ - lib/site_meta.rb
59
+ - rails/init.rb
60
+ - README.rdoc
61
+ has_rdoc: true
62
+ homepage: http://github.com/bragi/site_meta
63
+ licenses: []
64
+
65
+ post_install_message:
66
+ rdoc_options:
67
+ - --main
68
+ - README.rdoc
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ version:
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ requirements: []
84
+
85
+ rubyforge_project: site_meta
86
+ rubygems_version: 1.3.5
87
+ signing_key:
88
+ specification_version: 2
89
+ summary: Provides helpers for easy adding of html meta information to Rails applications.
90
+ test_files: []
91
+