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 @last = nil @indent = 0 @pretty = options[:pretty] @pre_tags = Regexp.new(options[:pre_tags].map {|t| "<#{t}" }.join('|')) 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 37 def on_dynamic(code) if @pretty tmp = unique_name indent_code = '' indent_code << "#{tmp} = #{tmp}.sub(/\\A\\s*\\n?/, \"\\n\"); " if options[:indent_tags].include?(@last) indent_code << "#{tmp} = #{tmp}.gsub(\"\n\", #{indent.inspect}); " if ''.respond_to?(:html_safe) safe = unique_name # we have to first save if the string was html_safe # otherwise the gsub operation will lose that knowledge indent_code = "#{safe} = #{tmp}.html_safe?; #{indent_code}#{tmp} = #{tmp}.html_safe if #{safe}; " end @last = :dynamic [:multi, [:code, "#{tmp} = (#{code}).to_s"], [:code, "if #{@pre_tags_name} !~ #{tmp}; #{indent_code}end"], [:dynamic, tmp]] else [:dynamic, code] end 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 64 def on_html_comment(content) return super unless @pretty result = [:multi, [:static, tag_indent('comment')], super] @last = :comment 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 59 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 71 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 && xhtml? ? ' /' : '') + '>'] @pretty = !options[:pre_tags].include?(name) if content @indent += 1 result << compile(content) @indent -= 1 end result << [:static, "#{content && !empty_exp?(content) ? tag_indent(name) : ''}</#{name}>"] unless closed @pretty = true result end
on_static(content)
click to toggle source
# File lib/temple/html/pretty.rb, line 26 def on_static(content) if @pretty if @pre_tags !~ content content = content.sub(/\A\s*\n?/, "\n") if options[:indent_tags].include?(@last) content = content.gsub("\n", indent) end @last = :static end [:static, content] end
Protected Instance Methods
indent()
click to toggle source
# File lib/temple/html/pretty.rb, line 100 def indent "\n" + (options[:indent] || '') * @indent end
preamble()
click to toggle source
# File lib/temple/html/pretty.rb, line 95 def preamble @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 105 def tag_indent(name) result = @last && (options[:indent_tags].include?(@last) || options[:indent_tags].include?(name)) ? indent : '' @last = name result end