smart_excerpt 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +67 -0
- data/Rakefile +2 -0
- data/lib/smart_excerpt.rb +75 -0
- data/lib/smart_excerpt/helper.rb +30 -0
- data/lib/smart_excerpt/util.rb +28 -0
- data/lib/smart_excerpt/version.rb +3 -0
- data/smart_excerpt.gemspec +26 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1030e6791609813f86fbddb62230288bc284da2a
|
4
|
+
data.tar.gz: 7da1ff34432fae665f1ed103c1cb00ab57ab188f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dcd5703df6a4878197600218d86d60d591bd31756171b6de1b4a788df4c1b7cd16bfee3aadaaa4ac6f91d4a3a1bd365f34fb4d445a744d58c3b81ccd1e65da13
|
7
|
+
data.tar.gz: 79774148a0db326c9c98effb087467529716f63997943fc5c0341d44ddbce59001db0a36216c45ca6a404c543e8f368b3e5fdee5eac7f40d8fe956a999cd4f21
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 glebtv
|
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,67 @@
|
|
1
|
+
[](http://badge.fury.io/rb/smart_excerpt)
|
2
|
+
|
3
|
+
# SmartExcerpt
|
4
|
+
|
5
|
+
Allows to create intellegent excerpt fields.
|
6
|
+
|
7
|
+
This is an extraction from [RocketCMS](https://github.com/rs-pro/rocket_cms)
|
8
|
+
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'smart_excerpt'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install smart_excerpt
|
25
|
+
|
26
|
+
## What it does
|
27
|
+
|
28
|
+
You have two fields: a mandatory ```text``` and an optional ```excerpt```.
|
29
|
+
If your ```excerpt``` field is empty, it generates an excerpt of a given length from ```text```.
|
30
|
+
It will also truncate excerpts if they are more than 20% longer then allowed value
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
Add two fields: ```excerpt``` and ```text```
|
35
|
+
|
36
|
+
Add it to your model:
|
37
|
+
|
38
|
+
class Article
|
39
|
+
...
|
40
|
+
include SmartExcerpt
|
41
|
+
smart_excerpt :excerpt, :text
|
42
|
+
end
|
43
|
+
|
44
|
+
Then display excerpt:
|
45
|
+
|
46
|
+
Article.first.get_excerpt
|
47
|
+
|
48
|
+
All excerpts are passed through strip_tags and html entity decode.
|
49
|
+
|
50
|
+
## Options
|
51
|
+
|
52
|
+
Article.first.get_excerpt(25) # same as {words: 25}
|
53
|
+
Article.first.get_excerpt(letters: 25)
|
54
|
+
Article.first.get_excerpt(sentences: 5)
|
55
|
+
Article.first.get_excerpt(trust_multiplier: 3) # allow for much longer manual excerpts
|
56
|
+
Article.first.get_excerpt(trust_excerpts: true) # allow any manual excerpts
|
57
|
+
Article.first.get_excerpt(keep_headers: true) # do not remove HTML headers from excerpt
|
58
|
+
Article.first.get_excerpt(keep_newlines: true) # do not remove newlines from excerpt
|
59
|
+
Article.first.get_excerpt(keep_html: true) # do not remove HTML tags from excerpt (might be unsafe/dangerous!)
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
1. Fork it ( https://github.com/[my-github-username]/smart_excerpt/fork )
|
64
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
65
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
66
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
67
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require "smart_excerpt/version"
|
2
|
+
require 'htmlentities'
|
3
|
+
require "smart_excerpt/util"
|
4
|
+
require "smart_excerpt/helper"
|
5
|
+
|
6
|
+
module SmartExcerpt
|
7
|
+
extend Util
|
8
|
+
|
9
|
+
@@h = Helper.new
|
10
|
+
@@he = HTMLEntities.new
|
11
|
+
mattr_reader :h
|
12
|
+
mattr_reader :he
|
13
|
+
|
14
|
+
def self.included(base)
|
15
|
+
base.send(:extend, ClassMethods)
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
def smart_truncate(obj, base_field, excerpt_field, words)
|
20
|
+
trust_multiplier = 1.2
|
21
|
+
if words.is_a?(Hash) && words[:trust_multiplier]
|
22
|
+
trust_multiplier = words[:trust_multiplier]
|
23
|
+
end
|
24
|
+
|
25
|
+
if obj.send(excerpt_field).blank?
|
26
|
+
tx = obj.send(base_field)
|
27
|
+
else
|
28
|
+
tx = obj.send(excerpt_field)
|
29
|
+
if words.is_a?(Fixnum)
|
30
|
+
words *= trust_multiplier
|
31
|
+
elsif words.is_a?(Hash)
|
32
|
+
if words[:trust_excerpts]
|
33
|
+
words = {words: Float::INFINITY}
|
34
|
+
else
|
35
|
+
words = Hash[words.map {|k, v| [k, v.is_a?(Fixnum) ? (v * trust_multiplier).to_i : v] }]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
if words.is_a?(Numeric)
|
41
|
+
options = {words: words.to_i}
|
42
|
+
elsif words.is_a?(Hash)
|
43
|
+
options = words
|
44
|
+
else
|
45
|
+
raise 'bad parameter for get_excerpt'
|
46
|
+
end
|
47
|
+
|
48
|
+
if tx.blank?
|
49
|
+
''
|
50
|
+
else
|
51
|
+
# kill headers and newlines
|
52
|
+
unless options[:keep_headers]
|
53
|
+
tx = tx.gsub(/<h\d[^>]*?>(.*?)<\/h\d>/mi, '')
|
54
|
+
end
|
55
|
+
unless options[:keep_newlines]
|
56
|
+
tx = tx.gsub("\n", ' ').gsub("\r", '').gsub("\t", '').strip
|
57
|
+
end
|
58
|
+
tx = @@he.decode(tx)
|
59
|
+
unless options[:keep_html]
|
60
|
+
tx = @@h.strip_tags(tx)
|
61
|
+
end
|
62
|
+
@@h.smart_truncate(tx, options)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
module ClassMethods
|
68
|
+
def smart_excerpt(excerpt_field, base_field, words = 25)
|
69
|
+
define_method("get_#{excerpt_field}".to_sym) do |c_words = words|
|
70
|
+
smart_truncate(self, base_field, excerpt_field, c_words)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module SmartExcerpt
|
2
|
+
class Helper
|
3
|
+
include ActionView::Helpers::TextHelper
|
4
|
+
|
5
|
+
def smart_truncate(s, opts = {})
|
6
|
+
return '' if s.blank?
|
7
|
+
opts = {words: 25}.merge(opts)
|
8
|
+
if opts[:sentences]
|
9
|
+
return s.split(/\.(\s|$)+/)[0, opts[:sentences]].map{|s| s.strip}.join('. ') + '.'
|
10
|
+
end
|
11
|
+
if opts[:letters]
|
12
|
+
return truncate(s, length: opts[:letters], separator: ' ', omission: '...')
|
13
|
+
end
|
14
|
+
n = opts[:words]
|
15
|
+
if n === Float::INFINITY
|
16
|
+
return s
|
17
|
+
end
|
18
|
+
a = s.split(/\s/) # or /[ ]+/ to only split on spaces
|
19
|
+
r = a[0...n].join(' ') + (a.size > n ? '...' : '')
|
20
|
+
|
21
|
+
# replace with regular spaces
|
22
|
+
r.gsub!(' ', ' ')
|
23
|
+
|
24
|
+
# strip newlines
|
25
|
+
r = r.strip.gsub("\r", '').gsub("\n", ' ')
|
26
|
+
|
27
|
+
r.gsub(/\s+/, ' ')
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module SmartExcerpt
|
2
|
+
module Util
|
3
|
+
def decode(str)
|
4
|
+
SmartExcerpt.he.decode(str)
|
5
|
+
end
|
6
|
+
|
7
|
+
def encode(str)
|
8
|
+
SmartExcerpt.he.encode(str)
|
9
|
+
end
|
10
|
+
|
11
|
+
def strip_tags(str)
|
12
|
+
SmartExcerpt.h.strip_tags(str)
|
13
|
+
end
|
14
|
+
|
15
|
+
def decode_and_strip(str)
|
16
|
+
self.strip_tags(self.decode(str))
|
17
|
+
end
|
18
|
+
|
19
|
+
def truncate(str, opts = {})
|
20
|
+
return '' if str.blank?
|
21
|
+
tx = str.gsub(/<h\d[^>]*?>(.*)<\/h\d>/mi, '').gsub("\n", ' ').gsub("\r", '').gsub("\t", '').strip
|
22
|
+
tx = SmartExcerpt.he.decode(tx)
|
23
|
+
tx = SmartExcerpt.h.strip_tags(tx)
|
24
|
+
SmartExcerpt.h.smart_truncate(tx, opts)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'smart_excerpt/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "smart_excerpt"
|
8
|
+
spec.version = SmartExcerpt::VERSION
|
9
|
+
spec.authors = ["glebtv"]
|
10
|
+
spec.email = ["glebtv@gmail.com"]
|
11
|
+
spec.summary = %q{Allows to create intellegent excerpt fields.}
|
12
|
+
spec.description = %q{}
|
13
|
+
spec.homepage = "https://github.com/rs-pro/smart_excerpt"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'activesupport'
|
22
|
+
spec.add_dependency 'htmlentities'
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: smart_excerpt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- glebtv
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: htmlentities
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: ''
|
70
|
+
email:
|
71
|
+
- glebtv@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/smart_excerpt.rb
|
82
|
+
- lib/smart_excerpt/helper.rb
|
83
|
+
- lib/smart_excerpt/util.rb
|
84
|
+
- lib/smart_excerpt/version.rb
|
85
|
+
- smart_excerpt.gemspec
|
86
|
+
homepage: https://github.com/rs-pro/smart_excerpt
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.2.2
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: Allows to create intellegent excerpt fields.
|
110
|
+
test_files: []
|