class HtmlBeautifier::Builder

Constants

DEFAULT_OPTIONS

Public Class Methods

new(output, options = {}) click to toggle source
# File lib/htmlbeautifier/builder.rb, line 13
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(e) click to toggle source
# File lib/htmlbeautifier/builder.rb, line 100
def close_block_element(e)
  close_element e
  new_line
end
close_element(e) click to toggle source
# File lib/htmlbeautifier/builder.rb, line 95
def close_element(e)
  outdent
  emit e
end
close_ie_cc(e) click to toggle source
# File lib/htmlbeautifier/builder.rb, line 115
def close_ie_cc(e)
  if @ie_cc_levels.empty?
    error "Unclosed conditional comment"
  else
    @level = @ie_cc_levels.pop
  end
  emit e
end
embed(opening, code, closing) click to toggle source
# File lib/htmlbeautifier/builder.rb, line 54
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 42
def emit(*strings)
  @output << "\n" if @new_line && !@empty
  @output << (@tab * @level) if @new_line
  @output << strings.join("")
  @new_line = false
  @empty = false
end
emit_reindented_block_content(code) click to toggle source
# File lib/htmlbeautifier/builder.rb, line 67
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 28
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 61
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 80
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 33
def indent
  @level += 1
end
new_line() click to toggle source
# File lib/htmlbeautifier/builder.rb, line 50
def new_line
  @new_line = true
end
new_lines(*content) click to toggle source
# File lib/htmlbeautifier/builder.rb, line 130
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(e) click to toggle source
# File lib/htmlbeautifier/builder.rb, line 110
def open_block_element(e)
  new_line
  open_element e
end
open_element(e) click to toggle source
# File lib/htmlbeautifier/builder.rb, line 105
def open_element(e)
  emit e
  indent
end
open_ie_cc(e) click to toggle source
# File lib/htmlbeautifier/builder.rb, line 124
def open_ie_cc(e)
  emit e
  @ie_cc_levels.push @level
  indent
end
outdent() click to toggle source
# File lib/htmlbeautifier/builder.rb, line 37
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 84
def preformatted_block(opening, content, closing)
  new_line
  emit opening, content, closing
  new_line
end
standalone_element(e) click to toggle source
# File lib/htmlbeautifier/builder.rb, line 90
def standalone_element(e)
  emit e
  new_line if e =~ %r{^<br[^\w]}
end
text(t) click to toggle source
# File lib/htmlbeautifier/builder.rb, line 137
def text(t)
  emit t
end