class HtmlBeautifier::Builder
Constants
- DEFAULT_OPTIONS
Public Class Methods
new(output, options = {})
click to toggle source
# File lib/htmlbeautifier/builder.rb, line 15 def initialize(output, options = {}) options = DEFAULT_OPTIONS.merge(options) @tab = options[:indent] @stop_on_errors = options[:stop_on_errors] @level = options[:initial_level] @keep_blank_lines = options[:keep_blank_lines] @new_line = false @empty = true @ie_cc_levels = [] @output = output @embedded_indenter = RubyIndenter.new end
Private Instance Methods
close_block_element(elem)
click to toggle source
# File lib/htmlbeautifier/builder.rb, line 104 def close_block_element(elem) close_element elem new_line end
close_element(elem)
click to toggle source
# File lib/htmlbeautifier/builder.rb, line 99 def close_element(elem) outdent emit elem end
close_ie_cc(elem)
click to toggle source
# File lib/htmlbeautifier/builder.rb, line 119 def close_ie_cc(elem) if @ie_cc_levels.empty? error "Unclosed conditional comment" else @level = @ie_cc_levels.pop end emit elem end
embed(opening, code, closing)
click to toggle source
# File lib/htmlbeautifier/builder.rb, line 58 def embed(opening, code, closing) lines = code.split(%r{\n}).map(&:strip) outdent if @embedded_indenter.outdent?(lines) emit opening, code, closing indent if @embedded_indenter.indent?(lines) end
emit(*strings)
click to toggle source
# File lib/htmlbeautifier/builder.rb, line 45 def emit(*strings) strings_join = strings.join("") @output << "\n" if @new_line && !@empty @output << (@tab * @level) if @new_line && !strings_join.strip.empty? @output << strings_join @new_line = false @empty = false end
Also aliased as: text
emit_reindented_block_content(code)
click to toggle source
# File lib/htmlbeautifier/builder.rb, line 71 def emit_reindented_block_content(code) lines = code.strip.split(%r{\n}) indentation = foreign_block_indentation(code) indent new_line lines.each do |line| emit line.rstrip.sub(%r{^#{indentation}}, "") new_line end outdent end
error(text)
click to toggle source
# File lib/htmlbeautifier/builder.rb, line 30 def error(text) return unless @stop_on_errors raise text end
foreign_block(opening, code, closing)
click to toggle source
# File lib/htmlbeautifier/builder.rb, line 65 def foreign_block(opening, code, closing) emit opening emit_reindented_block_content code unless code.strip.empty? emit closing end
foreign_block_indentation(code)
click to toggle source
# File lib/htmlbeautifier/builder.rb, line 84 def foreign_block_indentation(code) code.split(%r{\n}).find { |ln| !ln.strip.empty? }[%r{^\s+}] end
indent()
click to toggle source
# File lib/htmlbeautifier/builder.rb, line 36 def indent @level += 1 end
new_line()
click to toggle source
# File lib/htmlbeautifier/builder.rb, line 54 def new_line @new_line = true end
new_lines(*content)
click to toggle source
# File lib/htmlbeautifier/builder.rb, line 134 def new_lines(*content) blank_lines = content.first.scan(%r{\n}).count - 1 blank_lines = [blank_lines, @keep_blank_lines].min @output << ("\n" * blank_lines) new_line end
open_block_element(elem)
click to toggle source
# File lib/htmlbeautifier/builder.rb, line 114 def open_block_element(elem) new_line open_element elem end
open_element(elem)
click to toggle source
# File lib/htmlbeautifier/builder.rb, line 109 def open_element(elem) emit elem indent end
open_ie_cc(elem)
click to toggle source
# File lib/htmlbeautifier/builder.rb, line 128 def open_ie_cc(elem) emit elem @ie_cc_levels.push @level indent end
outdent()
click to toggle source
# File lib/htmlbeautifier/builder.rb, line 40 def outdent error "Extraneous closing tag" if @level == 0 @level = [@level - 1, 0].max end
preformatted_block(opening, content, closing)
click to toggle source
# File lib/htmlbeautifier/builder.rb, line 88 def preformatted_block(opening, content, closing) new_line emit opening, content, closing new_line end
standalone_element(elem)
click to toggle source
# File lib/htmlbeautifier/builder.rb, line 94 def standalone_element(elem) emit elem new_line if elem =~ %r{^<br[^\w]} end