Class: Temple::HTML::Pretty

Inherits:
Fast show all
Defined in:
lib/temple/html/pretty.rb

Constant Summary

Constants inherited from Fast

Fast::DOCTYPES, Fast::HTML_VOID_ELEMENTS

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 inherited from Fast

#on_html_attr, #on_html_attrs, #on_html_condcomment, #on_html_js

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

#compile

Methods included from Utils

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

Constructor Details

#initialize(opts = {}) ⇒ Pretty

Returns a new instance of Pretty.



15
16
17
18
19
20
21
# File 'lib/temple/html/pretty.rb', line 15

def initialize(opts = {})
  super
  @indent_next = nil
  @indent = 0
  @pretty = options[:pretty]
  @pre_tags = @format != :xml && Regexp.union(options[:pre_tags].map {|t| "<#{t}" })
end

Instance Method Details

#call(exp) ⇒ Object



23
24
25
# File 'lib/temple/html/pretty.rb', line 23

def call(exp)
  @pretty ? [:multi, preamble, compile(exp)] : super
end

#on_dynamic(code) ⇒ Object



37
38
39
40
41
# File 'lib/temple/html/pretty.rb', line 37

def on_dynamic(code)
  return [:dynamic, code] unless @pretty
  indent_next, @indent_next = @indent_next, false
  [:dynamic, "::Temple::Utils.indent_dynamic((#{code}), #{indent_next.inspect}, #{indent.inspect}#{@pre_tags ? ', ' + @pre_tags_name : ''})"]
end

#on_html_comment(content) ⇒ Object



48
49
50
51
52
53
# File 'lib/temple/html/pretty.rb', line 48

def on_html_comment(content)
  return super unless @pretty
  result = [:multi, [:static, tag_indent('comment')], super]
  @indent_next = false
  result
end

#on_html_doctype(type) ⇒ Object



43
44
45
46
# File 'lib/temple/html/pretty.rb', line 43

def on_html_doctype(type)
  return super unless @pretty
  [:multi, [:static, tag_indent('doctype')], super]
end

#on_html_tag(name, attrs, content = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/temple/html/pretty.rb', line 55

def on_html_tag(name, attrs, content = nil)
  return super unless @pretty

  name = name.to_s
  closed = !content || (empty_exp?(content) && options[:autoclose].include?(name))

  @pretty = false
  result = [:multi, [:static, "#{tag_indent(name)}<#{name}"], compile(attrs)]
  result << [:static, (closed && @format != :html ? ' /' : '') + '>']

  @pretty = !@pre_tags || !options[:pre_tags].include?(name)
  if content
    @indent += 1
    result << compile(content)
    @indent -= 1
  end
  unless closed
    indent = tag_indent(name)
    result << [:static, "#{content && !empty_exp?(content) ? indent : ''}</#{name}>"]
  end
  @pretty = true
  result
end

#on_static(content) ⇒ Object



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

def on_static(content)
  return [:static, content] unless @pretty
  unless @pre_tags && @pre_tags =~ content
    content = content.sub(/\A\s*\n?/, "\n".freeze) if @indent_next
    content = content.gsub("\n".freeze, indent)
  end
  @indent_next = false
  [:static, content]
end