 helpContents = (name, commands) ->
 
   """
+<!DOCTYPE html>
 <html>
   <head>
+  <meta charset="utf-8">
   <title>#{name} Help</title>
   <style type="text/css">
     body {

# Description: 
#   Generates help commands for Hubot.
#
# Commands:

  """

module.exports = (robot) ->
  robot.respond /help(?:\s+(.*))?$/i, (msg) ->
    cmds = renamedHelpCommands(robot)
    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

    emit = cmds.join "\n"

    msg.send emit
    msg.send "Full list of commands: " + process.env.HUBOT_HELP_LINK

  robot.router.get "/#{robot.name}/help", (req, res) ->
    cmds = renamedHelpCommands(robot).map (cmd) ->
      cmd.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;')

    emit = "<p>#{cmds.join '</p><p>'}</p>"

    emit = emit.replace new RegExp("#{robot.name}", "ig"), "<b>#{robot.name}</b>"

    res.setHeader 'content-type', 'text/html'
    res.end helpContents robot.name, emit

renamedHelpCommands = (robot) ->
  robot_name = robot.alias or robot.name
  help_commands = robot.helpCommands().map (command) ->
    command.replace /^hubot/i, robot_name
  help_commands.sort()
