lm_docstache 3.0.3 → 3.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/CHANGELOG.md +4 -0
- data/lib/lm_docstache/parser.rb +44 -1
- data/lib/lm_docstache/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: ce8fea2c12829636bd22622e1c022cf2ead4ec09997b7e13322f2b94b4261654
|
4
|
+
data.tar.gz: a25d02153cb1a53bf74111dc59710d83a133a821c4632607c60e06531b28b4ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99bcdac9eea8b1d62e0733b692c600be5e8dcb88710f58d22299b6daa7eabab5f5b01eeb83e61668709655869f6580e918ef38d11bb5ae781db4462b38abba65
|
7
|
+
data.tar.gz: ea96603b65d984edfeeb9aa9f9b760c9665af2c5df163e4c3dae076b24ebc10fccb8e1bdee1cc6e01761496a4b89b7454e80965d6a24e96eb67b53b17e46be84
|
data/CHANGELOG.md
CHANGED
data/lib/lm_docstache/parser.rb
CHANGED
@@ -18,7 +18,22 @@ module LMDocstache
|
|
18
18
|
VARIABLE_MATCHER = /{{([^#\^\/].*?)}}/
|
19
19
|
|
20
20
|
attr_reader :document, :data, :blocks, :special_variable_replacements, :hide_custom_tags
|
21
|
+
attr_reader :data_sequential_replacement
|
21
22
|
|
23
|
+
# Constructor +data+ argument is a +Hash+ where the key is
|
24
|
+
# expected to be a +String+ representing the replacement block value. +Hash+
|
25
|
+
# key must not contain the `{{}}` part, but only the pattern characters.
|
26
|
+
# As for the values of the +Hash+, we have options:
|
27
|
+
#
|
28
|
+
# * +String+ will be the value that will replace matching string.
|
29
|
+
# * +Array<String>+ will be an ordered sequence of values that will replace the matched string following
|
30
|
+
# document matching order.
|
31
|
+
#
|
32
|
+
# Example:
|
33
|
+
# { 'full_name' => 'John Doe', 'text|req|Client' => ['John', 'Matt', 'Paul'] }
|
34
|
+
#
|
35
|
+
# Constructor +options+ argument is a +Hash+ where keys can be:
|
36
|
+
#
|
22
37
|
# The +special_variable_replacements+ option is a +Hash+ where the key is
|
23
38
|
# expected to be either a +Regexp+ or a +String+ representing the pattern
|
24
39
|
# of more specific type of variables that deserves a special treatment. The
|
@@ -47,7 +62,8 @@ module LMDocstache
|
|
47
62
|
# will be the value that will replace the matched string
|
48
63
|
def initialize(document, data, options = {})
|
49
64
|
@document = document
|
50
|
-
@data = data.transform_keys(&:to_s)
|
65
|
+
@data = data.transform_keys(&:to_s).select {|e, v| !v.is_a?(Array) }
|
66
|
+
@data_sequential_replacement = data.transform_keys(&:to_s).select {|e, v| v.is_a?(Array) }
|
51
67
|
@special_variable_replacements = add_blocks_to_regexp(options.fetch(:special_variable_replacements, {}))
|
52
68
|
@hide_custom_tags = add_blocks_to_regexp(options.fetch(:hide_custom_tags, {}))
|
53
69
|
end
|
@@ -65,6 +81,7 @@ module LMDocstache
|
|
65
81
|
hide_custom_tags!
|
66
82
|
find_blocks
|
67
83
|
replace_conditional_blocks_in_document!
|
84
|
+
replace_data_sequentially_in_document!
|
68
85
|
replace_variables_in_document!
|
69
86
|
end
|
70
87
|
|
@@ -140,6 +157,32 @@ module LMDocstache
|
|
140
157
|
end
|
141
158
|
end
|
142
159
|
|
160
|
+
def replace_data_sequentially_in_document!
|
161
|
+
data_sequential_replacement.each do |tag_key, values|
|
162
|
+
|
163
|
+
tag = Regexp.escape("{{#{tag_key}}}")
|
164
|
+
pattern_found = 0
|
165
|
+
|
166
|
+
document.css('w|t').each do |text_node|
|
167
|
+
text = text_node.text
|
168
|
+
|
169
|
+
if text.match(tag)
|
170
|
+
|
171
|
+
text.gsub!(/#{tag}/) do |_match|
|
172
|
+
value = values[pattern_found]
|
173
|
+
# if there is no more available value replace the content with empty string
|
174
|
+
return '' unless value
|
175
|
+
|
176
|
+
pattern_found +=1
|
177
|
+
value
|
178
|
+
end
|
179
|
+
|
180
|
+
text_node.content = text
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
143
186
|
def has_skippable_variable?(text)
|
144
187
|
return true if hide_custom_tags.find { |(pattern, value)| text =~ pattern }
|
145
188
|
!!special_variable_replacements.find do |(pattern, value)|
|
data/lib/lm_docstache/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lm_docstache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.
|
4
|
+
version: 3.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roey Chasman
|
@@ -12,7 +12,7 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date: 2021-
|
15
|
+
date: 2021-05-14 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: nokogiri
|