Created
January 5, 2013 05:34
-
-
Save nixpulvis/4459958 to your computer and use it in GitHub Desktop.
normalize my code files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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