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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 012dee8b1fc72f257b2e457836d2b8f56cc3faac819e57fb0f29336cf3ff2436
|
4
|
+
data.tar.gz: b3203689baedeeef368523c60a6bc272c74642f4348bf7dfc332665836085188
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 443768121201fcbb3a56405c1d25ca797278b4178070445b5ff745113a6efc1f694aff1156f9ea6e073b553c690348eb423db86ae0154e7d4e8847792d015a04
|
7
|
+
data.tar.gz: ed61ccbf42f6b92577d68f8efbc4258f884185b69866a93c93b1484dc2e10c7937177b3bc9303e6b2cdaf6993e89637270fc1e5cc89be8120fd735a5ca6442af
|
data/lib/jekyll-kw-shorten.rb
CHANGED
@@ -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
|
-
|
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(
|
28
|
-
parser = Jekyll::KargWare::Shorten::Parser.new(
|
29
|
-
|
30
|
-
|
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)
|
@@ -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
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
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.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:
|
12
|
+
date: 2021-01-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: jekyll
|