elipsa 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +9 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +22 -0
- data/README.md +99 -0
- data/Rakefile +8 -0
- data/elipsa.gemspec +28 -0
- data/lib/elipsa.rb +49 -0
- data/lib/elipsa/version.rb +5 -0
- data/spec/elipsa_spec.rb +71 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6601d104f56f3358b82a1c5b5065bebbb26d64c7
|
4
|
+
data.tar.gz: f87478c90746393a7ac5db65e0107afbf1eee2bc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c2b58bd5d80a23fc94f17109e123dc258f249b4b15d730b14e94f7bdb54da8ddf951502d625408b283ef8bd57557b0ef234ec27c26cf73ade09c5d43fd83a2b6
|
7
|
+
data.tar.gz: 3fb2bd550af5215f251da8496f40faa92c38fcd3de31c08729dbec43612730bb7cd8fd16a1faad25be6e62135341ce20f4765e89636c07f696c3e2d75ce7ead5
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
source 'https://rubygems.org'
|
3
|
+
|
4
|
+
platforms :rbx do
|
5
|
+
# These are the transitive
|
6
|
+
# ruby standard library dependencies
|
7
|
+
gem 'rubysl-singleton'
|
8
|
+
|
9
|
+
# This gem has no direct rubysl dependencies
|
10
|
+
end
|
11
|
+
|
12
|
+
# Specify your gem's dependencies in elipsa.gemspec
|
13
|
+
gemspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Ryan Biesemeyer
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
# Elipsa
|
2
|
+
|
3
|
+
Elipsa is a tool for conditionally and smartly truncating a string to a
|
4
|
+
specific length. It pays attention to word-boundaries, and tries to give
|
5
|
+
you as much of the string as it can, while still fitting your parameters.
|
6
|
+
|
7
|
+
The name of this gem is derived from the Serbo-Croatian [`елипса`][elipsa],
|
8
|
+
which can be translated directly to English `ellipsis`.
|
9
|
+
|
10
|
+
[elipsa]: http://en.wiktionary.org/wiki/elipsa
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
gem 'elipsa'
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install elipsa
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
~~~ ruby
|
29
|
+
require 'elipsa'
|
30
|
+
extend Elipsa # (note: `self` is `main`)
|
31
|
+
|
32
|
+
# it leaves short strings alone
|
33
|
+
elipsa("a short string", length: 20)
|
34
|
+
# => "a short string"
|
35
|
+
|
36
|
+
# and breaks at word-boundaries when it can
|
37
|
+
elipsa("a longer string that goes over the limit", length: 20)
|
38
|
+
# => "a longer string..."
|
39
|
+
|
40
|
+
# but never truncates too much
|
41
|
+
elipsa("supercalifragilisticexpialidocious", length: 20)
|
42
|
+
# => "supercalifragilis..."
|
43
|
+
|
44
|
+
# and it never chops when it doesn't need to.
|
45
|
+
elipsa("nineteen characters", length: 20)
|
46
|
+
# => "nineteen characters"
|
47
|
+
~~~
|
48
|
+
|
49
|
+
## Advanced Usage
|
50
|
+
|
51
|
+
### Length
|
52
|
+
|
53
|
+
The `:length` parameter is optional and defaults to 80 characters.
|
54
|
+
|
55
|
+
~~~ ruby
|
56
|
+
elipsa(lorem_ipsum)
|
57
|
+
# => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis mattis semper..."
|
58
|
+
|
59
|
+
elipsa(lorem_ipsum, length: 25)
|
60
|
+
# => "Lorem ipsum dolor sit..."
|
61
|
+
~~~
|
62
|
+
|
63
|
+
### Symbol
|
64
|
+
|
65
|
+
The `:symbol` parameter is optional and defaults to the three-character
|
66
|
+
sequence `...`.
|
67
|
+
|
68
|
+
~~~ ruby
|
69
|
+
elipsa(lorem_ipsum, length: 20)
|
70
|
+
# => "Lorem ipsum..."
|
71
|
+
|
72
|
+
elipsa(lorem_ipsum, symbol: '…', length: 20)
|
73
|
+
# => "Lorem ipsum dolor…"
|
74
|
+
~~~
|
75
|
+
|
76
|
+
### Ratio
|
77
|
+
|
78
|
+
The `:ratio` parameter is option and defaults to the rational `7/8`. This
|
79
|
+
parameter helps elipsa know when to abandon word-boundary splitting and just
|
80
|
+
show as much as possible. If `word-split-length`:`desired-length` ratio
|
81
|
+
falls below the `:ratio`, word-split is not used.
|
82
|
+
|
83
|
+
~~~ ruby
|
84
|
+
# without ratio support, too much could get truncated when there is no
|
85
|
+
# word boundary near the desired length.
|
86
|
+
elipsa("the word is supercalifragilisticexpialidocious", length: 40, ratio: 0)
|
87
|
+
# => "the word is..."
|
88
|
+
|
89
|
+
elipsa("merge branch feature/ISSUE-12345/fix-important-issue", length: 40)
|
90
|
+
# => "the word is supercalifragilisticexpia..."
|
91
|
+
~~~
|
92
|
+
|
93
|
+
## Contributing
|
94
|
+
|
95
|
+
1. Fork it
|
96
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
97
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
98
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
99
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/elipsa.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'elipsa/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'elipsa'
|
8
|
+
spec.version = Elipsa::VERSION
|
9
|
+
spec.authors = ['Ryan Biesemeyer']
|
10
|
+
spec.email = ['ryan@yaauie.com']
|
11
|
+
spec.summary = 'Smart string truncation'
|
12
|
+
spec.description = <<-EODESC
|
13
|
+
Elipsa is a tool for conditionally and smartly truncating a string to a
|
14
|
+
specific length. It pays attention to word-boundaries, and tries to give
|
15
|
+
you as much of the string as it can, while still fitting your parameters.
|
16
|
+
EODESC
|
17
|
+
spec.homepage = 'https://github.com/yaauie/elipsa'
|
18
|
+
spec.license = 'MIT'
|
19
|
+
|
20
|
+
spec.files = `git ls-files`.split($/)
|
21
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
22
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
26
|
+
spec.add_development_dependency 'rake'
|
27
|
+
spec.add_development_dependency 'rspec', '~> 2.14'
|
28
|
+
end
|
data/lib/elipsa.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'elipsa/version'
|
4
|
+
|
5
|
+
module Elipsa
|
6
|
+
extend self
|
7
|
+
|
8
|
+
# @param str [String]
|
9
|
+
# @param options [Hash{Symbol=>Object}]
|
10
|
+
# @option options [Integer] :length (80)
|
11
|
+
# The maximum length of the result string,
|
12
|
+
# including an ellipsis if present.
|
13
|
+
# @option options [String] :symbol ('...')
|
14
|
+
# The string to use as an ellipsis symbol
|
15
|
+
# @option options [Numeric] :ratio (7/8)
|
16
|
+
# The ratio of available space that the
|
17
|
+
# resulting string must fill, if truncated.
|
18
|
+
# This ensures that if a word-boundary is
|
19
|
+
# not found close enough to your length
|
20
|
+
# to make the result meaningful, the string
|
21
|
+
# is broken mid-word instead.
|
22
|
+
def elipsa(str, options = {})
|
23
|
+
max_length = options.fetch(:length) { 80 }
|
24
|
+
return str if str.length <= max_length
|
25
|
+
|
26
|
+
symbol = options.fetch(:symbol) { '...' }
|
27
|
+
available = max_length - symbol.length
|
28
|
+
ratio = options.fetch(:ratio) { Rational(7, 8) }
|
29
|
+
|
30
|
+
substr = str[/\A(.{0,#{available}})(?=\b\W+)/mu] || ''
|
31
|
+
|
32
|
+
if Rational(substr.length, available) < ratio
|
33
|
+
substr = str[0...available]
|
34
|
+
end
|
35
|
+
|
36
|
+
"#{substr}#{symbol}"
|
37
|
+
end
|
38
|
+
|
39
|
+
# Extend into a String, or
|
40
|
+
# if you're feeling ambitious,
|
41
|
+
# include globally into String
|
42
|
+
module String
|
43
|
+
# Passes self as first argument to Elipsa::elipsa
|
44
|
+
# @see Elipsa::elipsa
|
45
|
+
def elipsa(options = {})
|
46
|
+
Elipsa::elipsa(self, options)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/spec/elipsa_spec.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'elipsa'
|
4
|
+
|
5
|
+
shared_examples_for :elipsa do
|
6
|
+
subject { return_value }
|
7
|
+
its(:length) { should be <= length }
|
8
|
+
it { should eq expected }
|
9
|
+
end
|
10
|
+
|
11
|
+
describe Elipsa do
|
12
|
+
context '#elipsa' do
|
13
|
+
let(:length) { 20 }
|
14
|
+
let(:params) { {length: length} }
|
15
|
+
let(:return_value) { Elipsa::elipsa(input, params)}
|
16
|
+
|
17
|
+
context 'a short string' do
|
18
|
+
let(:input) { 'a short string' }
|
19
|
+
let(:expected) { input }
|
20
|
+
|
21
|
+
it_should_behave_like :elipsa
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'word-boundary' do
|
25
|
+
let(:input) { 'a longer string that goes over the limit' }
|
26
|
+
let(:expected) { 'a longer string...' }
|
27
|
+
|
28
|
+
it_should_behave_like :elipsa
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'mid-large-word' do
|
32
|
+
let(:input) { 'supercalifragilisticexpialidocious' }
|
33
|
+
let(:expected) { 'supercalifragilis...' }
|
34
|
+
|
35
|
+
it_should_behave_like :elipsa
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'exact length' do
|
39
|
+
let(:input) { 'at twenty characters' }
|
40
|
+
let(:expected) { input }
|
41
|
+
|
42
|
+
it_should_behave_like :elipsa
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'one less' do
|
46
|
+
let(:input) { 'nineteen characters' }
|
47
|
+
let(:expected) { input }
|
48
|
+
|
49
|
+
it_should_behave_like :elipsa
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'trim punctuation' do
|
53
|
+
let(:input) { 'Let\'s go fly a kite, up to the highest height' }
|
54
|
+
# ^ this comma should not be included
|
55
|
+
let(:length) { 24 }
|
56
|
+
let(:expected) { 'Let\'s go fly a kite...' }
|
57
|
+
|
58
|
+
it_should_behave_like :elipsa
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'alternate symbol' do
|
62
|
+
before(:each) { params.merge!(symbol: '…') }
|
63
|
+
|
64
|
+
let(:input) { 'lorem ipsum dolor sit' }
|
65
|
+
# with default symbol '...', this would be 'lorem ipsum...'
|
66
|
+
let(:expected) { 'lorem ipsum dolor…' }
|
67
|
+
|
68
|
+
it_should_behave_like :elipsa
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: elipsa
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Biesemeyer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2013-12-20 00:00:00 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
prerelease: false
|
17
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: "1.3"
|
22
|
+
type: :development
|
23
|
+
version_requirements: *id001
|
24
|
+
- !ruby/object:Gem::Dependency
|
25
|
+
name: rake
|
26
|
+
prerelease: false
|
27
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
28
|
+
requirements:
|
29
|
+
- &id004
|
30
|
+
- ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id002
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: rspec
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ~>
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: "2.14"
|
43
|
+
type: :development
|
44
|
+
version_requirements: *id003
|
45
|
+
description: " Elipsa is a tool for conditionally and smartly truncating a string to a\n specific length. It pays attention to word-boundaries, and tries to give\n you as much of the string as it can, while still fitting your parameters.\n"
|
46
|
+
email:
|
47
|
+
- ryan@yaauie.com
|
48
|
+
executables: []
|
49
|
+
|
50
|
+
extensions: []
|
51
|
+
|
52
|
+
extra_rdoc_files: []
|
53
|
+
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- .travis.yml
|
57
|
+
- Gemfile
|
58
|
+
- LICENSE.txt
|
59
|
+
- README.md
|
60
|
+
- Rakefile
|
61
|
+
- elipsa.gemspec
|
62
|
+
- lib/elipsa.rb
|
63
|
+
- lib/elipsa/version.rb
|
64
|
+
- spec/elipsa_spec.rb
|
65
|
+
homepage: https://github.com/yaauie/elipsa
|
66
|
+
licenses:
|
67
|
+
- MIT
|
68
|
+
metadata: {}
|
69
|
+
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- *id004
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- *id004
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 2.1.10
|
85
|
+
signing_key:
|
86
|
+
specification_version: 4
|
87
|
+
summary: Smart string truncation
|
88
|
+
test_files:
|
89
|
+
- spec/elipsa_spec.rb
|