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) ⇒ 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) ⇒ Formatter
Returns a new instance of Formatter.
8 9 10 11 12 13 14 |
# File 'lib/moxml/adapter/customized_rexml/formatter.rb', line 8 def initialize(indentation: 2, self_close_empty: false) @indentation = " " * indentation @level = 0 @compact = true @width = -1 # Disable line wrapping @self_close_empty = self_close_empty end |
Instance Method Details
#escape_text(text) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/moxml/adapter/customized_rexml/formatter.rb', line 83 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
16 17 18 19 20 21 22 23 |
# File 'lib/moxml/adapter/customized_rexml/formatter.rb', line 16 def write(node, output) case node when ::REXML::XMLDecl write_declaration(node, output) else super end end |
#write_element(node, output) ⇒ Object
25 26 27 28 29 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 |
# File 'lib/moxml/adapter/customized_rexml/formatter.rb', line 25 def write_element(node, output) # output << ' ' * @level output << "<#{node.expanded_name}" write_attributes(node, output) if node.children.empty? && @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? { |c| c.is_a?(::REXML::Element) } mixed = has_text && has_elements # Handle children based on content type unless node.children.empty? @level += @indentation.length unless mixed 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?) # Indent non-text nodes in non-mixed content # if !mixed && !child.is_a?(::REXML::Text) # output << ' ' * @level # end write(child, output) # Add newlines between elements in non-mixed content # if !mixed && !child.is_a?(::REXML::Text) && index < node.children.size - 1 # output << "\n" # end end # Reset indentation for closing tag in non-mixed content unless mixed @level -= @indentation.length # output << ' ' * @level end end output << "</#{node.expanded_name}>" # output << "\n" unless mixed end |
#write_text(node, output) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/moxml/adapter/customized_rexml/formatter.rb', line 76 def write_text(node, output) text = node.value return if text.empty? output << escape_text(text) end |