Class: PrettyDiff::Diff
- Inherits:
-
Object
- Object
- PrettyDiff::Diff
- Defined in:
- lib/pretty_diff/diff.rb
Constant Summary collapse
- CHUNK_REGEXP =
/^@@ .+? @@.*\n?$/
Instance Attribute Summary collapse
-
#generator ⇒ Object
readonly
Returns the value of attribute generator.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#out_encoding ⇒ Object
readonly
Returns the value of attribute out_encoding.
-
#unified_diff ⇒ Object
readonly
Returns the value of attribute unified_diff.
Instance Method Summary collapse
- #chunks ⇒ Object
- #contents ⇒ Object
-
#initialize(unified_diff, options = {}) ⇒ Diff
constructor
Create new Diff object.
- #metadata ⇒ Object
- #to_html ⇒ Object
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, ={}) @unified_diff = unified_diff @generator = validate_generator([:generator] || BasicGenerator) @out_encoding = [:out_encoding] || 'utf-8' @options = end |
Instance Attribute Details
#generator ⇒ Object (readonly)
Returns the value of attribute generator.
16 17 18 |
# File 'lib/pretty_diff/diff.rb', line 16 def generator @generator end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
16 17 18 |
# File 'lib/pretty_diff/diff.rb', line 16 def @options end |
#out_encoding ⇒ Object (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_diff ⇒ Object (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
#chunks ⇒ Object
54 55 56 |
# File 'lib/pretty_diff/diff.rb', line 54 def chunks @_chunks ||= find_chunks end |
#contents ⇒ Object
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 |
#metadata ⇒ Object
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_html ⇒ Object
50 51 52 |
# File 'lib/pretty_diff/diff.rb', line 50 def to_html generator.new(self).generate end |