re_duxml 0.1.4 → 0.2.0

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/re_duxml.rb +19 -12
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4d2ccb9fa84a9e969220ff9ab2cbe9a945cb9f07
4
- data.tar.gz: 9f9c52ad59e65c6d888e18c35cdbdf900c962aa5
3
+ metadata.gz: b3d5fa8b77a1b0b291c282574fc9a17cbd1e0de6
4
+ data.tar.gz: 9e29841defd5a865ce9644da31f58b6dfc1f955a
5
5
  SHA512:
6
- metadata.gz: 61dfd7e3bc30423fbc216947232d60d7da2247a74d18c26f984450d00c92e627c79fc250fba7e3b70695f70e59545c37521c575e7015e12eef62407f01a342df
7
- data.tar.gz: 6e6c50737da85d318f516f2f10d6daa905b0ddae7ddbb60ea1e11d312a9f2b95b49920e3fc80f70cbf760424b6224b8985652994d56e83ad5eed123ca4381711
6
+ metadata.gz: 55ef6a539b7887e2b23055cb7e1ad8f751fc7661d2f91d92e095238ddb8c834580097311a837e77594d7f4e998033048e1ff91bd212526a00c89b449a06297ee
7
+ data.tar.gz: 4f89848c40a34c56fd6ee9545ff5bbb2d5bd2c0b6d290f42d94669e5016772cccd94d5136ccec3ab869b07909be4e6d0365a581332a44a159cb0452e751ba010
data/lib/re_duxml.rb CHANGED
@@ -85,20 +85,27 @@ module ReDuxml
85
85
  # @param param_hash [Hash] keys are parameter names, values are parameter values e.g. {var0: 'param', var1: 'eter'}
86
86
  # @return [String] content_str with parameter values substituted and algebraically reduced if any unknown parameters remain e.g. 'a parameter string @(var2)'
87
87
  def resolve_str(content_str, param_hash)
88
- question = find_expr content_str
89
- return content_str if question.nil?
90
- reply = Macro.new e.evaluate(question, param_hash).to_s
91
- replacement_str = reply.parameterized? ? reply.macro_string : reply.demacro
92
- macro_string = Macro.new(question).macro_string
93
- content_str.gsub(macro_string, replacement_str)
88
+ output_str = content_str.clone
89
+ questions = find_exprs content_str
90
+ return output_str if questions.empty?
91
+ questions.each do |question|
92
+ reply = Macro.new e.evaluate(question, param_hash).to_s
93
+ replacement_str = reply.parameterized? ? reply.macro_string : reply.demacro
94
+ macro_string = Macro.new(question).macro_string
95
+ output_str.gsub!(macro_string, replacement_str)
96
+ end
97
+ output_str
94
98
  end
95
99
 
96
100
  # @param str [String] string that may contain parameter expression e.g. 'asdf @(param + 2) asdf'
97
- # @return [String] macro string e.g. 'asdf @(param + 2) asdf' => 'param + 2'
98
- def find_expr(str)
99
- expr_start_index = str.index('@(')
100
- return nil if expr_start_index.nil?
101
- expr_end_index = find_close_parens_index str[expr_start_index+1..-1]
102
- str[expr_start_index+2, expr_end_index-1]
101
+ # @return [Array[String]] array of macro strings e.g. 'asdf @(param + 2) asdf' => 'param + 2'
102
+ def find_exprs(str)
103
+ expressions = []
104
+ str.scan('@(') do |c|
105
+ expr_start_index = $~.offset(0)[0] # gets index of current match
106
+ expr_end_index = find_close_parens_index str[expr_start_index+1..-1]
107
+ expressions << str[expr_start_index+2, expr_end_index-1]
108
+ end
109
+ expressions
103
110
  end
104
111
  end # module Dux
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: re_duxml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Kong