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
- #indented? ⇒ Boolean
-
#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
111 112 113 114 115 116 117 118 119 |
# File 'lib/moxml/adapter/customized_rexml/formatter.rb', line 111 def escape_text(text) text.to_s.gsub(/[<>&]/) do |match| case match when "<" then "<" when ">" then ">" when "&" then "&" end end end |
#indented? ⇒ Boolean
30 31 32 |
# File 'lib/moxml/adapter/customized_rexml/formatter.rb', line 30 def indented? !@indentation.empty? 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
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 101 102 |
# File 'lib/moxml/adapter/customized_rexml/formatter.rb', line 34 def write_element(node, output) 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 << ">" has_text = node.children.any? { |c| c.is_a?(::REXML::Text) && !c.to_s.strip.empty? } has_elements = node.children.any?(::REXML::Element) indent_children = indented? && has_elements && !has_text # Handle children based on content type all_children_empty = node.children.empty? && !(entity_refs && !entity_refs.empty?) unless all_children_empty @level += @indentation.length if indent_children if entity_refs && !entity_refs.empty? && child_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?) output << "\n" << (' ' * @level) if indent_children write(child, output) end when :eref if eref_idx < entity_refs.size output << "\n" << (' ' * @level) if indent_children write(entity_refs[eref_idx], output) eref_idx += 1 end end end else node.children.each_with_index do |child, _index| next if child.is_a?(::REXML::Text) && child.to_s.strip.empty? && !(child.next_sibling.nil? && child.previous_sibling.nil?) output << "\n" << (' ' * @level) if indent_children write(child, output) end end if indent_children @level -= @indentation.length output << "\n" << (' ' * @level) end end output << "</#{node.}>" end |
#write_text(node, output) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/moxml/adapter/customized_rexml/formatter.rb', line 104 def write_text(node, output) text = node.value return if text.empty? output << escape_text(text) end |