|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# This is free and unencumbered software released into the public |
| 4 | +# domain. |
| 5 | + |
| 6 | +# Anyone is free to copy, modify, publish, use, compile, sell, or |
| 7 | +# distribute this software, either in source code form or as a |
| 8 | +# compiled binary, for any purpose, commercial or non-commercial, and |
| 9 | +# by any means. |
| 10 | + |
| 11 | +# In jurisdictions that recognize copyright laws, the author or |
| 12 | +# authors of this software dedicate any and all copyright interest in |
| 13 | +# the software to the public domain. We make this dedication for the |
| 14 | +# benefit of the public at large and to the detriment of our heirs |
| 15 | +# and successors. We intend this dedication to be an overt act of |
| 16 | +# relinquishment in perpetuity of all present and future rights to |
| 17 | +# this software under copyright law. |
| 18 | + |
| 19 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 | +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
| 23 | +# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 24 | +# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 25 | +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 | + |
| 27 | +# For more information, please refer to <http://unlicense.org> |
| 28 | + |
| 29 | +"""Strip trailing whitespace from markdown files.""" |
| 30 | + |
| 31 | +import common |
| 32 | + |
| 33 | + |
| 34 | +def needs_stripping(file): |
| 35 | + with open(file, 'r') as f: |
| 36 | + for line in f: |
| 37 | + line = line.rstrip('\n') |
| 38 | + if line != line.rstrip(): |
| 39 | + # contains trailing whitespace other than '\n' |
| 40 | + print(repr(line)) |
| 41 | + return True |
| 42 | + return False |
| 43 | + |
| 44 | + |
| 45 | +def strip(file): |
| 46 | + lines = [] |
| 47 | + with open(file, 'r') as f: |
| 48 | + for line in f: |
| 49 | + lines.append(line.rstrip()) |
| 50 | + with open(file, 'w') as f: |
| 51 | + for line in lines: |
| 52 | + print(line, file=f) |
| 53 | + |
| 54 | + |
| 55 | +def main(): |
| 56 | + print("Stripping trailing whitespace...") |
| 57 | + for file in common.get_markdown_files(): |
| 58 | + if needs_stripping(file): |
| 59 | + strip(file) |
| 60 | + print(" Was stripped:", file) |
| 61 | + else: |
| 62 | + print(" No trailing whitespace:", file) |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == '__main__': |
| 66 | + main() |
0 commit comments