Class: Canon::DiffFormatter::ByLine::XmlFormatter

Inherits:
BaseFormatter
  • Object
show all
Defined in:
lib/canon/diff_formatter/by_line/xml_formatter.rb

Overview

XML formatter with DOM-guided diffing Uses DOM parsing and element matching for intelligent XML diffs

Instance Attribute Summary

Attributes inherited from BaseFormatter

#context_lines, #diff_grouping_lines, #show_diffs, #use_color, #visualization_map

Instance Method Summary collapse

Methods inherited from BaseFormatter

for_format, #initialize

Constructor Details

This class inherits a constructor from Canon::DiffFormatter::ByLine::BaseFormatter

Instance Method Details

#format(doc1, doc2) ⇒ String

Format DOM-guided XML diff

Parameters:

  • doc1 (String)

    First XML document

  • doc2 (String)

    Second XML document

Returns:

  • (String)

    Formatted diff



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/canon/diff_formatter/by_line/xml_formatter.rb', line 19

def format(doc1, doc2)
  # If we have DiffNodes from comparison, check if there are normative diffs
  # based on show_diffs setting
  if @differences&.any?(Canon::Diff::DiffNode)
    # Check if we should skip based on show_diffs setting
    if should_skip_diff_display?
      return ""
    end

    # Use new pipeline when DiffNodes available
    return format_with_pipeline(doc1, doc2)
  end

  # LEGACY: Fall back to old behavior for backward compatibility
  # This happens when @differences is nil (no comparison result provided)
  format_legacy(doc1, doc2)
end

#format_context_from_lines(context, lines1, _lines2) ⇒ Object

Format a context using its DiffLines



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/canon/diff_formatter/by_line/xml_formatter.rb', line 91

def format_context_from_lines(context, lines1, _lines2)
  output = []

  context.lines.each do |diff_line|
    case diff_line.type
    when :unchanged
      line_num = diff_line.line_number + 1
      output << format_unified_line(line_num, line_num, " ",
                                    diff_line.content)
    when :removed
      line_num = diff_line.line_number + 1
      formatting = diff_line.formatting?
      informative = diff_line.informative?

      output << if formatting
                  # Formatting-only removal: [ marker in dark gray
                  format_unified_line(line_num, nil, "[",
                                      diff_line.content,
                                      :black,
                                      formatting: true)
                elsif informative
                  # Informative removal: < marker in blue
                  format_unified_line(line_num, nil, "<",
                                      diff_line.content,
                                      :blue,
                                      informative: true)
                else
                  # Normative removal: - marker in red
                  format_unified_line(line_num, nil, "-",
                                      diff_line.content,
                                      :red)
                end
    when :added
      line_num = diff_line.line_number + 1
      formatting = diff_line.formatting?
      informative = diff_line.informative?

      output << if formatting
                  # Formatting-only addition: ] marker in light gray
                  format_unified_line(nil, line_num, "]",
                                      diff_line.content,
                                      :white,
                                      formatting: true)
                elsif informative
                  # Informative addition: > marker in cyan
                  format_unified_line(nil, line_num, ">",
                                      diff_line.content,
                                      :cyan,
                                      informative: true)
                else
                  # Normative addition: + marker in green
                  format_unified_line(nil, line_num, "+",
                                      diff_line.content,
                                      :green)
                end
    when :changed
      line_num = diff_line.line_number + 1
      formatting = diff_line.formatting?
      informative = diff_line.informative?
      # For changed lines, we need both old and new content
      # For now, show as removed + added
      old_content = lines1[diff_line.line_number]
      new_content = diff_line.content

      if formatting
        output << format_unified_line(line_num, nil, "[",
                                      old_content,
                                      :black,
                                      formatting: true)
        output << format_unified_line(nil, line_num, "]",
                                      new_content,
                                      :white,
                                      formatting: true)
      elsif informative
        output << format_unified_line(line_num, nil, "<",
                                      old_content,
                                      :blue,
                                      informative: true)
        output << format_unified_line(nil, line_num, ">",
                                      new_content,
                                      :cyan,
                                      informative: true)
      else
        output << format_unified_line(line_num, nil, "-",
                                      old_content,
                                      :red)
        output << format_unified_line(nil, line_num, "+",
                                      new_content,
                                      :green)
      end
    end
  end

  output.join("\n")
