mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-03-29 10:16:52 +00:00
Post-review fixes
This commit is contained in:
parent
c3c870c398
commit
cb23c48b0d
2 changed files with 16 additions and 17 deletions
|
|
@ -3,26 +3,25 @@ from __future__ import print_function
|
|||
import argparse
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
from pre_commit_hooks.util import cmd_output
|
||||
|
||||
|
||||
def _fix_file(filename, markdown=False):
|
||||
with tempfile.NamedTemporaryFile(mode='w+b', delete=False) as tmp_file:
|
||||
with open(filename, mode='rb') as original_file:
|
||||
for line in original_file.readlines():
|
||||
# preserve trailing two-space for non-blank lines in markdown files
|
||||
eol = b'\r\n' if line[-2:] == b'\r\n' else b'\n'
|
||||
if markdown and (not line.isspace()) and line.endswith(b' ' + eol):
|
||||
line = line.rstrip(b' \r\n') # restricted stripping: e.g. \t are not stripped
|
||||
# only preserve if there are no trailing tabs or unusual whitespace
|
||||
if not line[-1:].isspace():
|
||||
tmp_file.write(line + b' ' + eol)
|
||||
continue
|
||||
tmp_file.write(line.rstrip() + eol)
|
||||
os.remove(filename)
|
||||
os.rename(tmp_file.name, filename)
|
||||
def _fix_file(filename, is_markdown):
|
||||
with open(filename, mode='rb') as file_processed:
|
||||
lines = file_processed.readlines()
|
||||
lines = [_process_line(line, is_markdown) for line in lines]
|
||||
with open(filename, mode='wb') as file_processed:
|
||||
for line in lines:
|
||||
file_processed.write(line)
|
||||
|
||||
|
||||
def _process_line(line, is_markdown):
|
||||
# preserve trailing two-space for non-blank lines in markdown files
|
||||
eol = b'\r\n' if line[-2:] == b'\r\n' else b'\n'
|
||||
if is_markdown and (not line.isspace()) and line.endswith(b' ' + eol):
|
||||
return line.rstrip() + b' ' + eol
|
||||
return line.rstrip() + eol
|
||||
|
||||
|
||||
def fix_trailing_whitespace(argv=None):
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ def test_fixes_trailing_markdown_whitespace(filename, input_s, output, tmpdir):
|
|||
MD_TESTS_2 = (
|
||||
('foo.txt', 'foo \nbar \n \n', 'foo \nbar\n\n'),
|
||||
('bar.Markdown', 'bar \nbaz\t\n\t\n', 'bar \nbaz\n\n'),
|
||||
('bar.MD', 'bar \nbaz\t \n\t\n', 'bar \nbaz\n\n'),
|
||||
('bar.MD', 'bar \nbaz\t \n\t\n', 'bar \nbaz \n\n'),
|
||||
('.txt', 'baz \nquux \t\n\t\n', 'baz\nquux\n\n'),
|
||||
('txt', 'foo \nbaz \n\t\n', 'foo\nbaz\n\n'),
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue