-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertHyperlinkCommand.py
More file actions
194 lines (166 loc) · 7.48 KB
/
Copy pathInsertHyperlinkCommand.py
File metadata and controls
194 lines (166 loc) · 7.48 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import sublime, sublime_plugin
import re
import urllib.parse
import os
class InsertHyperlinkCommand(sublime_plugin.TextCommand):
def __init__(self, view):
# each entry is a tuple with 1+ elements, 2nd element is abbreviation if it exists
self.bible_abbrs = self.load_bible_abbrs()
self.view = view
def load_bible_abbrs(self):
path = os.path.dirname(os.path.realpath(__file__))
with open(path + '/bible_abbr.txt', 'r') as f:
return [s.replace('\n','').split(', ') for s in f.readlines()]
def find_bible_abbr(self, abbr, full=False):
clean = lambda s: s.replace(' ','').replace('_', '').lower()
# remove space and underscore from '1 John', '1_Corinthians', etc.
it = filter(lambda b: list(filter(lambda x: clean(x).startswith(clean(abbr)), b)), self.bible_abbrs)
book = next(it)
s = book[1] if len(book) >= 2 and not full else book[0]
return re.sub(r'^([1-3]) ', '\\1 ', s)
def format_range(self, ch, start, end, end2):
if end2:
return '{}:{}–{}:{}'.format(ch, start, end, end2)
elif start and end:
return '{}:{}–{}'.format(ch, start, end)
elif end:
return '{}–{}'.format(ch, end)
elif start:
return '{}:{}'.format(ch, start)
else:
return '{}'.format(ch)
def format_bible_ref(self, b, ch, start, end, end2, comma, full=False):
book = self.find_bible_abbr(b, full)
if comma:
extras = [', ' + self.format_range(ch, start, end, end2) for (ch, start, end, end2) in re.findall(r'(\d+)(?:[.:](\d+))?(?:-(\d+)(?:[.:](\d+))?)?', comma)]
else:
extras = []
return '{} {}{}'.format(book, self.format_range(ch, start, end, end2), ''.join(extras))
# delimit passages within a book by comma (,) and books by semicolon (;)
def parse_bible_refs(self, c, full=False):
m = re.search(r'^https?://(?:(?:www\.biblegateway\.com/passage/\?search=|(?:www\.)?blueletterbible\.org/tools/MultiVerse\.cfm\?mvText=)([^&]+)|biblehub\.com/(?:(?:text|interlinear|parallel)/)?([^/]+)/([0-9]+)-([0-9]+))', urllib.parse.unquote(c))
if not m:
return
if m.group(2):
refs = [(m.group(2), m.group(3), m.group(4), '', '', '')]
#print(refs)
elif m.group(1):
# deut12.32-13:5,17.14-20
refs = re.findall(r'(?i)([1-3]?[a-z ]+)(?:(\d+)(?:[.:](\d+))?(?:-(\d+)(?:[.:](\d+))?)?)?(?:,([^;]+))?', m.group(1))
#print(refs);
fixed = [self.format_bible_ref(b, ch, start, end, end2, comma, full) for (b, ch, start, end, end2, comma) in refs]
return fixed[0] if len(fixed) == 1 else ' and '.join([', '.join(fixed[:-1])] + [fixed[-1]])
def format_url(self, s, url, is_markdown):
if is_markdown:
return f"[{self.markdown_escape_url_text(s)}]({self.markdown_escape_url(url)})"
else:
return f'<a href="{url}">{s}</a>'
def rotate_preceding(self, url, options, is_markdown, edit, region):
for i, opt in enumerate(options):
href = self.format_url(opt, url, is_markdown)
href_region = sublime.Region(region.a - len(href), region.a)
if href == self.view.substr(href_region):
s = options[(i + 1) % len(options)]
self.view.erase(edit, href_region)
return (s, self.view.sel()[0])
return None
def run(self, edit):
region = self.view.sel()[0]
sel = self.view.substr(region)
clip = sublime.get_clipboard().strip()
url = re.sub(r'\#:~:text=.*', '', clip)
bibleref = self.parse_bible_refs(url) if len(sel) == 0 else None
wiki_m = re.search(r'^https?://en.wikipedia.org/wiki/(.*)', url)
dict_m = re.search(r'^https://www.dictionary.com/browse/(\w+)$', url)
book_m = re.search(r'^[^<>]*<a href="[^"]+">[^<]+(</a>)?$', url)
url_m = re.search(r'^(https?://|#|/)', url)
get_index = None
is_markdown = self.view.syntax() and self.view.syntax().name == 'Markdown'
if len(sel) == 0 and wiki_m:
s = "WP: " + wiki_m.group(1).replace('_', ' ').replace('#', ' § ')#.replace('%27', '\'')
s = urllib.parse.unquote(s)
elif len(sel) == 0 and dict_m:
s = "dictionary.com: **" + dict_m.group(1) + "**"
elif bibleref:
s = bibleref
elif url_m:
s = sel
elif len(sel) == 0 and book_m:
url = None
#print(book_m.group(1))
s = clip
if not book_m.group(1):
s = s + "</a>"
if is_markdown:
s = re.sub(r'<a href=["\']([^"\']+).*?>(.*?)</a>', '[\\2](\\1)', s)
else:
url = None
# the clipbord does not contain a hyperlink
# ignore sel
if is_markdown:
left = "[" + clip + "]("
right = ")"
get_index = lambda r: r.end() - len(right)
else:
left = "<a href=\""
right = "\">" + clip + "</a>"
get_index = lambda r: r.begin() + len(left)
s = left + right
if len(sel) == 0 and wiki_m:
options = [s]
prefix_len = len("WP: ")
if len(re.findall(r'[A-Z]', s[prefix_len:])) == 1:
options.append(s[prefix_len].lower() + s[prefix_len + 1:])
options.append(s[prefix_len:])
if s.find('(') >= 0:
options.extend(re.sub(r" ?\(.*\)", "", o) for o in options[1:])
tuple = self.rotate_preceding(url, options, is_markdown, edit, region)
if tuple:
(s, region) = tuple
if len(sel) == 0 and bibleref:
full = self.parse_bible_refs(url, True)
options = [s, full]
tuple = self.rotate_preceding(url, options, is_markdown, edit, region)
if tuple:
(s, region) = tuple
if url:
if is_markdown:
# a [x] b -> [a \[x\] b](url)
s = self.markdown_escape_url_text(s)
url = self.markdown_escape_url(url)
left = "[" + s
right = "](" + url + ")"
if len(s) == 0:
get_index = lambda r: r.begin() + len(left)
else:
left = "<a href=\"" + url + "\">"
right = s + "</a>"
if len(s) == 0:
get_index = lambda r: r.end() - len(right)
if len(sel) == 0 and wiki_m:
href = left + right
href_region = sublime.Region(region.a - len(href), region.a)
s = left + right
if get_index == None:
get_index = lambda r: r.end()
self.view.replace(edit, region, s)
region = self.view.sel()[0]
self.view.sel().clear()
idx = get_index(region)
self.view.sel().add(sublime.Region(idx, idx))
def find_previous(self, rx):
cur = self.view.sel()[0]
x = []
p = self.view.find_all(rx, 0, "$1", x)
v = ""
# in case we're located after the last match
x.append(v)
p.append(cur)
for r, s in zip(p, x):
if r.a >= cur.a:
return v
v = s
def markdown_escape_url(self, url):
return re.sub(r'(?<!\\)([\)])', '\\\\\\1', url)
def markdown_escape_url_text(self, s):
return re.sub(r'(?<!\\)([\[\]])', '\\\\\\1', s)