class Leg::Diff::DiffFile
Attributes
file_contents[R]
filename[R]
Public Class Methods
new(filename)
click to toggle source
Calls superclass method
Leg::Diff::DiffSection::new
# File lib/leg/diff.rb, line 53 def initialize(filename) super(:file) @filename = filename @file_contents = "" end
Public Instance Methods
append_line(line)
click to toggle source
# File lib/leg/diff.rb, line 59 def append_line(line) @file_contents << line @file_contents << "\n" unless line.end_with? "\n" end
new_file!()
click to toggle source
# File lib/leg/diff.rb, line 64 def new_file!; @new_file = true; end
new_file?()
click to toggle source
# File lib/leg/diff.rb, line 65 def new_file?; @new_file; end
omit_adjacent_removals!()
click to toggle source
# File lib/leg/diff.rb, line 67 def omit_adjacent_removals! change_chain = [] to_render = @contents.dup until to_render.empty? cur = to_render.shift if cur.is_a? DiffSection if cur.dirty? to_render = cur.contents + to_render else [change_chain.first, change_chain.last].compact.each do |line| line.type = :nochange if line.empty? end change_chain = [] end else if cur.type == :nochange [change_chain.first, change_chain.last].compact.each do |line| line.type = :nochange if line.empty? end change_chain = [] else change_chain << cur if cur.type == :add change_chain.each { |c| c.omit! if c.type == :remove } elsif cur.type == :remove cur.omit! if change_chain.any? { |c| c.type == :add } end end end end end
to_html()
click to toggle source
# File lib/leg/diff.rb, line 99 def to_html formatter = Rouge::Formatters::HTML.new formatter = HTMLLineByLine.new(formatter) lexer = Rouge::Lexer.guess(filename: @filename) code_hl = formatter.format(lexer.lex(@file_contents)).lines.each(&:chomp!) html = "" html << "<div class=\"diff\">\n" html << "<div class=\"filename\">#{@filename}</div>\n" html << "<pre class=\"highlight\"><code>" to_render = @contents.dup until to_render.empty? cur = to_render.shift if cur.is_a? DiffSection if cur.dirty? to_render = cur.contents + to_render else summary = cur.lines.map { |n| code_hl[n] }.join(" ... ").gsub("\n", "") html << "<div class=\"line folded\">#{summary}</div>" end elsif !cur.omit? tag = {nochange: :div, add: :ins, remove: :del}[cur.type] tag = :div if new_file? html << "<#{tag} class=\"line\">#{code_hl[cur.line]}</#{tag}>" end end html << "</code></pre>\n</div>\n" html end