Skip to content

Commit

Permalink
Added: Strip whitespace from single file
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Apr 21, 2024
1 parent a75d7d1 commit 98f15ea
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
17 changes: 7 additions & 10 deletions strip_whitespace.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os
import sys

def strip_whitespace(file_path):
if file_path.endswith('.md'):
Expand All @@ -19,13 +19,10 @@ def strip_whitespace(file_path):

file.write(line)

def main():
git_root = os.popen('git rev-parse --show-toplevel').read().strip()
docs_dir = os.path.join(git_root, 'docs')
for dirpath, dirnames, filenames in os.walk(docs_dir):
for filename in filenames:
file_path = os.path.join(dirpath, filename)
strip_whitespace(file_path)

if __name__ == '__main__':
main()
if len(sys.argv) < 2:
print("Usage: python strip_whitespace.py <file_path>")
sys.exit(1)

file_path = sys.argv[1]
strip_whitespace(file_path)
31 changes: 31 additions & 0 deletions strip_whitespace_from_all_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os

def strip_whitespace(file_path):
if file_path.endswith('.md'):
with open(file_path, 'r') as file:
lines = file.readlines()

# Check if the last line ends with a newline
has_newline_at_end = lines and lines[-1].endswith('\n')

with open(file_path, 'w') as file:
for i, line in enumerate(lines):
# Remove trailing spaces
line = line.rstrip()

# Add newline to all lines except the last one if it didn't originally have a newline
if i < len(lines) - 1 or has_newline_at_end:
line += '\n'

file.write(line)

def main():
git_root = os.popen('git rev-parse --show-toplevel').read().strip()
docs_dir = os.path.join(git_root, 'docs')
for dirpath, dirnames, filenames in os.walk(docs_dir):
for filename in filenames:
file_path = os.path.join(dirpath, filename)
strip_whitespace(file_path)

if __name__ == '__main__':
main()

0 comments on commit 98f15ea

Please sign in to comment.