jekyll-kw-shorten 0.0.9 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 97496aede7c98b125cb9e846af73359d5fbc9bf7a1d2bf64209cd1b029e2a8c8
4
- data.tar.gz: f10ea06d240481bbf7dc057ea48bd30a915d23a1df0c20a4faedd276d9879475
3
+ metadata.gz: e0b8e21ea523c1e4ff5487f6e50750e609ab59ff0f7d0a6932c01ffe45246362
4
+ data.tar.gz: 88c23330b3c708e2633ce84df8785f910e9fe85fd1dafae7db5e5a8033c839b2
5
5
  SHA512:
6
- metadata.gz: dbe3c662114d8a867b4e1ed5718215a62826eb35e3e08934fdf460a967757ec00f2b35283a2411e551796b69c2aae0b093cd38e5aa081e8235fbd284acda348a
7
- data.tar.gz: 0112cb09a60e5dc1bf4d7a9f9de3a035ed178dcbac030fe2d8f5e5924fc79205d8284907ace1a0175f954acad24e56a2a7e42651a3b3bc3aa53a71983085eba9
6
+ metadata.gz: bcbd4a45c256e3d496bba8e7db42f00570f6d99bf510961406d9edcac1278a593d5486bf6882401e70f55336fd66f142dc41192703eb5be65a67c8148b7a3543
7
+ data.tar.gz: 655a299563860560d9893c454f82064a273856721c41a195fd303adf4fbcd23a5d5e1624d2d4645dfaa9ffe1916bb07327f2c7c1368aa524f121544095cf7394
data/README.md CHANGED
@@ -1,27 +1,115 @@
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
+ **HINTS**:
59
+
60
+ * 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.
61
+ * The filter `{{ - 44 | shorten }}` will show nothing (empty string). The filter tries to parse the `-` as first part, not the number behind.
20
62
 
21
63
  ## Installation
22
64
 
65
+ Add `jekyll-kw-shorten` plugin in your Gemfile inside the `jekyll_plugins` group.
66
+
67
+ ```ruby
68
+ group :jekyll_plugins do
69
+ ...
70
+ gem "jekyll-kw-shorten"
71
+ ...
72
+ end
73
+ ```
74
+
75
+ Run `bundle install` to install the gem and update the Gemfile.lock.
76
+
77
+ Add `jekyll-kw-shorten` to the plugins section in your site's `_config.yml`. Then [configure](#configuration) your plug-in.
78
+
79
+ ```yaml
80
+ plugins:
81
+ - jekyll-kw-shorten
82
+ ```
83
+
23
84
  ## Configuration
24
85
 
25
- ## Contribution
86
+ 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`.
87
+
88
+ * **shorten_gt3_digit** will be used for numbers between 1000 and 999999. Default is `' K'`.
89
+ * **shorten_gt6_digit** will be used for numbers between 1000000 and 999999999. Default is `' M'`.
90
+ * **shorten_gt9_digit** will be used for numbers between 1000000000 and 999999999999. Default is `' B'`.
91
+
92
+ ```yaml
93
+ ...
94
+ jekyll-kw-shorten:
95
+ shorten_gt3_digit: ' K'
96
+ shorten_gt6_digit: ' M'
97
+ shorten_gt9_digit: ' B'
98
+ ...
99
+ ```
100
+
101
+ When the config values are omit then the default values are used.
26
102
 
27
103
  ## Test locally
104
+
105
+ Run linting
106
+
107
+ ```shell
108
+ bundle exec rubocop
109
+ ```
110
+
111
+ Run tests
112
+
113
+ ```shell
114
+ bundle exec rake test
115
+ ```
@@ -5,66 +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
- # shorten tag {% shorten input %} for Jekyll
13
- class ShortenTag < Liquid::Tag
14
-
15
- class << self
16
- def tag_name
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
- @input = input
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
25
+ @input = input.to_s.strip
29
26
  end
30
27
 
31
28
  def render(context)
32
- 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
+ )
33
32
  parser.parse(@input)
34
33
  end
35
-
36
- private
37
-
38
- def get_plugin_config(context)
39
- if defined? context.registers[:site].config
40
- context.registers[:site].config[Jekyll::KargWare::Shorten::RUBYGEM_NAME] || {}
41
- else
42
- {}
43
- end
44
- end
45
34
  end
46
35
 
47
36
  # shorten filter {{ number | shorten }} for Jekyll
48
37
  module ShortenFilter
49
- def shorten(number)
50
- parser = Jekyll::KargWare::Shorten::Parser.new(get_plugin_config)
51
- parser.parse(number)
52
- end
53
-
54
- private
55
-
56
- def get_plugin_config
57
- if defined? @context.registers[:site].config
58
- @context.registers[:site].config[Jekyll::KargWare::Shorten::RUBYGEM_NAME] || {}
59
- else
60
- {}
61
- 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)
62
43
  end
63
44
  end
64
-
65
45
  end
66
46
  end
67
47
  end
68
48
 
69
49
  Liquid::Template.register_tag('shorten', Jekyll::KargWare::Shorten::ShortenTag)
70
- 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.9'
7
+ VERSION = '0.1.1'
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.9
4
+ version: 0.1.1
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 `{{ 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: []
@@ -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: []