Basic docs for everything under Oga::XML.
This commit is contained in:
parent
13a9228563
commit
54ef125637
|
@ -1,9 +1,14 @@
|
||||||
module Oga
|
module Oga
|
||||||
module XML
|
module XML
|
||||||
##
|
##
|
||||||
#
|
# Class used for storing information about CDATA tags.
|
||||||
#
|
#
|
||||||
class Cdata < Text
|
class Cdata < Text
|
||||||
|
##
|
||||||
|
# Converts the node back to XML.
|
||||||
|
#
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
def to_xml
|
def to_xml
|
||||||
return "<![CDATA[#{text}]]>"
|
return "<![CDATA[#{text}]]>"
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,8 +1,14 @@
|
||||||
module Oga
|
module Oga
|
||||||
module XML
|
module XML
|
||||||
##
|
##
|
||||||
|
# Class used for storing information about XML comments.
|
||||||
#
|
#
|
||||||
class Comment < Text
|
class Comment < Text
|
||||||
|
##
|
||||||
|
# Converts the node back to XML.
|
||||||
|
#
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
def to_xml
|
def to_xml
|
||||||
return "<!--#{text}-->"
|
return "<!--#{text}-->"
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,20 +1,49 @@
|
||||||
module Oga
|
module Oga
|
||||||
module XML
|
module XML
|
||||||
##
|
##
|
||||||
# Class description
|
# Class used for storing information about Doctypes.
|
||||||
|
#
|
||||||
|
# @!attribute [rw] name
|
||||||
|
# The name of the doctype (e.g. "HTML").
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
|
# @!attribute [rw] type
|
||||||
|
# The type of the doctype (e.g. "PUBLIC").
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
|
# @!attribute [rw] public_id
|
||||||
|
# The public ID of the doctype.
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
|
# @!attribute [rw] system_id
|
||||||
|
# The system ID of the doctype.
|
||||||
|
# @return [String]
|
||||||
#
|
#
|
||||||
class Doctype
|
class Doctype
|
||||||
attr_accessor :name, :type, :public_id, :system_id
|
attr_accessor :name, :type, :public_id, :system_id
|
||||||
|
|
||||||
##
|
##
|
||||||
|
# @example
|
||||||
|
# dtd = Doctype.new(:name => 'html', :type => 'PUBLIC')
|
||||||
|
#
|
||||||
# @param [Hash] options
|
# @param [Hash] options
|
||||||
#
|
#
|
||||||
|
# @option options [String] :name
|
||||||
|
# @option options [String] :type
|
||||||
|
# @option options [String] :public_id
|
||||||
|
# @option options [String] :system_id
|
||||||
|
#
|
||||||
def initialize(options = {})
|
def initialize(options = {})
|
||||||
options.each do |key, value|
|
options.each do |key, value|
|
||||||
instance_variable_set("@#{key}", value) if respond_to?(key)
|
instance_variable_set("@#{key}", value) if respond_to?(key)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Converts the doctype back to XML.
|
||||||
|
#
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
def to_xml
|
def to_xml
|
||||||
segments = "<!DOCTYPE #{name}"
|
segments = "<!DOCTYPE #{name}"
|
||||||
|
|
||||||
|
@ -25,6 +54,12 @@ module Oga
|
||||||
return segments + '>'
|
return segments + '>'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Inspects the doctype.
|
||||||
|
#
|
||||||
|
# @param [Fixnum] indent The indentation level for each line.
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
def inspect(indent = 0)
|
def inspect(indent = 0)
|
||||||
class_name = self.class.to_s.split('::').last
|
class_name = self.class.to_s.split('::').last
|
||||||
spacing = ' ' * indent
|
spacing = ' ' * indent
|
||||||
|
|
|
@ -1,7 +1,20 @@
|
||||||
module Oga
|
module Oga
|
||||||
module XML
|
module XML
|
||||||
##
|
##
|
||||||
# Class description
|
# Class used for storing information about an entire XML document. This
|
||||||
|
# includes the doctype, XML declaration, child nodes and more.
|
||||||
|
#
|
||||||
|
# @!attribute [rw] children
|
||||||
|
# The child nodes of the document.
|
||||||
|
# @return [Array]
|
||||||
|
#
|
||||||
|
# @!attribute [rw] doctype
|
||||||
|
# The doctype of the document.
|
||||||
|
# @return [Oga::XML::Doctype]
|
||||||
|
#
|
||||||
|
# @!attribute [rw] xml_declaration
|
||||||
|
# The XML declaration of the document.
|
||||||
|
# @return [Oga::XML::XmlDeclaration]
|
||||||
#
|
#
|
||||||
class Document
|
class Document
|
||||||
attr_accessor :children, :doctype, :xml_declaration
|
attr_accessor :children, :doctype, :xml_declaration
|
||||||
|
@ -9,6 +22,10 @@ module Oga
|
||||||
##
|
##
|
||||||
# @param [Hash] options
|
# @param [Hash] options
|
||||||
#
|
#
|
||||||
|
# @option options [Array] :children
|
||||||
|
# @option options [Oga::XML::Doctype] :doctype
|
||||||
|
# @option options [Oga::XML::XmlDeclaration] :xml_declaration
|
||||||
|
#
|
||||||
def initialize(options = {})
|
def initialize(options = {})
|
||||||
options.each do |key, value|
|
options.each do |key, value|
|
||||||
instance_variable_set("@#{key}", value) if respond_to?(key)
|
instance_variable_set("@#{key}", value) if respond_to?(key)
|
||||||
|
@ -17,6 +34,11 @@ module Oga
|
||||||
@children ||= []
|
@children ||= []
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Converts the document and its child nodes to XML.
|
||||||
|
#
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
def to_xml
|
def to_xml
|
||||||
xml = children.map(&:to_xml).join('')
|
xml = children.map(&:to_xml).join('')
|
||||||
|
|
||||||
|
@ -31,6 +53,12 @@ module Oga
|
||||||
return xml
|
return xml
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Inspects the document and its child nodes. Child nodes are indented for
|
||||||
|
# each nesting level.
|
||||||
|
#
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
def inspect
|
def inspect
|
||||||
class_name = self.class.to_s.split('::').last
|
class_name = self.class.to_s.split('::').last
|
||||||
child_lines = children.map { |child| child.inspect(4) }.join("\n")
|
child_lines = children.map { |child| child.inspect(4) }.join("\n")
|
||||||
|
|
|
@ -4,6 +4,18 @@ module Oga
|
||||||
# Class that contains information about an XML element such as the name,
|
# Class that contains information about an XML element such as the name,
|
||||||
# attributes and child nodes.
|
# attributes and child nodes.
|
||||||
#
|
#
|
||||||
|
# @!attribute [rw] name
|
||||||
|
# The name of the element.
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
|
# @!attribute [rw] namespace
|
||||||
|
# The namespace of the element, if any.
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
|
# @!attribute [rw] attributes
|
||||||
|
# The attributes of the element.
|
||||||
|
# @return [Hash]
|
||||||
|
#
|
||||||
class Element < Node
|
class Element < Node
|
||||||
attr_accessor :name, :namespace, :attributes
|
attr_accessor :name, :namespace, :attributes
|
||||||
|
|
||||||
|
@ -11,12 +23,23 @@ module Oga
|
||||||
@attributes ||= {}
|
@attributes ||= {}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Returns the value of the specified attribute.
|
||||||
|
#
|
||||||
|
# @param [String] name
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
def attribute(name)
|
def attribute(name)
|
||||||
return attributes[name]
|
return attributes[name]
|
||||||
end
|
end
|
||||||
|
|
||||||
alias_method :attr, :attribute
|
alias_method :attr, :attribute
|
||||||
|
|
||||||
|
##
|
||||||
|
# Converts the element and its child elements to XML.
|
||||||
|
#
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
def to_xml
|
def to_xml
|
||||||
ns = namespace ? "#{namespace}:" : ''
|
ns = namespace ? "#{namespace}:" : ''
|
||||||
body = children.map(&:to_xml).join('')
|
body = children.map(&:to_xml).join('')
|
||||||
|
@ -31,6 +54,12 @@ module Oga
|
||||||
return "<#{ns}#{name}#{attrs}>#{body}</#{name}>"
|
return "<#{ns}#{name}#{attrs}>#{body}</#{name}>"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Returns extra data to use when calling {#inspect} on an element.
|
||||||
|
#
|
||||||
|
# @param [Fixnum] indent
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
def extra_inspect_data(indent)
|
def extra_inspect_data(indent)
|
||||||
spacing = ' ' * indent
|
spacing = ' ' * indent
|
||||||
child_lines = children.map { |child| child.inspect(indent + 4) }
|
child_lines = children.map { |child| child.inspect(indent + 4) }
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
module Oga
|
module Oga
|
||||||
module XML
|
module XML
|
||||||
##
|
##
|
||||||
|
# A single, generic XML node that can have a parent, next, previous and
|
||||||
|
# child nodes.
|
||||||
|
#
|
||||||
# @!attribute [rw] parent
|
# @!attribute [rw] parent
|
||||||
# @return [Oga::XML::Node]
|
# @return [Oga::XML::Node]
|
||||||
#
|
#
|
||||||
|
@ -36,6 +39,14 @@ module Oga
|
||||||
after_initialize if respond_to?(:after_initialize)
|
after_initialize if respond_to?(:after_initialize)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Generates the inspect value for the current node. Sub classes can
|
||||||
|
# overwrite the {#extra_inspect_data} method to customize the output
|
||||||
|
# format.
|
||||||
|
#
|
||||||
|
# @param [Fixnum] indent
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
def inspect(indent = 0)
|
def inspect(indent = 0)
|
||||||
class_name = self.class.to_s.split('::').last
|
class_name = self.class.to_s.split('::').last
|
||||||
spacing = ' ' * indent
|
spacing = ' ' * indent
|
||||||
|
@ -43,8 +54,10 @@ module Oga
|
||||||
return "#{spacing}#{class_name}(#{extra_inspect_data(indent)})"
|
return "#{spacing}#{class_name}(#{extra_inspect_data(indent)})"
|
||||||
end
|
end
|
||||||
|
|
||||||
def extra_inspect_data
|
##
|
||||||
end
|
# @return [String]
|
||||||
|
#
|
||||||
|
def extra_inspect_data; end
|
||||||
end # Element
|
end # Element
|
||||||
end # XML
|
end # XML
|
||||||
end # Oga
|
end # Oga
|
||||||
|
|
|
@ -1,14 +1,26 @@
|
||||||
module Oga
|
module Oga
|
||||||
module XML
|
module XML
|
||||||
##
|
##
|
||||||
|
# Class containing information about a single text node. Text nodes don't
|
||||||
|
# have any children, attributes and the likes; just text.
|
||||||
|
#
|
||||||
|
# @!attribute [rw] text
|
||||||
|
# @return [String]
|
||||||
#
|
#
|
||||||
class Text < Node
|
class Text < Node
|
||||||
attr_accessor :text
|
attr_accessor :text
|
||||||
|
|
||||||
|
##
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
def to_xml
|
def to_xml
|
||||||
return text.to_s
|
return text.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# @param [Fixnum] indent
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
def extra_inspect_data(indent)
|
def extra_inspect_data(indent)
|
||||||
return "text: #{text.inspect}"
|
return "text: #{text.inspect}"
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,19 @@
|
||||||
module Oga
|
module Oga
|
||||||
module XML
|
module XML
|
||||||
##
|
##
|
||||||
|
# Class containing information about an XML declaration tag.
|
||||||
#
|
#
|
||||||
|
# @!attribute [rw] version
|
||||||
|
# The XML version.
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
|
# @!attribute [rw] encoding
|
||||||
|
# The XML document's encoding.
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
|
# @!attribute [rw] standalone
|
||||||
|
# Whether or not the document is a standalone document.
|
||||||
|
# @return [String]
|
||||||
#
|
#
|
||||||
class XmlDeclaration
|
class XmlDeclaration
|
||||||
attr_accessor :version, :encoding, :standalone
|
attr_accessor :version, :encoding, :standalone
|
||||||
|
@ -9,8 +21,8 @@ module Oga
|
||||||
##
|
##
|
||||||
# @param [Hash] options
|
# @param [Hash] options
|
||||||
#
|
#
|
||||||
# @option options [String] :version The XML version.
|
# @option options [String] :version
|
||||||
# @option options [String] :encoding The XML encoding.
|
# @option options [String] :encoding
|
||||||
# @option options [String] :standalone
|
# @option options [String] :standalone
|
||||||
#
|
#
|
||||||
def initialize(options = {})
|
def initialize(options = {})
|
||||||
|
@ -22,6 +34,11 @@ module Oga
|
||||||
@encoding ||= 'UTF-8'
|
@encoding ||= 'UTF-8'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Converts the declaration tag to XML.
|
||||||
|
#
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
def to_xml
|
def to_xml
|
||||||
pairs = []
|
pairs = []
|
||||||
|
|
||||||
|
@ -34,7 +51,10 @@ module Oga
|
||||||
return "<?xml #{pairs.join(' ')} ?>"
|
return "<?xml #{pairs.join(' ')} ?>"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# @param [Fixnum] indent
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
def inspect(indent = 0)
|
def inspect(indent = 0)
|
||||||
class_name = self.class.to_s.split('::').last
|
class_name = self.class.to_s.split('::').last
|
||||||
spacing = ' ' * indent
|
spacing = ' ' * indent
|
||||||
|
|
Loading…
Reference in New Issue