Class: Canon::DiffFormatter::ByLine::HtmlFormatter
- Inherits:
-
BaseFormatter
- Object
- BaseFormatter
- Canon::DiffFormatter::ByLine::HtmlFormatter
- Defined in:
- lib/canon/diff_formatter/by_line/html_formatter.rb
Overview
HTML formatter with DOM-guided diffing Uses DOM parsing and element matching for intelligent HTML diffs
Instance Attribute Summary collapse
-
#html_version ⇒ Object
readonly
Returns the value of attribute html_version.
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 HTML diff.
-
#format_context_from_lines(context, lines1, _lines2) ⇒ Object
Format a context using its DiffLines.
-
#format_report(report, doc1, doc2) ⇒ Object
Format a DiffReport for display.
-
#format_with_pipeline(doc1, doc2) ⇒ Object
Format using new DiffReportBuilder pipeline.
-
#initialize(use_color: true, context_lines: 3, diff_grouping_lines: nil, visualization_map: nil, html_version: :html4, show_diffs: :all, differences: [], diff_mode: :separate, legacy_terminal: false, equivalent: nil) ⇒ HtmlFormatter
constructor
A new instance of HtmlFormatter.
Methods inherited from BaseFormatter
#apply_bg, #apply_color, #apply_theme_style, #changed_content_styles, #compute_line_num_width, #content_style, #display_mode, for_format, #marker_style, #normalize_color_for_rainbow, #structure_color, #structure_styles, #styled_marker, #theme, #theme_color, #theme_style, #unchanged_content_style, #visualization_chars
Constructor Details
#initialize(use_color: true, context_lines: 3, diff_grouping_lines: nil, visualization_map: nil, html_version: :html4, show_diffs: :all, differences: [], diff_mode: :separate, legacy_terminal: false, equivalent: nil) ⇒ HtmlFormatter
Returns a new instance of HtmlFormatter.
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/canon/diff_formatter/by_line/html_formatter.rb', line 15 def initialize(use_color: true, context_lines: 3, diff_grouping_lines: nil, visualization_map: nil, html_version: :html4, show_diffs: :all, differences: [], diff_mode: :separate, legacy_terminal: false, equivalent: nil) super(use_color: use_color, context_lines: context_lines, diff_grouping_lines: diff_grouping_lines, visualization_map: visualization_map, show_diffs: show_diffs, differences: differences, diff_mode: diff_mode, legacy_terminal: legacy_terminal, equivalent: equivalent) @html_version = html_version end |
Instance Attribute Details
#html_version ⇒ Object (readonly)
Returns the value of attribute html_version.
13 14 15 |
# File 'lib/canon/diff_formatter/by_line/html_formatter.rb', line 13 def html_version @html_version end |
Instance Method Details
#format(doc1, doc2) ⇒ String
Format DOM-guided HTML diff
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 79 80 81 82 83 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/by_line/html_formatter.rb', line 34 def format(doc1, doc2) compute_line_num_width(doc1, doc2) # If we have DiffNodes from comparison, use the new pipeline 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 DOM-based behavior # Check if we should show any diffs based on differences array if should_skip_diff_display? return "" end require_relative "../../html/data_model" require_relative "../../xml/element_matcher" require_relative "../../xml/line_range_mapper" require_relative "../../pretty_printer/html" output = [] begin # Parse to DOM using HTML parser root1 = Canon::Html::DataModel.from_html(doc1, version: @html_version) root2 = Canon::Html::DataModel.from_html(doc2, version: @html_version) # Match elements semantically matcher = Canon::Xml::ElementMatcher.new matches = matcher.match_trees(root1, root2) # Pretty-print HTML for line mapping pretty_printer = Canon::PrettyPrinter::Html.new(indent: 2) pretty1 = pretty_printer.format(doc1) pretty2 = pretty_printer.format(doc2) # Build line range maps using pretty-printed documents mapper1 = Canon::Xml::LineRangeMapper.new(indent: 2) mapper2 = Canon::Xml::LineRangeMapper.new(indent: 2) map1 = mapper1.build_map(root1, pretty1) map2 = mapper2.build_map(root2, pretty2) # Use pretty-printed document lines for display lines1 = pretty1.split("\n") lines2 = pretty2.split("\n") # Display diffs based on element matches result = format_element_matches(matches, map1, map2, lines1, lines2) output << result 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_context_from_lines(context, lines1, _lines2) ⇒ Object
Format a context using its DiffLines
169 170 171 172 173 174 175 176 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 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 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/canon/diff_formatter/by_line/html_formatter.rb', line 169 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 format_unified_line(line_num, nil, "[", diff_line.content, theme_color(:formatting, :marker) || :black, formatting: true) elsif informative # Informative removal: < marker format_unified_line(line_num, nil, "<", diff_line.content, theme_color(:informative, :marker) || :blue, informative: true) else # Normative removal: - marker format_unified_line(line_num, nil, "-", diff_line.content, theme_color(:removed, :marker) || :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 format_unified_line(nil, line_num, "]", diff_line.content, theme_color(:formatting, :marker) || :white, formatting: true) elsif informative # Informative addition: > marker format_unified_line(nil, line_num, ">", diff_line.content, theme_color(:informative, :marker) || :cyan, informative: true) else # Normative addition: + marker format_unified_line(nil, line_num, "+", diff_line.content, theme_color(:added, :marker) || :green) end when :changed line_num = diff_line.line_number + 1 formatting = diff_line.formatting? informative = diff_line.informative? old_content = lines1[diff_line.line_number] new_content = diff_line.content if formatting fmt_color = theme_color(:formatting, :marker) || :black output << format_unified_line(line_num, nil, "[", old_content, fmt_color, formatting: true) output << format_unified_line(nil, line_num, "]", new_content, fmt_color, formatting: true) elsif informative inf_color = theme_color(:informative, :marker) || :blue output << format_unified_line(line_num, nil, "<", old_content, inf_color, informative: true) output << format_unified_line(nil, line_num, ">", new_content, inf_color, informative: true) else output << format_unified_line(line_num, nil, "-", old_content, theme_color(:removed, :marker) || :red) output << format_unified_line(nil, line_num, "+", new_content, theme_color(:added, :marker) || :green) end end end output.join("\n") end |
#format_report(report, doc1, doc2) ⇒ Object
Format a DiffReport for display
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 |
# File 'lib/canon/diff_formatter/by_line/html_formatter.rb', line 141 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
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/canon/diff_formatter/by_line/html_formatter.rb', line 121 def format_with_pipeline(doc1, doc2) 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 |