-
Notifications
You must be signed in to change notification settings - Fork 103
/
searchdocs.py
33 lines (26 loc) · 971 Bytes
/
searchdocs.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
import re
from errbot import BotPlugin, botcmd
from plugins import constants
class Searchdocs(BotPlugin):
"""
Search API and user docs
"""
API_DOCS = constants.API_DOCS
USER_DOCS = constants.USER_DOCS
@botcmd
def search(self, msg, arg):
"""
Gives the url of the relevant docs search page with given search string.
Syntax: `<bot> search api|user here goes the search string.`
"""
match = re.match(r'((?:api)|(?:user))\s+(.+)', arg, flags=re.IGNORECASE)
if match is None:
return ('Invalid syntax, try again. It should be of the form '
'`search api|user search string`.')
doc_type = match.group(1).lower()
search_string = match.group(2)
base_url = self.API_DOCS if doc_type == 'api' else self.USER_DOCS
return '{}/search.html?q={}'.format(
base_url,
'+'.join(re.split(r'\s+', search_string)),
)