Class: Moxml::Adapter::CustomizedRexml::Formatter
- Inherits:
-
REXML::Formatters::Pretty
- Object
- REXML::Formatters::Pretty
- Moxml::Adapter::CustomizedRexml::Formatter
- Defined in:
- lib/moxml/adapter/customized_rexml/formatter.rb
Overview
Custom REXML formatter that fixes indentation and wrapping issues
Instance Method Summary collapse
- #escape_text(text) ⇒ Object
-
#initialize(indentation: 2, self_close_empty: false, adapter: nil) ⇒ Formatter
constructor
A new instance of Formatter.
- #write(node, output) ⇒ Object
- #write_element(node, output) ⇒ Object
- #write_text(node, output) ⇒ Object
Constructor Details
#initialize(indentation: 2, self_close_empty: false, adapter: nil) ⇒ Formatter
Returns a new instance of Formatter.
10 11 12 13 14 15 16 17 |
# File 'lib/moxml/adapter/customized_rexml/formatter.rb', line 10 def initialize(indentation: 2, self_close_empty: false, adapter: nil) @indentation = " " * indentation @level = 0 @compact = true @width = -1 # Disable line wrapping @self_close_empty = self_close_empty @adapter = adapter end |
Instance Method Details
#escape_text(text) ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/moxml/adapter/customized_rexml/formatter.rb', line 109 def escape_text(text) text.to_s.gsub(/[<>&]/) do |match| case match when "<" then "<" when ">" then ">" when "&" then "&" end end end |
#write(node, output) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/moxml/adapter/customized_rexml/formatter.rb', line 19 def write(node, output) case node when ::REXML::XMLDecl write_declaration(node, output) when ::Moxml::Adapter::CustomizedRexml::EntityReference output << "&#{node.name};" else super end end |
#write_element(node, output) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/moxml/adapter/customized_rexml/formatter.rb', line 30 def write_element(node, output) # output << ' ' * @level output << "<#{node.}" write_attributes(node, output) # Check for entity refs stored in adapter attachments entity_refs = @adapter&.&.get(node, :entity_refs) child_sequence = @adapter&.&.get(node, :child_sequence) has_no_children = node.children.empty? && !(entity_refs && !entity_refs.empty?) if has_no_children && @self_close_empty output << "/>" return end output << ">" # Check for mixed content has_text = node.children.any? { |c| c.is_a?(::REXML::Text) && !c.to_s.strip.empty? } has_elements = node.children.any?(::REXML::Element) mixed = has_text && has_elements # Handle children based on content type all_children_empty = node.children.empty? && !(entity_refs && !entity_refs.empty?) unless all_children_empty @level += @indentation.length unless mixed if entity_refs && !entity_refs.empty? && child_sequence # Interleave native children with entity refs using tracked sequence eref_idx = 0 native_idx = 0 child_sequence.each do |type| case type when :native if native_idx < node.children.size child = node.children[native_idx] native_idx += 1 next if child.is_a?(::REXML::Text) && child.to_s.strip.empty? && !(child.next_sibling.nil? && child.previous_sibling.nil?) write(child, output) end when :eref if eref_idx < entity_refs.size write(entity_refs[eref_idx], output) eref_idx += 1 end end end else node.children.each_with_index do |child, _index| # Skip insignificant whitespace next if child.is_a?(::REXML::Text) && child.to_s.strip.empty? && !(child.next_sibling.nil? && child.previous_sibling.nil?) write(child, output) end end # Reset indentation for closing tag in non-mixed content unless mixed @level -= @indentation.length # output << ' ' * @level end end output << "</#{node.}>" # output << "\n" unless mixed end |
#write_text(node, output) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/moxml/adapter/customized_rexml/formatter.rb', line 102 def write_text(node, output) text = node.value return if text.empty? output << escape_text(text) end |