Class: Snaptoken::Diff::DiffFile
- Inherits:
-
DiffSection
- Object
- DiffSection
- Snaptoken::Diff::DiffFile
- Defined in:
- lib/snaptoken/diff.rb
Instance Attribute Summary collapse
-
#file_contents ⇒ Object
readonly
Returns the value of attribute file_contents.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
Attributes inherited from DiffSection
Instance Method Summary collapse
- #append_line(line) ⇒ Object
-
#initialize(filename) ⇒ DiffFile
constructor
A new instance of DiffFile.
- #new_file! ⇒ Object
- #new_file? ⇒ Boolean
- #omit_adjacent_removals! ⇒ Object
- #to_html(project, step) ⇒ Object
Methods inherited from DiffSection
Constructor Details
#initialize(filename) ⇒ DiffFile
Returns a new instance of DiffFile.
54 55 56 57 58 |
# File 'lib/snaptoken/diff.rb', line 54 def initialize(filename) super(:file) @filename = filename @file_contents = "" end |
Instance Attribute Details
#file_contents ⇒ Object (readonly)
Returns the value of attribute file_contents.
52 53 54 |
# File 'lib/snaptoken/diff.rb', line 52 def file_contents @file_contents end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
52 53 54 |
# File 'lib/snaptoken/diff.rb', line 52 def filename @filename end |
Instance Method Details
#append_line(line) ⇒ Object
60 61 62 63 |
# File 'lib/snaptoken/diff.rb', line 60 def append_line(line) @file_contents << line @file_contents << "\n" unless line.end_with? "\n" end |
#new_file! ⇒ Object
65 |
# File 'lib/snaptoken/diff.rb', line 65 def new_file!; @new_file = true; end |
#new_file? ⇒ Boolean
66 |
# File 'lib/snaptoken/diff.rb', line 66 def new_file?; @new_file; end |
#omit_adjacent_removals! ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/snaptoken/diff.rb', line 68 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(project, step) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/snaptoken/diff.rb', line 100 def to_html(project, step) 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\"><a href=\"https://github.com/snaptoken/#{project}/blob/#{step.name}/#{@filename}\">#{@filename}</a></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 |