\n"
str += "
#{PrettyPatch.linkifyFilename(@filename)}
\n"
if @image then
str += self.image_to_html
elsif @git_image then
if @image_error
str += @image_error
else
for i in (0...2)
image_url = @image_urls[i]
image_checksum = @image_checksums[i]
style = ["remove", "add"][i]
str += "
"
if image_checksum
str += image_checksum + "
"
end
if image_url
str += "
"
else
str += ["
Added", "Removed"][i]
end
end
end
elsif @binary then
$last_prettify_part_count["binary"] += 1
str += "
Binary file, nothing to see here"
else
str += @sections.collect{ |section| section.to_html }.join("
\n") unless @sections.nil?
end
if @from then
str += "
" + @from + ""
end
str += "
\n"
end
def self.parse(string)
haveSeenDiffHeader = false
linesForDiffs = []
string.each_line do |line|
if (PrettyPatch.diff_header?(line))
linesForDiffs << []
haveSeenDiffHeader = true
elsif (!haveSeenDiffHeader && line =~ /^--- /)
linesForDiffs << []
haveSeenDiffHeader = false
end
linesForDiffs.last << line unless linesForDiffs.last.nil?
end
linesForDiffs.collect { |lines| FileDiff.new(lines) }
end
def self.read_checksum_from_png(png_bytes)
match = png_bytes && png_bytes.match(/tEXtchecksum\0([a-fA-F0-9]{32})/)
match ? match[1] : nil
end
def self.git_new_file_binary_patch(filename, encoded_chunk, git_index)
return <\n"
end
end
class DiffBlockPart
attr_reader :className
attr :lines
def initialize(className, container)
$last_prettify_part_count[className] += 1
@className = className
@lines = []
container.parts << self
end
def to_html
str = "\n" % @className
str += @lines.collect{ |line| line.to_html }.join
# Don't put white-space after this so adjacent inline-block DiffBlockParts will not wrap.
str += "
"
end
end
class DiffSection
def initialize(lines)
lines.length >= 1 or raise "DiffSection.parse only received %d lines" % lines.length
matches = START_OF_SECTION_FORMAT.match(lines[0])
if matches
from, to = [matches[1].to_i, matches[3].to_i]
if matches[2] and matches[4]
from_end = from + matches[2].to_i
to_end = to + matches[4].to_i
end
end
@blocks = []
diff_block = nil
diff_block_part = nil
for line in lines[1...lines.length]
startOfLine = line =~ /^[-\+ ]/ ? 1 : 0
text = line[startOfLine...line.length].chomp
case line[0]
when ?-
if (diff_block_part.nil? or diff_block_part.className != 'remove')
diff_block = DiffBlock.new(@blocks)
diff_block_part = DiffBlockPart.new('remove', diff_block)
end
diff_block_part.lines << CodeLine.new(from, nil, text)
from += 1 unless from.nil?
when ?+
if (diff_block_part.nil? or diff_block_part.className != 'add')
# Put add lines that immediately follow remove lines into the same DiffBlock.
if (diff_block.nil? or diff_block_part.className != 'remove')
diff_block = DiffBlock.new(@blocks)
end
diff_block_part = DiffBlockPart.new('add', diff_block)
end
diff_block_part.lines << CodeLine.new(nil, to, text)
to += 1 unless to.nil?
else
if (diff_block_part.nil? or diff_block_part.className != 'shared')
diff_block = DiffBlock.new(@blocks)
diff_block_part = DiffBlockPart.new('shared', diff_block)
end
diff_block_part.lines << CodeLine.new(from, to, text)
from += 1 unless from.nil?
to += 1 unless to.nil?
end
break if from_end and to_end and from == from_end and to == to_end
end
changes = [ [ [], [] ] ]
for block in @blocks
for block_part in block.parts
for line in block_part.lines
if (!line.fromLineNumber.nil? and !line.toLineNumber.nil?) then
changes << [ [], [] ]
next
end
changes.last.first << line if line.toLineNumber.nil?
changes.last.last << line if line.fromLineNumber.nil?
end
end
end
for change in changes
next unless change.first.length == change.last.length
for i in (0...change.first.length)
from_text = change.first[i].text
to_text = change.last[i].text
next if from_text.length > MAXIMUM_INTRALINE_DIFF_LINE_LENGTH or to_text.length > MAXIMUM_INTRALINE_DIFF_LINE_LENGTH
raw_operations = HTMLDiff::DiffBuilder.new(from_text, to_text).operations
operations = []
back = 0
raw_operations.each_with_index do |operation, j|
if operation.action == :equal and j < raw_operations.length - 1
length = operation.end_in_new - operation.start_in_new
if length < SMALLEST_EQUAL_OPERATION
back = length
next
end
end
operation.start_in_old -= back
operation.start_in_new -= back
back = 0
operations << operation
end
change.first[i].operations = operations
change.last[i].operations = operations
end
end
@blocks.unshift(ContextLine.new(matches[5])) unless matches.nil? || matches[5].empty?
end
def to_html
str = "\n"
str += @blocks.collect{ |block| block.to_html }.join
str += "
\n"
end
def self.parse(lines)
linesForSections = lines.inject([[]]) do |sections, line|
sections << [] if line =~ /^@@/
sections.last << line
sections
end
linesForSections.delete_if { |lines| lines.nil? or lines.empty? }
linesForSections.collect { |lines| DiffSection.new(lines) }
end
end
class Line
attr_reader :fromLineNumber
attr_reader :toLineNumber
attr_reader :text
def initialize(from, to, text)
@fromLineNumber = from
@toLineNumber = to
@text = text
end
def text_as_html
CGI.escapeHTML(text)
end
def classes
lineClasses = ["Line", "LineContainer"]
lineClasses << ["add"] unless @toLineNumber.nil? or !@fromLineNumber.nil?
lineClasses << ["remove"] unless @fromLineNumber.nil? or !@toLineNumber.nil?
lineClasses
end
def to_html
markedUpText = self.text_as_html
str = "\n" % self.classes.join(' ')
str += "%s%s" %
[@fromLineNumber.nil? ? ' ' : @fromLineNumber,
@toLineNumber.nil? ? ' ' : @toLineNumber] unless @fromLineNumber.nil? and @toLineNumber.nil?
str += "%s\n" % markedUpText
str += "
\n"
end
end
class CodeLine < Line
attr :operations, true
def text_as_html
html = []
tag = @fromLineNumber.nil? ? "ins" : "del"
if @operations.nil? or @operations.empty?
return CGI.escapeHTML(@text)
end
@operations.each do |operation|
start = @fromLineNumber.nil? ? operation.start_in_new : operation.start_in_old
eend = @fromLineNumber.nil? ? operation.end_in_new : operation.end_in_old
escaped_text = CGI.escapeHTML(@text[start...eend])
if eend - start === 0 or operation.action === :equal
html << escaped_text
else
html << "<#{tag}>#{escaped_text}#{tag}>"
end
end
html.join
end
end
class ContextLine < Line
def initialize(context)
super("@", "@", context)
end
def classes
super << "context"
end
end
end