Skip to content

Commit

Permalink
Spin given common spintax formatted text
Browse files Browse the repository at this point in the history
spin me <query> - Returns a spun version of the input. Supports nested
sets and multiline input.
  • Loading branch information
markomanninen committed Oct 30, 2011
1 parent 9a8c074 commit cc95733
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
npm-debug.log
.DS_Store*
32 changes: 32 additions & 0 deletions src/scripts/spin.coffee
Original file line number Diff line number Diff line change
@@ -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.