Class: Canon::DiffFormatter::ByLine::XmlFormatter
- Inherits:
-
BaseFormatter
- Object
- BaseFormatter
- Canon::DiffFormatter::ByLine::XmlFormatter
- 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, #diff_mode, #legacy_terminal, #show_diffs, #use_color, #visualization_map
Instance Method Summary collapse
-
#format(doc1, doc2) ⇒ String
Format DOM-guided XML diff.
-
#format_context_from_lines(context, lines1, _lines2) ⇒ Object
Format a context using its DiffLines.
-
#format_legacy(doc1, doc2) ⇒ Object
Legacy format method (for backward compatibility).
-
#format_report(report, doc1, doc2) ⇒ Object
Format a DiffReport for display.
-
#format_with_pipeline(doc1, doc2) ⇒ Object
Format using new DiffReportBuilder pipeline.
Methods inherited from BaseFormatter
#apply_bg, #apply_color, #apply_theme_style, #changed_content_styles, #compute_line_num_width, #content_style, #display_mode, for_format, #initialize, #marker_style, #normalize_color_for_rainbow, #structure_color, #structure_styles, #styled_marker, #theme, #theme_color, #theme_style, #unchanged_content_style, #visualization_chars
Constructor Details
This class inherits a constructor from Canon::DiffFormatter::ByLine::BaseFormatter
Instance Method Details
#format(doc1, doc2) ⇒ String
Format DOM-guided XML diff
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/canon/diff_formatter/by_line/xml_formatter.rb', line 20 def format(doc1, doc2) compute_line_num_width(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 handles formatting-only differences (0 DiffNodes) and legacy Hash entries format_legacy(doc1, doc2) end |
#format_context_from_lines(context, lines1, _lines2) ⇒ Object
Format a context using its DiffLines
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 186 |
# File 'lib/canon/diff_formatter/by_line/xml_formatter.rb', line 102 def format_context_from_lines(context, lines1, _lines2) output = [] context.lines.each do |diff_line| case diff_line.type when :unchanged old_num = diff_line.line_number + 1 new_num = (diff_line.new_position || diff_line.line_number) + 1 output << format_unified_line(old_num, new_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: use theme formatting style format_unified_line(line_num, nil, "[", diff_line.content, theme_color(:formatting, :content), formatting: true) elsif informative # Informative removal: use theme informative style format_unified_line(line_num, nil, "<", diff_line.content, theme_color(:informative, :content), informative: true) elsif diff_line.has_char_ranges? # Use character-level highlighting when char_ranges available. highlighted = render_line_from_char_ranges( diff_line.content, diff_line.char_ranges, :old ) old_str = "%#{@line_num_width}d" % line_num blank = " " * @line_num_width if @use_color yellow_old = colorize(old_str, structure_color(:line_number) || :yellow) yellow_pipe1 = colorize("|", structure_color(:pipe) || :yellow) yellow_pipe2 = colorize("|", structure_color(:pipe) || :yellow) red_marker = styled_marker("-", :removed) "#{yellow_old}#{yellow_pipe1}#{blank}#{red_marker} #{yellow_pipe2} #{highlighted}" else "#{old_str}|#{blank}- | #{highlighted}" end else # Normative removal: use theme removed style format_unified_line(line_num, nil, "-", diff_line.content, theme_color(:removed, :content)) end when :added line_num = (diff_line.new_position || diff_line.line_number) + 1 formatting = diff_line.formatting? informative = diff_line.informative? output << if formatting # Formatting-only addition: use theme formatting style format_unified_line(nil, line_num, "]", diff_line.content, theme_color(:formatting, :content), formatting: true) elsif informative # Informative addition: use theme informative style format_unified_line(nil, line_num, ">", diff_line.content, theme_color(:informative, :content), informative: true) else # Normative addition: use theme added style format_unified_line(nil, line_num, "+", diff_line.content, theme_color(:added, :content)) end when :changed output << format_changed_line(diff_line, lines1) when :reflow_summary # Reflow summary: show collapsed formatting-only reflow output << format_reflow_summary(diff_line) end end output.join("\n") end |
#format_legacy(doc1, doc2) ⇒ Object
Legacy format method (for backward compatibility)
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 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/canon/diff_formatter/by_line/xml_formatter.rb', line 189 def format_legacy(doc1, doc2) # Check if we should show any diffs based on differences array if should_skip_diff_display? return "" end # If documents are equivalent (no normative diffs), do NOT run legacy formatter. # The legacy formatter shows ALL differences it finds via LCS on lines, # which can be misleading when the comparison found no normative differences. # Only run legacy formatter when we have actual DiffNode data to display. if @equivalent == true && (@differences.nil? || @differences.empty? || @differences.none?(Canon::Diff::DiffNode)) 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.}", :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
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 |
# File 'lib/canon/diff_formatter/by_line/xml_formatter.rb', line 74 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
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 |
# File 'lib/canon/diff_formatter/by_line/xml_formatter.rb', line 41 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_enricher" require_relative "../../diff/diff_line_builder" require_relative "../../diff/diff_report_builder" # Compute line number width BEFORE formatting compute_line_num_width(doc1, doc2) # Phase 1: Enrich DiffNodes with character positions Canon::Diff::DiffNodeEnricher.build(@differences, doc1, doc2) # Phase 2: Assemble DiffLines from enriched DiffNodes diff_lines = Canon::Diff::DiffLineBuilder.build(@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 |