generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathformat_random_verses.py
71 lines (59 loc) · 1.83 KB
/
format_random_verses.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
from dataclasses import dataclass
import json
import re
from typing import Any
VERSE_RE = r"((?:\d[\s\-_]*)?[A-z]+(?:[\s\-_]+[A-z]+)*)(?:\s*(\d+)(?:\s*:\s*(\d+)(?:\s*\-\s*(\d+))?)?)?"
@dataclass
class Reference:
book:str = ""
chapter:int = 0
verse:int = 0
verse_end:int|None = 0
@staticmethod
def from_tuple(tuple:tuple[Any, ...]) -> "Reference":
end = None
if tuple[3] is not None:
end = int(tuple[3])
return Reference(str(tuple[0]), int(tuple[1]), int(tuple[2]), end)
[2, 4, 6 ,8]
def main() -> None:
print("open random_verses_source.json")
with open("random_verses_source.json") as f:
print("load random_verses_source.json")
data:list = json.load(f)
print("assemble array")
array:list[str] = []
for x in data:
array.extend(x["verses"])
array = list(set(array))
array.sort()
to_remove:list[int] = []
for i, verse in enumerate(array):
if verse.count(":") > 1:
# Remove references with multiple ranges
to_remove.append(i)
continue
verse_match = re.match(VERSE_RE, verse)
if verse_match is None:
continue
reference = Reference.from_tuple(verse_match.groups())
if (match := re.search(r"\s*(\d*)-(\d*)", verse)) and match != None:
if int(match.groups()[1]) - int(match.groups()[0]) > 2:
to_remove.append(i)
continue
match reference:
case Reference("Proverbs", 7, verse_start) if int(verse_start) > 0:
# Remove references to Proverbs 7 to avoid awkward verses like
# "I have spread my couch with carpets of tapestry, with striped cloths
# of the yarn of Egypt."
# which are likely not what people want from random verses.
to_remove.append(i)
continue
for i in reversed(to_remove):
array.pop(i)
print("open random_verses.json")
with open("random_verses.json", "w+") as f:
print("write random_verses.json")
f.write(json.dumps(array))
if __name__ == "__main__":
main()