-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__init__.py
121 lines (91 loc) · 3.36 KB
/
__init__.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
# -*- coding: utf-8 -*-
"""Search the Laravel Documentation"""
from os import path
import urllib.parse
import html
from algoliasearch.search_client import SearchClient
from albert import *
__title__ = "Laravel Docs"
__prettyname__ = "Laravel Docs"
__doc__ = "Albert extension for quickly and easily searching the Laravel documentation"
__version__ = "0.4.1"
__triggers__ = "ld "
__authors__ = "Rick West"
__py_dep__ = ["algoliasearch"]
client = SearchClient.create("8BB87I11DE", "8e1d446d61fce359f69cd7c8b86a50de")
index = client.init_index("docs")
icon = "{}/icon.png".format(path.dirname(__file__))
google_icon = "{}/google.png".format(path.dirname(__file__))
docs = "https://laravel.com/docs/"
def getSubtitle(hit):
if hit["h4"] is not None:
return hit["h4"]
if hit["h3"] is not None:
return hit["h3"]
if hit["h2"] is not None:
return hit["h2"]
return None
def handleQuery(query):
items = []
if query.isTriggered:
if not query.isValid:
return
if query.string.strip():
search = index.search(
query.string, {"tagFilters": "master", "hitsPerPage": 5}
)
for hit in search["hits"]:
title = hit["h1"]
subtitle = getSubtitle(hit)
url = "{}{}".format(docs, hit["link"])
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=__prettyname__,
icon=icon,
text=html.unescape(title),
subtext=html.unescape(subtitle if subtitle is not None else ""),
actions=[UrlAction("Open in the Laravel Documentation", url)],
)
)
if len(items) == 0:
term = "laravel {}".format(query.string)
google = "https://www.google.com/search?q={}".format(
urllib.parse.quote(term)
)
items.append(
Item(
id=__prettyname__,
icon=google_icon,
text="Search Google",
subtext='No match found. Search Google for: "{}"'.format(term),
actions=[UrlAction("No match found. Search Google", google)],
)
)
items.append(
Item(
id=__prettyname__,
icon=icon,
text="Open Docs",
subtext="No match found. Open laravel.com/docs...",
actions=[UrlAction("Open the Laravel Documentation", docs)],
)
)
else:
items.append(
Item(
id=__prettyname__,
icon=icon,
text="Open Docs",
subtext="Open laravel.com/docs...",
actions=[UrlAction("Open the Laravel Documentation", docs)],
)
)
return items