jekyll-kw-shorten 0.0.10 → 0.2.0
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 +4 -4
- data/LICENSE +21 -0
- data/README.md +112 -14
- data/lib/jekyll-kw-shorten.rb +18 -39
- data/lib/jekyll/KargWare/Shorten/configuration.rb +1 -3
- data/lib/jekyll/KargWare/Shorten/parser.rb +30 -15
- data/lib/jekyll/KargWare/Shorten/version.rb +1 -1
- metadata +13 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '07989180b1671628809d64a3d8b133f9e9aaf3658347c6f276a1186647164cf8'
|
4
|
+
data.tar.gz: b5096ec5457258fcb653150c73d2eafbededce8a9b6fe97a6f60ff33a5b08eba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6753059fad6404643de5c976d144e35e744ff9403de382c1e36f5ae241de35488af7f09a14255ea194ab48cc826b3f178782eb5a05150f3ef60552db76a8139a
|
7
|
+
data.tar.gz: 3065298a4a053f45b27a44721934fbfae964faef774345ceaba6d5769239dc9beb1f5be542ad0c6ea739615d5b7d8b37cb67f7bfd15d1504be772ad3e3b54055
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2021 n13.org
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -1,27 +1,125 @@
|
|
1
|
+
# jekyll-kw-shorten
|
1
2
|
|
2
|
-
|
3
|
+

|
4
|
+

|
5
|
+

