class Temple::HTML::Pretty
def call(exp)
def call(exp) @pretty ? [:multi, preamble, compile(exp)] : super end
def indent
def indent "\n" + (options[:indent] || '') * @indent end
def initialize(opts = {})
def initialize(opts = {}) super @last = :noindent @indent = 0 @pretty = options[:pretty] @pre_tags = Regexp.new(options[:pre_tags].map {|t| "<#{t}" }.join('|')) end
def on_dynamic(content)
def on_dynamic(content) @last = :noindent [:dynamic, @pretty ? "Temple::Utils.indent((#{content}), #{indent.inspect}, _temple_pre_tags)" : content] end
def on_html_comment(content)
def on_html_comment(content) return super unless @pretty @last = nil [:multi, [:static, indent], super] end
def on_html_doctype(type)
def on_html_doctype(type) @last = nil super end
def on_html_tag(name, attrs, closed, content)
def on_html_tag(name, attrs, closed, content) return super unless @pretty closed ||= options[:autoclose].include?(name) raise "Closed tag #{name} has content" if closed && !empty_exp?(content) @pretty = false result = [:multi, [:static, "#{tag_indent(name)}<#{name}"], compile(attrs)] result << [:static, ' /'] if closed && xhtml? result << [:static, '>'] @pretty = !options[:pre_tags].include?(name) @indent += 1 result << compile(content) @indent -= 1 result << [:static, "#{tag_indent(name)}</#{name}>"] if !closed @pretty = true result end
def on_static(content)
def on_static(content) if @pretty content = Utils.indent(content, indent, @pre_tags) @last = content.sub!(/\r?\n\s*$/, ' ') ? nil : :noindent end [:static, content] end
def preamble
def preamble [:block, "_temple_pre_tags = /#{@pre_tags.source}/"] end
def tag_indent(name)
def tag_indent(name) result = @last != :noindent && (options[:indent_tags].include?(@last) || options[:indent_tags].include?(name)) ? indent : '' @last = name result end