-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripture2url.py
executable file
·170 lines (157 loc) · 5.46 KB
/
scripture2url.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
import re
import argparse
books = {
# Old Testament
'Genesis' : 'ot/gen',
'Exodus' : 'ot/ex',
'Leviticus' : 'ot/lev',
'Numbers' : 'ot/num',
'Deuteronomy' : 'ot/deut',
'Joshua' : 'ot/josh',
'Judges' : 'ot/judg',
'Ruth' : 'ot/ruth',
'1 Samuel' : 'ot/1-sam',
'2 Samuel' : 'ot/2-sam',
'1 Kings' : 'ot/1-kgs',
'2 Kings' : 'ot/2-kgs',
'1 Chronicles' : 'ot/1-chr',
'2 Chronicles' : 'ot/2-chr',
'Ezra' : 'ot/ezra',
'Nehemiah' : 'ot/neh',
'Esther' : 'ot/esth',
'Job' : 'ot/job',
'Psalms' : 'ot/ps',
'Proverbs' : 'ot/prov',
'Ecclesiastes' : 'ot/eccl',
'Song of Solomon' : 'ot/song',
'Isaiah' : 'ot/isa',
'Jeremiah' : 'ot/jer',
'Lamentations' : 'ot/lam',
'Ezekiel' : 'ot/ezek',
'Daniel' : 'ot/dan',
'Hosea' : 'ot/hosea',
'Joel' : 'ot/joel',
'Amos' : 'ot/amos',
'Obadiah' : 'ot/obad',
'Jonah' : 'ot/jonah',
'Micah' : 'ot/micah',
'Nahum' : 'ot/nahum',
'Habakkuk' : 'ot/hab',
'Zephaniah' : 'ot/zeph',
'Haggai' : 'ot/Haggai',
'Zechariah' : 'ot/zech',
'Malachi' : 'ot/mal',
# New Testament
'Matthew' : 'nt/matt',
'Mark' : 'nt/mark',
'Luke' : 'nt/luke',
'John' : 'nt/john',
'Acts' : 'nt/acts',
'Romans' : 'nt/rom',
'1 Corinthians' : 'nt/1-cor',
'2 Corinthians' : 'nt/2-cor',
'Galatians' : 'nt/gal',
'Ephesians' : 'nt/eph',
'Philippians' : 'nt/philip',
'Colossians' : 'nt/col',
'1 Thessalonians' : 'nt/1-thes',
'2 Thessalonians' : 'nt/2-thes',
'1 Timothy' : 'nt/1-tim',
'2 Timothy' : 'nt/2-tim',
'Titus' : 'nt/titus',
'Philemon' : 'nt/philem',
'Hebrews' : 'nt/heb',
'James' : 'nt/james',
'1 Peter' : 'nt/1-pet',
'2 Peter' : 'nt/2-pet',
'1 John' : 'nt/1-jn',
'2 John' : 'nt/2-jn',
'3 John' : 'nt/3-jn',
'Jude' : 'nt/jude',
'Revelation' : 'nt/rev',
# Book of Mormon
'1 Nephi' : 'bofm/1-ne',
'2 Nephi' : 'bofm/2-ne',
'Jacob' : 'bofm/jacob',
'Enos' : 'bofm/enos',
'Jarom' : 'bofm/jarom',
'Omni' : 'bofm/omni',
'Words of Mormon' : 'bofm/w-of-m',
'Mosiah' : 'bofm/mosiah',
'Alma' : 'bofm/alma',
'Helaman' : 'bofm/hel',
'3 Nephi' : 'bofm/3-ne',
'4 Nephi' : 'bofm/4-ne',
'Mormon' : 'bofm/morm',
'Ether' : 'bofm/ether',
'Moroni' : 'bofm/moro',
# Doctrine and Covenants
'D&C' : 'dc-testament/dc',
# Pearl of Great Price
'Moses' : 'pgp/moses',
'Abraham' : 'pgp/abr',
'Joseph Smith-History' : 'pgp/js-h',
'Joseph Smith–History' : 'pgp/js-h',
'Joseph Smith-Matthew' : 'pgp/js-m',
'Joseph Smith–Matthew' : 'pgp/js-h',
}
regex=re.compile("""
(?P<book>([1-4] )?[A-Za-z- ]+)\s+
(?P<chapter>[0-9]+):
(?P<verse>[0-9]*)
(-(?P<other>[0-9]+))?
""", re.VERBOSE)
def ref2url(scripture, language):
"""
"""
found = regex.search(scripture)
if found:
d = found.groupdict()
book = books.get(d['book'])
url="https://www.churchofjesuschrist.org/study/scriptures/{Book:}/{chapter:}?lang={lang:}&id=p{verse:}".format(lang=language, Book=book, **d)
if d['other']:
url += "-p{}".format(d['other'])
return url
else:
print("Scripture reference not found in: '{}'".format(scripture))
def convert(reference, url, form):
"""
Convert the reference to a particular format. Available formats are:
markdown
html
"""
forms = {
'markdown': '[{ref:}]({url:})',
'html': '<a href="{url:}">{ref:}</a>'
}
return forms.get(form).format(ref=reference, url=url)
if __name__ == "__main__":
description = """
Convert scripture reference to URL link to
\thttps://www.churchofjesuschrist.org/study/scriptures
"""
parser = argparse.ArgumentParser(description=description)
parser.add_argument('reference', type=str,
help='Scripture reference. Note the reference must be in quotes')
parser.add_argument('-l', '--language', type=str,
default="eng",
help='3-letter language option for URL query')
parser.add_argument('--form', choices=['markdown', 'md', 'html', 'plain'],
default="markdown",
help="What form should the link be given.")
parser.add_argument('--dont-copy', default=False, action='store_true',
help="Don't copy output to clipboard")
args = parser.parse_args()
url = ref2url(args.reference, args.language)
if args.form != "plain":
url = convert(args.reference, url, args.form)
if not args.dont_copy:
try:
import pyperclip
pyperclip.copy(url)
except ImportError as e:
print("Can't copy to clipboard.\n"
"Please install pyperclip Python package.")
else:
print(url)