Class: PrettyDiff::Diff
- Inherits:
-
Object
- Object
- PrettyDiff::Diff
- Defined in:
- lib/pretty-diff.rb
Overview
This class does the actual work of running the diff command and has instance methods to turn the results of diff into html, or a string
Instance Attribute Summary collapse
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
Instance Method Summary collapse
-
#initialize(one, two, options = {}) ⇒ Diff
constructor
runs the unix diff command and saves the output to instance varaiable lines.
-
#to_html ⇒ Object
removes +‘s and -’s and wraps the changed lines in either ins or del html tags so it can be styles accordingly.
-
#to_s ⇒ Object
simply returns lines.
Constructor Details
#initialize(one, two, options = {}) ⇒ Diff
runs the unix diff command and saves the output to instance varaiable lines
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/pretty-diff.rb', line 53 def initialize one, two, = {} defaults = { :remove_signs => false, :remove_leading_file_lines => false, :as_list_items => true, :list_style => 'ul', :no_newline_warning => false, :fake_tab => 4 } @options = defaults.merge command = "diff -u #{one.to_s} #{two.to_s}" @lines = %x(#{command}) end |
Instance Attribute Details
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
49 50 51 |
# File 'lib/pretty-diff.rb', line 49 def lines @lines end |
Instance Method Details
#to_html ⇒ Object
removes +‘s and -’s and wraps the changed lines in either ins or del html tags so it can be styles accordingly
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 |
# File 'lib/pretty-diff.rb', line 77 def to_html lines = @lines.each_line.map do |line| line.chomp! unless @options[:no_newline_warning] next if line == '\ No newline at end of file' end if @options[:remove_leading_file_lines] if line =~ /\A[\+|-]{3}/ next end end if line !~ /\A[\+|-]{3}\s/ && line =~ /\A(\+|-)/ tag = $~[0] == '-' ? 'del' : 'ins' line = line.gsub(/\A./, '') if @options[:remove_signs] line = "<#{tag}>#{line.gsub(/\s/,' ')}</#{tag}>" end if @options[:fake_tab] line = link.gsub(/\t/, ' ' * @options[:fake_tab]) end if @options[:as_list_items] line = "<li#{ " class=\"#{tag}\"" if tag }>#{line}</li>" end end.join("\n") if @options[:list_style] lines = "<#{@options[:list_style]} class=\"pretty-diff\">\n#{lines}\n</#{@options[:list_style]}>" end return lines end |
#to_s ⇒ Object
simply returns lines
71 72 73 |
# File 'lib/pretty-diff.rb', line 71 def to_s @lines end |