Skip to content

Instantly share code, notes, and snippets.

@kgaughan
Last active August 29, 2015 13:57
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 kgaughan/9368331 to your computer and use it in GitHub Desktop.
Save kgaughan/9368331 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Cleans up basic whitespace problems in .rst and .py files. Run before
# commits.
#
# Tabs to spaces.
for i in `fgrep -rl $'\t' --include=\*.py --include=\*.rst .`; do
if test -f $i; then
tmp=`mktemp`
expand --tabs=4 $i >$tmp
mv $tmp $i
fi
done
# Spaces to tabs
for i in `fgrep -rl $'\t' --include=\*.php --include=\*.html --include=\*.js --include=\*.css --include=\*.mst .`; do
if test -f $i; then
tmp=`mktemp`
unexpand --first-only --tabs=4 $i >$tmp
mv $tmp $i
fi
done
# Trailing whitespace
for i in `egrep -rl '[[:space:]]$' --include=\*.py --include=\*.php --include=\*.rst --include=\*.js --include=\*.css --include=\*.html --include=\*.mst .`; do
if test -f $i; then
sed -i 's/\s\+$//g' $i 2>/dev/null
fi
done
# Trailing and leading lines
for ext in py rst php js html css mst; do
find . -name \*.$ext -type f -exec sed -i -e :a -e '/./,$!d;/^\n*$/{$d;N;};/\n$/ba' {} \;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment