class Snaptoken::Diff::DiffFile

Attributes

file_contents[R]
filename[R]

Public Class Methods

new(filename) click to toggle source
Calls superclass method Snaptoken::Diff::DiffSection::new
# File lib/snaptoken/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/snaptoken/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/snaptoken/diff.rb, line 64
def new_file!; @new_file = true; end
new_file?() click to toggle source
# File lib/snaptoken/diff.rb, line 65
def new_file?; @new_file; end
omit_adjacent_removals!() click to toggle source
# File lib/snaptoken/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(config, step) click to toggle source
# File lib/snaptoken/diff.rb, line 99
def to_html(config, step)
  formatter = Rouge::Formatters::HTML.new
  formatter = HTMLLineByLine.new(formatter)

  lexer = Rouge::Lexer.guess(filename: @filename, source: @file_contents)
  code_hl = formatter.format(lexer.lex(@file_contents)).lines.each(&:chomp!)

  html = ""
  html << "<div class=\"diff\">\n"
  html << "<div class=\"diff-header\">\n"
  html << "  <div class=\"step-filename\"><a href=\"https://github.com/snaptoken/#{config[:name]}-src/blob/#{step.name}/#{@filename}\">#{@filename}</a></div>\n"
  html << "  <div class=\"step-number\">Step #{step.number}</div>\n"
  html << "  <div class=\"step-name\"><a href=\"https://github.com/snaptoken/#{config[:name]}-src/tree/#{step.name}\">#{step.name}</a></div>\n"
  html << "</div>"
  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(" &hellip; ").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"

  unless step.data.empty?
    html << "<div class=\"diff-footer\">\n"
    step.data.each do |tag|
      html << "  <div class=\"diff-tag-#{tag}\">#{config[:tags][tag.to_sym]}</div>\n"
    end
    html << "</div>\n"
  end

  html << "</div>\n"

  html
end