shi-args 0.3.4.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bfbd25a286ba3bb91c1041824ff8213256c096b057fd5b9e6eb76cec88bdc8e1
4
+ data.tar.gz: 9b1399ee131c6c59c7a5fa7445289d566ed0b626003ee17383df6eb2eefd5122
5
+ SHA512:
6
+ metadata.gz: 8404c34c8ac3c4e850e5ab0748e80c29c18ae8d35ff06cbaf9f09e9858e522f74e17e29633ba30167a6104302bad276a5b434f9d4055221dfb75e4d3280eae7f
7
+ data.tar.gz: 9c30d6838447228f98fff39fb67c7deaeb10a2429f943a9f3d3f5418088c72573ba82942c774452a1061d8dde1f668f6572e82f97be88a7c6d1df91d912b1c52
data/.rufo ADDED
@@ -0,0 +1,6 @@
1
+
2
+ align_case_when true
3
+ parens_in_def :dynamic
4
+ align_chained_calls true
5
+ trailing_commas false
6
+ quote_style :single
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --no-private
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-12-19
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in shi-args.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Ivan Shikhalev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # Shi::Args
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'shi-args'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle install
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install shi-args
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## License
24
+
25
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,176 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'version'
4
+ require_relative 'values'
5
+
6
+ class Shi::Args::Params
7
+ class << self
8
+ # @param context [Liquid::Context]
9
+ # @param markup [String]
10
+ # @return [Params]
11
+ def parse context, markup
12
+ obj = self.new context
13
+ obj.parse! markup
14
+ end
15
+ end
16
+
17
+ # @param context [Liquid::Context]
18
+ def initialize context
19
+ @context = context
20
+ @params = []
21
+ @attrs = {}
22
+ end
23
+
24
+ ESCAPES = {
25
+ "\\'" => '(#SINGLE#)',
26
+ '\"' => '(#DOUBLE#)',
27
+ '\ ' => '(#SPACE#)'
28
+ }
29
+
30
+ def escape str
31
+ return nil if str.nil?
32
+ result = str
33
+ ESCAPES.each do |key, value|
34
+ result.gsub!(key, value)
35
+ end
36
+ return result
37
+ end
38
+
39
+ def descape str
40
+ return nil if str.nil?
41
+ result = str
42
+ ESCAPES.each do |key, value|
43
+ result.gsub!(value, key)
44
+ end
45
+ return result
46
+ end
47
+
48
+ private :escape, :descape
49
+
50
+ def add_key! name
51
+ name = name.intern
52
+ value = true
53
+ @params << { name: name, value: value }
54
+ @attrs[name] = value
55
+ end
56
+
57
+ def add_param! source
58
+ value = Shi::Args::Value::parse @context, source
59
+ @params << { value: value }
60
+ end
61
+
62
+ def add_attr! name, source
63
+ name = name.intern
64
+ value = Shi::Args::Value::parse @context, source
65
+ @params << { name: name, value: value }
66
+ @attrs[name] = value
67
+ end
68
+
69
+ private :add_key!, :add_param!, :add_attr!
70
+
71
+ PATTERN_ATTR_KEY = /^(?<key>[a-zA-Z_]\w*)\s*(?<rest>.*)$/
72
+ PATTERN_PARA_VARIABLE = /^(?<value>\{\{\-?\s+[a-zA-Z_][\w\.]*\s+\-?\}\})\s*(?<rest>.*)$/
73
+ PATTERN_PARA_SINGLE_QUOTED = /^(?<value>@?'.*?')\s*(?<rest>.*)$/
74
+ PATTERN_PARA_DOUBLE_QUOTED = /^(?<value>@?".*?")\s*(?<rest>.*)$/
75
+ PATTERN_PARA_SIMPLE = /^(?<value>@?\S+)\s*(?<rest>.*)$/
76
+ PATTERN_ATTR_VARIABLE = /^(?<key>[a-zA-Z_]\w*)=(?<value>\{\{\-?\s*[a-zA-Z_][\w\.]*\s+\-?\}\})\s+(?<rest>.*)$/
77
+ PATTERN_ATTR_SINGLE_QUOTED = /^(?<key>[a-zA-Z_]\w*)=(?<value>@?'.*?')\s*(?<rest>.*)$/
78
+ PATTERN_ATTR_DOUBLE_QUOTED = /^(?<key>[a-zA-Z_]\w*)=(?<value>@?".*?")\s*(?<rest>.*)$/
79
+ PATTERN_ATTR_SIMPLE = /^(?<key>[a-zA-Z_]\w*)=(?<value>@?\S+)\s*(?<rest>.*)$/
80
+
81
+ # @param markup [String]
82
+ # @return [self]
83
+ def parse! markup
84
+ source = escape markup.strip.gsub("\n", ' ')
85
+ until source.empty?
86
+ case source
87
+ when PATTERN_ATTR_VARIABLE
88
+ add_attr! $~[:key].strip, $~[:value].strip
89
+ source = $~[:rest].strip
90
+ when PATTERN_ATTR_SINGLE_QUOTED
91
+ add_attr! $~[:key].strip, $~[:value].strip
92
+ source = $~[:rest].strip
93
+ when PATTERN_ATTR_DOUBLE_QUOTED # TODO: проверить возможность схлопывания
94
+ add_attr! $~[:key].strip, $~[:value].strip
95
+ source = $~[:rest].strip
96
+ when PATTERN_ATTR_SIMPLE
97
+ add_attr! $~[:key].strip, $~[:value].strip
98
+ source = $~[:rest].strip
99
+ when PATTERN_ATTR_KEY
100
+ add_key! $~[:key].strip
101
+ source = $~[:rest].strip
102
+ when PATTERN_PARA_VARIABLE
103
+ add_param! $~[:value].strip
104
+ source = $~[:rest].strip
105
+ when PATTERN_PARA_SINGLE_QUOTED
106
+ add_param! $~[:value].strip
107
+ source = $~[:rest].strip
108
+ when PATTERN_PARA_DOUBLE_QUOTED
109
+ add_param! $~[:value].strip
110
+ source = $~[:rest].strip
111
+ when PATTERN_PARA_SIMPLE
112
+ add_param! $~[:value].strip
113
+ source = $~[:rest].strip
114
+ else
115
+ raise ArgumentError, "Invalid param(s): #{source}!"
116
+ end
117
+ end
118
+ self
119
+ end
120
+
121
+ # @param key_or_index [String, Symbol, Integer]
122
+ # @return [Object, nil]
123
+ def [] key_or_index
124
+ case key_or_index
125
+ when Integer
126
+ @params[key_or_index]&.fetch(:value, nil)
127
+ when String
128
+ @attrs[key_or_index.intern]
129
+ when Symbol
130
+ @attrs[key_or_index]
131
+ else
132
+ nil
133
+ end
134
+ end
135
+
136
+ def each
137
+ @params.each do |param|
138
+ yield param
139
+ end
140
+ end
141
+
142
+ def each_value
143
+ @params.each_with_index do |param, index|
144
+ if param[:name]
145
+ yield param[:name], param[:value]
146
+ else
147
+ yield index, param[:value]
148
+ end
149
+ end
150
+ end
151
+
152
+ def each_attribute
153
+ @attrs.each do |key, value|
154
+ yield key, value
155
+ end
156
+ end
157
+
158
+ def each_positional
159
+ index = 0
160
+ @params.each do |param|
161
+ if !(param[:name])
162
+ index += 1
163
+ yield index, param[:value]
164
+ end
165
+ end
166
+ end
167
+
168
+ # @return [Hash]
169
+ def to_h
170
+ result = {}
171
+ each_value do |key, value|
172
+ result[key] = value
173
+ end
174
+ result
175
+ end
176
+ end
@@ -0,0 +1,209 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'version'
4
+
5
+ require 'shi/tools'
6
+ require 'jekyll/path_manager'
7
+
8
+ module Shi::Args::Value
9
+ class Color
10
+ # @return [String]
11
+ attr_reader :value
12
+
13
+ # @return [Integer]
14
+ attr_reader :red
15
+
16
+ # @return [Integer]
17
+ attr_reader :green
18
+
19
+ # @return [Integer]
20
+ attr_reader :blue
21
+
22
+ # @return [Integer]
23
+ attr_reader :alpha
24
+
25
+ # @param source [String]
26
+ def initialize source
27
+ @value = source.strip
28
+ plain = @value.slice(1..-1)
29
+ case plain.length
30
+ when 3
31
+ @red = (plain.slice(0) * 2).to_i(16)
32
+ @green = (plain.slice(1) * 2).to_i(16)
33
+ @blue = (plain.slice(2) * 2).to_i(16)
34
+ @alpha = nil
35
+ when 4
36
+ @red = (plain.slice(0) * 2).to_i(16)
37
+ @green = (plain.slice(1) * 2).to_i(16)
38
+ @blue = (plain.slice(2) * 2).to_i(16)
39
+ @alpha = (plain.slice(3) * 2).to_i(16)
40
+ when 6
41
+ @red = plain.slice(0..1).to_i(16)
42
+ @green = plain.slice(2..3).to_i(16)
43
+ @blue = plain.slice(4..5).to_i(16)
44
+ @alpha = nil
45
+ when 8
46
+ @red = plain.slice(0..1).to_i(16)
47
+ @green = plain.slice(2..3).to_i(16)
48
+ @blue = plain.slice(4..5).to_i(16)
49
+ @alpha = plain.slice(6..7).to_i(16)
50
+ else
51
+ raise ArgumentError, "Invalid color: #{source}"
52
+ end
53
+ end
54
+
55
+ # @return [String]
56
+ def to_s
57
+ @value
58
+ end
59
+ end
60
+
61
+ class Measure
62
+ KS = {
63
+ :in => 288,
64
+ :px => 288 / 96,
65
+ :pt => 288 / 72,
66
+ :pc => 288 / 6,
67
+ :mm => 288 / 25.4,
68
+ :cm => 288 / 2.54,
69
+ :Q => 288 / (25.4 * 4)
70
+ }
71
+
72
+ class << self
73
+ # @param number [Numeric]
74
+ # @return [String]
75
+ def px number
76
+ new "#{number}px", number, :px
77
+ end
78
+ end
79
+
80
+ # @return [String]
81
+ attr_reader :value
82
+
83
+ # @return [Numeric]
84
+ attr_reader :number
85
+
86
+ # @return [Symbol]
87
+ attr_reader :unit
88
+
89
+ # @param value [String]
90
+ # @param number [Numeric]
91
+ # @param unit [Symnol, String]
92
+ def initialize value, number, unit
93
+ @value = value
94
+ @number = number
95
+ @unit = unit.intern
96
+ end
97
+
98
+ # @return [String]
99
+ def to_s
100
+ @value
101
+ end
102
+
103
+ def to unit
104
+ self_k = KS[@unit]
105
+ if self_k.nil?
106
+ raise ArgumentError, "Invalid unit for conversion: #{@unit.inspect}!"
107
+ end
108
+ unit_k = KS[unit.intern]
109
+ if unit_k.nil?
110
+ raise ArgumentError, "Invalid unit for conversion: #{unit.inspect}!"
111
+ end
112
+ r_number = @number * self_k / unit_k
113
+ r_value = "#{r_number}#{unit}"
114
+ Measure::new r_value, r_number, unit
115
+ end
116
+
117
+ def to_px
118
+ to(:px).number
119
+ end
120
+ end
121
+
122
+ class << self
123
+ include Shi::Tools
124
+
125
+ def lookup_file context, path
126
+ site = context.registers[:site]
127
+ relative_path = Liquid::Template.parse(path.strip).render(context)
128
+ relative_path_with_leading_slash = Jekyll::PathManager.join('', relative_path)
129
+ site.static_files.each do |item|
130
+ return item if item.relative_path == relative_path
131
+ return item if item.relative_path == relative_path_with_leading_slash
132
+ end
133
+ raise ArgumentError, "Couldn't find file: #{relative_path}"
134
+ end
135
+
136
+ # @private
137
+ def unquote source
138
+ source = source.strip
139
+ s = source.slice(0)
140
+ f = source.slice(-1)
141
+ if s == '"'
142
+ if f == '"'
143
+ return source.slice(1..-2)
144
+ else
145
+ raise ArgumentError, "Invalid quoted string: #{source}"
146
+ end
147
+ elsif s == "'"
148
+ if f == "'"
149
+ return source.slice(1..-2)
150
+ else
151
+ raise ArgumentError, "Invalid quoted string: #{source}"
152
+ end
153
+ else
154
+ return source
155
+ end
156
+ end
157
+
158
+ private :unquote
159
+
160
+ UNITS = %i[
161
+ %
162
+ cm mm Q in pc pt px
163
+ em ex ch rem lh rlh vw vh vmin vmax vb vi svw svh lvw lvh dvw dvh
164
+ ]
165
+ UNITS_PART = '(' + UNITS.map { |s| s.to_s }.join('|') + ')'
166
+
167
+ PATTERN_TRUE = 'true'
168
+ PATTERN_FALSE = 'false'
169
+ PATTERN_NIL = 'nil'
170
+ PATTERN_VARIABLE = /^\{\{\-?\s+(?<variable>[a-zA-Z_][\w\.]*)\s+\-?\}\}$/
171
+ PATTERN_LINK = /^@(?<path>.*)$/
172
+ PATTERN_COLOR = /^(?<color>#\h+)$/
173
+ PATTERN_INTEGER = /^(?<number>\d+)$/
174
+ PATTERN_FLOAT = /^(?<number>\d*\.\d+)$/
175
+ PATTERN_INTEGER_MEASURE = Regexp.compile '^(?<number>\d+)(?<unit>' + UNITS_PART + ')$'
176
+ PATTERN_FLOAT_MEASURE = Regexp.compile '^(?<number>\d*\.\d+)(?<unit>' + UNITS_PART + ')$'
177
+
178
+ # @param context [Liquid::Context]
179
+ # @param value [Sring]
180
+ # @return [Object]
181
+ def parse context, value
182
+ value = value.strip
183
+ case value
184
+ when PATTERN_TRUE
185
+ true
186
+ when PATTERN_FALSE
187
+ false
188
+ when PATTERN_NIL
189
+ nil
190
+ when PATTERN_VARIABLE
191
+ context[$~[:variable]]
192
+ when PATTERN_LINK
193
+ lookup_file context, unquote($~[:path])
194
+ when PATTERN_COLOR
195
+ Shi::Args::Value::Color::new $~[:color]
196
+ when PATTERN_INTEGER
197
+ $~[:number].to_i
198
+ when PATTERN_FLOAT
199
+ $~[:number].to_f
200
+ when PATTERN_INTEGER_MEASURE
201
+ Shi::Args::Value::Measure::new value, $~[:number].to_i, $~[:unit]
202
+ when PATTERN_FLOAT_MEASURE
203
+ Shi::Args::Value::Measure::new value, $~[:number].to_f, $~[:unit]
204
+ else
205
+ unquote value
206
+ end
207
+ end
208
+ end
209
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shi
4
+ module Args
5
+ VERSION = '0.3.4.2'
6
+ end
7
+ end
data/lib/shi/args.rb ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'args/version'
4
+ require_relative 'args/params'
data/lib/shi-args.rb ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'shi/args'
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shi-args
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.4.2
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Shikhalev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-01-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: shi-tools
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: jekyll
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '5.0'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '4.0'
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '5.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: liquid
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '4.0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '4.0'
61
+ description:
62
+ email:
63
+ - shikhalev@gmail.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - ".rufo"
69
+ - ".yardopts"
70
+ - CHANGELOG.md
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - lib/shi-args.rb
75
+ - lib/shi/args.rb
76
+ - lib/shi/args/params.rb
77
+ - lib/shi/args/values.rb
78
+ - lib/shi/args/version.rb
79
+ homepage: https://github.com/shikhalev/shi-args
80
+ licenses:
81
+ - MIT
82
+ metadata:
83
+ homepage_uri: https://github.com/shikhalev/shi-args
84
+ source_code_uri: https://github.com/shikhalev/shi-args
85
+ changelog_uri: https://github.com/shikhalev/shi-args/blob/main/CHANGELOG.md
86
+ documentation_uri: https://rubydoc.info/gems/shi-args/
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: 2.7.7
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubygems_version: 3.4.3
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Arguments parser for Jekyll custom tags
106
+ test_files: []