Modularized Helpers, allows enable/disable of self-closing XHTML

tags (<link .../>) by
enable :xhtml
in your Application file
This commit is contained in:
Lennart Melzer 2009-11-22 18:22:56 +01:00
parent bd53f6ae34
commit 48c1086562

View File

@ -3,74 +3,81 @@ require 'sinatra/url_for'
module Sinatra module Sinatra
module StaticAssets module StaticAssets
# In HTML <link> and <img> tags have no end tag. module Helpers
# In XHTML, on the contrary, these tags must be properly closed. # In HTML <link> and <img> tags have no end tag.
# # In XHTML, on the contrary, these tags must be properly closed.
# We can choose the appropriate behaviour with +closed+ option: #
# # We can choose the appropriate behaviour with +closed+ option:
# image_tag "/images/foo.png", :alt => "Foo itself", :closed => true #
# # image_tag "/images/foo.png", :alt => "Foo itself", :closed => true
# The default value of +closed+ option is +false+. #
# # The default value of +closed+ option is +false+.
def image_tag(source, options = {}) #
options[:src] = url_for(source) def image_tag(source, options = {})
tag("img", options) options[:src] = url_for(source)
end tag("img", options)
def stylesheet_link_tag(*sources)
list, options = extract_options(sources)
list.collect { |source| stylesheet_tag(source, options) }.join("\n")
end
def javascript_script_tag(*sources)
list, options = extract_options(sources)
list.collect { |source| javascript_tag(source, options) }.join("\n")
end
def link_to(desc, url, options = {})
tag("a", options.merge(:href => url_for(url))) do
desc
end end
end
private def stylesheet_link_tag(*sources)
list, options = extract_options(sources)
def tag(name, options = {}) list.collect { |source| stylesheet_tag(source, options) }.join("\n")
start_tag = "<#{name}#{tag_options(options) if options}"
if block_given?
content = yield
"#{start_tag}>#{content}</#{name}>"
else
"#{start_tag}/>"
end end
end
def tag_options(options) def javascript_script_tag(*sources)
unless options.empty? list, options = extract_options(sources)
attrs = [] list.collect { |source| javascript_tag(source, options) }.join("\n")
attrs = options.map { |key, value| %(#{key}="#{Rack::Utils.escape_html(value)}") }
" #{attrs.sort * ' '}" unless attrs.empty?
end end
end
def stylesheet_tag(source, options = {}) def link_to(desc, url, options = {})
tag("link", { :type => "text/css", tag("a", options.merge(:href => url_for(url))) do
desc
end
end
private
def tag(name, local_options = {})
start_tag = "<#{name}#{tag_options(local_options) if local_options}"
if block_given?
content = yield
"#{start_tag}>#{content}</#{name}>"
else
"#{start_tag}#{"/" if options.xhtml}>"
end
end
def tag_options(options)
unless options.empty?
attrs = []
attrs = options.map { |key, value| %(#{key}="#{Rack::Utils.escape_html(value)}") }
" #{attrs.sort * ' '}" unless attrs.empty?
end
end
def stylesheet_tag(source, options = {})
tag("link", { :type => "text/css",
:charset => "utf-8", :media => "screen", :rel => "stylesheet", :charset => "utf-8", :media => "screen", :rel => "stylesheet",
:href => url_for(source) }.merge(options)) :href => url_for(source) }.merge(options))
end
def javascript_tag(source, options = {})
tag("script", { :type => "text/javascript", :charset => "utf-8",
:src => url_for(source) }.merge(options)) do
end end
def javascript_tag(source, options = {})
tag("script", { :type => "text/javascript", :charset => "utf-8",
:src => url_for(source) }.merge(options)) do
end
end
def extract_options(a)
opts = a.last.is_a?(::Hash) ? a.pop : {}
[a, opts]
end
end end
def extract_options(a) def self.registered(app)
opts = a.last.is_a?(::Hash) ? a.pop : {} app.helpers StaticAssets::Helpers
[a, opts] app.disable :xhtml
end end
end end
helpers StaticAssets register StaticAssets
end end