Module: ManageMeta
- Defined in:
- lib/manage_meta/railtie.rb,
lib/manage_meta/version.rb,
lib/manage_meta/manage_meta.rb
Defined Under Namespace
Classes: Railtie
Constant Summary collapse
- VERSION =
'0.0.14'- LEGAL_HTML_VALUES =
{ html4: /^html\s*4(.01)?$/i, html5: /^html\s*5$/i, xhtml: /^xhtml\s*(1(\.0)?)?$/i }
Class Method Summary collapse
Instance Method Summary collapse
-
#add_meta(name, opt_value = nil, options = {}, &block) ⇒ Object
++ add_meta(name, value[, options]) - adds meta tag 'name' with value 'value' to meta tags to be displayed add_meta(name[, options] &block) - does same thing, except value is the return value of &block Note: if no both 'value' and 'block' are given, then the content of the meta tag is the concatenation of both values.
-
#add_meta_format(key, format) ⇒ Object
++ add_meta_format(key, format).
-
#del_meta(name) ⇒ Object
++ del_meta(name) - where name is a string or a symbol.
- #manage_meta_set_emit_encoding(bool) ⇒ Object
-
#manage_meta_set_encoding(encoding) ⇒ Object
++ manage_meta_set_encoding encoding - sets character encoding for page --.
-
#manage_meta_set_html_version(version = 'html5') ⇒ Object
++ manage_meta_set_html_version version.
-
#render_meta ⇒ Object
++ render_meta.
Class Method Details
.included(mod) ⇒ Object
7 8 9 10 11 12 |
# File 'lib/manage_meta/manage_meta.rb', line 7 def self.included(mod) begin mod.send(:helper_method, :render_meta) rescue Exception end end |
Instance Method Details
#add_meta(name, opt_value = nil, options = {}, &block) ⇒ Object
++ add_meta(name, value[, options]) - adds meta tag 'name' with value 'value' to meta tags to be displayed add_meta(name[, options] &block) - does same thing, except value is the return value of &block Note: if no both 'value' and 'block' are given, then the content of the meta tag is the concatenation of both values. options: :format => symbol - where 'symbol' is one of :named, :http_equiv, :canonical, :property, or a format added with 'add_meta_format' :no-capitalize => true or false - default is false all other options keys are ignored
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 |
# File 'lib/manage_meta/manage_meta.rb', line 61 def (name, opt_value = nil, = {}, &block) # make sure name is a string name = name # handle optional case when opt_value.is_a?(String) value = opt_value value += yield.to_s if block_given? when opt_value.is_a?(Hash) raise ArgumentError, "Value for meta tag #{name} missing" unless block_given? value = yield.to_s = opt_value when opt_value.nil? raise ArgumentError, "Value for meta tag #{name} missing" unless block_given? value = yield.to_s when opt_value.respond_to?(:to_s) value = opt_value.to_s value += yield.to_s if block_given? else raise ArgumentError, "add_meta(name, value[, options hash]) or add_meta(name[, option hash] do ... end)" end @manage_meta_meta_hash[name] = value name, # if format is explicitly called out or if name is not yet known unless @manage_meta_name_to_format.has_key?(name) @manage_meta_name_to_format[name] = :named end end |
#add_meta_format(key, format) ⇒ Object
++ add_meta_format(key, format)
adds the format format to @manage_meta_format_hash using the key key unless it is already defined
113 114 115 116 117 118 |
# File 'lib/manage_meta/manage_meta.rb', line 113 def (key, format) key = key @manage_meta_format_hash[key] = format unless @manage_meta_format_hash[key] end |
#del_meta(name) ⇒ Object
++ del_meta(name) - where name is a string or a symbol.
if name is in @manage_meta_meta_hash, then it will be deleted
100 101 102 103 104 105 |
# File 'lib/manage_meta/manage_meta.rb', line 100 def (name) name = name @manage_meta_meta_hash.delete name if @manage_meta_meta_hash.has_key? name end |
#manage_meta_set_emit_encoding(bool) ⇒ Object
148 149 150 |
# File 'lib/manage_meta/manage_meta.rb', line 148 def bool @@manage_meta_emit_encoding = !!bool end |
#manage_meta_set_encoding(encoding) ⇒ Object
++ manage_meta_set_encoding encoding - sets character encoding for page
156 157 158 |
# File 'lib/manage_meta/manage_meta.rb', line 156 def encoding @manage_meta_encoding = encoding end |
#manage_meta_set_html_version(version = 'html5') ⇒ Object
++ manage_meta_set_html_version version
--
165 166 167 168 169 170 171 172 173 |
# File 'lib/manage_meta/manage_meta.rb', line 165 def version = 'html5' version_downcase = version.to_s.downcase.strip LEGAL_HTML_VALUES.each do |html_key, regx| next unless version_downcase =~ regx @manage_meta_html_version = html_key return end raise ArgumentError.new("Illegal html version: #{version}") end |
#render_meta ⇒ Object
++ render_meta
returns a string consisting of all defined meta names in @manage_meta_meta_hash, formatted using their name-specific formats and indented with two spaces.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/manage_meta/manage_meta.rb', line 126 def leader = ' ' if @@manage_meta_emit_encoding if @manage_meta_html_version == :html5 # insert charset meta tag immediately after <head> leader += "<meta charset=\"#{@manage_meta_encoding}\">\n " else # covers both xhtml and html 4.01 # augment content_type meta tag with charset encoding and move to top of meta tags if (value = @manage_meta_meta_hash.delete(:content_type)) \ and value !~ /charset/i leader += "<meta http-equiv=\"Content-type\" content=\"text/html; charset=#{@manage_meta_encoding}\" />\n " end end end leader + @manage_meta_meta_hash.map do |name, content| @manage_meta_format_hash[@manage_meta_name_to_format[name]].sub('#{name}', (name)).sub('#{content}', content) end.join("\n ") + " \n" end |