module Precious module Views class Compare < Layout attr_reader :page, :diff, :versions def title "Comparison of #{@page.title}" end def path @page.path end def before @versions[0][0..6] end def after @versions[1][0..6] end def lines lines = [] @diff.diff.split("\n")[2..-1].each_with_index do |line, line_index| lines << { :line => line, :class => line_class(line), :ldln => left_diff_line_number(0, line), :rdln => right_diff_line_number(0, line) } end lines end # private def line_class(line) if line =~ /^@@/ 'gc' elsif line =~ /^\+/ 'gi' elsif line =~ /^\-/ 'gd' else '' end end @left_diff_line_number = nil def left_diff_line_number(id, line) if line =~ /^@@/ m, li = *line.match(/\-(\d+)/) @left_diff_line_number = li.to_i @current_line_number = @left_diff_line_number ret = '...' elsif line[0] == ?- ret = @left_diff_line_number.to_s @left_diff_line_number += 1 @current_line_number = @left_diff_line_number - 1 elsif line[0] == ?+ ret = ' ' else ret = @left_diff_line_number.to_s @left_diff_line_number += 1 @current_line_number = @left_diff_line_number - 1 end ret end @right_diff_line_number = nil def right_diff_line_number(id, line) if line =~ /^@@/ m, ri = *line.match(/\+(\d+)/) @right_diff_line_number = ri.to_i @current_line_number = @right_diff_line_number ret = '...' elsif line[0] == ?- ret = ' ' elsif line[0] == ?+ ret = @right_diff_line_number.to_s @right_diff_line_number += 1 @current_line_number = @right_diff_line_number - 1 else ret = @right_diff_line_number.to_s @right_diff_line_number += 1 @current_line_number = @right_diff_line_number - 1 end ret end end end end