class Temple::HTML::Pretty

@api public

Public Class Methods

new(opts = {}) click to toggle source
Calls superclass method Temple::HTML::Fast::new
# File lib/temple/html/pretty.rb, line 14
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

Public Instance Methods

call(exp) click to toggle source
Calls superclass method
# File lib/temple/html/pretty.rb, line 22
def call(exp)
  @pretty ? [:multi, preamble, compile(exp)] : super
end
on_dynamic(code) click to toggle source
# File lib/temple/html/pretty.rb, line 36
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) click to toggle source
Calls superclass method Temple::HTML::Fast#on_html_comment
# File lib/temple/html/pretty.rb, line 47
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) click to toggle source
Calls superclass method Temple::HTML::Fast#on_html_doctype
# File lib/temple/html/pretty.rb, line 42
def on_html_doctype(type)
  return super unless @pretty
  [:multi, [:static, tag_indent('doctype')], super]
end
on_html_tag(name, attrs, content = nil) click to toggle source
Calls superclass method Temple::HTML::Fast#on_html_tag
# File lib/temple/html/pretty.rb, line 54
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) click to toggle source
# File lib/temple/html/pretty.rb, line 26
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

Protected Instance Methods

indent() click to toggle source
# File lib/temple/html/pretty.rb, line 86
def indent
  "\n" + (options[:indent] || '') * @indent
end
preamble() click to toggle source
# File lib/temple/html/pretty.rb, line 80
def preamble
  return [:multi] unless @pre_tags
  @pre_tags_name = unique_name
  [:code, "#{@pre_tags_name} = /#{@pre_tags.source}/"]
end
tag_indent(name) click to toggle source

Return indentation before tag

# File lib/temple/html/pretty.rb, line 91
def tag_indent(name)
  if @format == :xml
    flag = @indent_next != nil
    @indent_next = true
  else
    flag = @indent_next != nil && (@indent_next || options[:indent_tags].include?(name))
    @indent_next = options[:indent_tags].include?(name)
  end
  flag ? indent : ''
end