class Paggio::Formatter

Constants

OPTIONS

Public Class Methods

for(klass, &block) click to toggle source
# File lib/paggio/formatter.rb, line 20
def self.for(klass, &block)
  if block
    to_h[klass] = block
  else
    to_h[klass]
  end
end
new(io = nil, options = {}) click to toggle source
# File lib/paggio/formatter.rb, line 46
def initialize(io = nil, options = {})
  if Hash === io
    @io      = StringIO.new
    @options = io
  else
    @io      = io || StringIO.new
    @options = options
  end

  @options = OPTIONS.merge(@options)
end
options(options, &block) click to toggle source
# File lib/paggio/formatter.rb, line 28
def self.options(options, &block)
  old = OPTIONS.dup
  Utils.deep_merge!(OPTIONS, options)

  result = block.call

  OPTIONS.replace(old)

  result
end
to_h() click to toggle source
# File lib/paggio/formatter.rb, line 16
def self.to_h
  @formatters ||= {}
end

Public Instance Methods

deindent() click to toggle source
# File lib/paggio/formatter.rb, line 93
def deindent
  if indent?
    @options[:indent][:level] -= 1
  end
end
escape(string) click to toggle source
# File lib/paggio/formatter.rb, line 109
def escape(string)
  string.to_s.gsub(/["><']|&(?!([a-zA-Z]+|(#\d+));)/, {
    '&' => '&amp;',
    '>' => '&gt;',
    '<' => '&lt;',
    '"' => '&quot;',
    "'" => '&#39;' })
end
format(item) click to toggle source
# File lib/paggio/formatter.rb, line 58
def format(item)
  Formatter.to_h.each {|klass, block|
    if klass === item
      block.call(self, item)
      break
    end
  }

  self
end
indent(&block) click to toggle source
# File lib/paggio/formatter.rb, line 79
def indent(&block)
  if indent?
    if block
      @options[:indent][:level] += 1
      block.call
      @options[:indent][:level] -= 1
    else
      @options[:indent][:level] += 1
    end
  else
    block.call if block
  end
end
indent?(&block) click to toggle source
# File lib/paggio/formatter.rb, line 73
def indent?(&block)
  @options[:indent][:level]
rescue
  false
end
print(text) click to toggle source
to_s() click to toggle source
# File lib/paggio/formatter.rb, line 69
def to_s
  @io.string
end