Skip to content

Instantly share code, notes, and snippets.

@CTimmerman
Last active November 29, 2021 12:19
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 CTimmerman/fd8a42cc6548cb92c0ce923c3293e1a0 to your computer and use it in GitHub Desktop.
Save CTimmerman/fd8a42cc6548cb92c0ce923c3293e1a0 to your computer and use it in GitHub Desktop.
Simple code cleaner. Use Git to check the non-whitespace edits such as <>.
# entire file, CLI code, inplace
perl -0777pe "
# whitespace
s/ /\t/g; # Tabs indent at a uniform and viewer-custom size.
s/\t +/\t\t/g; # Do not mix tabs and spaces.
s/ / /g;
s/\r\n/\n/g; # Nor line endings.
s/(catch|for|foreach|function|if|switch|while)\s+\(/\1\(/g; # Compact flow.
s/\}\s+el(if|se)/\}el\1/g;
s/\s+\{/\{/g; # Useless when indenting.
s/([,;])([^ \n'\"])/\1 \2/g; # Read and wrap better.
s/{(\d+), (\d+)}/{\1,\2}/g; # Fix regex ranges.
s/\s*([!<=>.+-]+=>*)\s*/ \1 /g; # Read and wrap != etc.
s/\s*([!<=>.+-]=+>*)([^\\ \t\n'\"=>&])\s*/ \1 \2/g;
s/\s*([^= -])(<|>)\\\$\s*/ \1\2 \\\$/g;
#s/ (class|href|language|style|type) = / \1=/g; # Should be in templates.
s/[\t ]+\n/\n/g; # No trailing whitespace.
s/\n\n\n+/\n\n/g; # One empty line is enough. Use functions or comments to separate more.
# standards
s/\s*<>\s*/ != /g; # Not using BASIC anymore.
" -i $1
@CTimmerman
Copy link
Author

GitHub failed to find "perl" in this gist, so maybe this comment helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment