class Temple::HTML::Fast

@api public

Constants

HTML
HTML_DOCTYPES
XHTML_DOCTYPES

Public Class Methods

new(opts = {}) click to toggle source
Calls superclass method Temple::Mixins::Options.new
# File lib/temple/html/fast.rb, line 31
def initialize(opts = {})
  super
  unless [:xhtml, *HTML].include?(options[:format])
    raise ArgumentError, "Invalid format #{options[:format].inspect}"
  end
  wrapper = options[:js_wrapper]
  wrapper = xhtml? ? :cdata : :comment if wrapper == :guess
  @js_wrapper =
    case wrapper
    when :comment
      [ "<!--\n", "\n//-->" ]
    when :cdata
      [ "\n//<![CDATA[\n", "\n//]]>\n" ]
    when :both
      [ "<!--\n//<![CDATA[\n", "\n//]]>\n//-->" ]
    when nil
    when Array
      wrapper
    else
      raise ArgumentError, "Invalid JavaScript wrapper #{wrapper.inspect}"
    end
end

Public Instance Methods

html?() click to toggle source
# File lib/temple/html/fast.rb, line 58
def html?
  HTML.include?(options[:format])
end
on_html_attr(name, value) click to toggle source
# File lib/temple/html/fast.rb, line 106
def on_html_attr(name, value)
  [:multi,
   [:static, " #{name}=#{options[:attr_quote]}"],
   compile(value),
   [:static, options[:attr_quote]]]
end
on_html_attrs(*attrs) click to toggle source
# File lib/temple/html/fast.rb, line 102
def on_html_attrs(*attrs)
  [:multi, *attrs.map {|attr| compile(attr) }]
end
on_html_comment(content) click to toggle source
# File lib/temple/html/fast.rb, line 78
def on_html_comment(content)
  [:multi,
    [:static, '<!--'],
    compile(content),
    [:static, '-->']]
end
on_html_condcomment(condition, content) click to toggle source
# File lib/temple/html/fast.rb, line 85
def on_html_condcomment(condition, content)
  on_html_comment [:multi,
                   [:static, "[#{condition}]>"],
                   content,
                   [:static, '<![endif]']]
end
on_html_doctype(type) click to toggle source
# File lib/temple/html/fast.rb, line 62
def on_html_doctype(type)
  type = type.to_s.downcase

  if type =~ /^xml(\s+(.+))?$/
    raise(FilterError, 'Invalid xml directive in html mode') if html?
    w = options[:attr_quote]
    str = "<?xml version=#{w}1.0#{w} encoding=#{w}#{$2 || 'utf-8'}#{w} ?>"
  elsif html?
    str = HTML_DOCTYPES[type] || raise(FilterError, "Invalid html doctype #{type}")
  else
    str = XHTML_DOCTYPES[type] || raise(FilterError, "Invalid xhtml doctype #{type}")
  end

  [:static, str]
end
on_html_js(content) click to toggle source
# File lib/temple/html/fast.rb, line 113
def on_html_js(content)
  if @js_wrapper
    [:multi,
     [:static, @js_wrapper.first],
     compile(content),
     [:static, @js_wrapper.last]]
  else
    compile(content)
  end
end
on_html_tag(name, attrs, content = nil) click to toggle source
# File lib/temple/html/fast.rb, line 92
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
xhtml?() click to toggle source
# File lib/temple/html/fast.rb, line 54
def xhtml?
  options[:format] == :xhtml
end