jekyll-kw-shorten 0.0.7 → 0.0.12

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: eb27dde8ac5b0e129bea77504f4d60f5056362c057cc1d3ac60b79641eca3199
4
- data.tar.gz: 72952c2ef3477374858a7069be47100f687bc8cc67ea0a02c7ca8ccbdeeb1e8c
3
+ metadata.gz: 012dee8b1fc72f257b2e457836d2b8f56cc3faac819e57fb0f29336cf3ff2436
4
+ data.tar.gz: b3203689baedeeef368523c60a6bc272c74642f4348bf7dfc332665836085188
5
5
  SHA512:
6
- metadata.gz: 7db034eb7c07fdc09713dc54279048e78663d22e15a98daca06821cc177329d3eb33c0115972100a7c6025a134eea3377ca11db66eba3feb84fb165cb41a1636
7
- data.tar.gz: 6a544ec6995693777fbf6d607510163ee09520aaeaed34c0484396b0a174bae1ee82e5d8eec24eda781e59c0955977a3af8c4bd500594dccd84c310291ab8843
6
+ metadata.gz: 443768121201fcbb3a56405c1d25ca797278b4178070445b5ff745113a6efc1f694aff1156f9ea6e073b553c690348eb423db86ae0154e7d4e8847792d015a04
7
+ data.tar.gz: ed61ccbf42f6b92577d68f8efbc4258f884185b69866a93c93b1484dc2e10c7937177b3bc9303e6b2cdaf6993e89637270fc1e5cc89be8120fd735a5ca6442af
@@ -1,44 +1,50 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'jekyll/KargWare/Shorten/parser'
4
+ require 'jekyll/KargWare/Shorten/version'
4
5
 
5
6
  module Jekyll
6
7
  module KargWare
8
+ # jekyll-kw-shorten jekyll (tag and filter) class
7
9
  module Shorten
8
10
  class Error < StandardError; end
9
11
  class Exception < Gem::Exception; end
10
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
+
11
21
  # shorten tag {% shorten input %} for Jekyll
12
22
  class ShortenTag < Liquid::Tag
13
- include Jekyll::KargWare::Shorten::ShortenFilter
14
-
15
23
  def initialize(tag_name, input, tokens)
16
24
  super
17
- @input = input
25
+ @input = input.to_s.strip
18
26
  end
19
27
 
20
28
  def render(context)
21
- shorten(@input)
29
+ parser = Jekyll::KargWare::Shorten::Parser.new(
30
+ Jekyll::KargWare::Shorten.get_plugin_config(context)
31
+ )
32
+ parser.parse(@input)
22
33
  end
23
34
  end
24
35
 
25
36
  # shorten filter {{ number | shorten }} for Jekyll
26
37
  module ShortenFilter
27
- def shorten(number)
28
- parser = Jekyll::KargWare::Shorten::Parser.new(get_site_config)
29
- parser.shorten(number)
30
- end
31
-
32
- private
33
-
34
- def get_site_config
35
- @context.registers[:site].config['jekyll-kw-shorten'] || {}
38
+ def shorten(input)
39
+ parser = Jekyll::KargWare::Shorten::Parser.new(
40
+ Jekyll::KargWare::Shorten.get_plugin_config(@context)
41
+ )
42
+ parser.parse(input)
36
43
  end
37
44
  end
38
-
39
45
  end
40
46
  end
41
47
  end
42
48
 
43
49
  Liquid::Template.register_tag('shorten', Jekyll::KargWare::Shorten::ShortenTag)
44
- 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,31 +7,56 @@ 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 = {})
13
16
  @configuration = Jekyll::KargWare::Shorten::Configuration.new(options)
14
17
  end
15
18
 
16
- def shorten(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
19
+ def parse(text)
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.7'
7
+ VERSION = '0.0.12'
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.7
4
+ version: 0.0.12
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