From c52b497ff10a52b3ab4365ddfb233a37ce992f88 Mon Sep 17 00:00:00 2001 From: Dmytro Shteflyuk Date: Thu, 5 Nov 2009 13:00:29 +0200 Subject: [PATCH] Gemified plugin --- .gitignore | 3 +- Rakefile | 19 ++- VERSION.yml | 4 + init.rb | 4 - lib/meta_tags.rb | 199 +---------------------------- lib/meta_tags/controller_helper.rb | 43 +++++++ lib/meta_tags/view_helper.rb | 199 +++++++++++++++++++++++++++++ lib/meta_tags_controller_helper.rb | 41 ------ meta-tags.gemspec | 48 +++++++ spec/meta_tags_spec.rb | 4 +- spec/spec_helper.rb | 2 +- 11 files changed, 321 insertions(+), 245 deletions(-) create mode 100644 VERSION.yml create mode 100644 lib/meta_tags/controller_helper.rb create mode 100644 lib/meta_tags/view_helper.rb delete mode 100644 lib/meta_tags_controller_helper.rb create mode 100644 meta-tags.gemspec diff --git a/.gitignore b/.gitignore index fb862ae5..64426930 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ rdoc -doc \ No newline at end of file +doc +pkg \ No newline at end of file diff --git a/Rakefile b/Rakefile index 0b583e82..e6b99d19 100644 --- a/Rakefile +++ b/Rakefile @@ -2,10 +2,25 @@ require 'rake' require 'spec/rake/spectask' require 'rake/rdoctask' +begin + require 'jeweler' + Jeweler::Tasks.new do |gemspec| + gemspec.name = 'meta-tags' + gemspec.summary = 'Collection of SEO helpers for Ruby on Rails' + gemspec.description = 'Search Engine Optimization (SEO) plugin for Ruby on Rails applications.' + gemspec.email = 'kpumuk@kpumuk.info' + gemspec.homepage = 'http://github.com/kpumuk/meta-tags' + gemspec.authors = ['Dmytro Shteflyuk'] + end + Jeweler::GemcutterTasks.new +rescue LoadError + puts 'Jeweler not available. Install it with: sudo gem install jeweler' +end + desc 'Default: run unit tests.' task :default => :spec -desc 'Test the meta_tags plugin.' +desc 'Test the meta-tags plugin.' Spec::Rake::SpecTask.new(:spec) do |t| t.libs << 'lib' t.pattern = 'spec/**/*_spec.rb' @@ -13,7 +28,7 @@ Spec::Rake::SpecTask.new(:spec) do |t| t.spec_opts = ['-cfs'] end -desc 'Generate documentation for the meta_tags plugin.' +desc 'Generate documentation for the meta-tags plugin.' Rake::RDocTask.new(:rdoc) do |rdoc| rdoc.rdoc_dir = 'rdoc' rdoc.title = 'MetaTags' diff --git a/VERSION.yml b/VERSION.yml new file mode 100644 index 00000000..51fbf57d --- /dev/null +++ b/VERSION.yml @@ -0,0 +1,4 @@ +--- +:major: 1 +:minor: 1 +:patch: 0 diff --git a/init.rb b/init.rb index 9553d9bb..fc28e629 100644 --- a/init.rb +++ b/init.rb @@ -1,5 +1 @@ require File.dirname(__FILE__) + '/lib/meta_tags' -require File.dirname(__FILE__) + '/lib/meta_tags_controller_helper' - -ActionView::Base.send :include, MetaTags -ActionController::Base.send :include, MetaTagsControllerHelper \ No newline at end of file diff --git a/lib/meta_tags.rb b/lib/meta_tags.rb index f8e618fd..d8f3c020 100644 --- a/lib/meta_tags.rb +++ b/lib/meta_tags.rb @@ -1,197 +1,8 @@ -# Contains methods to use in views and helpers. module MetaTags - # Set meta tags for the page. - # - # Method could be used several times, and all options passed will - # be merged. If you will set the same property several times, last one - # will take precedence. - # - # Examples: - # set_meta_tags :title => 'Login Page', :description => 'Here you can login' - # set_meta_tags :keywords => 'authorization, login' - # - # Usually you will not call this method directly. Use +title+, +keywords+, - # +description+ for your daily tasks. - # - # See +display_meta_tags+ for allowed options. - def set_meta_tags(meta_tags = {}) - @meta_tags ||= {} - @meta_tags.merge!(meta_tags || {}) - end - - # Set the page title and return it back. - # - # This method is best suited for use in helpers. It sets the page title - # and returns it (or +headline+ if specified). - # - # Examples: - # <%= title 'Login Page' %> => title='Login Page', return='Login Page' - # <%= title 'Login Page', 'Please login' %> => title='Login Page', return='Please Login' - # - # You can specify +title+ as a string or array: - # title :title => ['part1', 'part2'] - # # part1 | part2 - def title(title, headline = '') - set_meta_tags(:title => title) - headline.blank? ? title : headline - end - - # Set the page keywords. - # - # Keywords can be passed as string of comma-separated values, or as an array: - # - # set_meta_tags :keywords => ['tag1', 'tag2'] - # # tag1, tag2 - # - # Examples: - # <% keywords 'keyword1, keyword2' %> - # <% keywords %w(keyword1 keyword2) %> - def keywords(keywords) - set_meta_tags(:keywords => keywords) - keywords - end - - # Set the page description. - # - # Description is a string (HTML will be stripped from output string). - # - # Examples: - # <% description 'This is login page' %> - def description(description) - set_meta_tags(:description => description) - description - end - - # Set the noindex meta tag - # - # You can specify noindex as a boolean or string - # - # Examples: - # <% noindex true %> - # <% noindex 'googlebot' %> - def noindex(noindex) - set_meta_tags(:noindex => noindex) - noindex - end - - # Set the nofollow meta tag - # - # You can specify nofollow as a boolean or string - # - # Examples: - # <% nofollow true %> - # <% nofollow 'googlebot' %> - def nofollow(nofollow) - set_meta_tags(:nofollow => nofollow) - nofollow - end - - # Set default meta tag values and display meta tags. - # - # This method should be used in layout file. - # - # Examples: - # - # <%= display_meta_tags :site => 'My website' %> - # - # - # Allowed options: - # * :site -- site title; - # * :title -- page title; - # * :description -- page description; - # * :keywords -- page keywords; - # * :prefix -- text between site name and separator; - # * :separator -- text used to separate website name from page title; - # * :suffix -- text between separator and page title; - # * :lowercase -- when true, the page name will be lowercase; - # * :reverse -- when true, the page and site names will be reversed; - # * :noindex -- add noindex meta tag; when true, 'robots' will be used, otherwise the string will be used; - # * :nofollow -- add nofollow meta tag; when true, 'robots' will be used, otherwise the string will be used. - def display_meta_tags(default = {}) - meta_tags = (default || {}).merge(@meta_tags || {}) - - # Prefix (leading space) - if meta_tags[:prefix] - prefix = meta_tags[:prefix] - elsif meta_tags[:prefix] === false - prefix = '' - else - prefix = ' ' - end - - # Separator - unless meta_tags[:separator].blank? - separator = meta_tags[:separator] - else - separator = '|' - end - - # Suffix (trailing space) - if meta_tags[:suffix] - suffix = meta_tags[:suffix] - elsif meta_tags[:suffix] === false - suffix = '' - else - suffix = ' ' - end - - # Title - title = meta_tags[:title] - if meta_tags[:lowercase] === true - title = title.downcase unless title.blank? - end - - # title - if title.blank? - result = content_tag :title, meta_tags[:site] - else - title = normalize_title(title) - title = [meta_tags[:site]] + title - title.reverse! if meta_tags[:reverse] === true - sep = prefix + separator + suffix - result = content_tag(:title, title.join(sep)) - end - - # description - description = normalize_description(meta_tags[:description]) - result << "\n" + tag(:meta, :name => :description, :content => description) unless description.blank? - - # keywords - keywords = normalize_keywords(meta_tags[:keywords]) - result << "\n" + tag(:meta, :name => :keywords, :content => keywords) unless keywords.blank? +end - # noindex & nofollow - noindex_name = meta_tags[:noindex].is_a?(String) ? meta_tags[:noindex] : 'robots' - nofollow_name = meta_tags[:nofollow].is_a?(String) ? meta_tags[:nofollow] : 'robots' - - if noindex_name == nofollow_name - content = [(meta_tags[:noindex] ? 'noindex' : nil), (meta_tags[:nofollow] ? 'nofollow' : nil)].compact.join(', ') - result << "\n" + tag(:meta, :name => noindex_name, :content => content) unless content.blank? - else - result << "\n" + tag(:meta, :name => noindex_name, :content => 'noindex') if meta_tags[:noindex] - result << "\n" + tag(:meta, :name => nofollow_name, :content => 'nofollow') if meta_tags[:nofollow] - end +require File.dirname(__FILE__) + '/meta_tags/view_helper' +require File.dirname(__FILE__) + '/meta_tags/controller_helper' - return result - end - - private - - def normalize_title(title) - if title.is_a? String - title = [title] - end - title.map { |t| h(strip_tags(t)) } - end - - def normalize_description(description) - return '' unless description - truncate(strip_tags(description).gsub(/\s+/, ' '), :length => 200) - end - - def normalize_keywords(keywords) - return '' unless keywords - keywords = keywords.flatten.join(', ') if keywords.is_a?(Array) - strip_tags(keywords).mb_chars.downcase - end -end +ActionView::Base.send :include, MetaTags::ViewHelper +ActionController::Base.send :include, MetaTags::ControllerHelper diff --git a/lib/meta_tags/controller_helper.rb b/lib/meta_tags/controller_helper.rb new file mode 100644 index 00000000..6a5bdfe1 --- /dev/null +++ b/lib/meta_tags/controller_helper.rb @@ -0,0 +1,43 @@ +# Contains methods to use in controllers. +# +# You can define several instance variables to set meta tags: +# @page_title = 'Member Login' +# @page_description = 'Member login page.' +# @page_keywords = 'Site, Login, Members' +# +# Also you can use +set_meta_tags+ method, that have the same parameters +# as MetaTags.set_meta_tags. +module MetaTags + module ControllerHelper + def self.included(base) + base.send :include, InstanceMethods + base.class_eval do + alias_method_chain :render, :meta_tags + end + end + + module InstanceMethods + protected + + # Processes the @page_title, @page_keywords, and + # @page_description instance variables and calls +render+. + def render_with_meta_tags(options = nil, extra_options = {}, &block) + meta_tags = {} + meta_tags[:title] = @page_title if @page_title + meta_tags[:keywords] = @page_keywords if @page_keywords + meta_tags[:description] = @page_description if @page_description + set_meta_tags(meta_tags) + + render_without_meta_tags(options, extra_options, &block) + end + + # Set meta tags for the page. + # + # See MetaTags.set_meta_tags for details. + def set_meta_tags(meta_tags) + @meta_tags ||= {} + @meta_tags.merge!(meta_tags || {}) + end + end + end +end \ No newline at end of file diff --git a/lib/meta_tags/view_helper.rb b/lib/meta_tags/view_helper.rb new file mode 100644 index 00000000..f82a1c40 --- /dev/null +++ b/lib/meta_tags/view_helper.rb @@ -0,0 +1,199 @@ +# Contains methods to use in views and helpers. +module MetaTags + module ViewHelper + # Set meta tags for the page. + # + # Method could be used several times, and all options passed will + # be merged. If you will set the same property several times, last one + # will take precedence. + # + # Examples: + # set_meta_tags :title => 'Login Page', :description => 'Here you can login' + # set_meta_tags :keywords => 'authorization, login' + # + # Usually you will not call this method directly. Use +title+, +keywords+, + # +description+ for your daily tasks. + # + # See +display_meta_tags+ for allowed options. + def set_meta_tags(meta_tags = {}) + @meta_tags ||= {} + @meta_tags.merge!(meta_tags || {}) + end + + # Set the page title and return it back. + # + # This method is best suited for use in helpers. It sets the page title + # and returns it (or +headline+ if specified). + # + # Examples: + # <%= title 'Login Page' %> => title='Login Page', return='Login Page' + # <%= title 'Login Page', 'Please login' %> => title='Login Page', return='Please Login' + # + # You can specify +title+ as a string or array: + # title :title => ['part1', 'part2'] + # # part1 | part2 + def title(title, headline = '') + set_meta_tags(:title => title) + headline.blank? ? title : headline + end + + # Set the page keywords. + # + # Keywords can be passed as string of comma-separated values, or as an array: + # + # set_meta_tags :keywords => ['tag1', 'tag2'] + # # tag1, tag2 + # + # Examples: + # <% keywords 'keyword1, keyword2' %> + # <% keywords %w(keyword1 keyword2) %> + def keywords(keywords) + set_meta_tags(:keywords => keywords) + keywords + end + + # Set the page description. + # + # Description is a string (HTML will be stripped from output string). + # + # Examples: + # <% description 'This is login page' %> + def description(description) + set_meta_tags(:description => description) + description + end + + # Set the noindex meta tag + # + # You can specify noindex as a boolean or string + # + # Examples: + # <% noindex true %> + # <% noindex 'googlebot' %> + def noindex(noindex) + set_meta_tags(:noindex => noindex) + noindex + end + + # Set the nofollow meta tag + # + # You can specify nofollow as a boolean or string + # + # Examples: + # <% nofollow true %> + # <% nofollow 'googlebot' %> + def nofollow(nofollow) + set_meta_tags(:nofollow => nofollow) + nofollow + end + + # Set default meta tag values and display meta tags. + # + # This method should be used in layout file. + # + # Examples: + # + # <%= display_meta_tags :site => 'My website' %> + # + # + # Allowed options: + # * :site -- site title; + # * :title -- page title; + # * :description -- page description; + # * :keywords -- page keywords; + # * :prefix -- text between site name and separator; + # * :separator -- text used to separate website name from page title; + # * :suffix -- text between separator and page title; + # * :lowercase -- when true, the page name will be lowercase; + # * :reverse -- when true, the page and site names will be reversed; + # * :noindex -- add noindex meta tag; when true, 'robots' will be used, otherwise the string will be used; + # * :nofollow -- add nofollow meta tag; when true, 'robots' will be used, otherwise the string will be used. + def display_meta_tags(default = {}) + meta_tags = (default || {}).merge(@meta_tags || {}) + + # Prefix (leading space) + if meta_tags[:prefix] + prefix = meta_tags[:prefix] + elsif meta_tags[:prefix] === false + prefix = '' + else + prefix = ' ' + end + + # Separator + unless meta_tags[:separator].blank? + separator = meta_tags[:separator] + else + separator = '|' + end + + # Suffix (trailing space) + if meta_tags[:suffix] + suffix = meta_tags[:suffix] + elsif meta_tags[:suffix] === false + suffix = '' + else + suffix = ' ' + end + + # Title + title = meta_tags[:title] + if meta_tags[:lowercase] === true + title = title.downcase unless title.blank? + end + + # title + if title.blank? + result = content_tag :title, meta_tags[:site] + else + title = normalize_title(title) + title = [meta_tags[:site]] + title + title.reverse! if meta_tags[:reverse] === true + sep = prefix + separator + suffix + result = content_tag(:title, title.join(sep)) + end + + # description + description = normalize_description(meta_tags[:description]) + result << "\n" + tag(:meta, :name => :description, :content => description) unless description.blank? + + # keywords + keywords = normalize_keywords(meta_tags[:keywords]) + result << "\n" + tag(:meta, :name => :keywords, :content => keywords) unless keywords.blank? + + # noindex & nofollow + noindex_name = meta_tags[:noindex].is_a?(String) ? meta_tags[:noindex] : 'robots' + nofollow_name = meta_tags[:nofollow].is_a?(String) ? meta_tags[:nofollow] : 'robots' + + if noindex_name == nofollow_name + content = [(meta_tags[:noindex] ? 'noindex' : nil), (meta_tags[:nofollow] ? 'nofollow' : nil)].compact.join(', ') + result << "\n" + tag(:meta, :name => noindex_name, :content => content) unless content.blank? + else + result << "\n" + tag(:meta, :name => noindex_name, :content => 'noindex') if meta_tags[:noindex] + result << "\n" + tag(:meta, :name => nofollow_name, :content => 'nofollow') if meta_tags[:nofollow] + end + + return result + end + + private + + def normalize_title(title) + if title.is_a? String + title = [title] + end + title.map { |t| h(strip_tags(t)) } + end + + def normalize_description(description) + return '' unless description + truncate(strip_tags(description).gsub(/\s+/, ' '), :length => 200) + end + + def normalize_keywords(keywords) + return '' unless keywords + keywords = keywords.flatten.join(', ') if keywords.is_a?(Array) + strip_tags(keywords).mb_chars.downcase + end + end +end diff --git a/lib/meta_tags_controller_helper.rb b/lib/meta_tags_controller_helper.rb deleted file mode 100644 index 3fe56dd1..00000000 --- a/lib/meta_tags_controller_helper.rb +++ /dev/null @@ -1,41 +0,0 @@ -# Contains methods to use in controllers. -# -# You can define several instance variables to set meta tags: -# @page_title = 'Member Login' -# @page_description = 'Member login page.' -# @page_keywords = 'Site, Login, Members' -# -# Also you can use +set_meta_tags+ method, that have the same parameters -# as MetaTags.set_meta_tags. -module MetaTagsControllerHelper - def self.included(base) - base.send :include, InstanceMethods - base.class_eval do - alias_method_chain :render, :meta_tags - end - end - - module InstanceMethods - protected - - # Processes the @page_title, @page_keywords, and - # @page_description instance variables and calls +render+. - def render_with_meta_tags(options = nil, extra_options = {}, &block) - meta_tags = {} - meta_tags[:title] = @page_title if @page_title - meta_tags[:keywords] = @page_keywords if @page_keywords - meta_tags[:description] = @page_description if @page_description - set_meta_tags(meta_tags) - - render_without_meta_tags(options, extra_options, &block) - end - - # Set meta tags for the page. - # - # See MetaTags.set_meta_tags for details. - def set_meta_tags(meta_tags) - @meta_tags ||= {} - @meta_tags.merge!(meta_tags || {}) - end - end -end \ No newline at end of file diff --git a/meta-tags.gemspec b/meta-tags.gemspec new file mode 100644 index 00000000..485e8134 --- /dev/null +++ b/meta-tags.gemspec @@ -0,0 +1,48 @@ +# Generated by jeweler +# DO NOT EDIT THIS FILE DIRECTLY +# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command +# -*- encoding: utf-8 -*- + +Gem::Specification.new do |s| + s.name = %q{meta-tags} + s.version = "1.1.0" + + s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= + s.authors = ["Dmytro Shteflyuk"] + s.date = %q{2009-11-05} + s.description = %q{Search Engine Optimization (SEO) plugin for Ruby on Rails applications.} + s.email = %q{kpumuk@kpumuk.info} + s.extra_rdoc_files = [ + "README.rdoc" + ] + s.files = [ + ".gitignore", + "MIT-LICENSE", + "README.rdoc", + "Rakefile", + "init.rb", + "lib/meta_tags.rb", + "spec/meta_tags_spec.rb", + "spec/spec_helper.rb" + ] + s.homepage = %q{http://github.com/kpumuk/meta-tags} + s.rdoc_options = ["--charset=UTF-8"] + s.require_paths = ["lib"] + s.rubygems_version = %q{1.3.5} + s.summary = %q{Collection of SEO helpers for Ruby on Rails} + s.test_files = [ + "spec/meta_tags_spec.rb", + "spec/spec_helper.rb" + ] + + if s.respond_to? :specification_version then + current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION + s.specification_version = 3 + + if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then + else + end + else + end +end + diff --git a/spec/meta_tags_spec.rb b/spec/meta_tags_spec.rb index 5e1b2c15..81530bc4 100644 --- a/spec/meta_tags_spec.rb +++ b/spec/meta_tags_spec.rb @@ -1,13 +1,13 @@ require File.dirname(__FILE__) + '/spec_helper' -describe MetaTags do +describe MetaTags::ViewHelper do before :each do @view = ActionView::Base.new end context 'module' do it 'should be mixed into ActionView::Base' do - ActionView::Base.included_modules.should include(MetaTags) + ActionView::Base.included_modules.should include(MetaTags::ViewHelper) end it 'should respond to "title" helper' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2ec32f72..3e1d17d0 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,5 @@ require 'rubygems' require 'action_controller' require 'action_view' -require File.join(File.dirname(__FILE__), "../lib/meta_tags") + require File.join(File.dirname(__FILE__), "../init")