class Xlsxrb::Ooxml::XmlBuilder
Streams well-formed XML to a writable IO without building a DOM.
Constants
- EMPTY_HASH
- ESCAPE_MAP
- ESCAPE_RE
- XML_HEADER
Public Class Methods
(untyped io) → untyped
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 21 def initialize(io) @io = io end
Public Instance Methods
(untyped name) → untyped
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 52 def close_tag(name) @io.write("</") @io.write(name) @io.write(">") self end
() → untyped
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 25 def declaration @io << XML_HEADER self end
(untyped name, ?untyped attrs) → untyped
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 59 def empty_tag(name, attrs = EMPTY_HASH) @io.write("<") @io.write(name) write_attrs(attrs) unless attrs.empty? @io.write("/>") self end
(untyped name, ?untyped attrs) → untyped
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 44 def open_tag(name, attrs = EMPTY_HASH) @io.write("<") @io.write(name) write_attrs(attrs) unless attrs.empty? @io.write(">") self end
(untyped xml_string) → untyped
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 73 def raw(xml_string) @io << xml_string self end
Write raw XML string (for unmapped_data restoration).
(untyped name, ?untyped attrs) ?{ (?) → untyped } → untyped
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 33 def tag(name, attrs = EMPTY_HASH, &block) if block open_tag(name, attrs) yield self close_tag(name) else empty_tag(name, attrs) end self end
Opens a tag, yields for children, then closes the tag.
(untyped content) → untyped
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 67 def text(content) @io << escape(content.to_s) self end
() → untyped
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 97 def to_s @io.is_a?(StringIO) ? @io.string : @io.to_s end
(untyped node) → untyped
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 79 def write_unmapped(node) return unless node.is_a?(Hash) && node[:tag] tag_name = node[:tag] attrs = node[:attrs] || {} children = node[:children] || [] text_content = node[:text] if children.empty? && (text_content.nil? || text_content.empty?) empty_tag(tag_name, attrs) else open_tag(tag_name, attrs) text(text_content) if text_content && !text_content.empty? children.each { |child| write_unmapped(child) } close_tag(tag_name) end end
Serialize an unmapped_data hash back to XML.