module Temple
module HTML
# @api public
class Fast < Filter
XHTML_DOCTYPES = {
'1.1' => '',
'5' => '',
'html' => '',
'strict' => '',
'frameset' => '',
'mobile' => '',
'basic' => '',
'transitional' => '',
}.freeze
HTML_DOCTYPES = {
'5' => '',
'html' => '',
'strict' => '',
'frameset' => '',
'transitional' => '',
}.freeze
set_default_options :format => :xhtml,
:attr_wrapper => "'",
:autoclose => %w[meta img link br hr input area param col base]
HTML = [:html, :html4, :html5]
def initialize(opts = {})
super
unless [:xhtml, *HTML].include?(options[:format])
raise "Invalid format #{options[:format].inspect}"
end
end
def xhtml?
options[:format] == :xhtml
end
def html?
HTML.include?(options[:format])
end
def on_html_doctype(type)
type = type.to_s.downcase
if type =~ /^xml(\s+(.+))?$/
raise 'Invalid xml directive in html mode' if html?
w = options[:attr_wrapper]
str = ""
elsif html?
str = HTML_DOCTYPES[type] || raise("Invalid html doctype #{type}")
else
str = XHTML_DOCTYPES[type] || raise("Invalid xhtml doctype #{type}")
end
[:static, str]
end
def on_html_comment(content)
[:multi,
[:static, '']]
end
def on_html_tag(name, attrs, content = nil)
name = name.to_s
closed = !content || (empty_exp?(content) && options[:autoclose].include?(name))
result = [:multi, [:static, "<#{name}"], compile(attrs)]
result << [:static, (closed && xhtml? ? ' /' : '') + '>']
result << compile(content) if content
result << [:static, "#{name}>"] if !closed
result
end
def on_html_attrs(*attrs)
[:multi, *attrs.map {|attr| compile(attr) }]
end
def on_html_attr(name, value)
[:multi,
[:static, " #{name}=#{options[:attr_wrapper]}"],
compile(value),
[:static, options[:attr_wrapper]]]
end
end
end
end