Skip to content

Instantly share code, notes, and snippets.

@mp4096
Created April 15, 2016 15:52
Show Gist options
  • Save mp4096/c905e85bddb57e95c5d62bc9b058fe97 to your computer and use it in GitHub Desktop.
Save mp4096/c905e85bddb57e95c5d62bc9b058fe97 to your computer and use it in GitHub Desktop.
Strip trailing whitespaces and replace hard tabs
import codecs
import os
import fnmatch
def delete_if_exists(filename):
if os.path.exists(filename):
os.remove(filename)
def find_files(root_folder, filter_pattern):
for root, dirs, files in os.walk(root_folder):
for basename in files:
if fnmatch.fnmatch(basename, filter_pattern):
filename = os.path.join(root, basename)
yield filename
def normalize_line(line):
soft_tab_length = 2
return line.rstrip().replace("\t", " "*soft_tab_length)
def normalize_single_file(filename):
preferred_linesep = "\n"
filename_bak = os.path.splitext(filename)[0] + ".bak"
delete_if_exists(filename_bak)
os.rename(filename, filename_bak)
with codecs.open(filename_bak, "r", encoding="utf-8") as f_in, \
codecs.open(filename, "w", encoding="utf-8") as f_out:
for line in f_in:
normalized_line = normalize_line(line)
f_out.write(normalized_line)
f_out.write(preferred_linesep)
def normalize_in_all_subfolders(root_folder, filter_pattern):
for filename in find_files(root_folder, filter_pattern):
print("Processing '{:s}'...".format(filename))
normalize_single_file(filename)
normalize_in_all_subfolders(".", "*.tex")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment