class Erector::XMLWidget
Abstract base class for XML Widgets and HTMLWidget
. Declares “tags” which define methods that emit tags.
Public Class Methods
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
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 (<!– … –>) 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