general 1.2.4 → 1.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/gtemplate.rb +47 -33
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aa693f6a6d94f53eaedb644de5ee29cd920938da
4
- data.tar.gz: f4a1798024ee0d8d67d3365e3ff2d4e6fb93b1f1
3
+ metadata.gz: 1ab656ffd2493215af70ca4cd3f055c124dd027a
4
+ data.tar.gz: 880637049b15f23f04b617dba41ca035463fd2c6
5
5
  SHA512:
6
- metadata.gz: 7c77f1f3063b892d4a16e1ea4332d38e0a819bb5e8f488912458f3c1c8b2169dc17ce4c451f8079a2ca67dce895b2501e74a76cb9cf8a456ef4536c2e8879c0f
7
- data.tar.gz: aa221f8c6f1ffab3c91a8b8ed266437f34c652f3c69e0723c030ff2c5056afb3b9a7e9cfad718ac34afccd5c63c10852c961049334d3a614b8453315f14f0d8c
6
+ metadata.gz: 18c45c45a642d87b926cdc987a6c243e6d7a36ef5e5deab67b75102669f39d414288fbda8f8dc66dde3594117a5b2992393a369358d2bb6bc4fa362260c5eccd
7
+ data.tar.gz: 06cce36678474a7e5482b714893d66796ca1bd0c85e86a07ae1fdb317022a368b5ee3ebc46fd1ad4bbf6ce2138621dea0c16fa47cbe401f3cf8e7fbabf6dca0a
data/lib/gtemplate.rb CHANGED
@@ -28,7 +28,7 @@ module General
28
28
  PLACEHOLDER = /@\((?<name>[a-zA-Z]\w*)\s*(\:\s*(?<default>.*?))?\s*(->\s*(?<operation>[a-zA-Z]\w*))?\)/
29
29
 
30
30
  # Regular expression that matches array placeholders
31
- ARRAY_PLACEHOLDER = /@\[(?<name>[a-zA-Z]\w*)\]\s*(?<text>.*?)\s*@\[(?<delimeter>.+)?\]/m
31
+ ARRAY_PLACEHOLDER = /@\[(?<name>[a-zA-Z]\w*)\]( |\n)?(?<text>.*?)( |\n)?@\[(?<delimeter>.+)?\]/m
32
32
 
33
33
  # Operations that can be called on placeholder values
34
34
  OPERATIONS = {
@@ -79,7 +79,7 @@ module General
79
79
  if @array[key]
80
80
  applied_parts[place[:index]] = place[:template].apply_all(value).join(place[:delimeter])
81
81
  else
82
- applied_parts[place[:index]] = place[:operation].call(value)
82
+ applied_parts[place[:index]] = OPERATIONS[place[:operation]].call(value)
83
83
  end
84
84
  end
85
85
  else
@@ -121,37 +121,51 @@ module General
121
121
  def parse_string string
122
122
  # While there is still a placeholder in string
123
123
  while has_placeholder string
124
- if ARRAY_PLACEHOLDER =~ string
125
- # Split match and add parts
126
- match = ARRAY_PLACEHOLDER.match string
127
- @parts << string[0...match.begin(0)] << match
128
- string = string[match.end(0)..-1]
129
-
130
- # Get name
131
- name = match[:name].to_sym
132
-
133
- # Get delimeter (if any) and parse array template
134
- delimeter = match[:delimeter].nil? ? " " : match[:delimeter]
135
- template = GTemplate.new(match[:text])
136
-
137
- # Push place and array information
138
- push_place name, {index: @parts.length - 1, template: template, delimeter: delimeter}
139
- @array[name] = true
140
- elsif PLACEHOLDER =~ string
141
- # Split match and add parts
142
- match = PLACEHOLDER.match string
143
- @parts << string[0...match.begin(0)] << match
144
- string = string[match.end(0)..-1]
145
-
146
- # Get name
147
- name = match[:name].to_sym
148
-
149
- # Get operation and arguments (if any)
150
- operation = match[:operation].nil? ? OPERATIONS[:default] : OPERATIONS[match[:operation].to_sym]
151
-
152
- # Push place and default information
153
- push_place name, {index: @parts.length - 1, operation: operation}
154
- @defaults[name] = match[:default] unless @defaults.has_key? name
124
+ # Find locations of next placeholder and next array placeholder
125
+ next_p = PLACEHOLDER =~ string
126
+ next_a = ARRAY_PLACEHOLDER =~ string
127
+
128
+ # If found at least one
129
+ if !(next_a.nil? && next_p.nil?)
130
+
131
+ # If found only array placeholder (or if found array placeholder before placeholder)
132
+ # Process placeholder
133
+ if next_p.nil? && !next_a.nil? || !(next_a.nil? || next_p.nil?) && next_a < next_p
134
+ # Split match and add parts
135
+ match = ARRAY_PLACEHOLDER.match string
136
+ @parts << string[0...match.begin(0)] << match[:name].to_sym
137
+ string = string[match.end(0)..-1]
138
+
139
+ # Get name
140
+ name = match[:name].to_sym
141
+
142
+ # Get delimeter (if any) and parse array template
143
+ delimeter = match[:delimeter].nil? ? " " : match[:delimeter]
144
+ template = GTemplate.new(match[:text])
145
+
146
+ # Push place and array information
147
+ push_place name, {index: @parts.length - 1, template: template, delimeter: delimeter}
148
+ @array[name] = true
149
+
150
+ # If found only array placeholder (or if found array placeholder before placeholder)
151
+ # Process placeholder
152
+ elsif next_a.nil? && !next_p.nil? || !(next_a.nil? || next_p.nil?) && next_p < next_a
153
+ # Split match and add parts
154
+ match = PLACEHOLDER.match string
155
+ @parts << string[0...match.begin(0)] << match[:name].to_sym
156
+ string = string[match.end(0)..-1]
157
+
158
+ # Get name
159
+ name = match[:name].to_sym
160
+
161
+ # Get operation and arguments (if any)
162
+ operation = match[:operation].nil? ? :default : match[:operation].to_sym
163
+
164
+ # Push place and default information
165
+ push_place name, {index: @parts.length - 1, operation: operation}
166
+ @defaults[name] = match[:default] unless @defaults.has_key? name
167
+
168
+ end
155
169
  end
156
170
  end
157
171
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: general
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.4
4
+ version: 1.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anshul Kharbanda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-27 00:00:00.000000000 Z
11
+ date: 2016-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -27,7 +27,7 @@ dependencies:
27
27
  description: "General is a simple templating system in ruby that allows you to create
28
28
  templates from both \t\t\t\t\t\tpure strings and files (with the extension .general),
29
29
  as well as create new strings and files \t\t\t\t\t\twith these created objects.
30
- For more information, read the README for documentation."
30
+ For more information, visit the homepage"
31
31
  email: akanshul97@gmail.com
32
32
  executables: []
33
33
  extensions: []