curly_bracket_parser 0.9.2 → 1.0.4
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 +4 -4
- data/LICENSE +1 -1
- data/README.md +4 -1
- data/lib/curly_bracket_parser/version.rb +1 -1
- data/lib/curly_bracket_parser.rb +33 -16
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dbc6e9152c4a5928dd20a24cc8cb59ace5b9c5b5b66204b118061b28cff3f33b
|
4
|
+
data.tar.gz: 851fa1b633944e37968d31755b786939bea79afa624b4e2135167bb3e832555c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 162491b1ca8833964b29e38482806b479f6f1ab5d9f0505368ce83b219e9fe3953d2a03aac4d592ade1ad5182365ca344c471f5847cb9abd36a849dd0c8925f5
|
7
|
+
data.tar.gz: 697839790e56524019c7e35a2ebb58f441f27f74458cfbf2d6f4bf1e91d038e777368b9a637f20c2d48d7fe022baa3d8188ac439d400bf524989cb28731812a3
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
# curly_bracket_parser
|
2
|
+

|
3
|
+

|
4
|
+
[](LICENSE)
|
2
5
|
|
3
|
-
Ruby gem providing a simple parser to replace curly brackets `{{like_this}}` inside strings like URLs, texts or even files easily.
|
6
|
+
> Ruby gem providing a simple parser to replace curly brackets `{{like_this}}` inside strings like URLs, texts or even files easily.
|
4
7
|
|
5
8
|
Additional support for build-in filters and custom filters make them more powerful. `{{example|my_filter}}`
|
6
9
|
|
data/lib/curly_bracket_parser.rb
CHANGED
@@ -37,10 +37,14 @@ module CurlyBracketParser
|
|
37
37
|
def self.parse(string, variables, unresolved_vars: :raise, replace_pattern: "##\\1##")
|
38
38
|
variables ||= {}
|
39
39
|
result_string = string.clone
|
40
|
+
# symbolize keys
|
41
|
+
variables = variables.map { |key, value| [key.to_sym, value] }.to_h
|
40
42
|
if CurlyBracketParser.any_variable_included? string
|
41
43
|
loop do
|
42
|
-
variables(
|
43
|
-
|
44
|
+
variables(result_string).each do |string_var|
|
45
|
+
dec = decode_variable(string_var)
|
46
|
+
name = dec[:name]
|
47
|
+
filter = dec[:filter]
|
44
48
|
if variables[name.to_sym]
|
45
49
|
value = if filter
|
46
50
|
process_filter(filter, variables[name.to_sym])
|
@@ -54,7 +58,9 @@ module CurlyBracketParser
|
|
54
58
|
end
|
55
59
|
end
|
56
60
|
# break if no more given variable is available
|
57
|
-
|
61
|
+
unless any_variable_included?(result_string) && includes_one_variable_of(variables.keys, result_string)
|
62
|
+
break
|
63
|
+
end
|
58
64
|
end
|
59
65
|
case unresolved_vars
|
60
66
|
when :raise
|
@@ -212,11 +218,16 @@ module CurlyBracketParser
|
|
212
218
|
# Unregister / remove an existing default variable
|
213
219
|
#
|
214
220
|
# @param [String] name of the variable
|
221
|
+
# @return [Boolean] true if variable existed and was unregistered, false if it didn't exist
|
215
222
|
def self.unregister_default_var(name)
|
216
223
|
@@registered_default_vars ||= {}
|
217
224
|
name = name.to_s
|
218
|
-
@@registered_default_vars
|
219
|
-
|
225
|
+
if @@registered_default_vars[name]
|
226
|
+
@@registered_default_vars.delete(name)
|
227
|
+
true
|
228
|
+
else
|
229
|
+
false
|
230
|
+
end
|
220
231
|
end
|
221
232
|
|
222
233
|
#----------------------------------------------------------------------------------------------------
|
@@ -247,26 +258,30 @@ module CurlyBracketParser
|
|
247
258
|
# '{{var_name|filter_name}}' => { name: 'var_name', filter: 'filter_name' }
|
248
259
|
#
|
249
260
|
# @param [String] variable
|
250
|
-
# @return [
|
261
|
+
# @return [Hash<String => String>] name, filter
|
251
262
|
def self.decode_variable(variable)
|
252
|
-
|
253
|
-
[var.keys.first, var.values.first]
|
263
|
+
decoded_variables(variable).first
|
254
264
|
end
|
255
265
|
|
256
266
|
#----------------------------------------------------------------------------------------------------
|
257
267
|
|
258
|
-
#
|
268
|
+
# Scans the given url for variables with pattern '{{var|optional_filter}}'
|
269
|
+
#
|
270
|
+
# @example
|
271
|
+
# 'The variable {{my_var|my_filter}} is inside this string' => [{ name: "my_var", filter: "my_filter"}]
|
272
|
+
#
|
259
273
|
# @param [String] string to scan
|
260
274
|
# @return [Array<Hash<Symbol => String>>] array of variable names and its filters
|
261
275
|
def self.decoded_variables(string)
|
262
|
-
|
263
|
-
|
264
|
-
string.scan(VARIABLE_DECODER_REGEX).map { |e| { "#{e[
|
276
|
+
var_name_index = 0
|
277
|
+
var_filter_index = 1
|
278
|
+
string.scan(VARIABLE_DECODER_REGEX).map { |e| { name: "#{e[var_name_index].strip}", filter: e[var_filter_index].strip != '' ? e[var_filter_index].strip : nil } }.flatten
|
265
279
|
end
|
266
280
|
|
267
281
|
#----------------------------------------------------------------------------------------------------
|
268
282
|
|
269
|
-
#
|
283
|
+
# Scans the given url for variables with pattern '{{var|optional_filter}}'
|
284
|
+
#
|
270
285
|
# @param [String] string to scan
|
271
286
|
# @return [Array<String>] array of variable names and its filters
|
272
287
|
def self.variables(string)
|
@@ -276,7 +291,8 @@ module CurlyBracketParser
|
|
276
291
|
#----------------------------------------------------------------------------------------------------
|
277
292
|
|
278
293
|
# Check if any variable is included in the given string
|
279
|
-
#
|
294
|
+
#
|
295
|
+
# @param [String] string name of variable to check for
|
280
296
|
# @return [Boolean] true if any variable is included in the given string, otherwise false
|
281
297
|
def self.any_variable_included?(string)
|
282
298
|
string.match(VARIABLE_REGEX) != nil
|
@@ -285,12 +301,13 @@ module CurlyBracketParser
|
|
285
301
|
#----------------------------------------------------------------------------------------------------
|
286
302
|
|
287
303
|
# Check if one of the given variable names is included in the given string
|
304
|
+
#
|
288
305
|
# @param [Array<String>] variable_names
|
289
|
-
# @param [String] string
|
306
|
+
# @param [String] string name of variable to check for
|
290
307
|
# @return [Boolean] true if one given variable name is included in given the string, otherwise false
|
291
308
|
def self.includes_one_variable_of(variable_names, string)
|
292
309
|
decoded_variables(string).each do |dvar|
|
293
|
-
return true if variable_names.include?(dvar[:name])
|
310
|
+
return true if variable_names.map(&:to_sym).include?(dvar[:name].to_sym)
|
294
311
|
end
|
295
312
|
false
|
296
313
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: curly_bracket_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthäus Beyrle
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-06-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: lucky_case
|
@@ -110,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
110
|
- !ruby/object:Gem::Version
|
111
111
|
version: '0'
|
112
112
|
requirements: []
|
113
|
-
rubygems_version: 3.
|
113
|
+
rubygems_version: 3.1.4
|
114
114
|
signing_key:
|
115
115
|
specification_version: 4
|
116
116
|
summary: Simple parser to replace variables inside templates/strings and files
|