Skip to content

Instantly share code, notes, and snippets.

@nixpulvis
Created January 5, 2013 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nixpulvis/4459958 to your computer and use it in GitHub Desktop.
Save nixpulvis/4459958 to your computer and use it in GitHub Desktop.
normalize my code files.
#!/usr/bin/env ruby
# Detrail.rb (remove all the damn trailing whitespace)
# because god hates people that leave trailing whitespace,
# don't use \n and use \t
#
# Usage:
# detrail (will assume current directory)
# detrail path/to/folder or/directory
#
class Detrailer
def initialize(paths)
paths = ["."] if paths.empty?
@files = []
paths.each do |path|
if File.directory? path
@files += Dir[path + "/**/*"].select { |p| File.file? p }
else
@files << path
end
end
end
def valid_paths?
@files.inject(true) { |m, e| m && File.exists?(e) }
end
def detrail
@files.each.with_object([]) do |path, obj|
begin
contents = File.read(path)
contents.gsub!(/\r\n?/, "\n") # normalize newline char
contents.gsub!(/\t/, ' ') # convert tabs to spaces (2)
contents.gsub!(/ +$/, '') # remove trailing spaces
File.open(path, "w") { |f| f.write contents }
obj << path
rescue ArgumentError
puts "skipping #{path}"
end
end
end
end
detrailed = Detrailer.new($*).detrail
puts "\nRemoved trailing whitespace from:"
puts detrailed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment