Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #135 from facto/master

Dialectizer
  • Loading branch information...
commit 34459ba3d5f13c46f9521b5e0613762fb39909b2 2 parents 6d2b279 + 7e4f38b
Tom Bell authored
Showing with 42 additions and 0 deletions.
  1. +42 −0 src/scripts/dialectizer.coffee
View
42 src/scripts/dialectizer.coffee
@@ -0,0 +1,42 @@
+Select = require("soupselect").select
+HtmlParser = require "htmlparser"
+
+#TODO: get this dynamically?
+dialects = ["redneck", "jive", "cockney", "fudd", "bork", "moron", "piglatin", "hckr", "censor"]
+
+module.exports = (robot) ->
+ robot.respond /(?:dialectize|dialect|dia) (\w+)(.*)/i, (msg) ->
+ [dialect, text] = msg.match[1..2]
+ if dialect in ["help", "h"]
+ showHelp(msg)
+ return
+
+ return unless text
+ trim text
+ return unless text.length > 0
+
+ if dialect in ["all", "a"]
+ showDialectizedText(msg, dialect, text, true) for dialect in dialects
+ return
+ else if dialect is "hacker"
+ dialect = "hckr"
+ showDialectizedText(msg, dialect, text, false)
+
+showDialectizedText = (msg, dialect, text, showPrefix) ->
+ msg.http("http://www.rinkworks.com/dialect/dialectt.cgi?dialect=" + encodeURIComponent(dialect) + "&text=" + encodeURIComponent(text))
+ .get() (err, res, body) ->
+ handler = new HtmlParser.DefaultHandler()
+ parser = new HtmlParser.Parser handler
+ parser.parseComplete body
+ result = Select handler.dom, ".dialectized_text p"
+ return unless result
+ dialectizedText = trim result[0].children[0].raw
+ dialectizedText = "#{dialect}: " + dialectizedText if showPrefix
+ msg.send dialectizedText
+
+showHelp = (msg) ->
+ msg.send "Dialects: " + dialects.join(", ")
+
+trim = (string) ->
+ return string.replace(/^\s*|\s*$/g, '')
+
Please sign in to comment.
Something went wrong with that request. Please try again.