forked from github/hubot-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 cc95733
Showing
2 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
npm-debug.log | ||
.DS_Store* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |