module JavascriptTags
include Radiant::Taggable
class TagError < StandardError; end
desc %{
Renders the content from or a reference to the javascript specified in the @slug@
attribute. Additionally, the @as@ attribute can be used to make the tag render
as one of the following:
* with no @as@ value the javascript's content is rendered by default.
* @inline@ - wraps the javascript's content in an (X)HTML @
}
tag 'javascript' do |tag|
slug = (tag.attr['slug'] || tag.attr['name'])
raise TagError.new("`javascript' tag must contain a `slug' attribute.") unless slug
if (javascript = JavascriptPage.find_by_slug(slug))
mime_type = tag.attr['type'] || javascript.headers['Content-Type']
path = javascript.path
optional_attributes = tag.attr.except('slug', 'name', 'as', 'type').inject('') { |s, (k, v)| s << %{#{k}="#{v}" } }.strip
optional_attributes = " #{optional_attributes}" unless optional_attributes.empty?
case tag.attr['as']
when 'url','path'
path
when 'inline'
%{}
when 'link'
%{}
else
javascript.render_part('body')
end
else
raise TagError.new("javascript #{slug} not found")
end
end
end