This repository has been archived by the owner on Apr 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcljdocs.py
324 lines (274 loc) · 10.1 KB
/
cljdocs.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env python
"""
Copyright (c) 2012, Morten "Foxboron" Linderud
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
- Neither the name of Morten "Foxboron" Linderud nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import sublime
import sublime_plugin
import webbrowser
import re
import sys
PY3 = sys.version_info[0] == 3
if PY3:
import urllib.request as urllib2
from .py3k import BeautifulSoup
from .edit import Edit
else:
import urllib2
from py2k import BeautifulSoup
from edit import Edit
def request(var):
var = "http://clojuredocs.org/search?q=%s" % var
req = urllib2.urlopen(var).read()
return req
def content_request(url):
req = urllib2.urlopen(url).read()
return req
def bs4_parse(var):
"""Parses out the indvidual search item"""
var = request(var)
soup = BeautifulSoup(var)
stuff = soup.body.find_all("div", "search_result")
items = []
sites = []
for i in stuff:
item, web = parse_list(i)
items.append(item)
sites.append(web)
return (items, sites)
def parse_list(item):
"""Parses out the main search results."""
dic = []
#What
dic.append(str(item.find("a").text))
#Doc
p = item.find("p", "doc").text
if p:
if len(p) > 66:
p = p[:66] + "....."
dic.append("".join([i.replace("\n", " ") for i in p]))
else:
dic.append("No Documentation")
#What lib
dic.append("Namespace: %s" % str(item.find("span", "ns").a.text))
#Available exmaples
dic.append("Available Examples: %s" % str(item.find("span", "examples_count").text.split()[0]))
#http
web = item.find("span", "linktext").text
return (dic, web)
def seealso_search(url):
"""Searches the 'See Also...' part. """
v = content_request(url)
soup = BeautifulSoup(v)
stuff = soup.body.find_all("li", "see_also_item")
items = []
sites = []
for i in stuff:
item, web = new_parse(i)
items.append(item)
sites.append(web)
return (items, sites)
def new_parse(n):
"""Parsing the HTML 'see also...' links and names."""
http = "http://clojuredocs.org"
dic = []
#Name
dic.append(str(n.find("span", "name").text))
#Doc
p = n.find("p", "shortdoc").text
if p:
if len(p) > 66:
p = p[:66] + "....."
dic.append("".join([i.replace("\n", " ") for i in p]))
else:
dic.append("No Documentation")
#Namespace
dic.append("Namespace: %s" % str(n.find("span", "ns").text[:-1]))
#http
web = http + n.find("a").get("href")
return (dic, web)
def parse_source(url):
"""Parsing source"""
v = content_request(url)
soup = BeautifulSoup(v)
stuff = soup.find("div", "source_content")
l = []
if not stuff:
ret = " \n" + "No source code available!\n"
l.append(ret.split("\n"))
return l
stuff = stuff.find("pre", "brush: clojure")
ret = "Source: \n" + stuff.text
l.append(ret.split("\n"))
return l
def parse_example(url):
"""Parsing examples"""
v = content_request(url)
soup = BeautifulSoup(v)
stuff = soup.find_all("div", "hidden plain_content")
l = []
if not stuff:
ret = " \n" + "No examples available!\n"
l.append(ret.split("\n"))
return l
num = 1
for i in stuff:
ret = "Example #" + str(num) + ": \n" + i.text.rstrip("\n")
l.append(ret.split("\n"))
num += 1
return l
def parse_doc(url):
"""Parsing the documentation."""
v = content_request(url)
soup = BeautifulSoup(v)
stuff = soup.find("div", "doc").find("div", "content")
for e in stuff.findAll("br"):
e.replace_with("\n")
l = []
if not stuff:
ret = " \n" + "No documentation available!\n"
l.append(ret.split("\n"))
return l
ret = "Documentation: \n" + stuff.text
l.append(ret.split("\n"))
return l
def show_quick_panel_hack(l):
"""show_quick_panel looks at the first element of a list's lenght and forces it
on any element after after this. If the element got a shorter lenght then
the first item, you get a index error. This makes sure all elements of the
list is the same lenght as the first element by appending nothing."""
lenght = len(l[0])
for i in l[1:]:
if len(i) < lenght:
print(i)
[i.append("") for n in range(0,(lenght-len(i)))]
return l
class CljSearchCommand(sublime_plugin.WindowCommand):
def run(self):
"""Runs the initial plugin"""
self.window.show_input_panel("Search", "", self.on_done, None, None)
def on_done(self, search):
"""Fetches the search results"""
self.res, self.search_links = bs4_parse(search)
for i in range(0, len(self.res)):
if search == self.res[i][0]:
self.panel_items = self.res
self.done(i)
return
self.search(s=self.res, link=self.search_links)
def search(self, s=None, link=None):
"""Created a seperate method for the search.
Thus we can apply a back function"""
if s:
self.panel_items = s
if link:
self.search_links = link
sublime.set_timeout(lambda: self.window.show_quick_panel(self.panel_items, self.done), 1)
def done(self, num):
"""Main menu!"""
if num == -1: return
options = [
"View Docs",
"View Source",
"View Examples",
"See Also...",
"Open website",
"Back"
]
self.num = num
sublime.set_timeout(lambda: self.window.show_quick_panel(options, self.selected_item), 1)
def doc_view(self, lis):
"""Directs back to the sub menu"""
self.inser_content = lis
if isinstance(lis[0], list):
lis = show_quick_panel_hack(lis)
sublime.set_timeout(lambda: self.window.show_quick_panel(lis, self.select_edit), 1)
def doc_check(self, lis):
"""Returns directly back to the main menu."""
sublime.set_timeout(lambda: self.window.show_quick_panel(lis, self.return_from_doc), 1)
def return_from_doc(self, num):
"""Because of the depedancy on 'self.num' to actually know where we are
I had to do this....yeah i know...dat method...."""
self.done(self.num)
def select_edit(self, num):
"""Sub menu!"""
if num == -1: return self.done(self.num)
self.selected_example = num
options = [
"Insert",
"Back"
]
sublime.set_timeout(lambda: self.window.show_quick_panel(options, self.inser_back), 1)
def inser_back(self, num):
"""2nd menu directing the Insert/Back options."""
if num == 0:
buffr = "\n".join(self.inser_content[self.selected_example])
view = self.window.active_view()
with Edit(view) as edit:
for r in view.sel():
if r.empty():
edit.insert(r.a, buffr[16:])
else:
edit.replace(r, buffr[16:])
if num == 1:
self.done(self.num)
def selected_item(self, num):
"""1st menu. Directing the options to the main menu."""
if num == -1:
self.search(s=self.res, link=self.search_links)
if num == 0:
self.doc_check(parse_doc(self.search_links[self.num]))
if num == 1:
self.doc_view(parse_source(self.search_links[self.num]))
if num == 2:
self.doc_view(parse_example(self.search_links[self.num]))
if num == 3:
self.see_also, self.see_also_links = seealso_search(self.search_links[self.num])
self.search(s=self.see_also, link=self.see_also_links)
if num == 4:
webbrowser.open(self.search_links[self.num])
if num == 5:
self.search()
# Select word hack from bronson :D
# https://github.com/bronson/GotoFile
def expanded_selection(view, line, left, right):
pat = re.compile('^[A-Za-z0-9_.-?*]+$')
while left > line.begin() and re.match(pat, view.substr(left - 1)):
left -= 1
while right < line.end() and re.match(pat, view.substr(right)):
right += 1
return view.substr(sublime.Region(left, right))
def selection_words(view):
words = []
for sel in view.sel():
if sel.empty():
line = view.line(sel.begin())
words.append(expanded_selection(view, line, sel.begin(), sel.begin()))
else:
words.append(view.substr(sel))
# print "FILES: " + repr(words)
return words
class GotoSelectionCommand(sublime_plugin.TextCommand, sublime.View):
def run(self, edit):
word = selection_words(self.view)[0]
CljSearchCommand(self.view.window()).on_done(word)