jekyll-kw-shorten 0.0.8 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bb2cbbc69a7c6412f1a4b27a513beb32a3f660f5277d5d5f33703c67581d5add
4
- data.tar.gz: dffc3d98ce15f1c6f974bbd66ca547c18ca368ef3caebcd424a7a1a67219a6a9
3
+ metadata.gz: 02e501c4b564c70c2b80b73ade6532272ddfc0a8b0bcf271f0e959172fd9f551
4
+ data.tar.gz: 193d395096556484947539aab6658bf287e199247001620c6ea932e36d4b4f96
5
5
  SHA512:
6
- metadata.gz: 4f0e2d28dac1294c1349e39c3d3b7def088a24aae1949a64ae9b418bb14deee5b948abc8ca1edf33357fd6e26fea42d5c1764c6b5ee73c144a8d1075934ca214
7
- data.tar.gz: 7af4c26d2f71c4fa8566eb44fceaafe0606aac1855f8abc268c178943adb0f17f4261064c63f215f048872640852a03ff0103dbac9c5e3c05411559fb0cef931
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 **[filter](https://jekyllrb.com/docs/plugins/filters/)** plug-in for a [jekyll](https://jekyllrb.com/) static page blog.
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
- * {{ "txt" | shorten }}
9
- * {{ "text with spaces" | shorten }}
10
- * {{ 500 | shorten }}
11
- * {{ 777 | shorten }}
12
- * {{ 1000 | shorten }}
13
- * {{ 1200 | shorten }}
14
- * {{ 1450 | shorten }}
15
- * {{ 1777 | shorten }}
16
- * {{ 12345 | shorten }}
17
- * {{ 1000000 | shorten }}
18
- * {{ 1110000 | shorten }}
19
- * {{ 1000000000 | shorten }}
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
- ## Contribution
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
+ ```
@@ -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(get_plugin_config(context))
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(number)
34
- parser = Jekyll::KargWare::Shorten::Parser.new(get_plugin_config)
35
- parser.parse(number)
36
- end
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)
@@ -21,9 +21,7 @@ module Jekyll
21
21
  @shorten_gt9_digit = options['shorten_gt9_digit']
22
22
  end
23
23
 
24
- private
25
-
26
- def generate_option_hash(options)
24
+ private def generate_option_hash(options)
27
25
  DEFAULT_CONFIG.merge(options)
28
26
  rescue TypeError
29
27
  DEFAULT_CONFIG
@@ -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
- if text.is_a? String then
18
- return text
19
- else
20
- num = text.to_i
21
- if num >= 1000000000000 then
22
- return "∞ 🚀"
23
- elsif num >= 1000000000 then
24
- return (num / 1000000000.0).truncate(1).to_s + @configuration.shorten_gt9_digit
25
- elsif num >= 1000000 then
26
- return (num / 1000000.0).truncate(1).to_s + @configuration.shorten_gt6_digit
27
- elsif num >= 1000 then
28
- return (num / 1000.0).truncate(1).to_s + @configuration.shorten_gt3_digit
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
- return (num).truncate(1).to_s
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
@@ -4,7 +4,7 @@ module Jekyll
4
4
  module KargWare
5
5
  module Shorten
6
6
  RUBYGEM_NAME = 'jekyll-kw-shorten'
7
- VERSION = '0.0.8'
7
+ VERSION = '0.1.0'
8
8
  end
9
9
  end
10
10
  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.8
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: 2020-12-27 00:00:00.000000000 Z
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: " A template project for jekyll plugins with some additional content\n"
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://notes.n13.org/rubygems
43
+ homepage: https://n13.org/rubygems
41
44
  licenses: []
42
45
  metadata:
43
- homepage_uri: https://notes.n13.org/rubygems
44
- bug_tracker_uri: https://github.com/n13org/jekyll-plugin-template/issues
45
- source_code_uri: https://github.com/n13org/jekyll-plugin-template
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 template project for jekyll plugins.
67
+ summary: A jekyll plugin to shorten long numbers
65
68
  test_files: []