spintax_parser 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +2 -2
- data/lib/spintax_parser.rb +19 -12
- data/lib/spintax_parser/version.rb +1 -1
- data/spec/lib/spintax_parser_spec.rb +5 -3
- metadata +4 -4
data/.travis.yml
CHANGED
@@ -5,8 +5,8 @@ rvm:
|
|
5
5
|
- "1.9.3"
|
6
6
|
- jruby-18mode # JRuby in 1.8 mode
|
7
7
|
# - jruby-19mode # JRuby in 1.9 mode throws some error about Random
|
8
|
-
- rbx-18mode
|
9
|
-
- rbx-19mode
|
8
|
+
# - rbx-18mode
|
9
|
+
# - rbx-19mode
|
10
10
|
- ree
|
11
11
|
# uncomment this line if your project needs to run something other than `rake`:
|
12
12
|
# script: bundle exec rspec spec
|
data/lib/spintax_parser.rb
CHANGED
@@ -2,8 +2,8 @@ require 'spintax_parser/version'
|
|
2
2
|
require 'backports/1.9.1/array/sample' if RUBY_VERSION < '1.9.1'
|
3
3
|
|
4
4
|
module SpintaxParser
|
5
|
-
|
6
|
-
SPINTAX_PATTERN =
|
5
|
+
|
6
|
+
SPINTAX_PATTERN = /\{([^{}]*)\}/
|
7
7
|
|
8
8
|
def unspin(options={})
|
9
9
|
spun = dup.to_s
|
@@ -15,25 +15,32 @@ module SpintaxParser
|
|
15
15
|
|
16
16
|
def count_spintax_variations
|
17
17
|
spun = dup.to_s
|
18
|
-
spun
|
19
|
-
|
20
|
-
|
21
|
-
spun.gsub!
|
22
|
-
spun.gsub!
|
23
|
-
spun.gsub!
|
24
|
-
spun.gsub!
|
25
|
-
|
18
|
+
while spun =~ /([\{\|])([\|\}])/
|
19
|
+
spun.gsub! /([\{\|])([\|\}])/, '\11\2'
|
20
|
+
end
|
21
|
+
spun.gsub! /[^{|}]+/, '1'
|
22
|
+
spun.gsub! /\{/, '('
|
23
|
+
spun.gsub! /\|/, '+'
|
24
|
+
spun.gsub! /\}/, ')'
|
25
|
+
spun.gsub! /\)\(/, ')*('
|
26
|
+
spun.gsub! /\)1/, ')*1'
|
27
|
+
spun.gsub! /1\(/, '1*('
|
28
|
+
begin
|
29
|
+
eval spun
|
30
|
+
rescue SyntaxError
|
31
|
+
nil
|
32
|
+
end
|
26
33
|
end
|
27
34
|
|
28
35
|
private
|
29
36
|
|
30
37
|
if RUBY_VERSION >= '1.9.3'
|
31
38
|
def parse_the_spintax_in(spun, options={})
|
32
|
-
spun.gsub!(SPINTAX_PATTERN) { $1.split('|').sample
|
39
|
+
spun.gsub!(SPINTAX_PATTERN) { $1.split('|',-1).sample options }
|
33
40
|
end
|
34
41
|
else
|
35
42
|
def parse_the_spintax_in(spun, options={})
|
36
|
-
spun.gsub!(SPINTAX_PATTERN) { $1.split('|').sample }
|
43
|
+
spun.gsub!(SPINTAX_PATTERN) { $1.split('|',-1).sample }
|
37
44
|
end
|
38
45
|
end
|
39
46
|
end
|
@@ -6,12 +6,12 @@ end
|
|
6
6
|
|
7
7
|
describe SpintaxParser do
|
8
8
|
|
9
|
-
let(:plaintext) { 'Hello world.
|
9
|
+
let(:plaintext) { 'Hello world. Please do not spin this.' }
|
10
10
|
let(:spintext) { 'Find this. {Hello|Hi} to the {{world|worlds} out there|planet}{!|.|?} Cool.' }
|
11
|
-
let(:spintax_pattern) {
|
11
|
+
let(:spintax_pattern) { SpintaxParser::SPINTAX_PATTERN }
|
12
12
|
|
13
13
|
describe "calling unspin" do
|
14
|
-
|
14
|
+
|
15
15
|
context "on plaintext" do
|
16
16
|
it "does not change the plaintext" do
|
17
17
|
expect { plaintext.unspin }.not_to change { plaintext }
|
@@ -52,5 +52,7 @@ describe SpintaxParser do
|
|
52
52
|
'{one|two} three {four|five}'.count_spintax_variations.should eq 4
|
53
53
|
'one {{two|three} four|five {six|seven}} eight {nine|ten}'.count_spintax_variations.should eq 8
|
54
54
|
'{Hello|Hi} {{world|worlds}|planet}{!|.|?}'.count_spintax_variations.should eq 18
|
55
|
+
'{one|two|}'.count_spintax_variations.should eq 3
|
56
|
+
"{Can't|count|this one".count_spintax_variations.should eq nil
|
55
57
|
end
|
56
58
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spintax_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 2
|
10
|
+
version: 0.2.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Scott McCormack
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2014-03-10 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
version_requirements: &id001 !ruby/object:Gem::Requirement
|