Class: PrettyDiff::Diff

Inherits:
Object
  • Object
show all
Defined in:
lib/pretty_diff/diff.rb

Constant Summary collapse

CHUNK_REGEXP =
/^@@ .+? @@.*\n?$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unified_diff, options = {}) ⇒ Diff

Create new Diff object. Accept a String in unified diff format and options hash. Currrent options:

  • generator – your own custom implementation of HTML generator. Will use BasicGenerator by default.

  • out_encoding – convert encoding of diffs to the specififed encoding. utf-8 by default.


25
26
27
28
29
30
# File 'lib/pretty_diff/diff.rb', line 25

def initialize(unified_diff, options={})
  @unified_diff = unified_diff
  @generator = validate_generator(options[:generator] || BasicGenerator)
  @out_encoding = options[:out_encoding] || 'utf-8'
  @options = options
end

Instance Attribute Details

#generatorObject (readonly)

Returns the value of attribute generator


16
17
18
# File 'lib/pretty_diff/diff.rb', line 16

def generator
  @generator
end

#optionsObject (readonly)

Returns the value of attribute options


16
17
18
# File 'lib/pretty_diff/diff.rb', line 16

def options
  @options
end

#out_encodingObject (readonly)

Returns the value of attribute out_encoding


16
17
18
# File 'lib/pretty_diff/diff.rb', line 16

def out_encoding
  @out_encoding
end

#unified_diffObject (readonly)

Returns the value of attribute unified_diff


16
17
18
# File 'lib/pretty_diff/diff.rb', line 16

def unified_diff
  @unified_diff
end

Instance Method Details

#chunksObject


54
55
56
# File 'lib/pretty_diff/diff.rb', line 54

def chunks
  @_chunks ||= find_chunks
end

#contentsObject


43
44
45
46
47
48
# File 'lib/pretty_diff/diff.rb', line 43

def contents
  # We have to strip metadata from the rest of the diff
  # to enforce encoding. It's not uncommon for metadata to be in Unicode
  # while the diff itself is in some other encoding.
  @_contents ||= enforce_encoding(unified_diff.lines[.lines.size..-1].join(''))
end

#metadataObject


32
33
34
35
36
37
38
39
40
41
# File 'lib/pretty_diff/diff.rb', line 32

def 
  @_metadata ||= begin
    ''.tap do |result|
      unified_diff.each_line do |l|
        result << l
        break if l =~ /\+\+\+\s*/
      end
    end
  end
end

#to_htmlObject


50
51
52
# File 'lib/pretty_diff/diff.rb', line 50

def to_html
  generator.new(self).generate
end