MetaTags
Ruby
code posted
by
http://github.com/kpumuk/meta-tags/tree/master
created at 08 Jul 19:15
Edit
|
Back
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# 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 default meta tag values and display meta tags. # # This method should be used in layout file. # # Examples: # <head> # <%= display_meta_tags :site => 'My website' %> # </head> # # Allowed options: # * <tt>:site</tt> -- site title; # * <tt>:title</tt> -- page title; # * <tt>:description</tt> -- page description; # * <tt>:keywords</tt> -- page keywords; # * <tt>:prefix</tt> -- text between site name and separator; # * <tt>:separator</tt> -- text used to separate website name from page title; # * <tt>:suffix</tt> -- text between separator and page title; # * <tt>:lowercase</tt> -- when true, the page name will be lowercase; # * <tt>:reverse</tt> -- when true, the page and site names will be reversed. 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 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 = normalize_description(meta_tags[:description]) result << "\n" + tag(:meta, :name => :description, :content => description) unless description.blank? keywords = normalize_keywords(meta_tags[:keywords]) result << "\n" + tag(:meta, :name => :keywords, :content => keywords) unless keywords.blank? 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 |
4.68 KB in 9 ms with coderay