Module: Padrino::Helpers::AssetTagHelpers
- Included in:
- MiniTest::Spec
- Defined in:
- lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/asset_tag_helpers.rb
Overview
Helpers related to producing assets (images,stylesheets,js,etc) within templates.
Constant Summary collapse
- FRAGMENT_HASH =
"#".html_safe.freeze
- APPEND_ASSET_EXTENSIONS =
assets that require an appended extension
["js", "css"]
- ABSOLUTE_URL_PATTERN =
absolute url regex
%r{^(https?://)}
Instance Method Summary collapse
-
#asset_path(kind, source) ⇒ String
Returns the path to the specified asset (css or javascript).
-
#favicon_tag(source, options = {}) ⇒ String
Generates a favicon link.
-
#feed_tag(mime, url, options = {}) ⇒ String
Creates a link tag that browsers and news readers can use to auto-detect an RSS or ATOM feed.
-
#flash_tag(*args) ⇒ String
Creates a div to display the flash of given type if it exists.
-
#image_path(src) ⇒ String
Returns the path to the image, either relative or absolute.
-
#image_tag(url, options = {}) ⇒ String
Creates an image element with given url and options.
-
#javascript_include_tag(*sources, options = {}) ⇒ String
Returns an html script tag for each of the sources provided.
-
#link_to(*args, &block) ⇒ String
Creates a link element with given name, url and options.
-
#mail_to(email, caption = nil, mail_options = {}) ⇒ String
Creates a mail link element with given name and caption.
-
#meta_tag(content, options = {}) ⇒ String
Creates a meta element with the content and given options.
-
#stylesheet_link_tag(*sources, options = {}) ⇒ String
Returns an html script tag for each of the sources provided.
Instance Method Details
#asset_path(kind, source) ⇒ String
Returns the path to the specified asset (css or javascript)
324 325 326 327 328 329 330 331 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/asset_tag_helpers.rb', line 324 def asset_path(kind, source) source = asset_normalize_extension(kind, URI.escape(source.to_s)) return source if source =~ ABSOLUTE_URL_PATTERN || source =~ /^\// # absolute source source = File.join(asset_folder_name(kind), source) = (source) result_path = uri_root_path(source) "#{result_path}#{}" end |
#favicon_tag(source, options = {}) ⇒ String
Generates a favicon link. looks inside images folder
214 215 216 217 218 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/asset_tag_helpers.rb', line 214 def favicon_tag(source, ={}) type = File.extname(source).gsub('.','') = .dup.reverse_merge!(:href => image_path(source), :rel => 'icon', :type => "image/#{type}") tag(:link, ) end |
#feed_tag(mime, url, options = {}) ⇒ String
Creates a link tag that browsers and news readers can use to auto-detect an RSS or ATOM feed.
@param options
The options for the feed tag.
139 140 141 142 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/asset_tag_helpers.rb', line 139 def feed_tag(mime, url, ={}) full_mime = (mime == :atom) ? 'application/atom+xml' : 'application/rss+xml' tag(:link, .reverse_merge(:rel => 'alternate', :type => full_mime, :title => mime, :href => url)) end |
#flash_tag(*args) ⇒ String
Creates a div to display the flash of given type if it exists
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/asset_tag_helpers.rb', line 30 def flash_tag(*args) = args. bootstrap = .delete(:bootstrap) if [:bootstrap] args.inject(''.html_safe) do |html,kind| flash_text = flash[kind] next html if flash_text.blank? flash_text << safe_content_tag(:button, "×", {:type => :button, :class => :close, :'data-dismiss' => :alert}) if bootstrap html << safe_content_tag(:div, flash_text, .reverse_merge(:class => kind)) end end |
#image_path(src) ⇒ String
Returns the path to the image, either relative or absolute. We search it in your appname.public_folder
like app/public/images for inclusion. You can provide also a full path.
299 300 301 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/asset_tag_helpers.rb', line 299 def image_path(src) asset_path(:images, src) end |
#image_tag(url, options = {}) ⇒ String
Creates an image element with given url and options
234 235 236 237 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/asset_tag_helpers.rb', line 234 def image_tag(url, ={}) .reverse_merge!(:src => image_path(url)) tag(:img, ) end |
#javascript_include_tag(*sources, options = {}) ⇒ String
Returns an html script tag for each of the sources provided. You can pass in the filename without extension or a symbol and we search it in your appname.public_folder
like app/public/javascript for inclusion. You can provide also a full path.
277 278 279 280 281 282 283 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/asset_tag_helpers.rb', line 277 def javascript_include_tag(*sources) = sources..symbolize_keys .reverse_merge!(:type => 'text/javascript') sources.flatten.map { |source| content_tag(:script, nil, .reverse_merge(:src => asset_path(:js, source))) }.join("\n").html_safe end |
#link_to(caption, url, options = {}) ⇒ String #link_to(url, options = {}, &block) ⇒ String
Creates a link element with given name, url and options
Note that you can pass :if
or :unless
conditions, but if you provide :current as condition padrino return true/false if the request.path_info match the given url
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 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/asset_tag_helpers.rb', line 80 def link_to(*args, &block) = args. fragment = .delete(:anchor).to_s if [:anchor] fragment = .delete(:fragment).to_s if [:fragment] url = ActiveSupport::SafeBuffer.new if block_given? if args[0] url.concat(args[0]) url.concat(FRAGMENT_HASH).concat(fragment) if fragment else url.concat(FRAGMENT_HASH) url.concat(fragment) if fragment end .reverse_merge!(:href => url) link_content = capture_html(&block) return '' unless parse_conditions(url, ) result_link = content_tag(:a, link_content, ) block_is_template?(block) ? concat_content(result_link) : result_link else if args[1] url.concat(args[1]) url.safe_concat(FRAGMENT_HASH).concat(fragment) if fragment else url = FRAGMENT_HASH url.concat(fragment) if fragment end name = args[0] return name unless parse_conditions(url, ) .reverse_merge!(:href => url) content_tag(:a, name, ) end end |
#mail_to(email, caption = nil, mail_options = {}) ⇒ String
Creates a mail link element with given name and caption.
167 168 169 170 171 172 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/asset_tag_helpers.rb', line 167 def mail_to(email, =nil, ={}) = .slice!(:cc, :bcc, :subject, :body) mail_query = Rack::Utils.build_query().gsub(/\+/, '%20').gsub('%40', '@').gsub('&', '&') mail_href = "mailto:#{email}"; mail_href << "?#{mail_query}" if mail_query.present? link_to(( || email), mail_href, ) end |
#meta_tag(content, options = {}) ⇒ String
Creates a meta element with the content and given options.
192 193 194 195 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/asset_tag_helpers.rb', line 192 def (content, ={}) .reverse_merge!("content" => content) tag(:meta, ) end |
#stylesheet_link_tag(*sources, options = {}) ⇒ String
Returns an html script tag for each of the sources provided. You can pass in the filename without extension or a symbol and we search it in your appname.public_folder
like app/public/stylesheets for inclusion. You can provide also a full path.
254 255 256 257 258 259 260 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/asset_tag_helpers.rb', line 254 def stylesheet_link_tag(*sources) = sources..symbolize_keys .reverse_merge!(:media => 'screen', :rel => 'stylesheet', :type => 'text/css') sources.flatten.map { |source| tag(:link, .reverse_merge(:href => asset_path(:css, source))) }.join("\n").html_safe end |