Skip to content

Commit

Permalink
Merge pull request flintinatux#2 from flintinatux/update_spintax
Browse files Browse the repository at this point in the history
Update spintax to allow empty spin variations
  • Loading branch information
Scott McCormack committed Mar 10, 2014
2 parents 83b3564 + 5549f59 commit 3c2e316
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ rvm:
- "1.9.3"
- jruby-18mode # JRuby in 1.8 mode
# - jruby-19mode # JRuby in 1.9 mode throws some error about Random
- rbx-18mode
- rbx-19mode
# - rbx-18mode
# - rbx-19mode
- ree
# uncomment this line if your project needs to run something other than `rake`:
# script: bundle exec rspec spec
31 changes: 19 additions & 12 deletions lib/spintax_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
require 'backports/1.9.1/array/sample' if RUBY_VERSION < '1.9.1'

module SpintaxParser
SPINTAX_PATTERN = %r/\{([^{|}]+(\|[^{|}]+)+)\}/

SPINTAX_PATTERN = /\{([^{}]*)\}/

def unspin(options={})
spun = dup.to_s
Expand All @@ -15,25 +15,32 @@ def unspin(options={})

def count_spintax_variations
spun = dup.to_s
spun.gsub! %r/[^{|}]+/, '1'
spun.gsub! %r/\{/, '('
spun.gsub! %r/\|/, '+'
spun.gsub! %r/\}/, ')'
spun.gsub! %r/\)\(/, ')*('
spun.gsub! %r/\)1/, ')*1'
spun.gsub! %r/1\(/, '1*('
eval spun
while spun =~ /([\{\|])([\|\}])/
spun.gsub! /([\{\|])([\|\}])/, '\11\2'
end
spun.gsub! /[^{|}]+/, '1'
spun.gsub! /\{/, '('
spun.gsub! /\|/, '+'
spun.gsub! /\}/, ')'
spun.gsub! /\)\(/, ')*('
spun.gsub! /\)1/, ')*1'
spun.gsub! /1\(/, '1*('
begin
eval spun
rescue SyntaxError
nil
end
end

private

if RUBY_VERSION >= '1.9.3'
def parse_the_spintax_in(spun, options={})
spun.gsub!(SPINTAX_PATTERN) { $1.split('|').sample(options) }
spun.gsub!(SPINTAX_PATTERN) { $1.split('|',-1).sample options }
end
else
def parse_the_spintax_in(spun, options={})
spun.gsub!(SPINTAX_PATTERN) { $1.split('|').sample }
spun.gsub!(SPINTAX_PATTERN) { $1.split('|',-1).sample }
end
end
end
2 changes: 1 addition & 1 deletion lib/spintax_parser/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SpintaxParser
VERSION = "0.2.1"
VERSION = "0.2.2"
end
8 changes: 5 additions & 3 deletions spec/lib/spintax_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ class String

describe SpintaxParser do

let(:plaintext) { 'Hello world. { Please do not spin this. }' }
let(:plaintext) { 'Hello world. Please do not spin this.' }
let(:spintext) { 'Find this. {Hello|Hi} to the {{world|worlds} out there|planet}{!|.|?} Cool.' }
let(:spintax_pattern) { /\{[^{}]*\}/ }
let(:spintax_pattern) { SpintaxParser::SPINTAX_PATTERN }

describe "calling unspin" do

context "on plaintext" do
it "does not change the plaintext" do
expect { plaintext.unspin }.not_to change { plaintext }
Expand Down Expand Up @@ -52,5 +52,7 @@ class String
'{one|two} three {four|five}'.count_spintax_variations.should eq 4
'one {{two|three} four|five {six|seven}} eight {nine|ten}'.count_spintax_variations.should eq 8
'{Hello|Hi} {{world|worlds}|planet}{!|.|?}'.count_spintax_variations.should eq 18
'{one|two|}'.count_spintax_variations.should eq 3
"{Can't|count|this one".count_spintax_variations.should eq nil
end
end

0 comments on commit 3c2e316

Please sign in to comment.