class Erector::XmlWidget

Abstract base class for XML Widgets and HTMLWidget. Declares “tags” which define methods that emit tags.

Public Class Methods

add_tags(tags) click to toggle source
# File lib/erector/xml_widget.rb, line 21
def self.add_tags(tags)
  @tags ||= {}
  @tags = @tags.merge(tags)
end
full_tags() click to toggle source

Tags which can contain other stuff

# File lib/erector/xml_widget.rb, line 61
def self.full_tags
  @tags.values.select{|tag| !tag.self_closing?}.map{|tag| tag.name}
end
inherited(subclass) click to toggle source
Calls superclass method
# File lib/erector/xml_widget.rb, line 12
def self.inherited(subclass)
  super
  subclass.add_tags(@tags) if @tags
end
newliney?(tag_name) click to toggle source
# File lib/erector/xml_widget.rb, line 65
def self.newliney?(tag_name)
  if (tag = self.tag_named(tag_name))
    tag.newliney?
  else
    true
  end
end
self_closing_tags() click to toggle source

Tags which are always self-closing

# File lib/erector/xml_widget.rb, line 56
def self.self_closing_tags
  @tags.values.select{|tag| tag.self_closing?}.map{|tag| tag.name}
end
tag(*args) click to toggle source
# File lib/erector/xml_widget.rb, line 26
    def self.tag(*args)
      tag = Tag.new(*args)
      @tags ||= {}
      @tags[tag.name] = tag

      if instance_methods.include?(tag.method_name.to_sym)
        warn "method '#{tag.method_name}' is already defined; skipping #{caller[1]}"
        return
      end

      if tag.self_closing?
        self.class_eval(<<-SRC, __FILE__, __LINE__ + 1)
          def #{tag.method_name}(*args, &block)
            _empty_element('#{tag.name}', *args, &block)
          end
        SRC
      else
        self.class_eval(<<-SRC, __FILE__, __LINE__ + 1)
        def #{tag.method_name}(*args, &block)
              _element('#{tag.name}', *args, &block)
          end

          def #{tag.method_name}!(*args, &block)
            _element('#{tag.name}', *(args.map{|a|raw(a)}), &block)
          end
        SRC
      end
    end
tag_named(tag_name) click to toggle source
# File lib/erector/xml_widget.rb, line 17
def self.tag_named(tag_name)
  @tags && @tags[tag_name]
end

Public Instance Methods

comment(text = '') { || ... } click to toggle source

Emits an XML/HTML comment (&lt;!– … –&gt;) surrounding text and/or the output of block. see www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.4

If text is an Internet Explorer conditional comment condition such as “[if IE]”, the output includes the opening condition and closing “[endif]”. See www.quirksmode.org/css/condcom.html

Since “Authors should avoid putting two or more adjacent hyphens inside comments,” we emit a warning if you do that.

# File lib/erector/xml_widget.rb, line 88
def comment(text = '')
  puts "Warning: Authors should avoid putting two or more adjacent hyphens inside comments." if text =~ /--/

  conditional = text =~ /\[if .*\]/

  rawtext "<!--"
  rawtext text
  rawtext ">" if conditional

  if block_given?
    rawtext "\n"
    yield
    rawtext "\n"
  end

  rawtext "<![endif]" if conditional
  rawtext "-->\n"
end
instruct(attributes={:version => "1.0", :encoding => "UTF-8"}) click to toggle source

Emits an XML instruction, which looks like this: <?xml version="1.0" encoding="UTF-8" ?>

# File lib/erector/xml_widget.rb, line 74
def instruct(attributes={:version => "1.0", :encoding => "UTF-8"})
  output << raw("<?xml#{format_attributes(sort_for_xml_declaration(attributes))}?>")
end

Protected Instance Methods

sort_for_xml_declaration(attributes) click to toggle source
# File lib/erector/xml_widget.rb, line 111
def sort_for_xml_declaration(attributes)
  # correct order is "version, encoding, standalone" (XML 1.0 section 2.8).
  # But we only try to put version before encoding for now.
  stringized = []
  attributes.each do |key, value|
    stringized << [key.to_s, value]
  end
  stringized.sort{|a, b| b <=> a}
end