cmd/nosmartquotes.py

76 lines
1.8 KiB
Python
Raw Normal View History

2021-08-17 07:42:00 +00:00
import argparse
2021-10-05 00:21:14 +00:00
import os
2021-08-17 07:42:00 +00:00
import sys
from voussoirkit import betterhelp
2021-10-05 00:21:14 +00:00
from voussoirkit import pathclass
2022-02-13 03:50:00 +00:00
from voussoirkit import pipeable
2021-08-17 07:42:00 +00:00
from voussoirkit import spinal
from voussoirkit import vlogging
log = vlogging.getLogger(__name__, 'nosmartquotes')
2021-10-05 00:21:14 +00:00
THIS_FILE = os.path.abspath(__file__)
2021-08-17 07:42:00 +00:00
def replace_smartquotes(text):
text = text.replace('', '"')
text = text.replace('', '"')
text = text.replace('', "'")
text = text.replace('', "'")
return text
def nosmartquotes_argparse(args):
2022-02-13 03:50:00 +00:00
globs = list(pipeable.input_many(args.patterns))
2021-08-17 07:42:00 +00:00
files = spinal.walk(
2022-02-13 03:50:00 +00:00
glob_filenames=globs,
2021-10-05 00:21:14 +00:00
exclude_filenames={THIS_FILE},
2021-08-17 07:42:00 +00:00
recurse=args.recurse,
)
for file in files:
2021-10-05 00:21:14 +00:00
text = file.read('r', encoding='utf-8')
2021-08-17 07:42:00 +00:00
original_text = text
text = replace_smartquotes(text)
if text == original_text:
continue
2021-10-05 00:21:14 +00:00
file.write('w', text, encoding='utf-8')
2022-02-13 03:50:00 +00:00
pipeable.stdout(file.absolute_path)
2021-08-17 07:42:00 +00:00
return 0
@vlogging.main_decorator
def main(argv):
2022-02-13 03:50:00 +00:00
parser = argparse.ArgumentParser(
description='''
Replace smart quotes and smart apostrophes with regular ASCII values.
2021-08-17 07:42:00 +00:00
2022-02-13 03:50:00 +00:00
Just say no to smart quotes!
''',
)
parser.examples = [
'*.md --recurse',
]
parser.add_argument(
'patterns',
nargs='+',
help='''
One or more glob patterns for input files.
''',
)
parser.add_argument(
'--recurse',
action='store_true',
help='''
If provided, recurse into subdirectories and process those files too.
''',
)
2021-08-17 07:42:00 +00:00
parser.set_defaults(func=nosmartquotes_argparse)
2022-02-13 03:50:00 +00:00
return betterhelp.go(parser, argv)
2021-08-17 07:42:00 +00:00
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))