Class: Moxml::Adapter::CustomizedRexml::Formatter

Inherits:
REXML::Formatters::Pretty
  • Object
show all
Defined in:
lib/moxml/adapter/customized_rexml/formatter.rb

Overview

Custom REXML formatter that fixes indentation and wrapping issues

Instance Method Summary collapse

Constructor Details

#initialize(indentation: 2, self_close_empty: false) ⇒ Formatter

Returns a new instance of Formatter.



10
11
12
13
14
15
16
# File 'lib/moxml/adapter/customized_rexml/formatter.rb', line 10

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



87
88
89
90
91
92
93
94
95
# File 'lib/moxml/adapter/customized_rexml/formatter.rb', line 87

def escape_text(text)
  text.to_s.gsub(/[<>&]/) do |match|
    case match
    when "<" then "&lt;"
    when ">" then "&gt;"
    when "&" then "&amp;"
    end
  end
end

#write(node, output) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/moxml/adapter/customized_rexml/formatter.rb', line 18

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



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
75
76
77
78
# File 'lib/moxml/adapter/customized_rexml/formatter.rb', line 29

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?(::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



80
81
82
83
84
85
# File 'lib/moxml/adapter/customized_rexml/formatter.rb', line 80

def write_text(node, output)
  text = node.value
  return if text.empty?

  output << escape_text(text)
end