class HtmlFormatter::Builder

Constants

DEFAULT_OPTIONS

Public Class Methods

new(output, options = {}) click to toggle source
# File lib/htmlformatter/builder.rb, line 14
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

  if options[:engine] == "eex"
    @embedded_indenter = ElixirIndenter.new
  else
    @embedded_indenter = RubyIndenter.new
  end
end

Private Instance Methods

close_block_element(e) click to toggle source
# File lib/htmlformatter/builder.rb, line 106
def close_block_element(e)
  close_element e
  new_line
end
close_element(e) click to toggle source
# File lib/htmlformatter/builder.rb, line 101
def close_element(e)
  outdent
  emit e
end
close_ie_cc(e) click to toggle source
# File lib/htmlformatter/builder.rb, line 121
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/htmlformatter/builder.rb, line 60
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/htmlformatter/builder.rb, line 48
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/htmlformatter/builder.rb, line 73
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/htmlformatter/builder.rb, line 34
def error(text)
  return unless @stop_on_errors
  raise text
end
foreign_block(opening, code, closing) click to toggle source
# File lib/htmlformatter/builder.rb, line 67
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/htmlformatter/builder.rb, line 86
def foreign_block_indentation(code)
  code.split(%r{\n}).find { |ln| !ln.strip.empty? }[%r{^\s+}]
end
indent() click to toggle source
# File lib/htmlformatter/builder.rb, line 39
def indent
  @level += 1
end
new_line() click to toggle source
# File lib/htmlformatter/builder.rb, line 56
def new_line
  @new_line = true
end
new_lines(*content) click to toggle source
# File lib/htmlformatter/builder.rb, line 136
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/htmlformatter/builder.rb, line 116
def open_block_element(e)
  new_line
  open_element e
end
open_element(e) click to toggle source
# File lib/htmlformatter/builder.rb, line 111
def open_element(e)
  emit e
  indent
end
open_ie_cc(e) click to toggle source
# File lib/htmlformatter/builder.rb, line 130
def open_ie_cc(e)
  emit e
  @ie_cc_levels.push @level
  indent
end
outdent() click to toggle source
# File lib/htmlformatter/builder.rb, line 43
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/htmlformatter/builder.rb, line 90
def preformatted_block(opening, content, closing)
  new_line
  emit opening, content, closing
  new_line
end
standalone_element(e) click to toggle source
# File lib/htmlformatter/builder.rb, line 96
def standalone_element(e)
  emit e
  new_line if e =~ %r{^<br[^\w]}
end
text(t) click to toggle source
# File lib/htmlformatter/builder.rb, line 143
def text(t)
  emit t
end