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.
19 20 21 |
# File 'lib/xlsxrb/ooxml/xml_builder.rb', line 19 def initialize(io) @io = io end |
Instance Method Details
#close_tag(name) ⇒ Object
47 48 49 50 |
# File 'lib/xlsxrb/ooxml/xml_builder.rb', line 47 def close_tag(name) @io << "</#{name}>" self end |
#declaration ⇒ Object
23 24 25 26 |
# File 'lib/xlsxrb/ooxml/xml_builder.rb', line 23 def declaration @io << XML_HEADER self end |
#empty_tag(name, attrs = {}) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/xlsxrb/ooxml/xml_builder.rb', line 52 def empty_tag(name, attrs = {}) @io << "<#{name}" write_attrs(attrs) @io << "/>" self end |
#open_tag(name, attrs = {}) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/xlsxrb/ooxml/xml_builder.rb', line 40 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).
65 66 67 68 |
# File 'lib/xlsxrb/ooxml/xml_builder.rb', line 65 def raw(xml_string) @io << xml_string self end |
#tag(name, attrs = {}, &block) ⇒ Object
Opens a tag, yields for children, then closes the tag.
29 30 31 32 33 34 35 36 37 38 |
# 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 |
#text(content) ⇒ Object
59 60 61 62 |
# File 'lib/xlsxrb/ooxml/xml_builder.rb', line 59 def text(content) @io << escape(content.to_s) self end |
#to_s ⇒ Object
89 90 91 |
# File 'lib/xlsxrb/ooxml/xml_builder.rb', line 89 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.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# 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 |