content_spinning 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +21 -0
- data/README.md +24 -0
- data/lib/content_spinning/version.rb +6 -0
- data/lib/content_spinning.rb +73 -0
- metadata +52 -0
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) Maxime Garcia
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
data/README.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# Content Spinning
|
2
|
+
|
3
|
+
content_spinning is a ruby lib to spin some text.
|
4
|
+
|
5
|
+
## Example
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
"Hi {there|you}! I'm {efficient|productive}.".spin
|
9
|
+
# or
|
10
|
+
ContentSpinning.spin "Hi {there|you}! I'm {efficient|productive}."
|
11
|
+
```
|
12
|
+
|
13
|
+
returns this array :
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
["Hi there! I'm efficient.", "Hi there! I'm productive.", "Hi you! I'm efficient.", "Hi you! I'm productive."]
|
17
|
+
```
|
18
|
+
|
19
|
+
## Install
|
20
|
+
|
21
|
+
```
|
22
|
+
gem install content_spinning
|
23
|
+
```
|
24
|
+
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module ContentSpinning
|
2
|
+
class << self
|
3
|
+
|
4
|
+
def spin(text)
|
5
|
+
result = parse(text)
|
6
|
+
spin_all_level(result[:parsed], result[:max_level])
|
7
|
+
end
|
8
|
+
|
9
|
+
def parse(text, level = 1)
|
10
|
+
return {:parsed => text, :max_level => level - 1} unless text.include? "{"
|
11
|
+
|
12
|
+
text.gsub!(/\{\}/, '')
|
13
|
+
text.gsub!(/\{([^\{\}\|]+)\}/, '\1')
|
14
|
+
|
15
|
+
text.gsub!(/\{([^\{\}]+)\}/) do |match|
|
16
|
+
match.gsub!(/\{/, "__SPIN_BEGIN_" + level.to_s + "__")
|
17
|
+
match.gsub!(/\}/, "__SPIN_END_" + level.to_s + "__")
|
18
|
+
match.gsub!(/\|/, "__SPIN_OR_" + level.to_s + "__")
|
19
|
+
end
|
20
|
+
|
21
|
+
parse(text, level+1)
|
22
|
+
end
|
23
|
+
|
24
|
+
def spin_a_level(text_or_array, level)
|
25
|
+
text_or_array = [text_or_array] unless text_or_array.is_a? Array
|
26
|
+
|
27
|
+
spin_begin = '__SPIN_BEGIN_' + level.to_s + '__'
|
28
|
+
spin_end = '__SPIN_END_' + level.to_s + '__'
|
29
|
+
spin_or = '__SPIN_OR_' + level.to_s + '__'
|
30
|
+
|
31
|
+
text_or_array.map! do |text|
|
32
|
+
return [text] unless text.include? spin_begin
|
33
|
+
|
34
|
+
array = text.partition(Regexp.new(spin_begin + '.+?' + spin_end))
|
35
|
+
#puts array.inspect
|
36
|
+
|
37
|
+
deb = array[0]
|
38
|
+
fin = array[2]
|
39
|
+
|
40
|
+
vary = array[1]
|
41
|
+
vary.gsub!(Regexp.union(spin_begin, spin_end), '')
|
42
|
+
varies = vary.split(Regexp.new(spin_or))
|
43
|
+
|
44
|
+
varies.map! do |vary|
|
45
|
+
spin_a_level([deb + vary + fin], level)
|
46
|
+
end
|
47
|
+
|
48
|
+
varies
|
49
|
+
end
|
50
|
+
|
51
|
+
text_or_array.flatten
|
52
|
+
end
|
53
|
+
|
54
|
+
def spin_all_level(text_or_array, from_level)
|
55
|
+
text_or_array = [text_or_array] unless text_or_array.is_a? Array
|
56
|
+
return text_or_array if from_level == 0
|
57
|
+
|
58
|
+
(1..from_level).reverse_each do |level|
|
59
|
+
text_or_array = spin_a_level(text_or_array, level)
|
60
|
+
end
|
61
|
+
|
62
|
+
text_or_array
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
String.class_eval do
|
69
|
+
def spin
|
70
|
+
ContentSpinning.spin(self)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: content_spinning
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Maxime Garcia
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-29 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! " To spin some text, mainly for SEO purpose.\n\n Spinning the
|
15
|
+
string \"Hi {there|you}! I'm {efficient|productive}.\" gives\n these four strings
|
16
|
+
:\n\n * Hi there! I'm efficient.\n * Hi there! I'm productive.\n * Hi you!
|
17
|
+
I'm efficient.\n * Hi you! I'm productive.\n"
|
18
|
+
email:
|
19
|
+
- maxime.garcia@maxbusiness.fr
|
20
|
+
executables: []
|
21
|
+
extensions: []
|
22
|
+
extra_rdoc_files: []
|
23
|
+
files:
|
24
|
+
- README.md
|
25
|
+
- LICENSE
|
26
|
+
- lib/content_spinning/version.rb
|
27
|
+
- lib/content_spinning.rb
|
28
|
+
homepage: http://github.com/maximeg/content_spinning
|
29
|
+
licenses: []
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 1.8.15
|
49
|
+
signing_key:
|
50
|
+
specification_version: 3
|
51
|
+
summary: Content Spinning
|
52
|
+
test_files: []
|