Class: Temple::HTML::Fast

Inherits:
Filter show all
Defined in:
lib/temple/html/fast.rb

Direct Known Subclasses

Pretty

Constant Summary collapse

DOCTYPES =
{
  xml: {
    '1.1'          => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
    '5'            => '<!DOCTYPE html>',
    'html'         => '<!DOCTYPE html>',
    'strict'       => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
    'frameset'     => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
    'mobile'       => '<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">',
    'basic'        => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">',
    'transitional' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
    'svg'          => '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'
  },
  html: {
    '5'            => '<!DOCTYPE html>',
    'html'         => '<!DOCTYPE html>',
    'strict'       => '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
    'frameset'     => '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">',
    'transitional' => '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'
  }
}
HTML_VOID_ELEMENTS =
%w[area base br col embed hr img input keygen link menuitem meta param source track wbr]

Constants included from Utils

Utils::ESCAPE_HTML, Utils::ESCAPE_HTML_PATTERN

Instance Attribute Summary

Attributes included from Mixins::Options

#options

Instance Method Summary collapse

Methods included from Mixins::Options

included

Methods included from Mixins::ControlFlowDispatcher

#on_block, #on_case, #on_cond, #on_if

Methods included from Mixins::EscapeDispatcher

#on_escape

Methods included from Mixins::CoreDispatcher

#on_capture, #on_multi

Methods included from Mixins::CompiledDispatcher

#call, #compile

Methods included from Utils

#empty_exp?, #escape_html, #escape_html_safe, #indent_dynamic, #unique_name

Constructor Details

#initialize(opts = {}) ⇒ Fast

Returns a new instance of Fast.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/temple/html/fast.rb', line 37

def initialize(opts = {})
  super
  @format = options[:format]
  unless [:xhtml, :html, :xml].include?(@format)
    if @format == :html4 || @format == :html5
      warn "Format #{@format.inspect} is deprecated, use :html"
      @format = :html
    else
      raise ArgumentError, "Invalid format #{@format.inspect}"
    end
  end
  wrapper = options[:js_wrapper]
  wrapper = @format == :xml || @format == :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

Instance Method Details

#on_html_attr(name, value) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/temple/html/fast.rb', line 108

def on_html_attr(name, value)
  if @format == :html && empty_exp?(value)
    [:static, " #{name}"]
  else
    [:multi,
     [:static, " #{name}=#{options[:attr_quote]}"],
     compile(value),
     [:static, options[:attr_quote]]]
  end
end

#on_html_attrs(*attrs) ⇒ Object



104
105
106
# File 'lib/temple/html/fast.rb', line 104

def on_html_attrs(*attrs)
  [:multi, *attrs.map {|attr| compile(attr) }]
end

#on_html_comment(content) ⇒ Object



80
81
82
83
84
85
# File 'lib/temple/html/fast.rb', line 80

def on_html_comment(content)
  [:multi,
    [:static, '<!--'],
    compile(content),
    [:static, '-->']]
end

#on_html_condcomment(condition, content) ⇒ Object



87
88
89
90
91
92
# File 'lib/temple/html/fast.rb', line 87

def on_html_condcomment(condition, content)
  on_html_comment [:multi,
                   [:static, "[#{condition}]>"],
                   content,
                   [:static, '<![endif]']]
end

#on_html_doctype(type) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/temple/html/fast.rb', line 66

def on_html_doctype(type)
  type = type.to_s.downcase

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

  [:static, str]
end

#on_html_js(content) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/temple/html/fast.rb', line 119

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) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/temple/html/fast.rb', line 94

def on_html_tag(name, attrs, content = nil)
  name = name.to_s
  closed = !content || (empty_exp?(content) && (@format == :xml || options[:autoclose].include?(name)))
  result = [:multi, [:static, "<#{name}"], compile(attrs)]
  result << [:static, (closed && @format != :html ? ' /' : '') + '>']
  result << compile(content) if content
  result << [:static, "</#{name}>"] if !closed
  result
end