end

#format_legacy(doc1, doc2) ⇒ Object

Legacy format method (for backward compatibility)



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/canon/diff_formatter/by_line/xml_formatter.rb', line 188

def format_legacy(doc1, doc2)
  # Check if we should show any diffs based on differences array
  if should_skip_diff_display?
    return ""
  end

  require_relative "../../xml/data_model"
  require_relative "../../xml/element_matcher"
  require_relative "../../xml/line_range_mapper"

  output = []

  begin
    # Parse to DOM
    root1 = Canon::Xml::DataModel.from_xml(doc1)
    root2 = Canon::Xml::DataModel.from_xml(doc2)

    # Match elements semantically
    matcher = Canon::Xml::ElementMatcher.new
    matches = matcher.match_trees(root1, root2)

    # Build line range maps using ORIGINAL documents
    mapper1 = Canon::Xml::LineRangeMapper.new(indent: 2)
    mapper2 = Canon::Xml::LineRangeMapper.new(indent: 2)
    map1 = mapper1.build_map(root1, doc1)
    map2 = mapper2.build_map(root2, doc2)

    # Use ORIGINAL document lines for display
    lines1 = doc1.split("\n")
    lines2 = doc2.split("\n")

    # Display diffs based on element matches
    output << format_element_matches(matches, map1, map2, lines1,
                                     lines2)
  rescue StandardError => e
    # Fall back to simple diff on error
    output << colorize("Warning: DOM parsing failed, using simple diff",
                       :yellow)
    output << colorize("Error: #{e.class}: #{e.message}", :red)

    # Include relevant backtrace lines
    relevant_trace = e.backtrace.select do |line|
      line.include?("canon")
    end.take(3)
    unless relevant_trace.empty?
      output << colorize("Backtrace:", :yellow)
      relevant_trace.each do |line|
        output << colorize("  #{line}", :yellow)
      end
    end

    output << ""
    require_relative "simple_formatter"
    simple = SimpleFormatter.new(
      use_color: @use_color,
      context_lines: @context_lines,
      diff_grouping_lines: @diff_grouping_lines,
      visualization_map: @visualization_map,
    )
    output << simple.format(doc1, doc2)
  end

  output.join("\n")
end

#format_report(report, doc1, doc2) ⇒ Object

Format a DiffReport for display



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
# File 'lib/canon/diff_formatter/by_line/xml_formatter.rb', line 63

def format_report(report, doc1, doc2)
  return "" if report.contexts.empty?

  lines1 = doc1.split("\n")
  lines2 = doc2.split("\n")

  output = []

  # Detect non-ASCII characters
  all_text = (lines1 + lines2).join
  non_ascii = Legend.detect_non_ascii(all_text, @visualization_map)

  # Add Unicode legend if needed
  unless non_ascii.empty?
    output << Legend.build_legend(non_ascii, use_color: @use_color)
    output << ""
  end

  # Format each context
  report.contexts.each_with_index do |context, idx|
    output << "" if idx.positive?
    output << format_context_from_lines(context, lines1, lines2)
  end

  output.join("\n")
end

#format_with_pipeline(doc1, doc2) ⇒ Object

Format using new DiffReportBuilder pipeline



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/canon/diff_formatter/by_line/xml_formatter.rb', line 38

def format_with_pipeline(doc1, doc2)
  # Check if we should show any diffs
  if should_skip_diff_display?
    return ""
  end

  require_relative "../../diff/diff_node_mapper"
  require_relative "../../diff/diff_report_builder"

  # Layer 2: Map DiffNodes to DiffLines
  diff_lines = Canon::Diff::DiffNodeMapper.map(@differences, doc1, doc2)

  # Layers 3-5: Build report through pipeline
  report = Canon::Diff::DiffReportBuilder.build(
    diff_lines,
    show_diffs: @show_diffs,
    context_lines: @context_lines,
    grouping_lines: @diff_grouping_lines,
  )

  # Layer 6: Format the report
  format_report(report, doc1, doc2)
end