summary 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,4 @@
1
+ v0.6.0. Code review, Removed ActiveSupport dependence
2
+ v0.5.0. Added support for specific terminator (including html), Replaced the test units to Rspec specs, Now the summary method builds an object Summarize, Better support for versioning (MAJOR, TINY, PATCH constants)
3
+ v0.2. Adding the support as Ruby on Rails plugin
4
+ v0.1. First release!
data/Manifest ADDED
@@ -0,0 +1,9 @@
1
+ CHANGELOG
2
+ Manifest
3
+ README.rdoc
4
+ Rakefile
5
+ lib/summary.rb
6
+ rails/init.rb
7
+ spec/spec_helper.rb
8
+ spec/summary_spec.rb
9
+ summary.gemspec
data/README.rdoc ADDED
@@ -0,0 +1,62 @@
1
+ = Summary
2
+
3
+ == Description:
4
+
5
+ This is a simple gem that generates introduction text from a long text, it will always break the text at the end of the last word near to the limit you informed as argument.
6
+
7
+ == Synopsis
8
+
9
+ text = <<-TEXT
10
+ Cras eleifend sodales sem. Ut nec metus. Nulla sed nisl.
11
+ Praesent malesuada dui rhoncus quam. Aliquam ultricies. Praesent commodo.
12
+
13
+ Nam nec pede. Pellentesque egestas, urna vel accumsan venenatis, mi metus sagittis augue,
14
+ sed hendrerit nunc felis eget neque. Fusce vel massa. Quisque ligula enim,
15
+ tempor sed, lacinia non, malesuada ac, enim. Curabitur at enim scelerisque
16
+ neque egestas luctus. Mauris lacinia varius diam.'
17
+ TEXT
18
+
19
+ text.summary # Cras eleifend sodales sem. Ut nec metus. Nulla sed nisl. Praesent malesuada dui rhoncus quam...
20
+ text.summary(50) # Cras eleifend sodales sem. Ut nec metus. Nulla...
21
+ text.summary(50, ' :)') # Cras eleifend sodales sem. Ut nec metus. Nulla :)
22
+ text.summary(50, '... <a href="/page.html">read more</a>') # Cras eleifend sodales sem. Ut nec... <a href="/page.html">read more</a>
23
+
24
+ == Requirements
25
+
26
+ * Ruby
27
+
28
+ == Install
29
+
30
+ === as Ruby on Rails plugin
31
+
32
+ script/plugin install git://github.com/azisaka/summary.git
33
+
34
+ == Collaborators
35
+
36
+ * Bruno Azisaka Maciel
37
+ * Marcio Trindade [http://marciotrindade.com]
38
+
39
+ == License
40
+
41
+ (The MIT License)
42
+
43
+ Copyright (c) 2008 Philipe Casarotte
44
+
45
+ Permission is hereby granted, free of charge, to any person obtaining
46
+ a copy of this software and associated documentation files (the
47
+ 'Software'), to deal in the Software without restriction, including
48
+ without limitation the rights to use, copy, modify, merge, publish,
49
+ distribute, sublicense, and/or sell copies of the Software, and to
50
+ permit persons to whom the Software is furnished to do so, subject to
51
+ the following conditions:
52
+
53
+ The above copyright notice and this permission notice shall be
54
+ included in all copies or substantial portions of the Software.
55
+
56
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
57
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
58
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
59
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
60
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
61
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
62
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'echoe'
2
+ Echoe.new('summary') do |p|
3
+ p.project = 'summary'
4
+ p.author = 'Bruno Azisaka Maciel'
5
+ p.email = 'bruno [at] bubble [dot] com [dot] br'
6
+ p.url = 'http://github.com/azisaka/summary'
7
+ p.summary = 'This is a simple gem that generates introduction text from a long text, it will always break the text at the end of the last word near to the limit you informed as argument.'
8
+ # p.url = ''
9
+ # p.docs_host = ''
10
+ p.retain_gemspec = true
11
+ end
data/lib/summary.rb ADDED
@@ -0,0 +1,62 @@
1
+ module Summary
2
+ MAJOR = '0'
3
+ TINY = '6'
4
+ PATCH = '0'
5
+ VERSION = [MAJOR, TINY, PATCH] * '.'
6
+
7
+ module String
8
+ # This method sanitizes the string removing html tags, \t \n \r characters, and duplicated blank spaces.
9
+ # To use it just do:
10
+ #
11
+ # "your string".summary
12
+ #
13
+ # or with a custom limit
14
+ #
15
+ # "your string".summary(10)
16
+ #
17
+ # or with a custom limit and another terminator
18
+ #
19
+ # "your string".summary(10, ' :)')
20
+ #
21
+ # or with a custom limit and a html terminator
22
+ #
23
+ # "your string".summary(10, '... <a href="#">read more</a>')
24
+ def summary(size=100, terminator = '...')
25
+ Summarize.new(self, size, terminator).summary
26
+ end
27
+ end
28
+
29
+ class Summarize
30
+ def initialize(text, size = 100, terminator = '...')
31
+ @text = text
32
+ @size = size
33
+ @terminator = terminator
34
+ end
35
+
36
+ # Checks the need of "summarize" the string. When it is needed, the string is splitted.
37
+ # Then the last dot is removed and the separator and terminator are pushed into the resultant string.
38
+ def summary
39
+ if pure.size > @size
40
+ @text = pure[0...pure[0..(@size-backspace)].rindex(' ')]
41
+ @text.gsub(/\.$/,'') + @terminator
42
+ else
43
+ pure
44
+ end
45
+ end
46
+
47
+ protected
48
+ # It cleans up the text removing the html tags, break lines and white spaces
49
+ def pure
50
+ @pure ||= @text.strip.gsub(/<(.|\n)+?>|(\t|\n|\r)+/,'').gsub(/\s+/,' ')
51
+ end
52
+
53
+ # Measures the space needed by the terminator.
54
+ # Let's say you want a string with 50 chars, and your terminator is a '...'.
55
+ # That means your string can only have 47 chars + 3 chars from your terminator.
56
+ def backspace
57
+ (@terminator =~ /<(.|\n)+?>/ ? @terminator.gsub(/<(.|\n)+?>/,'') : @terminator).size
58
+ end
59
+ end
60
+ end
61
+
62
+ String.send(:include, Summary::String)
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/../lib/summary'
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require File.dirname(__FILE__) + '/../lib/summary'
3
+
4
+ module Summary
5
+ module Matchers
6
+ class SummarizableOf
7
+ def initialize(size)
8
+ @size = size
9
+ end
10
+
11
+ def matches?(actual)
12
+ @actual = actual
13
+ actual.gsub(/<(.|\n)+?>/,'').size.should <= @size
14
+ end
15
+ end
16
+
17
+ def be_summarizable_of(size)
18
+ SummarizableOf.new(size)
19
+ end
20
+ end
21
+ end
22
+
23
+ begin
24
+ require 'spec'
25
+ Spec::Runner.configure { |config| config.include(Summary::Matchers) }
26
+ rescue LoadError
27
+ require 'rspec'
28
+ RSpec.configure { |config| config.include(Summary::Matchers) }
29
+ end
@@ -0,0 +1,217 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ describe Summary do
4
+ it 'should be version 0.6.0' do
5
+ subject::VERSION.should == '0.6.0'
6
+ end
7
+ end
8
+
9
+ describe Summary, 'on plain text' do
10
+ before do
11
+ @text = 'Cras eleifend sodales sem. Ut nec metus. Nulla sed nisl. Praesent malesuada dui rhoncus quam. Aliquam ultricies. Praesent commodo. Nam nec pede. Pellentesque egestas, urna vel accumsan venenatis, mi metus sagittis augue, sed hendrerit nunc felis eget neque. Fusce vel massa. Quisque ligula enim, tempor sed, lacinia non, malesuada ac, enim. Curabitur at enim scelerisque neque egestas luctus. Mauris lacinia varius diam.'
12
+ end
13
+
14
+ context "by default" do
15
+ before do
16
+ @summarized = @text.summary
17
+ end
18
+
19
+ it 'should break the text' do
20
+ @summarized.should == 'Cras eleifend sodales sem. Ut nec metus. Nulla sed nisl. Praesent malesuada dui rhoncus quam...'
21
+ end
22
+
23
+ it "should have 100 chars" do
24
+ @summarized.should be_summarizable_of(100)
25
+ end
26
+ end
27
+
28
+ context "limited to 50 chars" do
29
+ before do
30
+ @summarized = @text.summary(50)
31
+ end
32
+
33
+ it "should break the text" do
34
+ @summarized.should == 'Cras eleifend sodales sem. Ut nec metus. Nulla...'
35
+ end
36
+
37
+ it "should have 50 chars" do
38
+ @summarized.should be_summarizable_of(50)
39
+ end
40
+ end
41
+ end
42
+
43
+
44
+ describe Summary, 'on html' do
45
+ before do
46
+ @text = 'Cras <strong>eleifend sodales sem</strong>. <img src="image.jpg" /><h1>Ut</h1> nec metus. <br />Nulla sed<p> nisl. Praesent malesuada</p> dui rhoncus quam. Aliquam ultricies. Praesent commodo. Nam nec pede. Pellentesque egestas, urna vel accumsan venenatis, mi metus sagittis augue, <strong>sed</strong> hendrerit nunc felis eget neque. Fusce vel massa. Quisque ligula enim, tempor sed, lacinia non, malesuada ac, enim. Curabitur at enim scelerisque neque egestas luctus. Mauris lacinia varius diam.'
47
+ end
48
+
49
+ context "by default" do
50
+ before do
51
+ @summarized = @text.summary
52
+ end
53
+
54
+ it "should break the text" do
55
+ @summarized.should == 'Cras eleifend sodales sem. Ut nec metus. Nulla sed nisl. Praesent malesuada dui rhoncus quam...'
56
+ end
57
+
58
+ it "should have 100 chars" do
59
+ @summarized.should be_summarizable_of(100)
60
+ end
61
+ end
62
+
63
+ context "limited to 50 chars" do
64
+ before do
65
+ @summarized = @text.summary(50)
66
+ end
67
+
68
+ it "should break the text" do
69
+ @summarized.should == 'Cras eleifend sodales sem. Ut nec metus. Nulla...'
70
+ end
71
+
72
+ it 'should have 50 chars' do
73
+ @summarized.should be_summarizable_of(50)
74
+ end
75
+ end
76
+ end
77
+
78
+ describe Summary, 'on complex text' do
79
+ before do
80
+ @text = <<-BREAK
81
+ Cras <strong>eleifend sodales sem</strong>.
82
+
83
+ <img src="image.jpg" /><h1>Ut</h1> nec metus.
84
+ <br />Nulla sed<p> nisl. Praesent malesuada</p> dui rhoncus quam. Aliquam ultricies.
85
+
86
+ Praesent commodo. Nam nec pede. Pellentesque egestas, urna vel accumsan venenatis, mi metus sagittis augue, <strong>sed</strong> hendrerit nunc felis eget neque. Fusce vel massa. Quisque ligula enim, tempor sed, lacinia non, malesuada ac, enim. Curabitur at enim scelerisque neque egestas luctus. Mauris lacinia varius diam.
87
+ BREAK
88
+ end
89
+
90
+ context "by default" do
91
+ before do
92
+ @summarized = @text.summary
93
+ end
94
+
95
+ it "should break the text" do
96
+ @summarized.should == 'Cras eleifend sodales sem. Ut nec metus. Nulla sed nisl. Praesent malesuada dui rhoncus quam...'
97
+ end
98
+
99
+ it 'should have 100 chars' do
100
+ @summarized.should be_summarizable_of(100)
101
+ end
102
+ end
103
+
104
+ context "limited to 50 chars" do
105
+ before do
106
+ @summarized = @text.summary(50)
107
+ end
108
+
109
+ it "should break the text" do
110
+ @summarized.should == 'Cras eleifend sodales sem. Ut nec metus. Nulla...'
111
+ end
112
+
113
+ it 'should have 50 chars' do
114
+ @summarized.should be_summarizable_of(50)
115
+ end
116
+ end
117
+ end
118
+
119
+ describe Summary, 'on tiny text' do
120
+ before do
121
+ @text = 'Praesent commodo. Nam nec pede. Pellentesque egestas, urna vel accumsan venenatis.'
122
+ end
123
+
124
+ context "by default" do
125
+ before do
126
+ @summarized = @text.summary
127
+ end
128
+
129
+ it "should not break the text" do
130
+ @summarized.should == 'Praesent commodo. Nam nec pede. Pellentesque egestas, urna vel accumsan venenatis.'
131
+ end
132
+
133
+ it "should have 100 chars" do
134
+ @summarized.should be_summarizable_of(100)
135
+ end
136
+ end
137
+
138
+ context "limited to 50 chars" do
139
+ before do
140
+ @summarized = @text.summary(50)
141
+ end
142
+
143
+ it "should break the text" do
144
+ @summarized.should == 'Praesent commodo. Nam nec pede. Pellentesque...'
145
+ end
146
+
147
+ it "should have 50 chars" do
148
+ @summarized.should be_summarizable_of(50)
149
+ end
150
+ end
151
+ end
152
+
153
+
154
+ describe Summary, 'with different terminators' do
155
+ before do
156
+ @text = 'Cras eleifend sodales sem. Ut nec metus. Nulla sed nisl. Praesent malesuada dui rhoncus quam. Aliquam ultricies. Praesent commodo. Nam nec pede. Pellentesque egestas, urna vel accumsan venenatis, mi metus sagittis augue, sed hendrerit nunc felis eget neque. Fusce vel massa. Quisque ligula enim, tempor sed, lacinia non, malesuada ac, enim. Curabitur at enim scelerisque neque egestas luctus. Mauris lacinia varius diam.'
157
+ end
158
+
159
+ context "limited to 50 chars" do
160
+ context "with !" do
161
+ before do
162
+ @summarized = @text.summary(50, '!')
163
+ end
164
+
165
+ it "should end with !" do
166
+ @summarized.should == 'Cras eleifend sodales sem. Ut nec metus. Nulla!'
167
+ end
168
+
169
+ it "should have 50 chars" do
170
+ @summarized.should be_summarizable_of 50
171
+ end
172
+ end
173
+
174
+ context "with ." do
175
+ before do
176
+ @summarized = @text.summary(50, '.')
177
+ end
178
+
179
+ it "should end with ." do
180
+ @summarized.should == 'Cras eleifend sodales sem. Ut nec metus. Nulla.'
181
+ end
182
+
183
+ it "should have 50 chars" do
184
+ @summarized.should be_summarizable_of(50)
185
+ end
186
+ end
187
+
188
+ context "with nothing" do
189
+ before do
190
+ @summarized = @text.summary(50, '')
191
+ end
192
+
193
+ it 'should end with nothing' do
194
+ @summarized.should == 'Cras eleifend sodales sem. Ut nec metus. Nulla sed'
195
+ end
196
+
197
+ it "should have 50 chars" do
198
+ @summarized.should be_summarizable_of(50)
199
+ end
200
+ end
201
+
202
+ context "with html" do
203
+ before do
204
+ @summarized = @text.summary(50, '... <a href="#read-more">read more</a>')
205
+ end
206
+
207
+ it "should end with ...<a href=\"#read-more\">read more</a>" do
208
+ @summarized.should == 'Cras eleifend sodales sem. Ut nec... <a href="#read-more">read more</a>'
209
+ end
210
+
211
+ it "should have 50 chars" do
212
+ @summarized.should be_summarizable_of(50)
213
+ end
214
+ end
215
+ end
216
+ end
217
+
data/summary.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{summary}
5
+ s.version = "0.6.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Bruno Azisaka Maciel"]
9
+ s.cert_chain = ["/Users/Bruno/.certs/gem-public_cert.pem"]
10
+ s.date = %q{2010-07-09}
11
+ s.description = %q{This is a simple gem that generates introduction text from a long text, it will always break the text at the end of the last word near to the limit you informed as argument.}
12
+ s.email = %q{bruno [at] bubble [dot] com [dot] br}
13
+ s.extra_rdoc_files = ["CHANGELOG", "README.rdoc", "lib/summary.rb"]
14
+ s.files = ["CHANGELOG", "Manifest", "README.rdoc", "Rakefile", "lib/summary.rb", "rails/init.rb", "spec/spec_helper.rb", "spec/summary_spec.rb", "summary.gemspec"]
15
+ s.homepage = %q{http://github.com/azisaka/summary}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Summary", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{summary}
19
+ s.rubygems_version = %q{1.3.7}
20
+ s.signing_key = %q{/Users/Bruno/.certs/gem-private_key.pem}
21
+ s.summary = %q{This is a simple gem that generates introduction text from a long text, it will always break the text at the end of the last word near to the limit you informed as argument.}
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 3
26
+
27
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
28
+ else
29
+ end
30
+ else
31
+ end
32
+ end
data.tar.gz.sig ADDED
Binary file
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: summary
3
+ version: !ruby/object:Gem::Version
4
+ hash: 7
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 6
9
+ - 0
10
+ version: 0.6.0
11
+ platform: ruby
12
+ authors:
13
+ - Bruno Azisaka Maciel
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain:
17
+ - |
18
+ -----BEGIN CERTIFICATE-----
19
+ MIIDVjCCAj6gAwIBAgIBADANBgkqhkiG9w0BAQUFADBRMQ4wDAYDVQQDDAVicnVu
20
+ bzEWMBQGCgmSJomT8ixkARkWBmJ1YmJsZTETMBEGCgmSJomT8ixkARkWA2NvbTES
21
+ MBAGCgmSJomT8ixkARkWAmJyMB4XDTEwMDcwOTAzMDgzMloXDTExMDcwOTAzMDgz
22
+ MlowUTEOMA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able:
41
+ dependencies: []
42
+
43
+ description: This is a simple gem that generates introduction text from a long text, it will always break the text at the end of the last word near to the limit you informed as argument.
44
+ email: bruno [at] bubble [dot] com [dot] br
45
+ executables: []
46
+
47
+ extensions: []
48
+
49
+ extra_rdoc_files:
50
+ - CHANGELOG
51
+ - README.rdoc
52
+ - lib/summary.rb
53
+ files:
54
+ - CHANGELOG
55
+ - Manifest
56
+ - README.rdoc
57
+ - Rakefile
58
+ - lib/summary.rb
59
+ - rails/init.rb
60
+ - spec/spec_helper.rb
61
+ - spec/summary_spec.rb
62
+ - summary.gemspec
63
+ has_rdoc: true
64
+ homepage: http://github.com/azisaka/summary
65
+ licenses: []
66
+
67
+ post_install_message:
68
+ rdoc_options:
69
+ - --line-numbers
70
+ - --inline-source
71
+ - --title
72
+ - Summary
73
+ - --main
74
+ - README.rdoc
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 11
92
+ segments:
93
+ - 1
94
+ - 2
95
+ version: "1.2"
96
+ requirements: []
97
+
98
+ rubyforge_project: summary
99
+ rubygems_version: 1.3.7
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: This is a simple gem that generates introduction text from a long text, it will always break the text at the end of the last word near to the limit you informed as argument.
103
+ test_files: []
104
+
metadata.gz.sig ADDED
@@ -0,0 +1,3 @@
1
+ ��8�� ����߀_Lnvi���#��:����F'P��p{R�%/0qP��&'�}J � �W��U�g�v�:.8
2
+ ç�Q����H6�M�Q��j�yH��U�O��I�!�
3
+ ]M�;�/��\C#�̽��������}��n�~Co�q� +�g