-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstupefy_typography.py
95 lines (70 loc) · 2.42 KB
/
stupefy_typography.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
Reverses the smart typography applied by tools such as SmartyPants or
smart quotes in MS Word.
TAKE CARE! The transformation is done in-place.
This script reads the files and converts the following:
- curly quotes to straight quotes
- em dashes to ---
- en dashes to --
- ellipsis to ...
To see documentation, run `$ python stupefy_typography.py --help`
"""
import argparse
from pathlib import Path
from util import get_note_paths
replacements = {
"“": '"',
"”": '"',
"‘": "'",
"’": "'",
"–": "--",
"—": "---",
"…": "..."
}
def stupefy_typography(path: Path, dry_run: bool) -> bool:
"""Adds title (based on filename) to the start of the note.
:return True if changes were made, False otherwise
"""
# Read in the file
with open(path, "r") as file:
contents = file.read()
# Replace the target string
new_contents = replace_all(contents, replacements)
# Check if there were changes; if none, return
if contents == new_contents:
print("No changes needed to file {}".format(path))
return False
# print(new_contents)
# Write the file out again
print("Stupefying file {}".format(path))
if not dry_run:
with open(path, "w") as file:
file.write(new_contents)
return True
def replace_all(text: str, text_replacements: dict) -> str:
for old, new in text_replacements.items():
text = text.replace(old, new)
return text
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("directory", type=Path, metavar="path",
help="directory of the zettelkasten library")
parser.add_argument("--extensions", type=str, nargs="+", default=["md", "txt"], metavar="ext",
help="extensions to check (default: md, txt)")
parser.add_argument("--dry-run", action="store_true",
help="Perform a dry run without changing files on disk")
args = parser.parse_args()
if args.dry_run:
print("Running in dry mode.\n")
# Get note filenames
note_paths = get_note_paths(args.directory, args.extensions)
counter = 0
for path in note_paths:
if stupefy_typography(path, args.dry_run):
counter += 1
if counter == 0:
print("No files to modify")
else:
print("\nStupefied typography in {} files.".format(counter))
if __name__ == '__main__':
main()