class Xlsxrb::Ooxml::XmlBuilder
Streams well-formed XML to a writable IO without building a DOM.
Constants
- EMPTY_HASH
- ESCAPE_MAP
- ESCAPE_RE
- PLAIN_XML_HEADER
- UNESCAPE_MAP
- UNESCAPE_RE
- XML_HEADER
Public Class Methods
(?declaration: (:standalone | :plain | false)?) { (XmlBuilder) → void } → String
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 17 def self.build(declaration: nil) io = +"" builder = new(io) builder.declaration(standalone: declaration == :standalone) if declaration yield builder io end
Builds an XML string with a new {XmlBuilder}.
@param declaration [Symbol, false, nil] :standalone, :plain, or falsy for none. @return [String]
(String value) → String
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 170 def self.encode_xstring(value) return value unless value.match?(XSTRING_ENCODE_RE) # The underscore is U+005F, so the same format gives _x005F_. value.gsub(XSTRING_ENCODE_RE) { |char| format("_x%04X_", char.ord) } end
Encodes cell text as ISO/IEC 29500-1 ST_Xstring, the inverse of the xHHHH part of unescape: an underscore that starts an xHHHH sequence becomes x005F, and a character that XML 1.0 cannot hold, such as U+0001 or U+FFFE, becomes xHHHH. Excel and unescape read the original text back. The value must be UTF-8.
@param value [String] @return [String]
(untyped value) → String
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 153 def self.escape(value) str = value.to_s str = str.gsub(INVALID_XML_CHARS_RE, "") if !str.encoding.equal?(Encoding::BINARY) && str.match?(INVALID_XML_CHARS_RE) str.match?(ESCAPE_RE) ? str.gsub(ESCAPE_RE, ESCAPE_MAP) : str end
Escapes XML special characters in a string, and removes characters that XML 1.0 cannot hold, such as U+0001 and U+FFFE: a part that contains one is not well-formed. Binary strings are only escaped.
@param value [Object] @return [String]
(untyped io) → untyped
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 35 def initialize(io) @io = io end
(untyped value) → String
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 188 def self.unescape(value) str = value.to_s return str unless str.include?("&") || str.include?("_x") # Predefined and numeric entities in one pass, so that the output of one is not read # as the start of another. res = str.gsub(ENTITY_RE) do |match| dec = Regexp.last_match(1) hex = Regexp.last_match(2) next UNESCAPE_MAP.fetch(match) unless dec || hex (dec ? Integer(dec) : Integer(hex, 16)).chr(Encoding::UTF_8) rescue RangeError match end res.gsub(XSTRING_RE) do |match| codepoint = Integer(Regexp.last_match(1), 16) codepoint.chr(Encoding::UTF_8) rescue RangeError match end end
Unescapes predefined and numeric/hex XML entities, as well as ISO/IEC 29500-1 ST_Xstring (xHHHH) control character sequences. Each entity is decoded once: “&lt;” is the text “<”, not “<”.
@param value [Object] @return [String]
Public Instance Methods
(untyped name) → untyped
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 67 def close_tag(name) @io << "</" @io << name @io << ">" self end
(?standalone: bool) → self
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 40 def declaration(standalone: true) @io << (standalone ? XML_HEADER : PLAIN_XML_HEADER) self end
(untyped name, ?untyped attrs) → untyped
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 74 def empty_tag(name, attrs = EMPTY_HASH) @io << "<" @io << name write_attrs(attrs) unless attrs.empty? @io << "/>" self end
(untyped name, ?untyped attrs) → untyped
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 59 def open_tag(name, attrs = EMPTY_HASH) @io << "<" @io << name write_attrs(attrs) unless attrs.empty? @io << ">" self end
(untyped xml_string) → untyped
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 105 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 48 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 82 def text(content) @io << escape(content.to_s) self end
(String name, untyped content, ?Hash[untyped, untyped] attrs) → self
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 89 def text_tag(name, content, attrs = EMPTY_HASH) open_tag(name, attrs) text(content) close_tag(name) end
Writes an element containing escaped text content.
() → untyped
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 129 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 111 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.
(untyped content) → self
Source
# File lib/xlsxrb/ooxml/xml_builder.rb, line 97 def xstring_text(content) str = content.to_s str = str.dup.force_encoding(Encoding::UTF_8) if str.encoding.equal?(Encoding::BINARY) @io << escape(self.class.encode_xstring(str)) self end
Writes ST_Xstring-encoded and XML-escaped text content.