2022-01-16 08:24:05 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2014-03-15 06:18:29 +08:00
|
|
|
import argparse
|
2015-05-10 16:00:54 +08:00
|
|
|
import os
|
2024-10-12 07:30:07 +08:00
|
|
|
from collections.abc import Sequence
|
2015-01-05 08:05:20 +08:00
|
|
|
|
2014-03-23 09:21:54 +08:00
|
|
|
|
2020-02-06 03:10:42 +08:00
|
|
|
def _fix_file(
|
|
|
|
filename: str,
|
|
|
|
is_markdown: bool,
|
2022-01-16 08:24:05 +08:00
|
|
|
chars: bytes | None,
|
2020-02-06 03:10:42 +08:00
|
|
|
) -> bool:
|
2016-08-31 09:02:33 +08:00
|
|
|
with open(filename, mode='rb') as file_processed:
|
|
|
|
lines = file_processed.readlines()
|
2019-10-26 00:20:04 +08:00
|
|
|
newlines = [_process_line(line, is_markdown, chars) for line in lines]
|
2017-02-08 01:36:39 +08:00
|
|
|
if newlines != lines:
|
|
|
|
with open(filename, mode='wb') as file_processed:
|
|
|
|
for line in newlines:
|
|
|
|
file_processed.write(line)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2016-08-31 09:02:33 +08:00
|
|
|
|
|
|
|
|
2020-02-06 03:10:42 +08:00
|
|
|
def _process_line(
|
|
|
|
line: bytes,
|
|
|
|
is_markdown: bool,
|
2022-01-16 08:24:05 +08:00
|
|
|
chars: bytes | None,
|
2020-02-06 03:10:42 +08:00
|
|
|
) -> bytes:
|
2018-03-01 00:43:07 +08:00
|
|
|
if line[-2:] == b'\r\n':
|
|
|
|
eol = b'\r\n'
|
2019-10-25 23:34:26 +08:00
|
|
|
line = line[:-2]
|
2018-03-01 00:43:07 +08:00
|
|
|
elif line[-1:] == b'\n':
|
|
|
|
eol = b'\n'
|
2019-10-25 23:34:26 +08:00
|
|
|
line = line[:-1]
|
2018-03-01 00:43:07 +08:00
|
|
|
else:
|
|
|
|
eol = b''
|
2016-08-31 09:02:33 +08:00
|
|
|
# preserve trailing two-space for non-blank lines in markdown files
|
2019-10-25 23:34:26 +08:00
|
|
|
if is_markdown and (not line.isspace()) and line.endswith(b' '):
|
2019-10-26 00:20:04 +08:00
|
|
|
return line[:-2].rstrip(chars) + b' ' + eol
|
|
|
|
return line.rstrip(chars) + eol
|
2014-12-24 04:04:19 +08:00
|
|
|
|
|
|
|
|
2022-01-16 08:24:05 +08:00
|
|
|
def main(argv: Sequence[str] | None = None) -> int:
|
2014-03-15 06:18:29 +08:00
|
|
|
parser = argparse.ArgumentParser()
|
2015-05-10 16:00:54 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'--no-markdown-linebreak-ext',
|
2018-10-13 09:10:02 +08:00
|
|
|
action='store_true',
|
|
|
|
help=argparse.SUPPRESS,
|
2015-05-10 16:00:54 +08:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--markdown-linebreak-ext',
|
|
|
|
action='append',
|
2018-10-13 09:10:02 +08:00
|
|
|
default=[],
|
2015-05-10 16:00:54 +08:00
|
|
|
metavar='*|EXT[,EXT,...]',
|
2018-10-13 09:10:02 +08:00
|
|
|
help=(
|
|
|
|
'Markdown extensions (or *) to not strip linebreak spaces. '
|
|
|
|
'default: %(default)s'
|
|
|
|
),
|
2015-05-10 16:00:54 +08:00
|
|
|
)
|
2019-10-25 23:12:49 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'--chars',
|
2019-10-26 00:20:04 +08:00
|
|
|
help=(
|
|
|
|
'The set of characters to strip from the end of lines. '
|
|
|
|
'Defaults to all whitespace characters.'
|
|
|
|
),
|
2019-10-25 23:12:49 +08:00
|
|
|
)
|
2014-03-15 06:18:29 +08:00
|
|
|
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
|
|
|
|
args = parser.parse_args(argv)
|
|
|
|
|
2018-10-13 09:10:02 +08:00
|
|
|
if args.no_markdown_linebreak_ext:
|
|
|
|
print('--no-markdown-linebreak-ext now does nothing!')
|
|
|
|
|
2015-05-10 16:00:54 +08:00
|
|
|
md_args = args.markdown_linebreak_ext
|
|
|
|
if '' in md_args:
|
|
|
|
parser.error('--markdown-linebreak-ext requires a non-empty argument')
|
|
|
|
all_markdown = '*' in md_args
|
2019-02-12 11:56:15 +08:00
|
|
|
# normalize extensions; split at ',', lowercase, and force 1 leading '.'
|
2017-02-08 01:36:39 +08:00
|
|
|
md_exts = [
|
|
|
|
'.' + x.lower().lstrip('.') for x in ','.join(md_args).split(',')
|
|
|
|
]
|
2015-05-10 16:00:54 +08:00
|
|
|
|
2019-02-12 11:56:15 +08:00
|
|
|
# reject probable "eaten" filename as extension: skip leading '.' with [1:]
|
2015-05-10 16:00:54 +08:00
|
|
|
for ext in md_exts:
|
|
|
|
if any(c in ext[1:] for c in r'./\:'):
|
|
|
|
parser.error(
|
2020-02-06 03:10:42 +08:00
|
|
|
f'bad --markdown-linebreak-ext extension '
|
|
|
|
f'{ext!r} (has . / \\ :)\n'
|
|
|
|
f" (probably filename; use '--markdown-linebreak-ext=EXT')",
|
2015-05-10 16:00:54 +08:00
|
|
|
)
|
2020-02-06 03:10:42 +08:00
|
|
|
chars = None if args.chars is None else args.chars.encode()
|
2016-08-18 22:39:06 +08:00
|
|
|
return_code = 0
|
2017-10-10 01:39:58 +08:00
|
|
|
for filename in args.filenames:
|
|
|
|
_, extension = os.path.splitext(filename.lower())
|
2017-02-08 01:36:39 +08:00
|
|
|
md = all_markdown or extension in md_exts
|
2019-10-26 00:20:04 +08:00
|
|
|
if _fix_file(filename, md, chars):
|
2020-02-06 03:10:42 +08:00
|
|
|
print(f'Fixing {filename}')
|
2017-02-08 01:36:39 +08:00
|
|
|
return_code = 1
|
2016-08-18 22:39:06 +08:00
|
|
|
return return_code
|
2014-03-15 06:18:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-10-24 01:23:50 +08:00
|
|
|
raise SystemExit(main())
|