-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename_date_references.py
47 lines (40 loc) · 1.1 KB
/
rename_date_references.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
"""
Finds roam references (e.g., January 1st, 2020) and replaces them with our daily note format
"""
import re
from datetime import date
from util import recursive_file_generator
months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
]
months = {month: i + 1 for i, month in enumerate(months)}
reference_re = re.compile(
r"(\[\[(?P<month>"
+ "|".join(months)
+ r") (?P<day>\d{1,2})(st|nd|rd|th), (?P<year>\d{4})\]\])"
)
for file in recursive_file_generator():
text = file.read_text()
if matches := reference_re.findall(text):
substitutions = {}
for full, month, day, _, year in matches:
month = months[month]
day = int(day)
year = int(year)
ref_date = date(year, month, day)
substitutions[full] = ref_date.strftime("[[%Y-%m-%d]]")
for old, new in substitutions.items():
print(file.name, old, "->", new)
text = text.replace(old, new)
file.write_text(text)