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.
57 58 59 60 61 |
# File 'lib/snaptoken/diff.rb', line 57 def initialize(filename) super(:file) @filename = filename @file_contents = "" end |
Instance Attribute Details
#file_contents ⇒ Object (readonly)
Returns the value of attribute file_contents.
55 56 57 |
# File 'lib/snaptoken/diff.rb', line 55 def file_contents @file_contents end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
55 56 57 |
# File 'lib/snaptoken/diff.rb', line 55 def filename @filename end |
Instance Method Details
#append_line(line) ⇒ Object
63 64 65 66 |
# File 'lib/snaptoken/diff.rb', line 63 def append_line(line) @file_contents << line @file_contents << "\n" unless line.end_with? "\n" end |
#new_file! ⇒ Object
68 |
# File 'lib/snaptoken/diff.rb', line 68 def new_file!; @new_file = true; end |
#new_file? ⇒ Boolean
69 |
# File 'lib/snaptoken/diff.rb', line 69 def new_file?; @new_file; end |
#omit_adjacent_removals! ⇒ Object
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 99 100 101 |
# File 'lib/snaptoken/diff.rb', line 71 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
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 132 133 134 |
# File 'lib/snaptoken/diff.rb', line 103 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}/#{@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 |