generated from use-the-fork/albert-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tailwind.py
145 lines (113 loc) · 4.6 KB
/
tailwind.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
import urllib.parse
from algoliasearch.search_client import SearchClient
import os
from albert import Action, Item, QueryHandler, openUrl, info, debug
import sys
sys.path.append(os.path.dirname(__file__))
class Tailwind:
client = SearchClient.create("KNPXZI5B0M", "5fc87cef58bb80203d2207578309fab6")
index = client.init_index("tailwindcss")
icon = "{}/images/tailwind.png".format(os.path.dirname(__file__))
docs = "https://tailwindcss.com/docs/"
def __init__(self, md_name):
self.md_name = md_name
def getTitle(self, hierarchy):
if hierarchy["lvl6"] is not None:
return hierarchy["lvl6"]
if hierarchy["lvl5"] is not None:
return hierarchy["lvl5"]
if hierarchy["lvl4"] is not None:
return hierarchy["lvl4"]
if hierarchy["lvl3"] is not None:
return hierarchy["lvl3"]
if hierarchy["lvl2"] is not None:
return hierarchy["lvl2"]
if hierarchy["lvl1"] is not None:
return hierarchy["lvl1"]
if hierarchy["lvl0"] is not None:
return hierarchy["lvl0"]
return None
def getSubtitle(self, hierarchy):
if hierarchy["lvl6"] is not None:
return hierarchy["lvl5"]
if hierarchy["lvl5"] is not None:
return hierarchy["lvl4"]
if hierarchy["lvl4"] is not None:
return hierarchy["lvl3"]
if hierarchy["lvl3"] is not None:
return hierarchy["lvl2"]
if hierarchy["lvl2"] is not None:
return hierarchy["lvl1"]
if hierarchy["lvl1"] is not None:
return hierarchy["lvl0"]
return None
def handleQuery(self, search_item):
items = []
if search_item:
search = self.index.search(
search_item,
{"facetFilters": "version:v3", "hitsPerPage": 5, "highlightPreTag": "...", "highlightPostTag": "..."}
)
for hit in search["hits"]:
title = self.getTitle(hit['hierarchy'])
subtitle = self.getSubtitle(hit['hierarchy'])
url = hit["url"]
text = False
try:
text = hit["_highlightResult"]["content"]["value"]
except KeyError:
pass
if text and subtitle:
title = "{} - {}".format(title, subtitle)
subtitle = text
items.append(
Item(
id=f'{self.md_name}/{hit["objectID"]}',
icon=[self.icon],
text=title,
subtext=subtitle if subtitle is not None else "",
actions=[
Action(
"Open",
'Open the {} Documentation'.format(self.md_name),
lambda u=url: openUrl(u)
)
],
)
)
if len(items) == 0:
term = "tailwindcss {}".format(search_item)
google = "https://www.google.com/search?q={}".format(
urllib.parse.quote(term)
)
items.append(
Item(
id=f'{self.md_name}/search_google',
icon=["{}/images/google.png".format(os.path.dirname(__file__))],
text="Search Google",
subtext='No match found. Search Google for: "{}"'.format(term),
actions=[
Action(
"Open",
'No match found. Search Google',
lambda u=google: openUrl(u)
)
],
)
)
items.append(
Item(
id=f'{self.md_name}/open_{self.md_name}_docs',
icon=[self.icon],
text='Open {} Docs'.format(self.md_name),
subtext="No match found. Open {}".format(self.docs),
actions=[
Action(
"Open",
'Open the {} Documentation'.format(self.md_name.replace("https://", "")),
lambda u=self.docs: openUrl(u)
)
],
)
)
return items