Permalink
Browse files

Spin given common spintax formatted text

spin me <query> - Returns a spun version of the input. Supports nested
sets and multiline input.
  • Loading branch information...
1 parent 9a8c074 commit cc957338aeb559feefe55ebca3c3c0e793ba52d7 @markomanninen markomanninen committed Oct 30, 2011
Showing with 33 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +32 −0 src/scripts/spin.coffee
View
@@ -1,2 +1,3 @@
node_modules
npm-debug.log
+.DS_Store*
View
@@ -0,0 +1,32 @@
+# Spin given common spintax formatted text
+#
+# - supports nested sets
+# - supports multiline input for robot
+#
+# spin me <query> - Returns a spun version of the input.
+#
+# Example: spin me This is {my nested {spintax|spuntext} formatted string|your nested {spintax|spuntext} formatted string} test.
+#
+# TODO: different syntaxes like '{', '|', '}' OR '[spin]', '~', '[/spin]'
+#
+split_str = '|'
+replace_regexp_str = /[{}]/gi
+match_regexp_str = /{[^{}]+?}/gi
+
+spin = (txt) ->
+ if txt
+ txt = capture(txt) while txt.match(match_regexp_str)
+ txt
+
+capture = (txt) ->
+ match = txt.match(match_regexp_str)
+ if (match[0])
+ words = match[0].split(split_str)
+ replace = words[Math.floor(Math.random()*words.length)].replace(replace_regexp_str, '')
+ txt = txt.replace(match[0], replace)
+ txt
+
+module.exports = (robot) ->
+ robot.respond /spin me ([\s\S]*)/i, (msg) ->
+ output = spin(msg.match[0])
+ msg.send output

0 comments on commit cc95733

Please sign in to comment.