seo_meta_builder 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +3 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +15 -0
- data/Rakefile +13 -0
- data/init.rb +1 -0
- data/lib/seo_meta_builder.rb +104 -0
- data/spec/locales/en.yml +15 -0
- data/spec/locales/fr.yml +14 -0
- data/spec/seo_meta_builder/seo_meta_builder_spec.rb +100 -0
- data/spec/spec_helper.rb +19 -0
- metadata +73 -0
data/CHANGELOG.rdoc
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Guillaume Luccisano - g-mai|: guillaume.luccisano
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
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.
|
data/README.rdoc
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
= SeoMetaBuilder
|
2
|
+
|
3
|
+
Seo Meta Builder is a plugin for Ruby on Rails that lets you easily manage your meta titles and descriptions.
|
4
|
+
|
5
|
+
All necessary informations are available on the wiki : http://wiki.github.com/kwi/seo_meta_builder
|
6
|
+
|
7
|
+
== Functionalities :
|
8
|
+
|
9
|
+
* Help to eradicate duplicate titles and descriptions (Very good for SEO)
|
10
|
+
* Easy to set up
|
11
|
+
* Build title/description with multiple parameters
|
12
|
+
* Works with multiple languages through the I18n Api.
|
13
|
+
* Works with Rails 2 and Rails 3
|
14
|
+
|
15
|
+
Copyright (c) 2010 Guillaume Luccisano - g-mai|: guillaume.luccisano, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'spec/rake/spectask'
|
4
|
+
|
5
|
+
spec_files = Rake::FileList["spec/**/*_spec.rb"]
|
6
|
+
|
7
|
+
desc "Run specs for current Rails version"
|
8
|
+
Spec::Rake::SpecTask.new do |t|
|
9
|
+
t.spec_files = spec_files
|
10
|
+
t.spec_opts = ["-c"]
|
11
|
+
end
|
12
|
+
|
13
|
+
task :default => :spec
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'seo_meta_builder'
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# Build localized titles and description for webpages
|
3
|
+
module SeoMetaBuilder
|
4
|
+
|
5
|
+
# Set title and description at the same time
|
6
|
+
def set_page_metas(hash, order = nil)
|
7
|
+
set_page_title(hash, order)
|
8
|
+
set_page_description(hash, order)
|
9
|
+
end
|
10
|
+
|
11
|
+
# Set the page title with an hash of parameters
|
12
|
+
def set_page_title(hash, order = nil, type = :title)
|
13
|
+
@page_title = page_meta_string_builder(type, hash, order)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Set the page description with an hash of parameters
|
17
|
+
def set_page_description(hash, order = nil, type = :description)
|
18
|
+
@page_description = page_meta_string_builder(type, hash, order)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def page_meta_auto_hash
|
23
|
+
hash = {}
|
24
|
+
hash[:id] = params[:id] if !params[:id].blank?
|
25
|
+
if !params[:page].blank?
|
26
|
+
(hash[:page] = params[:page].to_i) rescue nil
|
27
|
+
end
|
28
|
+
return hash
|
29
|
+
end
|
30
|
+
|
31
|
+
AutoOrder = [:id, :page]
|
32
|
+
|
33
|
+
# Return the default action name to use as scope
|
34
|
+
def page_meta_action_name
|
35
|
+
case params[:action]
|
36
|
+
when 'create'
|
37
|
+
:new
|
38
|
+
when 'update'
|
39
|
+
:edit
|
40
|
+
when 'destroy'
|
41
|
+
:show
|
42
|
+
else
|
43
|
+
params[:action]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Return an escaped value for building metas
|
48
|
+
def page_meta_escape_value(val)
|
49
|
+
val.to_s.gsub("<", "<").gsub(">", ">")
|
50
|
+
end
|
51
|
+
|
52
|
+
# Build a meta string for i18n dependings on parameters
|
53
|
+
def page_meta_string_builder(type = :title, hash = {}, order = nil)
|
54
|
+
pt = "page_#{type}"
|
55
|
+
|
56
|
+
# Build order table
|
57
|
+
order = hash.keys if !order
|
58
|
+
order.each do |k|
|
59
|
+
pt << " - #{k}: %s" if hash[k]
|
60
|
+
end
|
61
|
+
|
62
|
+
# Build values table ordered and escaped
|
63
|
+
values = []
|
64
|
+
order.each do |k|
|
65
|
+
# Escape value :
|
66
|
+
values << page_meta_escape_value(hash[k]) if hash[k]
|
67
|
+
end
|
68
|
+
|
69
|
+
pt = I18n.t(pt, :default => 'no', :scope => :"meta.#{params[:controller]}.#{page_meta_action_name}")
|
70
|
+
|
71
|
+
# No translation found
|
72
|
+
# So use meta/default translation !
|
73
|
+
if pt == 'no'
|
74
|
+
pt = "page_#{type}"
|
75
|
+
pt << (' - %s' * values.size)
|
76
|
+
pt = I18n.t(pt, :default => pt, :scope => :'meta.default')
|
77
|
+
end
|
78
|
+
|
79
|
+
begin
|
80
|
+
sprintf(pt, *values)
|
81
|
+
rescue Exception
|
82
|
+
raise 'Mal formed string : ' + pt.to_s + ' | for : ' + values.join(' - ').to_s
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
public
|
87
|
+
def page_title
|
88
|
+
@page_title ||= page_meta_string_builder(:title, page_meta_auto_hash, AutoOrder)
|
89
|
+
end
|
90
|
+
|
91
|
+
def page_description
|
92
|
+
@page_description ||= page_meta_string_builder(:description, page_meta_auto_hash, AutoOrder)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
# In order to use with rails :
|
98
|
+
if defined? ActionView
|
99
|
+
ActionView::Base.send :include, SeoMetaBuilder
|
100
|
+
end
|
101
|
+
|
102
|
+
if defined? ActionController
|
103
|
+
ActionController::Base.send :include, SeoMetaBuilder
|
104
|
+
end
|
data/spec/locales/en.yml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
en:
|
2
|
+
yo: 'salut'
|
3
|
+
meta:
|
4
|
+
default:
|
5
|
+
page_title: 'Default Title'
|
6
|
+
page_description: 'Default description'
|
7
|
+
"page_title - %s": 'Default Title %s'
|
8
|
+
"page_title - %s - %s": 'Default Title %s - %s'
|
9
|
+
users:
|
10
|
+
index:
|
11
|
+
page_title: 'Members list'
|
12
|
+
page_description: 'Members list description'
|
13
|
+
show:
|
14
|
+
"page_title - login: %s": '%s, member of our site'
|
15
|
+
"page_description - login: %s": '%s is member of our website'
|
data/spec/locales/fr.yml
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
fr:
|
2
|
+
meta:
|
3
|
+
default:
|
4
|
+
page_title: 'Default Title Fr'
|
5
|
+
page_description: 'Default description Fr'
|
6
|
+
"page_title - %s": 'Default Title %s Fr'
|
7
|
+
"page_title - %s - %s": 'Default Title %s Fr - %s'
|
8
|
+
users:
|
9
|
+
index:
|
10
|
+
page_title: 'Listing des membres'
|
11
|
+
page_description: 'La page de la liste des membres'
|
12
|
+
show:
|
13
|
+
"page_title - login: %s": '%s, membre de notre site'
|
14
|
+
"page_description - login: %s": '%s est membre de notre site'
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe :seo_meat_builder do
|
4
|
+
before(:all) do
|
5
|
+
I18n.load_path = []
|
6
|
+
I18n.reload!
|
7
|
+
end
|
8
|
+
|
9
|
+
before(:each) do
|
10
|
+
@o = SeoMetaTester.new
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should show page_title when nothing is translated" do
|
14
|
+
@o.page_title.should == 'page_title'
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should show page_description when nothing is translated" do
|
18
|
+
@o.page_description.should == 'page_description'
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should just load i18n translations test :p" do
|
22
|
+
# Add I18n load_path
|
23
|
+
I18n.load_path = (I18n.load_path << Dir[File.join(File.dirname(__FILE__), '../locales', '*.yml')]).uniq
|
24
|
+
I18n.reload!
|
25
|
+
I18n.locale = :en
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should show correct default for page_title" do
|
29
|
+
@o.page_title.should == 'Default Title'
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should show correct default for page_description" do
|
33
|
+
@o.page_description.should == 'Default description'
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should show correct default for page_title with auto id parameters" do
|
37
|
+
@o.params = {:controller => :u, :action => :a, :id => 1}
|
38
|
+
@o.page_title.should == 'Default Title 1'
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should show correct default for page_title with auto id parameters in fr" do
|
42
|
+
I18n.locale = :fr
|
43
|
+
@o.params = {:controller => :u, :action => :a, :id => 1}
|
44
|
+
@o.page_title.should == 'Default Title 1 Fr'
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should show correct default for page_title with auto id and page parameters" do
|
48
|
+
I18n.locale = :en
|
49
|
+
@o.params = {:controller => :u, :action => :a, :id => 1, :page => 32}
|
50
|
+
@o.page_title.should == 'Default Title 1 - 32'
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should show correct default for page_title with auto id and page parameters in fr" do
|
54
|
+
I18n.locale = :fr
|
55
|
+
@o.params = {:controller => :u, :action => :a, :id => 1, :page => 32}
|
56
|
+
@o.page_title.should == 'Default Title 1 Fr - 32'
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should show correct page_title with translation done" do
|
60
|
+
I18n.locale = :en
|
61
|
+
@o.params = {:controller => :users, :action => :index}
|
62
|
+
@o.page_title.should == 'Members list'
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should show correct page_title with parameters" do
|
66
|
+
I18n.locale = :en
|
67
|
+
@o.params = {:controller => :users, :action => :show}
|
68
|
+
@o.set_page_title(:login => 'kwi')
|
69
|
+
@o.page_title.should == 'kwi, member of our site'
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should show correct page_title with parameters in fr" do
|
73
|
+
I18n.locale = :fr
|
74
|
+
@o.params = {:controller => :users, :action => :show}
|
75
|
+
@o.set_page_title(:login => 'kwi')
|
76
|
+
@o.page_title.should == 'kwi, membre de notre site'
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should show correct ordered parameters" do
|
80
|
+
I18n.locale = :en
|
81
|
+
@o.params = {:controller => :users, :action => :show}
|
82
|
+
@o.set_page_title({:p1 => :p1, :p3 => :p3, :p2 => :p2, :login => 'kwi'}, [:login, :p1, :p2, :p3])
|
83
|
+
@o.page_title.should == 'page_title - kwi - p1 - p2 - p3'
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should show not show parameters if no order is specified" do
|
87
|
+
I18n.locale = :en
|
88
|
+
@o.params = {:controller => :users, :action => :show}
|
89
|
+
@o.set_page_title({:p1 => :p1, :p3 => :p3, :p2 => :p2, :login => 'kwi'}, [:login, :p4, :p3])
|
90
|
+
@o.page_title.should == 'Default Title kwi - p3'
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should correctly escape parameters" do
|
94
|
+
I18n.locale = :fr
|
95
|
+
@o.params = {:controller => :users, :action => :show}
|
96
|
+
@o.set_page_title(:login => '<script></script>')
|
97
|
+
@o.page_title.should == '<script></script>, membre de notre site'
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
|
4
|
+
require 'i18n'
|
5
|
+
require 'active_support'
|
6
|
+
|
7
|
+
require File.dirname(__FILE__) + '/../init.rb'
|
8
|
+
|
9
|
+
class SeoMetaTester
|
10
|
+
include SeoMetaBuilder
|
11
|
+
|
12
|
+
def params=(params)
|
13
|
+
@params = params
|
14
|
+
end
|
15
|
+
|
16
|
+
def params
|
17
|
+
@params || {:controller => :controller, :action => :index}
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: seo_meta_builder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Guillaume Luccisano
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-04-14 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Seo Meta Builder is a plugin for Ruby on Rails that lets you easily manage your meta titles and descriptions.
|
22
|
+
email: guillaume.luccisano@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/seo_meta_builder.rb
|
31
|
+
- spec/locales/en.yml
|
32
|
+
- spec/locales/fr.yml
|
33
|
+
- spec/seo_meta_builder/seo_meta_builder_spec.rb
|
34
|
+
- spec/spec_helper.rb
|
35
|
+
- CHANGELOG.rdoc
|
36
|
+
- MIT-LICENSE
|
37
|
+
- Rakefile
|
38
|
+
- README.rdoc
|
39
|
+
- init.rb
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://github.com/kwi/seo_meta_builder
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
segments:
|
61
|
+
- 1
|
62
|
+
- 3
|
63
|
+
- 4
|
64
|
+
version: 1.3.4
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project: seo_meta_builder
|
68
|
+
rubygems_version: 1.3.6
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Manage easily your meta titles and descriptions with Ruby On Rails
|
72
|
+
test_files: []
|
73
|
+
|