Class: Canon::DiffFormatter::ByLine::BaseFormatter

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

Overview

Base formatter for line-by-line diffs Provides common LCS diff logic and hunk building

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(use_color: true, context_lines: 3, diff_grouping_lines: nil, visualization_map: nil, show_diffs: :all, differences: []) ⇒ BaseFormatter

Returns a new instance of BaseFormatter.



47
48
49
50
51
52
53
54
55
56
# File 'lib/canon/diff_formatter/by_line/base_formatter.rb', line 47

def initialize(use_color: true, context_lines: 3,
               diff_grouping_lines: nil, visualization_map: nil,
               show_diffs: :all, differences: [])
  @use_color = use_color
  @context_lines = context_lines
  @diff_grouping_lines = diff_grouping_lines
  @visualization_map = visualization_map
  @show_diffs = show_diffs
  @differences = differences
end

Instance Attribute Details

#context_linesObject (readonly)

Returns the value of attribute context_lines.



13
14
15
# File 'lib/canon/diff_formatter/by_line/base_formatter.rb', line 13

def context_lines
  @context_lines
end

#diff_grouping_linesObject (readonly)

Returns the value of attribute diff_grouping_lines.



13
14
15
# File 'lib/canon/diff_formatter/by_line/base_formatter.rb', line 13

def diff_grouping_lines
  @diff_grouping_lines
end

#show_diffsObject (readonly)

Returns the value of attribute show_diffs.



13
14
15
# File 'lib/canon/diff_formatter/by_line/base_formatter.rb', line 13

def show_diffs
  @show_diffs
end

#use_colorObject (readonly)

Returns the value of attribute use_color.



13
14
15
# File 'lib/canon/diff_formatter/by_line/base_formatter.rb', line 13

def use_color
  @use_color
end

#visualization_mapObject (readonly)

Returns the value of attribute visualization_map.



13
14
15
# File 'lib/canon/diff_formatter/by_line/base_formatter.rb', line 13

def visualization_map
  @visualization_map
end

Class Method Details

.for_format(format, **options) ⇒ BaseFormatter

Create a format-specific by-line formatter

Parameters:

  • format (Symbol)

    Format type (:xml, :html, :html4, :html5, :json, :yaml, :simple)

  • options (Hash)

    Formatting options

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/canon/diff_formatter/by_line/base_formatter.rb', line 21

def self.for_format(format, **options)
  case format
  when :xml
    require_relative "xml_formatter"
    XmlFormatter.new(**options)
  when :html, :html4, :html5
    require_relative "html_formatter"
    # Determine HTML version from format
    version = case format
              when :html5 then :html5
              when :html4 then :html4
              else :html4 # default to html4
              end
    HtmlFormatter.new(html_version: version, **options)
  when :json
    require_relative "json_formatter"
    JsonFormatter.new(**options)
  when :yaml
    require_relative "yaml_formatter"
    YamlFormatter.new(**options)
  else
    require_relative "simple_formatter"
    SimpleFormatter.new(**options)
  end
end

Instance Method Details

#format(doc1, doc2) ⇒ String

Format line-by-line diff Subclasses must implement this method

Parameters:

  • doc1 (String)

    First document

  • doc2 (String)

    Second document

Returns:

  • (String)

    Formatted diff

Raises:

  • (NotImplementedError)


64
65
66
67
# File 'lib/canon/diff_formatter/by_line/base_formatter.rb', line 64

def format(doc1, doc2)
  raise NotImplementedError,
        "Subclasses must implement the format method"
end