-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnosmartquotes.py
75 lines (61 loc) · 1.77 KB
/
nosmartquotes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import argparse
import os
import sys
from voussoirkit import betterhelp
from voussoirkit import pathclass
from voussoirkit import pipeable
from voussoirkit import spinal
from voussoirkit import vlogging
log = vlogging.getLogger(__name__, 'nosmartquotes')
THIS_FILE = os.path.abspath(__file__)
def replace_smartquotes(text):
text = text.replace('“', '"')
text = text.replace('”', '"')
text = text.replace('’', "'")
text = text.replace('‘', "'")
return text
def nosmartquotes_argparse(args):
globs = list(pipeable.input_many(args.patterns))
files = spinal.walk(
glob_filenames=globs,
exclude_filenames={THIS_FILE},
recurse=args.recurse,
)
for file in files:
text = file.read('r', encoding='utf-8')
original_text = text
text = replace_smartquotes(text)
if text == original_text:
continue
file.write('w', text, encoding='utf-8')
pipeable.stdout(file.absolute_path)
return 0
@vlogging.main_decorator
def main(argv):
parser = argparse.ArgumentParser(
description='''
Replace smart quotes and smart apostrophes with regular ASCII values.
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.
''',
)
parser.set_defaults(func=nosmartquotes_argparse)
return betterhelp.go(parser, argv)
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))