curly_bracket_parser 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +55 -18
- data/lib/curly_bracket_parser.rb +39 -6
- data/lib/curly_bracket_parser/version.rb +1 -1
- 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: e2581aa770602a42a73eb9f79f3840dd53609d2905a75db5aaf3a5b0dac1f018
|
4
|
+
data.tar.gz: 912c7fb9c2285515bf5b7af00da8c629222af329e68dd431f5cd649c20815df4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 05eba36a79347485fd42db86ecfe49824913b497a89b6f36aa3b2924c4b2b8b6b1fae89b4bb3099708f5b76488706d1d6da4ff035f0c41bbf52cb0633f96a624
|
7
|
+
data.tar.gz: 56a9306ca6375a6e7c4366781b638eb509918c2a101703de930d0f481a8661ef00e1054925c74ff845c3de0cd2ea51f6b7304f34a259aad27379a8a15fa51c3b
|
data/README.md
CHANGED
@@ -1,9 +1,14 @@
|
|
1
1
|
# curly_bracket_parser
|
2
2
|
|
3
|
-
Simple parser to replace curly brackets {{like_this}} inside strings like URLs,
|
3
|
+
Simple parser to replace curly brackets `{{like_this}}` inside strings like URLs, texts or even files easily.
|
4
4
|
|
5
|
-
|
5
|
+
Additional support for build-in filters and custom filters make them more powerful. `{{example|my_filter}}`
|
6
6
|
|
7
|
+
Using [LuckyCase](https://github.com/magynhard/lucky_case), all its case formats are supported as filter.
|
8
|
+
|
9
|
+
```diff
|
10
|
+
- beta version
|
11
|
+
```
|
7
12
|
|
8
13
|
|
9
14
|
|
@@ -36,23 +41,13 @@ Or install it yourself as:
|
|
36
41
|
|
37
42
|
$ gem install curly_bracket_parser
|
38
43
|
|
39
|
-
## Common information
|
40
|
-
|
41
|
-
TODO
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
44
|
|
48
45
|
<a name="usage"></a>
|
49
|
-
## Usage
|
50
|
-
|
51
|
-
TODO
|
46
|
+
## Usage examples
|
52
47
|
|
53
|
-
|
48
|
+
You can either parse variables inside strings or even directly in files.
|
54
49
|
|
55
|
-
|
50
|
+
### Basic
|
56
51
|
|
57
52
|
```ruby
|
58
53
|
url = "https://my-domain.com/items/{{item_id}}"
|
@@ -60,7 +55,7 @@ TODO
|
|
60
55
|
# => "https://my-domain.com/items/123"
|
61
56
|
```
|
62
57
|
|
63
|
-
|
58
|
+
### Filters
|
64
59
|
|
65
60
|
```ruby
|
66
61
|
url = "https://my-domain.com/catalog/{{item_name|snake_case}}"
|
@@ -68,9 +63,51 @@ TODO
|
|
68
63
|
# => "https://my-domain.com/catalog/mega_super_item"
|
69
64
|
```
|
70
65
|
|
71
|
-
|
66
|
+
For a list of built-in filters visit [LuckyCase](https://github.com/magynhard/lucky_case).
|
67
|
+
|
68
|
+
#### Define your custom filter
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
CurlyBracketParser.register_filter('7times') do |string|
|
72
|
+
string.to_s * 7
|
73
|
+
end
|
74
|
+
|
75
|
+
text = "Paul went out and screamed: A{{scream|7times}}h"
|
76
|
+
final_text = CurlyBracketParser.parse text, scream: 'a'
|
77
|
+
# => "Paul went out and screamed: Aaaaaaaah"
|
78
|
+
```
|
79
|
+
|
80
|
+
### Files
|
81
|
+
|
82
|
+
<ins>test.html</ins>
|
83
|
+
```html
|
84
|
+
<h1>{{title|sentence_case}}</h1>
|
85
|
+
```
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
parsed_file = CurlyBracketParser.parse_file './test.html', title: 'WelcomeAtHome'
|
89
|
+
# => "<h1>Welcome at home</h1>"
|
90
|
+
```
|
72
91
|
|
73
|
-
|
92
|
+
Use `#parse_file!` instead to write the parsed string directly into the file!
|
93
|
+
|
94
|
+
### Default variables
|
95
|
+
|
96
|
+
You can define default variables, which will be replaced automatically without passing them by parameters, but can be overwritten with parameters.
|
97
|
+
|
98
|
+
Because of providing blocks, your variables can dynamically depend on other states.
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
CurlyBracketParser.register_default_var('version') do
|
102
|
+
'1.0.2'
|
103
|
+
end
|
104
|
+
|
105
|
+
text = "You are running version {{version}}"
|
106
|
+
CurlyBracketParser.parse text
|
107
|
+
# => "You are running version 1.0.2"
|
108
|
+
CurlyBracketParser.parse text, version: '0.7.0'
|
109
|
+
# => "You are running version 0.7.0"
|
110
|
+
```
|
74
111
|
|
75
112
|
|
76
113
|
|
data/lib/curly_bracket_parser.rb
CHANGED
@@ -23,6 +23,8 @@ module CurlyBracketParser
|
|
23
23
|
|
24
24
|
#----------------------------------------------------------------------------------------------------
|
25
25
|
|
26
|
+
# Parse given string and replace the included variables by the given variables
|
27
|
+
#
|
26
28
|
# @param [String] string to parse
|
27
29
|
# @param [Hash<Symbol => String>] variables <key: 'value'>
|
28
30
|
# @param [Symbol] unresolved_vars :raise, :keep, :replace => define how to act when unresolved variables within the string are found.
|
@@ -56,23 +58,47 @@ module CurlyBracketParser
|
|
56
58
|
|
57
59
|
#----------------------------------------------------------------------------------------------------
|
58
60
|
|
59
|
-
|
60
|
-
|
61
|
+
# Parse the content of the file of the given path with #parse and return it.
|
62
|
+
# The original file keeps unmodified.
|
63
|
+
#
|
64
|
+
# @param [String] string to parse
|
65
|
+
# @param [Hash<Symbol => String>] variables <key: 'value'>
|
66
|
+
# @param [Symbol] unresolved_vars :raise, :keep, :replace => define how to act when unresolved variables within the string are found.
|
67
|
+
# @param [String] replace_pattern pattern used when param unresolved_vars is set to :replace. You can include the var name \\1 and filter \\1. Empty string to remove unresolved variables.
|
68
|
+
# @return [String, UnresolvedVariablesError] parsed string
|
69
|
+
def self.parse_file(path, variables, unresolved_vars: :raise, replace_pattern: "##\\1##")
|
70
|
+
file_content = File.read path
|
71
|
+
parse(file_content, variables, unresolved_vars: unresolved_vars, replace_pattern: replace_pattern)
|
61
72
|
end
|
62
73
|
|
63
|
-
|
64
|
-
|
74
|
+
# Parse the content of the file of the given path with #parse and return it.
|
75
|
+
# The original file will be overwritten by the parsed content.
|
76
|
+
#
|
77
|
+
# @param [String] string to parse
|
78
|
+
# @param [Hash<Symbol => String>] variables <key: 'value'>
|
79
|
+
# @param [Symbol] unresolved_vars :raise, :keep, :replace => define how to act when unresolved variables within the string are found.
|
80
|
+
# @param [String] replace_pattern pattern used when param unresolved_vars is set to :replace. You can include the var name \\1 and filter \\1. Empty string to remove unresolved variables.
|
81
|
+
# @return [String, UnresolvedVariablesError] parsed string
|
82
|
+
def self.parse_file!(path, variables, unresolved_vars: :raise, replace_pattern: "##\\1##")
|
83
|
+
parsed_file = parse_file path, variables, unresolved_vars: unresolved_vars, replace_pattern: replace_pattern
|
84
|
+
File.write path, parsed_file
|
85
|
+
parsed_file
|
65
86
|
end
|
66
87
|
|
67
88
|
#----------------------------------------------------------------------------------------------------
|
68
89
|
|
69
|
-
|
90
|
+
# Register your custom filter to the filter list
|
91
|
+
#
|
92
|
+
# @param [String] name of the filter, also used then in your strings, e.g. {{var_name|my_filter_name}}
|
93
|
+
# @param [Lambda] function of the filter to run the variable against
|
94
|
+
# @return [Boolean] true if filter was added, false if it already exists
|
95
|
+
def self.register_filter(name, &block)
|
70
96
|
raise "NOT IMPLEMENTED YET!"
|
71
97
|
end
|
72
98
|
|
73
99
|
#----------------------------------------------------------------------------------------------------
|
74
100
|
|
75
|
-
def self.register_default_variable(name,
|
101
|
+
def self.register_default_variable(name, &block)
|
76
102
|
raise "NOT IMPLEMENTED YET!"
|
77
103
|
end
|
78
104
|
|
@@ -133,12 +159,19 @@ module CurlyBracketParser
|
|
133
159
|
|
134
160
|
#----------------------------------------------------------------------------------------------------
|
135
161
|
|
162
|
+
# Check if any variable is included in the given string
|
163
|
+
# @param [Object] string
|
164
|
+
# @return [Boolean] true if any variable is included in the given string, otherwise false
|
136
165
|
def self.any_variable_included?(string)
|
137
166
|
string.match(VARIABLE_REGEX) != nil
|
138
167
|
end
|
139
168
|
|
140
169
|
#----------------------------------------------------------------------------------------------------
|
141
170
|
|
171
|
+
# Check if one of the given variable names is included in the given string
|
172
|
+
# @param [Array<String>] variable_names
|
173
|
+
# @param [String] string
|
174
|
+
# @return [Boolean] true if one given variable name is included in given the string, otherwise false
|
142
175
|
def self.includes_one_variable_of(variable_names, string)
|
143
176
|
decoded_variables(string).each do |dvar|
|
144
177
|
return true if variable_names.include?(dvar[:name])
|
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.0
|
4
|
+
version: 0.1.0
|
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: 2020-
|
11
|
+
date: 2020-12-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: lucky_case
|