Module: Sinatra::Sprockets::Helpers
- Defined in:
- lib/sinatra/sprockets/helpers.rb
Constant Summary collapse
- BOOLEAN_ATTRIBUTES =
%w(disabled readonly multiple checked autobuffer autoplay controls loop selected hidden scoped async defer reversed ismap seemless muted required autofocus novalidate formnovalidate open pubdate).to_set
Instance Method Summary collapse
- #asset_path(source, options = {}) ⇒ Object
- #audio_tag(source, options = {}) ⇒ Object
- #content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block) ⇒ Object
- #favicon_link_tag(source = 'favicon.ico', options = {}) ⇒ Object
- #image_tag(source, options = {}) ⇒ Object
- #javascript_include_tag(*sources) ⇒ Object
- #stylesheet_link_tag(*sources) ⇒ Object
- #tag(name, options = nil, open = false, escape = true) ⇒ Object
- #video_tag(sources, options = {}) ⇒ Object
Instance Method Details
#asset_path(source, options = {}) ⇒ Object
93 94 95 96 97 |
# File 'lib/sinatra/sprockets/helpers.rb', line 93 def asset_path(source, ={}) source = source.logical_path if source.respond_to?(:logical_path) path = asset_paths.compute_public_path(source, config.prefix, .merge(:body => true)) [:body] ? "#{path}?body=1" : path end |
#audio_tag(source, options = {}) ⇒ Object
49 50 51 52 53 |
# File 'lib/sinatra/sprockets/helpers.rb', line 49 def audio_tag(source, = {}) .symbolize_keys! [:src] = asset_path(source) tag("audio", ) end |
#content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block) ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/sinatra/sprockets/helpers.rb', line 103 def content_tag(name, = nil, = nil, escape = true, &block) if block_given? = if .is_a?(Hash) content_tag_string(name, block.call, , escape) else content_tag_string(name, , , escape) end end |
#favicon_link_tag(source = 'favicon.ico', options = {}) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/sinatra/sprockets/helpers.rb', line 10 def favicon_link_tag(source='favicon.ico', ={}) tag('link', { :rel => 'shortcut icon', :type => 'image/vnd.microsoft.icon', :href => asset_path(source) }.merge(.symbolize_keys)) end |
#image_tag(source, options = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/sinatra/sprockets/helpers.rb', line 18 def image_tag(source, = {}) .symbolize_keys! [:src] = asset_path(source) if size = .delete(:size) [:width], [:height] = size.split("x") if size =~ %r{^\d+x\d+$} end tag("img", ) end |
#javascript_include_tag(*sources) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/sinatra/sprockets/helpers.rb', line 55 def javascript_include_tag(*sources) = sources. debug = .key?(:debug) ? .delete(:debug) : config.debug_assets? body = .key?(:body) ? .delete(:body) : false digest = .key?(:digest) ? .delete(:digest) : config.digest_assets? sources.collect do |source| if debug && asset = asset_paths.asset_for(source, 'js') asset.to_a.map { |dep| src = asset_path(dep, :ext => 'js', :body => true, :digest => digest) content_tag("script", "", { "type" => "application/javascript", "src" => src }.merge!()) } else src = asset_path(source, :ext => 'js', :body => body, :digest => digest) content_tag("script", "", { "type" => "application/javascript", "src" => src }.merge!()) end end.join("\n").html_safe end |
#stylesheet_link_tag(*sources) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/sinatra/sprockets/helpers.rb', line 74 def stylesheet_link_tag(*sources) = sources. debug = .key?(:debug) ? .delete(:debug) : config.debug_assets? body = .key?(:body) ? .delete(:body) : false digest = .key?(:digest) ? .delete(:digest) : config.digest_assets? sources.collect do |source| if debug && asset = asset_paths.asset_for(source, 'css') asset.to_a.map { |dep| href = asset_path(dep, :ext => 'css', :body => true, :protocol => :request, :digest => digest) tag("link", { "rel" => "stylesheet", "type" => "text/css", "media" => "screen", "href" => href }.merge!()) } else href = asset_path(source, :ext => 'css', :body => body, :protocol => :request, :digest => digest) tag("link", { "rel" => "stylesheet", "type" => "text/css", "media" => "screen", "href" => href }.merge!()) end end.join("\n").html_safe end |
#tag(name, options = nil, open = false, escape = true) ⇒ Object
99 100 101 |
# File 'lib/sinatra/sprockets/helpers.rb', line 99 def tag(name, = nil, open = false, escape = true) "<#{name}#{(, escape) if }#{open ? ">" : " />"}".html_safe end |
#video_tag(sources, options = {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/sinatra/sprockets/helpers.rb', line 30 def video_tag(sources, = {}) .symbolize_keys! [:poster] = asset_path([:poster]) if [:poster] if size = .delete(:size) [:width], [:height] = size.split("x") if size =~ %r{^\d+x\d+$} end if sources.is_a?(Array) content_tag("video", ) do sources.map { |source| tag("source", :src => source) }.join.html_safe end else [:src] = asset_path(sources) tag("video", ) end end |