curly_bracket_parser 1.0.4 → 1.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +31 -5
- data/lib/curly_bracket_parser/version.rb +1 -1
- data/lib/curly_bracket_parser.rb +54 -16
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 40d43493980369bc381be05ab5ba6b68051555646e5d04c6baaaf0fb90e947a1
|
4
|
+
data.tar.gz: f51aaa5658b30890774a4aa8027e702ada49cabdf575192cdfb4913b9bd1c63e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ca8945a471c2404407c0c19fbbdcc7b19f5a65631f0c8b55d617c24713193c93ebd2a07b57b81e0ae517cd5e4a5ea3483c4006ab0dc64b157f8a88144c5595a
|
7
|
+
data.tar.gz: 14251164b9fcce7ce7f0721c5275655135387a7fd8688ad2f1651cc9b3e52a4f98d8ed411591d505fbefa63f64018bd58daf0dbedb99945522d6b8b94701dbb5
|
data/README.md
CHANGED
@@ -50,15 +50,25 @@ You can either parse variables inside strings or even directly in files.
|
|
50
50
|
|
51
51
|
```ruby
|
52
52
|
url = "https://my-domain.com/items/{{item_id}}"
|
53
|
-
final_url = CurlyBracketParser.parse url, item_id: 123
|
53
|
+
final_url = CurlyBracketParser.parse url, { item_id: 123 }
|
54
54
|
# => "https://my-domain.com/items/123"
|
55
55
|
```
|
56
56
|
|
57
|
+
Nested variables are supported as well:
|
58
|
+
```ruby
|
59
|
+
tmpl = "This is my template with {{my_nested_variable}}";
|
60
|
+
my_nested_variable = "my {{nested}} variable";
|
61
|
+
parsed_tmpl = CurlyBracketParser.parse tmpl, { my_nested_variable: my_nested_variable, nested: 'pizza'}
|
62
|
+
# => "This is my template with my pizza variable"
|
63
|
+
```
|
64
|
+
|
65
|
+
|
66
|
+
|
57
67
|
### Filters
|
58
68
|
|
59
69
|
```ruby
|
60
70
|
url = "https://my-domain.com/catalog/{{item_name|snake_case}}"
|
61
|
-
final_url = CurlyBracketParser.parse url, item_name: 'MegaSuperItem'
|
71
|
+
final_url = CurlyBracketParser.parse url, { item_name: 'MegaSuperItem' }
|
62
72
|
# => "https://my-domain.com/catalog/mega_super_item"
|
63
73
|
```
|
64
74
|
|
@@ -72,10 +82,26 @@ For a list of built-in filters visit [LuckyCase](https://github.com/magynhard/lu
|
|
72
82
|
end
|
73
83
|
|
74
84
|
text = "Paul went out and screamed: A{{scream|7times}}h"
|
75
|
-
final_text = CurlyBracketParser.parse text, scream: 'a'
|
85
|
+
final_text = CurlyBracketParser.parse text, { scream: 'a' }
|
76
86
|
# => "Paul went out and screamed: Aaaaaaaah"
|
77
87
|
```
|
78
88
|
|
89
|
+
|
90
|
+
### Value variables
|
91
|
+
|
92
|
+
For special cases you can directly define or set variables inside the template - usually it does only make sense, if you combine them with custom filters.
|
93
|
+
|
94
|
+
You can either use quotes to define a string or numbers (integer or floating point) directly.
|
95
|
+
|
96
|
+
Empty values are possible as well. They are equal to a empty string.
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
tmpl = %Q(This is a {{'string'|pascal_case}} and today is {{"today"|date_filter}}. Peter is {{'1990-10-05'|iso_date_age}} years old. His girlfriends name is {{girl|pascal_case}} and she is {{16|double_number}} years old. This article has been written at {{|date_now_formatted}}`)
|
100
|
+
parsed = CurlyBracketParser.parse tmpl, { girl: "anna" }
|
101
|
+
# => "This is a String and today is 2022-06-27. Peter is 32 years old. His girlfriends name is Anna and she is 32 years old. This article has been written at 6/28/2022, 12:46:40 PM."
|
102
|
+
```
|
103
|
+
|
104
|
+
|
79
105
|
### Files
|
80
106
|
|
81
107
|
<ins>test.html</ins>
|
@@ -84,7 +110,7 @@ For a list of built-in filters visit [LuckyCase](https://github.com/magynhard/lu
|
|
84
110
|
```
|
85
111
|
|
86
112
|
```ruby
|
87
|
-
parsed_file = CurlyBracketParser.parse_file './test.html', title: 'WelcomeAtHome'
|
113
|
+
parsed_file = CurlyBracketParser.parse_file './test.html', { title: 'WelcomeAtHome' }
|
88
114
|
# => "<h1>Welcome at home</h1>"
|
89
115
|
```
|
90
116
|
|
@@ -104,7 +130,7 @@ Because of providing blocks, your variables can dynamically depend on other stat
|
|
104
130
|
text = "You are running version {{version}}"
|
105
131
|
CurlyBracketParser.parse text
|
106
132
|
# => "You are running version 1.0.2"
|
107
|
-
CurlyBracketParser.parse text, version: '0.7.0'
|
133
|
+
CurlyBracketParser.parse text, { version: '0.7.0' }
|
108
134
|
# => "You are running version 0.7.0"
|
109
135
|
```
|
110
136
|
|
data/lib/curly_bracket_parser.rb
CHANGED
@@ -18,8 +18,8 @@ require_relative 'custom_errors/variable_already_registered_error'
|
|
18
18
|
module CurlyBracketParser
|
19
19
|
|
20
20
|
# {{variable_name|optional_filter}}
|
21
|
-
VARIABLE_DECODER_REGEX = /{{([^{}\|]
|
22
|
-
VARIABLE_REGEX = /{{[^{}]
|
21
|
+
VARIABLE_DECODER_REGEX = /{{([^{}\|]*)\|?([^{}\|]*)}}/
|
22
|
+
VARIABLE_REGEX = /{{[^{}]*}}/
|
23
23
|
|
24
24
|
VALID_DEFAULT_FILTERS = [
|
25
25
|
LuckyCase::CASES.keys.map(&:to_s)
|
@@ -31,11 +31,15 @@ module CurlyBracketParser
|
|
31
31
|
#
|
32
32
|
# @param [String] string to parse
|
33
33
|
# @param [Hash<Symbol => String>] variables <key: 'value'>
|
34
|
-
# @param [
|
35
|
-
# @param [
|
34
|
+
# @param [Hash] options
|
35
|
+
# @param [Symbol] options.unresolved_vars :raise, :keep, :replace => define how to act when unresolved variables within the string are found.
|
36
|
+
# @param [String] options.replace_pattern pattern used when param unresolved_vars is set to :replace. You can include the var name \\1 and filter \\2. Empty string to remove unresolved variables.
|
36
37
|
# @return [String, UnresolvedVariablesError] parsed string
|
37
|
-
def self.parse(string, variables, unresolved_vars: :raise, replace_pattern: "##\\1##")
|
38
|
+
def self.parse(string, variables = {}, options = { unresolved_vars: :raise, replace_pattern: "##\\1##" })
|
38
39
|
variables ||= {}
|
40
|
+
options ||= {}
|
41
|
+
options[:unresolved_vars] = :raise unless options[:unresolved_vars]
|
42
|
+
options[:replace_pattern] = "##\\1##" unless options[:replace_pattern]
|
39
43
|
result_string = string.clone
|
40
44
|
# symbolize keys
|
41
45
|
variables = variables.map { |key, value| [key.to_sym, value] }.to_h
|
@@ -43,18 +47,24 @@ module CurlyBracketParser
|
|
43
47
|
loop do
|
44
48
|
variables(result_string).each do |string_var|
|
45
49
|
dec = decode_variable(string_var)
|
46
|
-
name = dec[:name]
|
50
|
+
name = !dec[:name] || dec[:name] == '' ? "''" : dec[:name]
|
47
51
|
filter = dec[:filter]
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
52
|
+
value = nil
|
53
|
+
is_single_quoted = name.start_with?("'") && name.end_with?("'")
|
54
|
+
is_double_quoted = name.start_with?('"') && name.end_with?('"')
|
55
|
+
# When the name itself is quoted as string or is a number, we use it as a value itself
|
56
|
+
if is_single_quoted || is_double_quoted
|
57
|
+
value = name[1...-1]
|
58
|
+
elsif CurlyBracketParser.number_string? name
|
59
|
+
value = eval(name)
|
60
|
+
elsif variables[name.to_sym]
|
61
|
+
value = variables[name.to_sym]
|
55
62
|
elsif registered_default_var?(name.to_s)
|
56
63
|
value = process_default_var(name)
|
57
|
-
|
64
|
+
end
|
65
|
+
if value
|
66
|
+
value = process_filter(filter, value) if filter
|
67
|
+
result_string.gsub!(string_var, value.to_s)
|
58
68
|
end
|
59
69
|
end
|
60
70
|
# break if no more given variable is available
|
@@ -62,13 +72,13 @@ module CurlyBracketParser
|
|
62
72
|
break
|
63
73
|
end
|
64
74
|
end
|
65
|
-
case unresolved_vars
|
75
|
+
case options[:unresolved_vars]
|
66
76
|
when :raise
|
67
77
|
if any_variable_included? result_string
|
68
78
|
raise UnresolvedVariablesError, "There are unresolved variables in the given string: #{variables(result_string)}"
|
69
79
|
end
|
70
80
|
when :replace
|
71
|
-
result_string.gsub!(VARIABLE_DECODER_REGEX, replace_pattern)
|
81
|
+
result_string.gsub!(VARIABLE_DECODER_REGEX, options[:replace_pattern])
|
72
82
|
end
|
73
83
|
end
|
74
84
|
result_string
|
@@ -314,4 +324,32 @@ module CurlyBracketParser
|
|
314
324
|
|
315
325
|
#----------------------------------------------------------------------------------------------------
|
316
326
|
|
327
|
+
#
|
328
|
+
# Check if given variable is a valid number inside a string that evaluates to a number in Ruby.
|
329
|
+
#
|
330
|
+
# @example
|
331
|
+
# # valid number strings
|
332
|
+
# '200'
|
333
|
+
# '25.75'
|
334
|
+
# '500_000'
|
335
|
+
# '0x1fF'
|
336
|
+
#
|
337
|
+
# @param [String] name
|
338
|
+
# @return [Boolean]
|
339
|
+
def self.number_string?(name)
|
340
|
+
number_regex = /(^[0-9]+[0-9_]*([.][0-9_]*[0-9]+)?$|^0[xX][0-9A-Fa-f]+$)/
|
341
|
+
if (name =~ number_regex) != nil
|
342
|
+
begin
|
343
|
+
eval name
|
344
|
+
true
|
345
|
+
rescue StandardError, SyntaxError => e
|
346
|
+
false
|
347
|
+
end
|
348
|
+
else
|
349
|
+
false
|
350
|
+
end
|
351
|
+
end
|
352
|
+
|
353
|
+
#----------------------------------------------------------------------------------------------------
|
354
|
+
|
317
355
|
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: 1.
|
4
|
+
version: 1.1.6
|
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: 2022-
|
11
|
+
date: 2022-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: lucky_case
|