class PrettyDiff::Diff

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

Attributes

lines[R]

Public Class Methods

new(one, two, options = {}) click to toggle source

runs the unix diff command and saves the output to instance varaiable lines

# File lib/pretty-diff.rb, line 53
def initialize one, two, options = {}

  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 options

  command = "diff -u #{one.to_s} #{two.to_s}"
  @lines = %x(#{command})
end

Public Instance Methods

to_html() click to toggle source

removes +‘s and -’s and wraps the changed lines in either ins or del html tags so it can be styles accordingly

# 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/,'&nbsp;')}</#{tag}>"
    end
    if @options[:fake_tab]
      line = link.gsub(/\t/, '&nbsp;' * @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() click to toggle source

simply returns lines

# File lib/pretty-diff.rb, line 71
def to_s
  @lines
end