class Xlsxrb::Ooxml::XmlBuilder
Streams well-formed XML to a writable IO without building a DOM.
Constants
- ESCAPE_MAP
- ESCAPE_RE
- XML_HEADER
Public Class Methods
Public Instance Methods
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 47 def close_tag(name) @io << "</#{name}>" self end
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 23 def declaration @io << XML_HEADER self end
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 52 def empty_tag(name, attrs = {}) @io << "<#{name}" write_attrs(attrs) @io << "/>" self end
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 40 def open_tag(name, attrs = {}) @io << "<#{name}" write_attrs(attrs) @io << ">" self end
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 65 def raw(xml_string) @io << xml_string self end
Write raw XML string (for unmapped_data restoration).
# File lib/xlsxrb/ooxml/xml_builder.rb, line 29 def tag(name, attrs = {}, &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.
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 59 def text(content) @io << escape(content.to_s) self end
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 89 def to_s @io.is_a?(StringIO) ? @io.string : @io.to_s end
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 71 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.