class DYI::Formatter::XmlFormatter

@since 0.0.0

Attributes

namespace[R]

@since 1.1.0

Public Class Methods

new(canvas, options={}) click to toggle source
# File lib/dyi/formatter/base.rb, line 176
def initialize(canvas, options={})
  @canvas = canvas
  @indent = options[:indent] || 0
  @level = options[:level] || 0
  @inline_mode = options[:inline_mode]
  namespace = options[:namespace].to_s
  @namespace = namespace.empty? ? nil : namespace
end

Public Instance Methods

declaration() click to toggle source
# File lib/dyi/formatter/base.rb, line 223
def declaration
  ''
end
generator_comment() click to toggle source
# File lib/dyi/formatter/base.rb, line 219
def generator_comment
  %Q{<!-- Create with DYI #{DYI::VERSION} (#{DYI::URL}) -->}
end
inline_mode=(boolean) click to toggle source

@since 1.1.0

# File lib/dyi/formatter/base.rb, line 191
def inline_mode=(boolean)
  @inline_mode = boolean ? true : false
end
inline_mode?() click to toggle source

@since 1.1.0

# File lib/dyi/formatter/base.rb, line 186
def inline_mode?
  @inline_mode ? true : false
end
puts(io=$>) click to toggle source
# File lib/dyi/formatter/base.rb, line 227
def puts(io=$>)
  if @canvas.root_element? && !inline_mode?
    puts_line(io) {io << xml_instruction}
    @canvas.stylesheets.each do |stylesheet|
      if stylesheet.include_external_file?
        puts_line(io) {io << stylesheet_instruction(stylesheet)}
      end
    end
    puts_line(io) {io << generator_comment}
    declaration.each_line do |dec|
      puts_line(io) {io << dec}
    end
  end
  @canvas.write_as(self, io)
end
stylesheet_instruction(stylesheet) click to toggle source

@since 1.0.0

# File lib/dyi/formatter/base.rb, line 201
def stylesheet_instruction(stylesheet)
  styles = []
  styles << '<?xml-stylesheet href="'
  styles << stylesheet.href
  styles << '" type="'
  styles << stylesheet.content_type
  if stylesheet.title
    styles << '" title="'
    styles << stylesheet.title
  end
  if stylesheet.media
    styles << '" media="'
    styles << stylesheet.media
  end
  styles << '"?>'
  styles.join
end
xml_instruction() click to toggle source

@since 1.0.0

# File lib/dyi/formatter/base.rb, line 196
def xml_instruction
  %Q{<?xml version="1.0" encoding="UTF-8"?>}
end

Private Instance Methods

create_cdata_node(io, tag_name, attributes={}) { || ... } click to toggle source

@since 1.0.0

# File lib/dyi/formatter/base.rb, line 294
def create_cdata_node(io, tag_name, attributes={}, &block)
  _tag_name = @namespace ? "#{namespace}:#{tag_name}" : tag_name
  puts_line(io) {
    io << '<' << _tag_name
    attributes.each do |key, value|
      io << ' ' << key << '="' << attr_escape(value) << '"'
    end
    io << '><![CDATA['
  }
  yield
  puts_line(io) {io << ']]></' << _tag_name << '>'}
end
create_leaf_node(io, tag_name, *attr) click to toggle source
# File lib/dyi/formatter/base.rb, line 264
def create_leaf_node(io, tag_name, *attr)
  _tag_name = @namespace ? "#{namespace}:#{tag_name}" : tag_name
  puts_line(io) {
    io << '<' << _tag_name
    if attr.first.kind_of?(Hash)
      attr.first.each do |key, value|
        io << ' ' << key << '="' << attr_escape(value) << '"'
      end
      io << '/>'
    elsif attr[1].kind_of?(Hash)
      attr[1].each do |key, value|
        io << ' ' << key << '="' << attr_escape(value) << '"'
      end
      io << '>' << escape(attr.first) << '</' << _tag_name << '>'
    elsif attr.first.nil?
      io << '/>'
    else
      io << '>' << escape(attr.first) << '</' << _tag_name << '>'
    end
  }
end
create_nested_nodes(io) { |io| ... } click to toggle source
# File lib/dyi/formatter/base.rb, line 286
def create_nested_nodes(io, &block)
  @level += 1
  yield io
ensure
  @level -= 1
end
create_node(io, tag_name, attributes={}, &block) click to toggle source
# File lib/dyi/formatter/base.rb, line 251
def create_node(io, tag_name, attributes={}, &block)
  _tag_name = @namespace ? "#{namespace}:#{tag_name}" : tag_name
  puts_line(io) {
    io << '<' << _tag_name
    attributes.each do |key, value|
      io << ' ' << key << '="' << attr_escape(value) << '"'
    end
    io << '>'
  }
  create_nested_nodes(io, &block) if block
  puts_line(io) {io << '</' << _tag_name << '>'}
end
puts_line(io) { |io| ... } click to toggle source
# File lib/dyi/formatter/base.rb, line 245
def puts_line(io, &block)
  io << (' ' * (@indent * @level)) if @indent != 0 && @level != 0
  yield io
  io << "\n" if @indent != 0
end