# encoding: utf-8
#--
# Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see .
#++
module DiffHelper
# Takes a unified diff as input and renders it as html
def render_diff(udiff, display_mode = "inline")
return if udiff.blank?
udiff = force_utf8(udiff)
if sidebyside_diff?
render_sidebyside_diff(udiff)
else
render_inline_diff(udiff)
end
end
def sidebyside_diff?
@diffmode == "sidebyside"
end
def render_diffs(diffs)
if sidebyside_diff?
diffs.map do |file|
out = %Q{}
out << "
"
out << link_to(h(file.a_path), file_path(@repository, file.a_path, @commit.id))
out << "
"
out << force_utf8(render_diff(file.diff))
out
end.join("\n")
else
'' +
render_inline_diffs_controls("commits") +
render_inline_diffs_with_stats(diffs, :open) + "
"
end
end
# Supply a block that fetches an array of comments with the file path as parameter
def render_inline_diffs_with_stats(file_diffs, state = :closed)
file_diffs.map do |file|
diff_renderer = Diff::Display::Unified.new(file.diff)
out = %Q{}
out << %Q{"
out << %Q{
}
if file.diff[0..256].include?("\000")
out << "Binary files differ"
else
diff_options = {}
diff_options[:comments] = if block_given?
yield(file)
end
out << force_utf8(render_inline_diff(file.diff, diff_renderer, diff_options))
end
out << "
"
out
end.join("\n")
end
def render_inline_diffs_controls(cookie_prefix)
%Q{}
end
def render_inline_diff(udiff, differ = nil, options = {})
differ ||= Diff::Display::Unified.new(udiff)
out = %Q{\n}
out << "\n"
out << %Q{}
out << %Q{ | }
out << %Q{ | }
out << "  |
\n"
out << "\n"
if comments = options[:comments]
render_callback = Gitorious::Diff::InlineTableCallback.with_comments(comments, self)
out << differ.render(render_callback)
else
out << differ.render(Gitorious::Diff::InlineTableCallback.new)
end
out << "
"
out
end
def render_sidebyside_diff(udiff)
differ = Diff::Display::Unified.new(udiff)
out = %Q{\n}
out << %Q{}
out << %Q{}
out << %Q{ | | }
out << %Q{ | | }
out << differ.render(Gitorious::Diff::SidebysideTableCallback.new)
out << "
"
out
end
def render_diffmode_selector
out = %Q{}
out << %Q{}
if @diffmode == "sidebyside"
out << %Q{- inline
}
out << %Q{- side by side
}
else
out << %Q{- inline
}
out << %Q{- side by side
}
end
out << "
"
out
end
def render_diff_stats(stats)
out = %Q{\n}
stats.files.each do |filename, adds, deletes, total|
out << %Q{- #{h(filename)} #{total} }
out << %Q{#{(0...deletes).map{|i| "-" }.join}}
out << %Q{#{(0...adds).map{|i| "+" }.join}}
out << %Q{
}
end
out << "
\n"
out
end
def render_compact_diff_stats(stats)
%Q{(#{stats[:additions].to_s} / } +
%Q{#{stats[:deletions].to_s})}
end
end