Class: Xlsxrb::Ooxml::XmlBuilder
- Inherits:
-
Object
- Object
- Xlsxrb::Ooxml::XmlBuilder
- Defined in:
- lib/xlsxrb/ooxml/xml_builder.rb
Overview
Streams well-formed XML to a writable IO without building a DOM.
Constant Summary collapse
- XML_HEADER =
%(<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n)- ESCAPE_MAP =
{ "&" => "&", "<" => "<", ">" => ">", '"' => """, "'" => "'" }.freeze
- ESCAPE_RE =
/[&<>"']/
Instance Method Summary collapse
- #close_tag(name) ⇒ Object
- #declaration ⇒ Object
- #empty_tag(name, attrs = {}) ⇒ Object
-
#initialize(io) ⇒ XmlBuilder
constructor
A new instance of XmlBuilder.
- #open_tag(name, attrs = {}) ⇒ Object
-
#raw(xml_string) ⇒ Object
Write raw XML string (for unmapped_data restoration).
-
#tag(name, attrs = {}, &block) ⇒ Object
Opens a tag, yields for children, then closes the tag.
- #text(content) ⇒ Object
- #to_s ⇒ Object
-
#write_unmapped(node) ⇒ Object
Serialize an unmapped_data hash back to XML.
Constructor Details
#initialize(io) ⇒ XmlBuilder
Returns a new instance of XmlBuilder.
21 22 23 |
# File 'lib/xlsxrb/ooxml/xml_builder.rb', line 21 def initialize(io) @io = io end |
Instance Method Details
#close_tag(name) ⇒ Object
49 50 51 52 |
# File 'lib/xlsxrb/ooxml/xml_builder.rb', line 49 def close_tag(name) @io << "</#{name}>" self end |
#declaration ⇒ Object
25 26 27 28 |
# File 'lib/xlsxrb/ooxml/xml_builder.rb', line 25 def declaration @io << XML_HEADER self end |
#empty_tag(name, attrs = {}) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/xlsxrb/ooxml/xml_builder.rb', line 54 def empty_tag(name, attrs = {}) @io << "<#{name}" write_attrs(attrs) @io << "/>" self end |
#open_tag(name, attrs = {}) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/xlsxrb/ooxml/xml_builder.rb', line 42 def open_tag(name, attrs = {}) @io << "<#{name}" write_attrs(attrs) @io << ">" self end |
#raw(xml_string) ⇒ Object
Write raw XML string (for unmapped_data restoration).
67 68 69 70 |
# File 'lib/xlsxrb/ooxml/xml_builder.rb', line 67 def raw(xml_string) @io << xml_string self end |
#tag(name, attrs = {}, &block) ⇒ Object
Opens a tag, yields for children, then closes the tag.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/xlsxrb/ooxml/xml_builder.rb', line 31 def tag(name, attrs = {}, &block) if block open_tag(name, attrs) yield self close_tag(name) else empty_tag(name, attrs) end self end |
#text(content) ⇒ Object
61 62 63 64 |
# File 'lib/xlsxrb/ooxml/xml_builder.rb', line 61 def text(content) @io << escape(content.to_s) self end |
#to_s ⇒ Object
91 92 93 |
# File 'lib/xlsxrb/ooxml/xml_builder.rb', line 91 def to_s @io.is_a?(StringIO) ? @io.string : @io.to_s end |
#write_unmapped(node) ⇒ Object
Serialize an unmapped_data hash back to XML.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/xlsxrb/ooxml/xml_builder.rb', line 73 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 |