jekyll-kw-shorten 0.0.8 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +100 -14
- data/lib/jekyll-kw-shorten.rb +19 -19
- data/lib/jekyll/KargWare/Shorten/configuration.rb +1 -3
- data/lib/jekyll/KargWare/Shorten/parser.rb +38 -13
- data/lib/jekyll/KargWare/Shorten/version.rb +1 -1
- metadata +11 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 02e501c4b564c70c2b80b73ade6532272ddfc0a8b0bcf271f0e959172fd9f551
|
4
|
+
data.tar.gz: 193d395096556484947539aab6658bf287e199247001620c6ea932e36d4b4f96
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ab37e59c44e0fde55cafce3be02ddc6d9369131e5f76e3284fb46325d9f0f7752fcd44efb87ae8ca342825a9bbb45b8ed2f7fbae706e3e28f9f8d510fa335b1d
|
7
|
+
data.tar.gz: 8f955d0924134e3d446779a6183fc4b3cade744c240971212b555eeae8cc378bb9aa63d31199a4ef7c640b7045662284aa931de0f435bfbe14143859e8cc1c83
|
data/README.md
CHANGED
@@ -1,27 +1,113 @@
|
|
1
1
|
|
2
|
-
The project `jekyll-kw-shorten` is a
|
2
|
+
The project `jekyll-kw-shorten` is a plug-in for a [jekyll](https://jekyllrb.com/) static page blog.
|
3
|
+
|
4
|
+
It can be used as **[filter](https://jekyllrb.com/docs/plugins/filters/)** and as **[tag](https://jekyllrb.com/docs/plugins/tag/)**.
|
3
5
|
|
4
6
|
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
7
|
|
6
8
|
## Usage
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
*
|
13
|
-
*
|
14
|
-
*
|
15
|
-
*
|
16
|
-
*
|
17
|
-
*
|
18
|
-
*
|
19
|
-
|
10
|
+
Use the filter `{{ 1234 | shorten }}` or the tag `{% shorten 1234 %}` inside your markdown blog post file to get `1.2 K`.
|
11
|
+
|
12
|
+
The plug-in supports
|
13
|
+
|
14
|
+
* positiv numbers
|
15
|
+
* negativ numbers with leading `-` or `- `.
|
16
|
+
* float numbers (when multiple dots are in the text only the part to the first will be taken)
|
17
|
+
* Text with no numbers inside will simply echoed
|
18
|
+
* Text with numbers, will be reduced to just the digits
|
19
|
+
* Show a rocket `∞ 🚀` for very huge values (>= 1000000000000)
|
20
|
+
* Float numbers will be rounded
|
21
|
+
|
22
|
+
### Examples as Jekyll Tag
|
23
|
+
|
24
|
+
| Syntax | OK | Result |
|
25
|
+
|-------------------------------|:--:|----------|
|
26
|
+
| {% shorten "MyText" %} | ✔️ | "MyText" |
|
27
|
+
| {% shorten MyText %} | ✔️ | MyText |
|
28
|
+
| {% shorten "My43Text" %} | ✔️ | 43 |
|
29
|
+
| {% shorten "My43.56Text" %} | ✔️ | 44 |
|
30
|
+
| {% shorten "My43.56.7Text" %} | ✔️ | 44 |
|
31
|
+
| {% shorten "1234" %} | ✔️ | 1.2 K |
|
32
|
+
| {% shorten 1 %} | ✔️ | 1 |
|
33
|
+
| {% shorten -22 %} | ✔️ | -22 |
|
34
|
+
| {% shorten - 44 %} | ✔️ | -44 |
|
35
|
+
| {% shorten 1000 %} | ✔️ | 1.0 K |
|
36
|
+
| {% shorten 1000000 %} | ✔️ | 1.0 M |
|
37
|
+
| {% shorten 1000000000 %} | ✔️ | 1.0 B |
|
38
|
+
| {% shorten 1000000000000 %} | ✔️ | ∞ 🚀 |
|
39
|
+
|
40
|
+
### Examples as Jekyll Filter
|
41
|
+
|
42
|
+
| Syntax | OK | Result |
|
43
|
+
|----------------------------------|:--:|----------|
|
44
|
+
| {{ "MyText" \| shorten }} | ✔️ | "MyText" |
|
45
|
+
| {{ MyText \| shorten }} | ❌ | |
|
46
|
+
| {{ "My43Text" \| shorten }} | ✔️ | 43 |
|
47
|
+
| {{ "My43.56Text" \| shorten }} | ✔️ | 44 |
|
48
|
+
| {{ "My43.56.7Text" \| shorten }} | ✔️ | 44 |
|
49
|
+
| {{ "1234" \| shorten }} | ✔️ | 1.2 K |
|
50
|
+
| {{ 1 \| shorten }} | ✔️ | 1 |
|
51
|
+
| {{ -22 \| shorten }} | ✔️ | -22 |
|
52
|
+
| {{ - 44 \| shorten }} | ❌ | |
|
53
|
+
| {{ 1000 \| shorten }} | ✔️ | 1.0 K |
|
54
|
+
| {{ 1000000 \| shorten }} | ✔️ | 1.0 M |
|
55
|
+
| {{ 1000000000 \| shorten }} | ✔️ | 1.0 B |
|
56
|
+
| {{ 1000000000000 \| shorten }} | ✔️ | ∞ 🚀 |
|
57
|
+
|
58
|
+
**HINT**:
|
59
|
+
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.
|
20
60
|
|
21
61
|
## Installation
|
22
62
|
|
63
|
+
Add `jekyll-kw-shorten` plugin in your Gemfile inside the `jekyll_plugins` group.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
group :jekyll_plugins do
|
67
|
+
...
|
68
|
+
gem "jekyll-kw-shorten"
|
69
|
+
...
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
Run `bundle install` to install the gem and update the Gemfile.lock.
|
74
|
+
|
75
|
+
Add `jekyll-kw-shorten` to the plugins section in your site's `_config.yml`. Then [configure](#configuration) your plug-in.
|
76
|
+
|
77
|
+
```yaml
|
78
|
+
plugins:
|
79
|
+
- jekyll-kw-shorten
|
80
|
+
```
|
81
|
+
|
23
82
|
## Configuration
|
24
83
|
|
25
|
-
|
84
|
+
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`.
|
85
|
+
|
86
|
+
* **shorten_gt3_digit** will be used for numbers between 1000 and 999999. Default is `' K'`.
|
87
|
+
* **shorten_gt6_digit** will be used for numbers between 1000000 and 999999999. Default is `' M'`.
|
88
|
+
* **shorten_gt9_digit** will be used for numbers between 1000000000 and 999999999999. Default is `' B'`.
|
89
|
+
|
90
|
+
```yaml
|
91
|
+
...
|
92
|
+
jekyll-kw-shorten:
|
93
|
+
shorten_gt3_digit: ' K'
|
94
|
+
shorten_gt6_digit: ' M'
|
95
|
+
shorten_gt9_digit: ' B'
|
96
|
+
...
|
97
|
+
```
|
98
|
+
|
99
|
+
When the config values are omit then the default values are used.
|
26
100
|
|
27
101
|
## Test locally
|
102
|
+
|
103
|
+
Run linting
|
104
|
+
|
105
|
+
```shell
|
106
|
+
bundle exec rubocop
|
107
|
+
```
|
108
|
+
|
109
|
+
Run tests
|
110
|
+
|
111
|
+
```shell
|
112
|
+
bundle exec rake test
|
113
|
+
```
|
data/lib/jekyll-kw-shorten.rb
CHANGED
@@ -5,46 +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
|
|
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
|
+
{}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
12
21
|
# shorten tag {% shorten input %} for Jekyll
|
13
22
|
class ShortenTag < Liquid::Tag
|
14
23
|
def initialize(tag_name, input, tokens)
|
15
24
|
super
|
16
|
-
@input = input
|
25
|
+
@input = input.to_s.strip
|
17
26
|
end
|
18
27
|
|
19
28
|
def render(context)
|
20
|
-
parser = Jekyll::KargWare::Shorten::Parser.new(
|
29
|
+
parser = Jekyll::KargWare::Shorten::Parser.new(
|
30
|
+
Jekyll::KargWare::Shorten.get_plugin_config(context)
|
31
|
+
)
|
21
32
|
parser.parse(@input)
|
22
33
|
end
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
def get_plugin_config(context)
|
27
|
-
context.registers[:site].config[Jekyll::KargWare::Shorten::RUBYGEM_NAME] || {}
|
28
|
-
end
|
29
34
|
end
|
30
35
|
|
31
36
|
# shorten filter {{ number | shorten }} for Jekyll
|
32
37
|
module ShortenFilter
|
33
|
-
def shorten(
|
34
|
-
parser = Jekyll::KargWare::Shorten::Parser.new(
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
private
|
39
|
-
|
40
|
-
def get_plugin_config
|
41
|
-
@context.registers[:site].config[Jekyll::KargWare::Shorten::RUBYGEM_NAME] || {}
|
38
|
+
def shorten(input)
|
39
|
+
parser = Jekyll::KargWare::Shorten::Parser.new(
|
40
|
+
Jekyll::KargWare::Shorten.get_plugin_config(@context)
|
41
|
+
)
|
42
|
+
parser.parse(input)
|
42
43
|
end
|
43
44
|
end
|
44
|
-
|
45
45
|
end
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
49
|
Liquid::Template.register_tag('shorten', Jekyll::KargWare::Shorten::ShortenTag)
|
50
|
-
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,24 +17,46 @@ module Jekyll
|
|
14
17
|
end
|
15
18
|
|
16
19
|
def parse(text)
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
29
33
|
else
|
30
|
-
|
34
|
+
num.round(0).truncate(0).to_s.rjust(5)
|
31
35
|
end
|
36
|
+
rescue StandardError => e
|
37
|
+
puts e.message
|
38
|
+
text
|
32
39
|
end
|
33
40
|
end
|
34
41
|
|
42
|
+
# private
|
43
|
+
|
44
|
+
def format(num)
|
45
|
+
num.round(1).truncate(1).to_s
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.number?(string)
|
49
|
+
true if Float(string)
|
50
|
+
# true if Float(Parser.only_float_numbers(string))
|
51
|
+
rescue StandardError
|
52
|
+
false
|
53
|
+
end
|
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
|
35
60
|
end
|
36
61
|
end
|
37
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.1.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,7 +25,10 @@ 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](https://jekyllrb.com/docs/plugins/filters/) `{{ 1234 | shorten }}` and as [tag](https://jekyllrb.com/docs/plugins/tags/) `{% shorten 1234 %}`, the result will be **1.2 K**
|
29
32
|
email:
|
30
33
|
- rubygems.org@n13.org
|
31
34
|
executables: []
|
@@ -37,12 +40,12 @@ files:
|
|
37
40
|
- lib/jekyll/KargWare/Shorten/configuration.rb
|
38
41
|
- lib/jekyll/KargWare/Shorten/parser.rb
|
39
42
|
- lib/jekyll/KargWare/Shorten/version.rb
|
40
|
-
homepage: https://
|
43
|
+
homepage: https://n13.org/rubygems
|
41
44
|
licenses: []
|
42
45
|
metadata:
|
43
|
-
homepage_uri: https://
|
44
|
-
bug_tracker_uri: https://github.com/n13org/jekyll-
|
45
|
-
source_code_uri: https://github.com/n13org/jekyll-
|
46
|
+
homepage_uri: https://n13.org/rubygems
|
47
|
+
bug_tracker_uri: https://github.com/n13org/jekyll-kw-shorten/issues
|
48
|
+
source_code_uri: https://github.com/n13org/jekyll-kw-shorten
|
46
49
|
post_install_message:
|
47
50
|
rdoc_options: []
|
48
51
|
require_paths:
|
@@ -61,5 +64,5 @@ requirements: []
|
|
61
64
|
rubygems_version: 3.1.2
|
62
65
|
signing_key:
|
63
66
|
specification_version: 4
|
64
|
-
summary: A
|
67
|
+
summary: A jekyll plugin to shorten long numbers
|
65
68
|
test_files: []
|