Skip to content

Instantly share code, notes, and snippets.

@aycabta
Last active January 10, 2018 17:17
Show Gist options
  • Save aycabta/ecac4400f86985d4511e3d78cf3a85cf to your computer and use it in GitHub Desktop.
Save aycabta/ecac4400f86985d4511e3d78cf3a85cf to your computer and use it in GitHub Desktop.
# Reads unified diff file of CRuby documents between "before" and "after"
# from standard input and removes fixed line number changes
class MyRemover
def initialize
@lines = STDIN.readlines
@pos = 0
puts read_file_sections.flatten.join
end
def read_file_sections
results = []
file_section = []
until @lines.size <= @pos
if @lines[@pos] =~ /^diff -ru/
file_section << @lines[@pos]
file_section << @lines[@pos + 1]
file_section << @lines[@pos + 2]
@pos += 3
diff_sections = read_diff_sections
unless diff_sections.empty? or file_section.first =~ /\.js/
results << file_section
results << diff_sections
end
file_section = []
else
puts "WTF!?!?!?!??!"
puts @lines[@pos]
@pos += 1
end
end
results
end
def read_diff_sections
results = []
until @lines.size <= @pos
case @lines[@pos]
when /^@@.+@@$/
head = @lines[@pos]
@pos += 1
diff_result = read_diff_section(head)
results.push(diff_result) if diff_result
when /^diff -ru/
break
else
puts "WTF?"
puts @lines[@pos]
@pos += 1
end
end
results
end
def read_diff_section(head)
diff_section = []
until @lines.size <= @pos
if (@lines[@pos] =~ /^@@.+@@$/ or @lines[@pos] =~ /^diff -ru/)
break
end
diff_section << @lines[@pos]
@pos += 1
end
if diff_section.select { |l| l =~ /^[-+]/ }.all? { |l| l =~ %r{^[-+] *<pre><span class="ruby-comment"># File .+, line \d+</span>} }
nil
else
[head].concat(diff_section)
end
end
end
my_remover = MyRemover.new
my_remover.read_file_sections
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment