Class: Temple::HTML::Fast

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

Direct Known Subclasses

Pretty

Constant Summary

XHTML_DOCTYPES =
{
  '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">',
}.freeze
HTML4_DOCTYPES =
{
  '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">',
}.freeze

Constants included from Utils

Utils::ESCAPE_HTML

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, #unique_name

Constructor Details

- (Fast) initialize(opts = {})

Returns a new instance of Fast



27
28
29
30
31
32
33
34
# File 'lib/temple/html/fast.rb', line 27

def initialize(opts = {})
  super
  # html5 is now called html only
  options[:format] = :html5 if options[:format] == :html
  unless [:xhtml, :html4, :html5].include?(options[:format])
    raise "Invalid format #{options[:format].inspect}"
  end
end

Instance Method Details

- (Boolean) html?



40
41
42
# File 'lib/temple/html/fast.rb', line 40

def html?
  options[:format] == :html5 || options[:format] == :html4
end

- (Object) on_html_attr(name, value)



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

def on_html_attr(name, value)
  if empty_exp?(value)
    compile(value)
  elsif contains_static?(value)
    attribute(name, value)
  else
    tmp = unique_name
    [:multi,
     [:capture, tmp, compile(value)],
     [:if, "!#{tmp}.empty?",
      attribute(name, [:dynamic, tmp])]]
  end
end

- (Object) on_html_attrs(*attrs)



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/temple/html/fast.rb', line 85

def on_html_attrs(*attrs)
  result = {}
  attrs.each do |attr|
    raise(InvalidExpression, 'Attribute is not a html attr') if attr[0] != :html || attr[1] != :attr
    name, value = attr[2].to_s, attr[3]
    next if empty_exp?(value)
    if result[name]
      delimiter = options[:attr_delimiter][name]
      raise "Multiple #{name} attributes specified" unless delimiter
      if contains_static?(value)
        result[name] = [:html, :attr, name,
                        [:multi,
                         result[name][3],
                         [:static, delimiter],
                         value]]
      else
        tmp = unique_name
        result[name] = [:html, :attr, name,
                        [:multi,
                         result[name][3],
                         [:capture, tmp, value],
                         [:if, "!#{tmp}.empty?",
                          [:multi,
                           [:static, delimiter],
                           [:dynamic, tmp]]]]]
      end
    else
      result[name] = attr
    end
  end
  [:multi, *result.sort.map {|name,attr| compile(attr) }]
end

- (Object) on_html_comment(content)



68
69
70
71
72
73
# File 'lib/temple/html/fast.rb', line 68

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

- (Object) on_html_doctype(type)



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/temple/html/fast.rb', line 44

def on_html_doctype(type)
  type = type.to_s
  trailing_newlines = type[/(\A|[^\r])(\n+)\Z/, 2].to_s
  text = type.downcase.strip

  if text =~ /^xml/
    raise 'Invalid xml directive in html mode' if html?
    wrapper = options[:attr_wrapper]
    str = "<?xml version=#{wrapper}1.0#{wrapper} encoding=#{wrapper}#{text.split(' ')[1] || "utf-8"}#{wrapper} ?>"
  else
    case options[:format]
    when :html5
      str = '<!DOCTYPE html>'
    when :html4
      str = HTML4_DOCTYPES[text] || HTML4_DOCTYPES['transitional']
    when :xhtml
      str = XHTML_DOCTYPES[text] || XHTML_DOCTYPES['transitional']
    end
  end

  str << trailing_newlines
  [:static, str]
end

- (Object) on_html_tag(name, attrs, content = nil)



75
76
77
78
79
80
81
82
83
# File 'lib/temple/html/fast.rb', line 75

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

- (Boolean) xhtml?



36
37
38
# File 'lib/temple/html/fast.rb', line 36

def xhtml?
  options[:format] == :xhtml
end