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

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, options={})
  source = source.logical_path if source.respond_to?(:logical_path)
  path = asset_paths.compute_public_path(source, config.prefix, options.merge(:body => true))
  options[: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, options = {})
  options.symbolize_keys!
  options[:src] = asset_path(source)
  tag("audio", options)
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 (name, content_or_options_with_block = nil, options = nil, escape = true, &block)
  if block_given?
    options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
    (name, block.call, options, escape)
  else
    (name, content_or_options_with_block, options, escape)
  end
end


10
11
12
13
14
15
16
# File 'lib/sinatra/sprockets/helpers.rb', line 10

def favicon_link_tag(source='favicon.ico', options={})
  tag('link', {
    :rel  => 'shortcut icon',
    :type => 'image/vnd.microsoft.icon',
    :href => asset_path(source)
  }.merge(options.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, options = {})
  options.symbolize_keys!
  
  options[:src] = asset_path(source)
  
  if size = options.delete(:size)
    options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$}
  end
  
  tag("img", options)
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)
  options = sources.extract_options!
  debug = options.key?(:debug) ? options.delete(:debug) : config.debug_assets?
  body  = options.key?(:body)  ? options.delete(:body)  : false
  digest  = options.key?(:digest)  ? options.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)
        ("script", "", { "type" => "application/javascript", "src" => src }.merge!(options))
      }
    else
      src = asset_path(source, :ext => 'js', :body => body, :digest => digest)
      ("script", "", { "type" => "application/javascript", "src" => src }.merge!(options))
    end
  end.join("\n").html_safe
end


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)
  options = sources.extract_options!
  debug   = options.key?(:debug) ? options.delete(:debug) : config.debug_assets?
  body    = options.key?(:body)  ? options.delete(:body)  : false
  digest  = options.key?(:digest)  ? options.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!(options))
      }
    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!(options))
    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, options = nil, open = false, escape = true)
  "<#{name}#{tag_options(options, escape) if options}#{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, options = {})
  options.symbolize_keys!
  
  options[:poster] = asset_path(options[:poster]) if options[:poster]
  
  if size = options.delete(:size)
    options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$}
  end
  
  if sources.is_a?(Array)
    ("video", options) do
      sources.map { |source| tag("source", :src => source) }.join.html_safe
    end
  else
    options[:src] = asset_path(sources)
    tag("video", options)
  end
end