jekyll_html_truncatewords 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/test.yml +28 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +43 -0
- data/LICENSE.txt +21 -0
- data/README.md +42 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/jekyll_html_truncatewords.gemspec +31 -0
- data/lib/jekyll_html_truncatewords.rb +62 -0
- data/lib/jekyll_html_truncatewords/version.rb +3 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 19cd98177b7ba8c36be921e7316be12b92a5871a6ba8e244408a6b3f592a2b06
|
4
|
+
data.tar.gz: beead4981228ceb21022eb8cb6957f466c346c88a7819860e63acf2f6f24418d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 955a40e67d833d6ba70e4657a646613171f5ffb3f4994a70d87b1af643d5f0f2e8edb2bf44536cc1a4fb7654a4900a4db4e5ec13065b53c59c0debfd3764a9ef
|
7
|
+
data.tar.gz: 8e5e9c3d10e9a966ebd958cea1af3b9bf9df314032acb817667c832611de6409fe8395b9466f3f6e7ac196443c1d07391de915043fbc2836e569e92f6a8c19cb
|
@@ -0,0 +1,28 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [ master ]
|
6
|
+
pull_request:
|
7
|
+
branches: [ master ]
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
test:
|
11
|
+
|
12
|
+
runs-on: ubuntu-latest
|
13
|
+
|
14
|
+
strategy:
|
15
|
+
matrix:
|
16
|
+
ruby-version: [2.4, 2.5, 2.6, 2.7]
|
17
|
+
|
18
|
+
steps:
|
19
|
+
- uses: actions/checkout@v2
|
20
|
+
- name: Set up Ruby
|
21
|
+
uses: ruby/setup-ruby@v1
|
22
|
+
with:
|
23
|
+
bundler-cache: true
|
24
|
+
ruby-version: ${{ matrix.ruby-version }}
|
25
|
+
- name: Install dependencies
|
26
|
+
run: bundle install
|
27
|
+
- name: Run tests
|
28
|
+
run: bundle exec rake
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
jekyll_html_truncatewords (0.1.0)
|
5
|
+
liquid
|
6
|
+
liquid-c
|
7
|
+
nokogiri
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
diff-lcs (1.4.4)
|
13
|
+
liquid (4.0.3)
|
14
|
+
liquid-c (4.0.0)
|
15
|
+
liquid (>= 3.0.0)
|
16
|
+
mini_portile2 (2.4.0)
|
17
|
+
nokogiri (1.10.10)
|
18
|
+
mini_portile2 (~> 2.4.0)
|
19
|
+
rake (12.3.3)
|
20
|
+
rspec (3.10.0)
|
21
|
+
rspec-core (~> 3.10.0)
|
22
|
+
rspec-expectations (~> 3.10.0)
|
23
|
+
rspec-mocks (~> 3.10.0)
|
24
|
+
rspec-core (3.10.0)
|
25
|
+
rspec-support (~> 3.10.0)
|
26
|
+
rspec-expectations (3.10.0)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.10.0)
|
29
|
+
rspec-mocks (3.10.0)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.10.0)
|
32
|
+
rspec-support (3.10.0)
|
33
|
+
|
34
|
+
PLATFORMS
|
35
|
+
ruby
|
36
|
+
|
37
|
+
DEPENDENCIES
|
38
|
+
jekyll_html_truncatewords!
|
39
|
+
rake (~> 12.0)
|
40
|
+
rspec (~> 3.0)
|
41
|
+
|
42
|
+
BUNDLED WITH
|
43
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Mike Kasberg
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# JekyllHtmlTruncatewords
|
2
|
+
|
3
|
+
**WIP:** This gem is not ready for production use yet.`
|
4
|
+
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/jekyll/html/truncatewords`. To experiment with that code, run `bin/console` for an interactive prompt.
|
6
|
+
|
7
|
+
TODO: Delete this and the text above, and describe your gem
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'jekyll_html_truncatewords'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle install
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install jekyll_html_truncatewords
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
TODO: Write usage instructions here
|
28
|
+
|
29
|
+
## Development
|
30
|
+
|
31
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
32
|
+
|
33
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jekyll_html_truncatewords.
|
38
|
+
|
39
|
+
|
40
|
+
## License
|
41
|
+
|
42
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "jekyll/html/truncatewords"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative 'lib/jekyll_html_truncatewords/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "jekyll_html_truncatewords"
|
5
|
+
spec.version = JekyllHtmlTruncatewords::VERSION
|
6
|
+
spec.authors = ["Mike Kasberg"]
|
7
|
+
spec.email = ["mike@mikekasberg.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{A Jekyll filter to truncate HTML}
|
10
|
+
spec.description = %q{The Liquid truncatewords filter can't operate on HTML because it isn't aware of tags. But Jekyll blog posts usually contain HTML, so this makes it difficult to, for example, use the first 50 words of a blog post as the preview. jekyll_html_truncatewords solves that problem. It works the same as truncatewords, but it is aware of HTML tags so it counts words correctly within HTML and won't break HTML.}
|
11
|
+
spec.homepage = "https://github.com/mkasberg/jekyll_html_trunctewords"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = "https://github.com/mkasberg/jekyll_html_trunctewords"
|
17
|
+
spec.metadata["changelog_uri"] = "https://www.mikekasberg.com" # TODO
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = "exe"
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
|
28
|
+
spec.add_runtime_dependency 'liquid'
|
29
|
+
spec.add_runtime_dependency 'liquid-c'
|
30
|
+
spec.add_runtime_dependency 'nokogiri'
|
31
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'jekyll_html_truncatewords/version'
|
2
|
+
require 'liquid'
|
3
|
+
require 'nokogiri'
|
4
|
+
|
5
|
+
module JekyllHtmlTruncatewords
|
6
|
+
# Extending Liquid::StandardFilters allows us to call the Liquid version of truncatewords
|
7
|
+
# instead of re-creating our own.
|
8
|
+
extend Liquid::StandardFilters
|
9
|
+
|
10
|
+
extend self
|
11
|
+
|
12
|
+
# Truncate HTML input to have `words` words when rendered as HTML.
|
13
|
+
# Preserve HTML structure so tags are correctly matched.
|
14
|
+
def html_truncatewords(input, words = 15, truncate_string = "...")
|
15
|
+
words = words.to_i
|
16
|
+
fragment = Nokogiri::HTML.fragment(input)
|
17
|
+
|
18
|
+
if fragment.inner_text.split.length <= words
|
19
|
+
return input
|
20
|
+
end
|
21
|
+
|
22
|
+
truncate_node(fragment, words, truncate_string)
|
23
|
+
|
24
|
+
fragment.to_html
|
25
|
+
end
|
26
|
+
|
27
|
+
# A recursive function to truncate the node, if necessary.
|
28
|
+
# Return the words remaining after accounting for the current node.
|
29
|
+
private def truncate_node(node, words_remaining, truncate_string)
|
30
|
+
if node.text?
|
31
|
+
wordlist = node.text.to_s.split
|
32
|
+
if wordlist.length < words_remaining
|
33
|
+
return words_remaining - wordlist.length
|
34
|
+
elsif wordlist.length == words_remaining
|
35
|
+
node.content = node.content.rstrip + truncate_string
|
36
|
+
return 0
|
37
|
+
else
|
38
|
+
node.content = truncate_text(node.text, words_remaining, truncate_string)
|
39
|
+
return 0
|
40
|
+
end
|
41
|
+
else
|
42
|
+
node.children.each do |n|
|
43
|
+
if words_remaining > 0
|
44
|
+
words_remaining = truncate_node(n, words_remaining, truncate_string)
|
45
|
+
else
|
46
|
+
n.remove
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
return words_remaining
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
private def truncate_text(input, words, truncate_string)
|
55
|
+
leading_spaces = input.match(/^[ ]+/)
|
56
|
+
lead = leading_spaces.nil? ? '' : leading_spaces[0]
|
57
|
+
|
58
|
+
lead + truncatewords(input, words, truncate_string)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
Liquid::Template.register_filter(JekyllHtmlTruncatewords)
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jekyll_html_truncatewords
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Kasberg
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-12-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: liquid
|
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: liquid-c
|
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: nokogiri
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: The Liquid truncatewords filter can't operate on HTML because it isn't
|
56
|
+
aware of tags. But Jekyll blog posts usually contain HTML, so this makes it difficult
|
57
|
+
to, for example, use the first 50 words of a blog post as the preview. jekyll_html_truncatewords
|
58
|
+
solves that problem. It works the same as truncatewords, but it is aware of HTML
|
59
|
+
tags so it counts words correctly within HTML and won't break HTML.
|
60
|
+
email:
|
61
|
+
- mike@mikekasberg.com
|
62
|
+
executables: []
|
63
|
+
extensions: []
|
64
|
+
extra_rdoc_files: []
|
65
|
+
files:
|
66
|
+
- ".github/workflows/test.yml"
|
67
|
+
- ".gitignore"
|
68
|
+
- ".rspec"
|
69
|
+
- ".travis.yml"
|
70
|
+
- Gemfile
|
71
|
+
- Gemfile.lock
|
72
|
+
- LICENSE.txt
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- bin/console
|
76
|
+
- bin/setup
|
77
|
+
- jekyll_html_truncatewords.gemspec
|
78
|
+
- lib/jekyll_html_truncatewords.rb
|
79
|
+
- lib/jekyll_html_truncatewords/version.rb
|
80
|
+
homepage: https://github.com/mkasberg/jekyll_html_trunctewords
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata:
|
84
|
+
homepage_uri: https://github.com/mkasberg/jekyll_html_trunctewords
|
85
|
+
source_code_uri: https://github.com/mkasberg/jekyll_html_trunctewords
|
86
|
+
changelog_uri: https://www.mikekasberg.com
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: 2.4.0
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubygems_version: 3.1.2
|
103
|
+
signing_key:
|
104
|
+
specification_version: 4
|
105
|
+
summary: A Jekyll filter to truncate HTML
|
106
|
+
test_files: []
|