Module: Canon::DiffFormatter::DebugOutput
- Defined in:
- lib/canon/diff_formatter/debug_output.rb
Overview
Verbose diff output helper for CANON_VERBOSE mode Can be activated by:
-
Environment variable: CANON_VERBOSE=1
-
Diff option: verbose_diff: true
Provides beautiful, readable output
Class Method Summary collapse
- .debug_diff_structure(diff_report) ⇒ Object
-
.debug_info(comparison_result, formatter_options = {}) ⇒ Object
Backward compatibility alias.
- .dimension_description(dimension, behavior) ⇒ Object
- .enabled?(verbose_diff_option = false) ⇒ Boolean
- .format_attributes(node) ⇒ Object
- .format_comparison_summary(comparison_result) ⇒ Object
- .format_content_preview(content) ⇒ Object
- .format_differences_tree(differences) ⇒ Object
- .format_formatter_options_table(formatter_options) ⇒ Object
- .format_match_options_table(comparison_result) ⇒ Object
- .format_node_brief(node) ⇒ Object
-
.format_node_diff_detail(diff) ⇒ Object
Format detailed information about what differed in the nodes.
- .format_value(value) ⇒ Object
- .get_node_content(node) ⇒ Object
- .option_impact(key, value) ⇒ Object
-
.verbose_tables_only(comparison_result, formatter_options = {}) ⇒ Object
Return ONLY CANON VERBOSE tables (not Semantic Diff Report) Semantic Diff Report is now part of main diff output.
Class Method Details
.debug_diff_structure(diff_report) ⇒ Object
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/canon/diff_formatter/debug_output.rb', line 388 def debug_diff_structure(diff_report) return "" unless enabled? require "table_tennis" output = [] output << "" output << "DIFF STRUCTURE (DiffReport):" output << "" if diff_report.nil? || diff_report.contexts.empty? output << " (no diff contexts)" return output.join("\n") end output << " Total contexts: #{diff_report.contexts.length}" output << "" # Show contexts and blocks in table format diff_report.contexts.each_with_index do |context, ctx_idx| output << " Context #{ctx_idx + 1}: Lines #{context.start_line}-#{context.end_line}" output << "" if context.diff_blocks.any? block_rows = context.diff_blocks.map.with_index do |block, blk_idx| { "#": blk_idx + 1, range: "#{block.start_idx}-#{block.end_idx}", size: block.size, types: block.types.join(", "), normative: block.normative? ? "✓ NORMATIVE" : "✗ informative", dimension: block.diff_node&.dimension&.to_s || "-", lines: block.diff_lines&.length || 0, } end output << TableTennis.new( block_rows, title: " Diff Blocks in Context #{ctx_idx + 1}", columns: %i[# range size types normative dimension lines], headers: { "#": "#", range: "Line Range", size: "Size", types: "Types", normative: "Normative?", dimension: "Dimension", lines: "Lines", }, mark: ->(row) { row[:normative] == "✓ NORMATIVE" }, ).to_s output << "" end end output.join("\n") end |
.debug_info(comparison_result, formatter_options = {}) ⇒ Object
Backward compatibility alias
54 55 56 |
# File 'lib/canon/diff_formatter/debug_output.rb', line 54 def debug_info(comparison_result, = {}) verbose_tables_only(comparison_result, ) end |
.dimension_description(dimension, behavior) ⇒ Object
84 85 86 87 88 89 90 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 |
# File 'lib/canon/diff_formatter/debug_output.rb', line 84 def dimension_description(dimension, behavior) # Special handling for preprocessing dimension if dimension.to_s == "preprocessing" return case behavior when :none "No preprocessing (compare as-is)" when :c14n "Canonicalize (XML C14N normalization)" when :normalize "Normalize (collapse whitespace, trim lines)" when :format "Pretty-format (consistent indentation)" when :rendered "As browser-rendered (compacted whitespace, to_html)" else behavior.to_s end end # Standard dimension descriptions case behavior when :ignore "Differences IGNORED (informative)" when :normalize "Normalized then compared (normative if different after normalization)" when :strict "Must match exactly (normative)" when :strip "Strip leading/trailing whitespace only" when :compact "Collapse whitespace runs to single space" else behavior.to_s end end |
.enabled?(verbose_diff_option = false) ⇒ Boolean
14 15 16 17 18 |
# File 'lib/canon/diff_formatter/debug_output.rb', line 14 def enabled?(verbose_diff_option = false) verbose_diff_option || ENV["CANON_VERBOSE"] == "1" || ENV["CANON_VERBOSE"] == "true" end |
.format_attributes(node) ⇒ Object
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/canon/diff_formatter/debug_output.rb', line 342 def format_attributes(node) return "" unless node.respond_to?(:attributes) attrs = node.attributes return "" if attrs.empty? # Format as name="value" attr_strs = attrs.map do |key, val| name = if key.is_a?(String) key else (key.respond_to?(:name) ? key.name : key.to_s) end value = val.respond_to?(:value) ? val.value : val.to_s "#{name}=\"#{value}\"" end.sort # Limit to first 3 attributes if attr_strs.length > 3 "#{attr_strs[0..2].join(' ')} ..." else attr_strs.join(" ") end end |
.format_comparison_summary(comparison_result) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 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 |
# File 'lib/canon/diff_formatter/debug_output.rb', line 177 def format_comparison_summary(comparison_result) return "COMPARISON RESULT: (not a ComparisonResult object)" unless comparison_result.is_a?(Canon::Comparison::ComparisonResult) normative_count = comparison_result.normative_differences.length informative_count = comparison_result.informative_differences.length rows = [ { metric: "Equivalent?", value: comparison_result.equivalent? ? "✓ YES" : "✗ NO", detail: comparison_result.equivalent? ? "Documents are semantically equivalent" : "Documents have semantic differences", }, { metric: "Normative Diffs", value: normative_count.positive? ? "#{normative_count} diffs" : "0", detail: "Semantic differences that matter", }, { metric: "Informative Diffs", value: informative_count.positive? ? "#{informative_count} diffs" : "0", detail: "Textual/formatting differences (ignored)", }, { metric: "Total Diffs", value: comparison_result.differences.length.to_s, detail: "All differences found", }, ] TableTennis.new( rows, title: "Comparison Result Summary", columns: %i[metric value detail], headers: { metric: "Metric", value: "Value", detail: "Description" }, zebra: true, ).to_s end |
.format_content_preview(content) ⇒ Object
377 378 379 380 381 382 383 384 385 386 |
# File 'lib/canon/diff_formatter/debug_output.rb', line 377 def format_content_preview(content) return '""' if content.nil? || content.empty? # Show first 40 chars if content.length > 40 "\"#{content[0..37]}...\"" else "\"#{content}\"" end end |
.format_differences_tree(differences) ⇒ Object
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 262 263 264 265 266 267 268 |
# File 'lib/canon/diff_formatter/debug_output.rb', line 216 def format_differences_tree(differences) output = [] output << "DIFFERENCES TREE:" output << "" # Create table rows for each difference rows = differences.map.with_index do |diff, i| if diff.is_a?(Canon::Diff::DiffNode) detail1, detail2 = format_node_diff_detail(diff) { "#": i + 1, dimension: diff.dimension.to_s, marker: diff.normative? ? "+/-" : "~", diff1: detail1, diff2: detail2, } elsif diff.is_a?(Hash) { "#": i + 1, dimension: diff[:dimension] || "(unknown)", marker: "+/-", diff1: "(hash)", diff2: "(hash)", } else { "#": i + 1, dimension: "-", marker: "-", diff1: "-", diff2: "-", } end end output << TableTennis.new( rows, title: "Differences Detail (#{differences.length} total)", columns: %i[# dimension marker diff1 diff2], headers: { "#": "#", dimension: "Dimension", marker: "Marker", diff1: "Expected (File 1)", diff2: "Actual (File 2)", }, zebra: true, mark: ->(row) { row[:marker] == "+/-" }, ).to_s output.join("\n") end |
.format_formatter_options_table(formatter_options) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/canon/diff_formatter/debug_output.rb', line 120 def () rows = .map do |key, value| { option: key.to_s, value: format_value(value), impact: option_impact(key, value), } end TableTennis.new( rows, title: "Formatter Options", columns: %i[option value impact], headers: { option: "Option", value: "Value", impact: "Impact" }, zebra: true, ).to_s end |
.format_match_options_table(comparison_result) ⇒ Object
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 |
# File 'lib/canon/diff_formatter/debug_output.rb', line 58 def (comparison_result) return "MATCH OPTIONS: (not available)" unless comparison_result.is_a?(Canon::Comparison::ComparisonResult) return "MATCH OPTIONS: (not available)" unless comparison_result. # Filter out internal tree_diff metadata keys that should not be displayed internal_keys = %i[tree_diff_operations tree_diff_statistics tree_diff_matching] rows = comparison_result..except(*internal_keys).map do |dimension, behavior| { dimension: dimension.to_s, behavior: behavior.to_s, description: dimension_description(dimension, behavior), } end TableTennis.new( rows, title: "Match Options (#{comparison_result.format.to_s.upcase})", columns: %i[dimension behavior description], headers: { dimension: "Dimension", behavior: "Behavior", description: "Meaning" }, zebra: true, ).to_s end |
.format_node_brief(node) ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/canon/diff_formatter/debug_output.rb', line 270 def format_node_brief(node) return "(nil)" if node.nil? # Handle TextNode specially if node.is_a?(Canon::Xml::Nodes::TextNode) content = node.value.to_s if content.length > 30 "\"#{content[0..27]}...\"" else "\"#{content}\"" end # Handle CommentNode specially elsif node.is_a?(Canon::Xml::Nodes::CommentNode) content = node.value.to_s preview = content.length > 30 ? "#{content[0..27]}..." : content "<!--#{preview}-->" elsif node.respond_to?(:name) "<#{node.name}>" elsif node.respond_to?(:content) content = node.content.to_s if content.length > 30 "\"#{content[0..27]}...\"" else "\"#{content || ''}\"" end elsif node.respond_to?(:text) text = node.text.to_s if text.length > 30 "\"#{text[0..27]}...\"" else "\"#{text || ''}\"" end else node.class.name end end |
.format_node_diff_detail(diff) ⇒ Object
Format detailed information about what differed in the nodes
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/canon/diff_formatter/debug_output.rb', line 308 def format_node_diff_detail(diff) node1 = diff.node1 node2 = diff.node2 # For attribute differences, show which attributes differ if diff.dimension == :attribute_whitespace && node1.respond_to?(:attributes) && node2.respond_to?(:attributes) attrs1 = format_attributes(node1) attrs2 = format_attributes(node2) return ["<#{node1.name}> #{attrs1}", "<#{node2.name}> #{attrs2}"] end # For element differences, show element names if node1.respond_to?(:name) && node2.respond_to?(:name) if node1.name == node2.name # Same element name, different content end return ["<#{node1.name}>", "<#{node2.name}>"] return ["<#{node1.name}>", "<#{node2.name}>"] end # For text differences, show content preview if %i[text_content structural_whitespace].include?(diff.dimension) content1 = get_node_content(node1) content2 = get_node_content(node2) return [format_content_preview(content1), format_content_preview(content2)] end # Fallback to brief format [format_node_brief(node1), format_node_brief(node2)] end |
.format_value(value) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/canon/diff_formatter/debug_output.rb', line 138 def format_value(value) case value when Symbol value.to_s when Integer, String value.to_s when true, false value.to_s when nil "(nil)" else value.class.name end end |
.get_node_content(node) ⇒ Object
367 368 369 370 371 372 373 374 375 |
# File 'lib/canon/diff_formatter/debug_output.rb', line 367 def get_node_content(node) if node.respond_to?(:content) node.content.to_s elsif node.respond_to?(:text) node.text.to_s else "" end end |
.option_impact(key, value) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/canon/diff_formatter/debug_output.rb', line 153 def option_impact(key, value) case key when :show_diffs case value when :all "Show all diffs (normative + informative)" when :normative "Show only normative (semantic) diffs" when :informative "Show only informative (textual) diffs" else value.to_s end when :mode value == :by_line ? "Line-by-line diff" : "Object tree diff" when :context_lines "#{value} lines of context around diffs" when :diff_grouping_lines value ? "Group diffs within #{value} lines" : "No grouping" else "-" end end |
.verbose_tables_only(comparison_result, formatter_options = {}) ⇒ Object
Return ONLY CANON VERBOSE tables (not Semantic Diff Report) Semantic Diff Report is now part of main diff output
22 23 24 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 |
# File 'lib/canon/diff_formatter/debug_output.rb', line 22 def verbose_tables_only(comparison_result, = {}) verbose_diff = [:verbose_diff] || false return "" unless enabled?(verbose_diff) require "table_tennis" output = [] output << "" output << ("=" * 80) output << "CANON VERBOSE MODE - DETAILED OPTIONS" output << ("=" * 80) output << "" # Show match options as a table output << (comparison_result) output << "" # Show formatter options as a table output << () output << "" # Show comparison summary output << format_comparison_summary(comparison_result) output << "" output << ("=" * 80) output << "" output.join("\n") end |