-
Notifications
You must be signed in to change notification settings - Fork 1
/
help.coffee
85 lines (74 loc) · 1.87 KB
/
help.coffee
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
# Description:
# Generates help commands for Hubot.
#
# Commands:
# hubot help - Displays all of the help commands that Hubot knows about.
# hubot help <query> - Displays all help commands that match <query>.
#
# URLS:
# /hubot/help
#
# Notes:
# These commands are grabbed from comment blocks at the top of each file.
helpContents = (name, commands) ->
"""
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>#{name} Help</title>
<style type="text/css">
body {
background: #d3d6d9;
color: #636c75;
text-shadow: 0 1px 1px rgba(255, 255, 255, .5);
font-family: Helvetica, Arial, sans-serif;
}
h1 {
margin: 8px 0;
padding: 0;
}
.commands {
font-size: 13px;
}
p {
border-bottom: 1px solid #eee;
margin: 6px 0 0 0;
padding-bottom: 5px;
}
p:last-child {
border: 0;
}
</style>
</head>
<body>
<h1>#{name} Help</h1>
<div class="commands">
#{commands}
</div>
</body>
</html>
"""
module.exports = (robot) ->
robot.respond /help\s*(.*)?$/i, (msg) ->
cmds = robot.helpCommands()
filter = msg.match[1]
if filter
cmds = cmds.filter (cmd) ->
cmd.match new RegExp(filter, 'i')
if cmds.length == 0
msg.send "No available commands match #{filter}"
return
prefix = robot.alias or robot.name
cmds = cmds.map (cmd) ->
cmd = cmd.replace /^hubot/, prefix
cmd.replace /hubot/ig, robot.name
emit = cmds.join "\n"
msg.send emit
robot.router.get "/#{robot.name}/help", (req, res) ->
cmds = robot.helpCommands().map (cmd) ->
cmd.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>')
emit = "<p>#{cmds.join '</p><p>'}</p>"
emit = emit.replace /hubot/ig, "<b>#{robot.name}</b>"
res.setHeader 'content-type', 'text/html'
res.end helpContents robot.name, emit