|
6
|
+
|
7
|
+
[](https://codeclimate.com/github/n13org/jekyll-kw-shorten/maintainability)
|
8
|
+
[](https://codeclimate.com/github/n13org/jekyll-kw-shorten/test_coverage)
|
9
|
+
|
10
|
+
[](https://badge.fury.io/rb/jekyll-kw-shorten)
|
11
|
+
|
12
|
+
A jekyll plugin which can shorten long numbers, e.g. 1000 ➜ 1K or 1000000 ➜ 1M.
|
13
|
+
|
14
|
+
The project `jekyll-kw-shorten` is a plug-in for a [jekyll](https://jekyllrb.com/) static page blog. It can be used as **[filter](https://jekyllrb.com/docs/plugins/filters/)** and as **[tag](https://jekyllrb.com/docs/plugins/tag/)**.
|
3
15
|
|
4
16
|
It is published on [rubygems.org](https://rubygems.org/gems/jekyll-kw-shorten), the source code is hosted on [GitHub](https://github.com/n13org/jekyll-kw-shorten).
|
5
17
|
|
6
18
|
## Usage
|
7
19
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
*
|
13
|
-
*
|
14
|
-
*
|
15
|
-
*
|
16
|
-
*
|
17
|
-
*
|
18
|
-
*
|
19
|
-
|
20
|
+
Use the filter `{{ 1234 | shorten }}` or the tag `{% shorten 1234 %}` inside your markdown blog post file to get `1.2 K`.
|
21
|
+
|
22
|
+
The plug-in supports
|
23
|
+
|
24
|
+
* positiv numbers
|
25
|
+
* negativ numbers with leading `-` or `- `.
|
26
|
+
* float numbers (when multiple dots are in the text only the part to the first will be taken)
|
27
|
+
* Text with no numbers inside will simply echoed
|
28
|
+
* Text with numbers, will be reduced to just the digits
|
29
|
+
* Show a rocket `∞ 🚀` for very huge values (>= 1000000000000)
|
30
|
+
* Float numbers will be rounded
|
31
|
+
|
32
|
+
### Examples as Jekyll Tag
|
33
|
+
|
34
|
+
| Syntax | OK | Result |
|
35
|
+
|-------------------------------|:--:|----------|
|
36
|
+
| {% shorten "MyText" %} | ✔️ | "MyText" |
|
37
|
+
| {% shorten MyText %} | ✔️ | MyText |
|
38
|
+
| {% shorten "My43Text" %} | ✔️ | 43 |
|
39
|
+
| {% shorten "My43.56Text" %} | ✔️ | 44 |
|
40
|
+
| {% shorten "My43.56.7Text" %} | ✔️ | 44 |
|
41
|
+
| {% shorten "1234" %} | ✔️ | 1.2 K |
|
42
|
+
| {% shorten 1 %} | ✔️ | 1 |
|
43
|
+
| {% shorten -22 %} | ✔️ | -22 |
|
44
|
+
| {% shorten - 44 %} | ✔️ | -44 |
|
45
|
+
| {% shorten 1000 %} | ✔️ | 1.0 K |
|
46
|
+
| {% shorten 1000000 %} | ✔️ | 1.0 M |
|
47
|
+
| {% shorten 1000000000 %} | ✔️ | 1.0 B |
|
48
|
+
| {% shorten 1000000000000 %} | ✔️ | ∞ 🚀 |
|
49
|
+
|
50
|
+
### Examples as Jekyll Filter
|
51
|
+
|
52
|
+
| Syntax | OK | Result |
|
53
|
+
|----------------------------------|:--:|----------|
|
54
|
+
| {{ "MyText" \| shorten }} | ✔️ | "MyText" |
|
55
|
+
| {{ MyText \| shorten }} | ❌ | |
|
56
|
+
| {{ "My43Text" \| shorten }} | ✔️ | 43 |
|
57
|
+
| {{ "My43.56Text" \| shorten }} | ✔️ | 44 |
|
58
|
+
| {{ "My43.56.7Text" \| shorten }} | ✔️ | 44 |
|
59
|
+
| {{ "1234" \| shorten }} | ✔️ | 1.2 K |
|
60
|
+
| {{ 1 \| shorten }} | ✔️ | 1 |
|
61
|
+
| {{ -22 \| shorten }} | ✔️ | -22 |
|
62
|
+
| {{ - 44 \| shorten }} | ❌ | |
|
63
|
+
| {{ 1000 \| shorten }} | ✔️ | 1.0 K |
|
64
|
+
| {{ 1000000 \| shorten }} | ✔️ | 1.0 M |
|
65
|
+
| {{ 1000000000 \| shorten }} | ✔️ | 1.0 B |
|
66
|
+
| {{ 1000000000000 \| shorten }} | ✔️ | ∞ 🚀 |
|
67
|
+
|
68
|
+
**HINTS**:
|
69
|
+
|
70
|
+
* The filter `{{ MyText | shorten }}` will show nothing (empty string). The filter "thinks" the MyText is a variable (with the value nil). Numbers can be used with and without quotes. Text must be wrapped in quotes.
|
71
|
+
* The filter `{{ - 44 | shorten }}` will show nothing (empty string). The filter tries to parse the `-` as first part, not the number behind.
|
20
72
|
|
21
73
|
## Installation
|
22
74
|
|
75
|
+
Add `jekyll-kw-shorten` plugin in your Gemfile inside the `jekyll_plugins` group.
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
group :jekyll_plugins do
|
79
|
+
...
|
80
|
+
gem "jekyll-kw-shorten"
|
81
|
+
...
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
85
|
+
Run `bundle install` to install the gem and update the Gemfile.lock.
|
86
|
+
|
87
|
+
Add `jekyll-kw-shorten` to the plugins section in your site's `_config.yml`. Then [configure](#configuration) your plug-in.
|
88
|
+
|
89
|
+
```yaml
|
90
|
+
plugins:
|
91
|
+
- jekyll-kw-shorten
|
92
|
+
```
|
93
|
+
|
23
94
|
## Configuration
|
24
95
|
|
25
|
-
|
96
|
+
Add the setting to your `_config.yml` file. Details you can find in the [documentation](https://jekyllrb.com/docs/configuration/). The name of the group is `jekyll-kw-shorten`.
|
97
|
+
|
98
|
+
* **shorten_gt3_digit** will be used for numbers between 1000 and 999999. Default is `' K'`.
|
99
|
+
* **shorten_gt6_digit** will be used for numbers between 1000000 and 999999999. Default is `' M'`.
|
100
|
+
* **shorten_gt9_digit** will be used for numbers between 1000000000 and 999999999999. Default is `' B'`.
|
101
|
+
|
102
|
+
```yaml
|
103
|
+
...
|
104
|
+
jekyll-kw-shorten:
|
105
|
+
shorten_gt3_digit: ' K'
|
106
|
+
shorten_gt6_digit: ' M'
|
107
|
+
shorten_gt9_digit: ' B'
|
108
|
+
...
|
109
|
+
```
|
110
|
+
|
111
|
+
When the config values are omit then the default values are used.
|
26
112
|
|
27
113
|
## Test locally
|
114
|
+
|
115
|
+
Run linting
|
116
|
+
|
117
|
+
```shell
|
118
|
+
bundle exec rubocop
|
119
|
+
```
|
120
|
+
|
121
|
+
Run tests
|
122
|
+
|
123
|
+
```shell
|
124
|
+
bundle exec rake test
|
125
|
+
```
|
data/lib/jekyll-kw-shorten.rb
CHANGED
@@ -5,67 +5,46 @@ require 'jekyll/KargWare/Shorten/version'
|
|
5
5
|
|
6
6
|
module Jekyll
|
7
7
|
module KargWare
|
8
|
+
# jekyll-kw-shorten jekyll (tag and filter) class
|
8
9
|
module Shorten
|
9
10
|
class Error < StandardError; end
|
10
11
|
class Exception < Gem::Exception; end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
name.split("::").last.downcase
|
18
|
-
end
|
13
|
+
module_function def get_plugin_config(context)
|
14
|
+
if defined? context.registers[:site].config
|
15
|
+
context.registers[:site].config[Jekyll::KargWare::Shorten::RUBYGEM_NAME] || {}
|
16
|
+
else
|
17
|
+
{}
|
19
18
|
end
|
19
|
+
end
|
20
20
|
|
21
|
+
# shorten tag {% shorten input %} for Jekyll
|
22
|
+
class ShortenTag < Liquid::Tag
|
21
23
|
def initialize(tag_name, input, tokens)
|
22
24
|
super
|
23
25
|
@input = input.to_s.strip
|
24
|
-
|
25
|
-
# raise ArgumentError, <<~MSG
|
26
|
-
# Could not use '#{input}' in tag '#{self.class.tag_name}'.
|
27
|
-
# Make sure it is a string or a number.
|
28
|
-
# MSG
|
29
26
|
end
|
30
27
|
|
31
28
|
def render(context)
|
32
|
-
parser = Jekyll::KargWare::Shorten::Parser.new(
|
33
|
-
|
29
|
+
parser = Jekyll::KargWare::Shorten::Parser.new(
|
30
|
+
Jekyll::KargWare::Shorten.get_plugin_config(context)
|
31
|
+
)
|
34
32
|
parser.parse(@input)
|
35
33
|
end
|
36
|
-
|
37
|
-
private
|
38
|
-
|
39
|
-
def get_plugin_config(context)
|
40
|
-
if defined? context.registers[:site].config
|
41
|
-
context.registers[:site].config[Jekyll::KargWare::Shorten::RUBYGEM_NAME] || {}
|
42
|
-
else
|
43
|
-
{}
|
44
|
-
end
|
45
|
-
end
|
46
34
|
end
|
47
35
|
|
48
36
|
# shorten filter {{ number | shorten }} for Jekyll
|
49
37
|
module ShortenFilter
|
50
|
-
def shorten(
|
51
|
-
parser = Jekyll::KargWare::Shorten::Parser.new(
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
private
|
56
|
-
|
57
|
-
def get_plugin_config
|
58
|
-
if defined? @context.registers[:site].config
|
59
|
-
@context.registers[:site].config[Jekyll::KargWare::Shorten::RUBYGEM_NAME] || {}
|
60
|
-
else
|
61
|
-
{}
|
62
|
-
end
|
38
|
+
def shorten(input)
|
39
|
+
parser = Jekyll::KargWare::Shorten::Parser.new(
|
40
|
+
Jekyll::KargWare::Shorten.get_plugin_config(@context)
|
41
|
+
)
|
42
|
+
parser.parse(input)
|
63
43
|
end
|
64
44
|
end
|
65
|
-
|
66
45
|
end
|
67
46
|
end
|
68
47
|
end
|
69
48
|
|
70
49
|
Liquid::Template.register_tag('shorten', Jekyll::KargWare::Shorten::ShortenTag)
|
71
|
-
Liquid::Template.register_filter(Jekyll::KargWare::Shorten::ShortenFilter)
|
50
|
+
Liquid::Template.register_filter(Jekyll::KargWare::Shorten::ShortenFilter)
|
@@ -7,6 +7,9 @@ module Jekyll
|
|
7
7
|
module Shorten
|
8
8
|
# jekyll-kw-shorten parser class
|
9
9
|
class Parser
|
10
|
+
# https://stackoverflow.com/questions/33952093/how-to-allow-only-one-dot-in-regex
|
11
|
+
DIGITS_AND_SINGLE_DOT_ESCAPE_REGEXP = /(-?\s?[0-9]+(\.[0-9]+)?)/.freeze
|
12
|
+
|
10
13
|
attr_reader :configuration
|
11
14
|
|
12
15
|
def initialize(options = {})
|
@@ -14,21 +17,25 @@ module Jekyll
|
|
14
17
|
end
|
15
18
|
|
16
19
|
def parse(text)
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
20
|
+
num = Parser.only_float_numbers(text)
|
21
|
+
|
22
|
+
return text unless Parser.number?(num)
|
23
|
+
|
24
|
+
begin
|
25
|
+
if num >= 1000000000000
|
26
|
+
'∞ 🚀'
|
27
|
+
elsif num >= 1000000000
|
28
|
+
format(num / 1000000000.0) + @configuration.shorten_gt9_digit
|
29
|
+
elsif num >= 1000000
|
30
|
+
format(num / 1000000.0) + @configuration.shorten_gt6_digit
|
31
|
+
elsif num >= 1000
|
32
|
+
format(num / 1000.0) + @configuration.shorten_gt3_digit
|
27
33
|
else
|
28
|
-
|
34
|
+
num.round(0).truncate(0).to_s.rjust(5)
|
29
35
|
end
|
30
|
-
|
31
|
-
|
36
|
+
rescue StandardError => e
|
37
|
+
puts e.message
|
38
|
+
text
|
32
39
|
end
|
33
40
|
end
|
34
41
|
|
@@ -38,10 +45,18 @@ module Jekyll
|
|
38
45
|
num.round(1).truncate(1).to_s
|
39
46
|
end
|
40
47
|
|
41
|
-
def self.
|
42
|
-
true if Float(string)
|
48
|
+
def self.number?(string)
|
49
|
+
true if Float(string)
|
50
|
+
# true if Float(Parser.only_float_numbers(string))
|
51
|
+
rescue StandardError
|
52
|
+
false
|
43
53
|
end
|
44
54
|
|
55
|
+
def self.only_float_numbers(input)
|
56
|
+
input.to_s.scan(DIGITS_AND_SINGLE_DOT_ESCAPE_REGEXP).first.first.to_f
|
57
|
+
rescue StandardError
|
58
|
+
input
|
59
|
+
end
|
45
60
|
end
|
46
61
|
end
|
47
62
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jekyll-kw-shorten
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nicolas Karg
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2021-01-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: jekyll
|
@@ -25,24 +25,28 @@ dependencies:
|
|
25
25
|
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
version: '3.8'
|
28
|
-
description:
|
28
|
+
description: |2
|
29
|
+
A jekyll plugin which can shorten long numbers, e.g. 1000 == 1K or 1000000 == 1M.
|
30
|
+
|
31
|
+
It can be used as filter `{{ 1234 | shorten }}` and as tag `{% shorten 1234 %}`, the result will be **1.2 K**
|
29
32
|
email:
|
30
33
|
- rubygems.org@n13.org
|
31
34
|
executables: []
|
32
35
|
extensions: []
|
33
36
|
extra_rdoc_files: []
|
34
37
|
files:
|
38
|
+
- LICENSE
|
35
39
|
- README.md
|
36
40
|
- lib/jekyll-kw-shorten.rb
|
37
41
|
- lib/jekyll/KargWare/Shorten/configuration.rb
|
38
42
|
- lib/jekyll/KargWare/Shorten/parser.rb
|
39
43
|
- lib/jekyll/KargWare/Shorten/version.rb
|
40
|
-
homepage: https://
|
44
|
+
homepage: https://n13.org/rubygems
|
41
45
|
licenses: []
|
42
46
|
metadata:
|
43
|
-
homepage_uri: https://
|
44
|
-
bug_tracker_uri: https://github.com/n13org/jekyll-
|
45
|
-
source_code_uri: https://github.com/n13org/jekyll-
|
47
|
+
homepage_uri: https://n13.org/rubygems
|
48
|
+
bug_tracker_uri: https://github.com/n13org/jekyll-kw-shorten/issues
|
49
|
+
source_code_uri: https://github.com/n13org/jekyll-kw-shorten
|
46
50
|
post_install_message:
|
47
51
|
rdoc_options: []
|
48
52
|
require_paths:
|
@@ -51,7 +55,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
51
55
|
requirements:
|
52
56
|
- - ">="
|
53
57
|
- !ruby/object:Gem::Version
|
54
|
-
version: 2.
|
58
|
+
version: 2.5.0
|
55
59
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
60
|
requirements:
|
57
61
|
- - ">="
|
@@ -61,5 +65,5 @@ requirements: []
|
|
61
65
|
rubygems_version: 3.1.2
|
62
66
|
signing_key:
|
63
67
|
specification_version: 4
|
64
|
-
summary: A
|
68
|
+
summary: A jekyll plugin to shorten long numbers
|
65
69
|
test_files: []
|