lm_docstache 2.1.1 → 3.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +35 -0
- data/lib/lm_docstache/document.rb +4 -0
- data/lib/lm_docstache/hide_custom_tags.rb +31 -18
- data/lib/lm_docstache/parser.rb +19 -14
- data/lib/lm_docstache/renderer.rb +2 -1
- data/lib/lm_docstache/version.rb +1 -1
- data/spec/example_input/docx-no-rpr.docx +0 -0
- data/spec/example_input/sample-signature-with-tabs-spacing.docx +0 -0
- data/spec/hide_custom_tags_spec.rb +55 -12
- data/spec/integration_spec.rb +26 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 233db6459ecd3c23c9d566659730ce1e4f2fcfb11b0ad4054dd83631375587ec
|
4
|
+
data.tar.gz: 344689f276e851c835b550f6197cc11dc2c698842b57efc29fc981fbb0026238
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 342e46c6da0b34131af9cabd468fcae78e1dd272a8e08f4b395abfa2343f51b592d88ada3885c0c7db13d1ef89b66df30f49ff7237c1fba6f61356d5130e3e52
|
7
|
+
data.tar.gz: 03f3a44bf4a93b5d066cb141fabb9a2d6317940f9705213183ece98cdc2861bb41e7a0d12f399b80d4f6217f92be72bf96ca62a91ddbc790d9ccb57f4bf8d25c
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,40 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 3.0.3
|
4
|
+
|
5
|
+
### Bugfix
|
6
|
+
|
7
|
+
* Hide custom tags arguments was pushing blocks tags to end of paragraph. There are cases this approach
|
8
|
+
doesn't work. I changed to be an ordered replacement when we match hide tags.
|
9
|
+
* Avoid to merge Tab tags on fix errors methods. This was causing unexpected document changes.
|
10
|
+
|
11
|
+
## 3.0.2
|
12
|
+
|
13
|
+
### Bugfix
|
14
|
+
|
15
|
+
* Fix replacing tags related to hidden custom tags regexp formats. E.g. tab characters.
|
16
|
+
|
17
|
+
## 3.0.1
|
18
|
+
|
19
|
+
### Bugfix
|
20
|
+
|
21
|
+
* Fix Hide Custom Tag feature when document there is no text inside a w|r we
|
22
|
+
can't split content.
|
23
|
+
|
24
|
+
## 3.0.0
|
25
|
+
|
26
|
+
## Breaking Changes
|
27
|
+
* Replaced Renderer `hide_custom_tags` options to be a `Hash` instead of `Array`.
|
28
|
+
There are are edge cases which we want to replace the content on hide custom tags.
|
29
|
+
All documentations can be followed on `Renderer` and `HideCustomTag` classes.
|
30
|
+
|
31
|
+
## 2.1.2
|
32
|
+
|
33
|
+
### Bugfix
|
34
|
+
|
35
|
+
* Giving a document there is no *rPr* tag we should add new *rPr* tag before
|
36
|
+
*t* tag.
|
37
|
+
|
3
38
|
## 2.1.1
|
4
39
|
|
5
40
|
### Improvements
|
@@ -138,6 +138,10 @@ module LMDocstache
|
|
138
138
|
previous_text_node = previous_run_node.at_css('w|t')
|
139
139
|
current_text_node = run_node.at_css('w|t')
|
140
140
|
|
141
|
+
# avoid to merge blocks with tabs
|
142
|
+
next if run_node.at_css('w|tab')
|
143
|
+
next if previous_run_node.at_css('w|tab')
|
144
|
+
|
141
145
|
next if style_html != previous_style_html
|
142
146
|
next if current_text_node.nil? || previous_text_node.nil?
|
143
147
|
|
@@ -2,43 +2,56 @@ module LMDocstache
|
|
2
2
|
class HideCustomTags
|
3
3
|
attr_reader :document, :hide_custom_tags
|
4
4
|
|
5
|
-
# The +hide_custom_tags+ options is
|
6
|
-
# the pattern you expect to keep at the document but
|
5
|
+
# The +hide_custom_tags+ options is a +Hash+ of +Regexp+ or +String+ keys representing
|
6
|
+
# the pattern you expect to keep at the document but replacing the content to use
|
7
|
+
# font color equal to document background color or white.
|
8
|
+
# For the +Hash+ values we can have:
|
7
9
|
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
|
10
|
+
# * +false+ -> In this case we don't change the text content.
|
11
|
+
# * +Proc+ -> When a +Proc+ instance is provided, it's expected it to be
|
12
|
+
# able to receive the matched string and to return the string that will be
|
13
|
+
# used as replacement.
|
14
|
+
# * any other value that will be turned into a string -> in this case, this
|
15
|
+
# will be the value that will replace the matched string
|
16
|
+
def initialize(document:, hide_custom_tags: {})
|
12
17
|
@document = document
|
13
18
|
@hide_custom_tags = hide_custom_tags
|
14
19
|
end
|
15
20
|
|
16
21
|
# Find all run nodes matching hide custom tags +Regexp's+ options you defined, split it
|
17
22
|
# in multiple runs and replace font color to document background color or white in the matching tag run node.
|
23
|
+
# Replace content if you have defined any replacement value.
|
18
24
|
def hide_custom_tags!
|
19
|
-
hide_custom_tags.each do |full_pattern|
|
25
|
+
hide_custom_tags.each do |full_pattern, value|
|
20
26
|
paragraphs = document.css('w|p')
|
21
27
|
while paragraph = paragraphs.shift do
|
22
28
|
next unless paragraph.text =~ full_pattern
|
23
29
|
run_nodes = paragraph.css('w|r')
|
24
30
|
while run_node = run_nodes.shift
|
25
|
-
next
|
26
|
-
|
27
|
-
run_node.
|
28
|
-
|
31
|
+
next unless run_node.at_css('w|t')
|
32
|
+
next unless run_node.text =~ full_pattern
|
33
|
+
tag_contents = split_tag_content(run_node.text, full_pattern)
|
34
|
+
replacement_nodes = []
|
29
35
|
tag_contents[:content_list].each_with_index do |content, idx|
|
36
|
+
remainder_run_node = run_node.clone
|
30
37
|
replace_content(remainder_run_node, content)
|
31
|
-
run_node_with_match = remainder_run_node.dup
|
32
38
|
matched_tag = tag_contents[:matched_tags][idx]
|
33
|
-
|
39
|
+
replacement_nodes << remainder_run_node
|
34
40
|
if matched_tag
|
41
|
+
run_node_with_match = run_node.clone
|
35
42
|
replace_style(run_node_with_match)
|
36
|
-
|
37
|
-
|
43
|
+
matched_content = matched_tag
|
44
|
+
if value
|
45
|
+
matched_content = value.is_a?(Proc) ?
|
46
|
+
value.call(matched_tag) :
|
47
|
+
value.to_s
|
48
|
+
end
|
49
|
+
replace_content(run_node_with_match, matched_content)
|
50
|
+
replacement_nodes << run_node_with_match
|
38
51
|
end
|
39
|
-
paragraph << Nokogiri::XML::NodeSet.new(document, nodes_list)
|
40
|
-
remainder_run_node = remainder_run_node.clone
|
41
52
|
end
|
53
|
+
run_node.add_next_sibling(Nokogiri::XML::NodeSet.new(document, replacement_nodes))
|
54
|
+
run_node.unlink
|
42
55
|
end
|
43
56
|
end
|
44
57
|
end
|
@@ -64,7 +77,7 @@ module LMDocstache
|
|
64
77
|
w_color.unlink if w_color
|
65
78
|
style << "<w:color w:val=\"#{font_color}\"/>"
|
66
79
|
else
|
67
|
-
run_node
|
80
|
+
run_node.prepend_child("<w:rPr><w:color w:val=\"#{font_color}\"/></w:rPr>")
|
68
81
|
end
|
69
82
|
end
|
70
83
|
|
data/lib/lm_docstache/parser.rb
CHANGED
@@ -34,22 +34,30 @@ module LMDocstache
|
|
34
34
|
# * any other value that will be turned into a string -> in this case, this
|
35
35
|
# will be the value that will replace the matched string
|
36
36
|
#
|
37
|
-
# The +hide_custom_tags+ options is
|
38
|
-
# the pattern you expect to keep at the document but
|
37
|
+
# The +hide_custom_tags+ options is a +Hash+ of +Regexp+ or +String+ keys representing
|
38
|
+
# the pattern you expect to keep at the document but replacing the content to use
|
39
|
+
# font color equal to document background color or white.
|
40
|
+
# For the +Hash+ values we can have:
|
39
41
|
#
|
40
|
-
#
|
41
|
-
#
|
42
|
-
#
|
42
|
+
# * +false+ -> In this case we don't change the text content.
|
43
|
+
# * +Proc+ -> When a +Proc+ instance is provided, it's expected it to be
|
44
|
+
# able to receive the matched string and to return the string that will be
|
45
|
+
# used as replacement.
|
46
|
+
# * any other value that will be turned into a string -> in this case, this
|
47
|
+
# will be the value that will replace the matched string
|
43
48
|
def initialize(document, data, options = {})
|
44
49
|
@document = document
|
45
50
|
@data = data.transform_keys(&:to_s)
|
46
|
-
@special_variable_replacements = options.fetch(:special_variable_replacements, {})
|
47
|
-
@hide_custom_tags =
|
51
|
+
@special_variable_replacements = add_blocks_to_regexp(options.fetch(:special_variable_replacements, {}))
|
52
|
+
@hide_custom_tags = add_blocks_to_regexp(options.fetch(:hide_custom_tags, {}))
|
48
53
|
end
|
49
54
|
|
50
|
-
|
51
|
-
|
52
|
-
|
55
|
+
# Replace +Regepx+ or +String+ keys to have the enclosing blocks
|
56
|
+
def add_blocks_to_regexp(options)
|
57
|
+
options.inject({}) do |x, (regexp_str, value)|
|
58
|
+
key = regexp_str.is_a?(String) ? Regexp.new("{{#{regexp_str}}}") : /{{#{regexp_str.source}}/
|
59
|
+
x[key] = value
|
60
|
+
x
|
53
61
|
end
|
54
62
|
end
|
55
63
|
|
@@ -128,15 +136,13 @@ module LMDocstache
|
|
128
136
|
variable_replacement.call($1) :
|
129
137
|
variable_replacement.to_s
|
130
138
|
end
|
131
|
-
|
132
139
|
text_node.content = text
|
133
140
|
end
|
134
141
|
end
|
135
142
|
|
136
143
|
def has_skippable_variable?(text)
|
137
|
-
return true if hide_custom_tags.find { |pattern| text =~ pattern }
|
144
|
+
return true if hide_custom_tags.find { |(pattern, value)| text =~ pattern }
|
138
145
|
!!special_variable_replacements.find do |(pattern, value)|
|
139
|
-
pattern = pattern.is_a?(String) ? /{{#{pattern}}}/ : /{{#{pattern.source}}}/
|
140
146
|
text =~ pattern && value == false
|
141
147
|
end
|
142
148
|
end
|
@@ -144,7 +150,6 @@ module LMDocstache
|
|
144
150
|
def special_variable_replacement(text)
|
145
151
|
Array(
|
146
152
|
special_variable_replacements.find do |(pattern, value)|
|
147
|
-
pattern = pattern.is_a?(String) ? /{{#{pattern}}}/ : /{{#{pattern.source}}}/
|
148
153
|
text =~ pattern && !!value
|
149
154
|
end
|
150
155
|
).last
|
@@ -6,7 +6,8 @@ module LMDocstache
|
|
6
6
|
|
7
7
|
def initialize(xml, data, options = {})
|
8
8
|
@content = xml
|
9
|
-
|
9
|
+
option_types = [:special_variable_replacements, :hide_custom_tags]
|
10
|
+
@parser = Parser.new(xml, data, options.slice(*option_types))
|
10
11
|
end
|
11
12
|
|
12
13
|
def render
|
data/lib/lm_docstache/version.rb
CHANGED
Binary file
|
Binary file
|
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe LMDocstache::HideCustomTags do
|
4
|
-
|
5
4
|
context '#example' do
|
6
5
|
let(:output_dir) { "#{base_path}/tmp/" }
|
7
6
|
let(:output_file) { File.new("#{output_dir}/BlankTestOutput.docx", 'w') }
|
@@ -16,23 +15,31 @@ describe LMDocstache::HideCustomTags do
|
|
16
15
|
end
|
17
16
|
|
18
17
|
let(:base_path) { SPEC_BASE_PATH.join('example_input') }
|
19
|
-
let(:document) {
|
20
|
-
|
18
|
+
let(:document) {
|
19
|
+
doc = LMDocstache::Document.new(input_file)
|
20
|
+
doc.fix_errors
|
21
|
+
doc.instance_variable_get(:@document)
|
22
|
+
}
|
23
|
+
let(:regexp_tag) { /(?:sig|sigfirm|date|text|initial)\|(?:req|noreq)\|.+?/ }
|
24
|
+
let(:regexp_for_replacement) { /(?:check)\|(?:req|noreq)\|.+?/ }
|
21
25
|
let(:hide_custom_tags) {
|
22
|
-
LMDocstache::HideCustomTags.new(document: document, hide_custom_tags:
|
26
|
+
LMDocstache::HideCustomTags.new(document: document, hide_custom_tags: {
|
27
|
+
/#{regexp_tag}/ => false,
|
28
|
+
/#{regexp_for_replacement}/ => 'replaced_content'
|
29
|
+
})
|
23
30
|
}
|
24
31
|
|
25
32
|
context "giving a document with blue background" do
|
26
33
|
let(:input_file) { "#{base_path}/sample-signature-blue.docx" }
|
27
34
|
|
28
|
-
it 'expect to have a
|
35
|
+
it 'expect to have a white color on all hide custom tags matching and have first child node equal rPr tag' do
|
29
36
|
hide_custom_tags.hide_custom_tags!
|
30
37
|
d = hide_custom_tags.document
|
31
38
|
run_nodes = d.css('w|p w|r')
|
32
39
|
while run_node = run_nodes.shift
|
33
|
-
|
34
|
-
|
35
|
-
|
40
|
+
next unless run_node.text =~ regexp_tag
|
41
|
+
expect(run_node.at_css('w|rPr w|color').first[1]).to eq('4472C4')
|
42
|
+
expect(run_node.children.first.name).to eq('rPr')
|
36
43
|
end
|
37
44
|
end
|
38
45
|
end
|
@@ -40,16 +47,52 @@ describe LMDocstache::HideCustomTags do
|
|
40
47
|
context 'giving a document with white background' do
|
41
48
|
let(:input_file) { "#{base_path}/sample-signature.docx" }
|
42
49
|
|
43
|
-
it 'expect to have a white color on all hide custom tags matching' do
|
50
|
+
it 'expect to have a white color on all hide custom tags matching and have first child node equal rPr tag' do
|
44
51
|
hide_custom_tags.hide_custom_tags!
|
45
52
|
d = hide_custom_tags.document
|
46
53
|
run_nodes = d.css('w|p w|r')
|
47
54
|
while run_node = run_nodes.shift
|
48
|
-
|
49
|
-
|
50
|
-
|
55
|
+
next unless run_node.text =~ regexp_tag
|
56
|
+
expect(run_node.at_css('w|rPr w|color').first[1]).to eq('FFFFFF')
|
57
|
+
expect(run_node.children.first.name).to eq('rPr')
|
51
58
|
end
|
52
59
|
end
|
53
60
|
end
|
61
|
+
context 'giving a document without rpr and block tags on the left' do
|
62
|
+
let(:input_file) { "#{base_path}/docx-no-rpr.docx" }
|
63
|
+
|
64
|
+
it 'expect to have a white color on all hide custom tags matching and have first child node equal rPr tag' do
|
65
|
+
hide_custom_tags.hide_custom_tags!
|
66
|
+
d = hide_custom_tags.document
|
67
|
+
run_nodes = d.css('w|p w|r')
|
68
|
+
while run_node = run_nodes.shift
|
69
|
+
next unless run_node.text =~ regexp_tag
|
70
|
+
expect(run_node.at_css('w|rPr w|color').first[1]).to eq('FFFFFF')
|
71
|
+
expect(run_node.children.first.name).to eq('rPr')
|
72
|
+
end
|
73
|
+
end
|
74
|
+
it 'expect to have a white color on all replacement tags and content following replacement' do
|
75
|
+
hide_custom_tags.hide_custom_tags!
|
76
|
+
d = hide_custom_tags.document
|
77
|
+
run_nodes = d.css('w|p w|r')
|
78
|
+
total_replacement = 0
|
79
|
+
while run_node = run_nodes.shift
|
80
|
+
next unless run_node.text =~ /replaced_content/
|
81
|
+
total_replacement+=1
|
82
|
+
expect(run_node.at_css('w|rPr w|color').first[1]).to eq('FFFFFF')
|
83
|
+
expect(run_node.children.first.name).to eq('rPr')
|
84
|
+
end
|
85
|
+
expect(total_replacement).to eq(2)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context 'giving a document with tabs spacing in the middle of replacement tags' do
|
90
|
+
let(:input_file) { "#{base_path}/sample-signature-with-tabs-spacing.docx" }
|
91
|
+
it 'expect to not replace tabs' do
|
92
|
+
hide_custom_tags.hide_custom_tags!
|
93
|
+
d = hide_custom_tags.document
|
94
|
+
expect(d.css('w|p w|tab').size).to eq(11)
|
95
|
+
end
|
96
|
+
end
|
54
97
|
end
|
55
98
|
end
|
data/spec/integration_spec.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'securerandom'
|
2
3
|
require 'active_support/core_ext/object/blank.rb'
|
3
4
|
|
4
5
|
module LMDocstache
|
@@ -139,5 +140,30 @@ describe 'integration test', integration: true do
|
|
139
140
|
expect(output).to include('<w:t xml:space="preserve">Test Multiple text in the same line </w:t>')
|
140
141
|
end
|
141
142
|
end
|
143
|
+
|
144
|
+
context "yoooo" do
|
145
|
+
let(:input_file) { "#{base_path}/multi_o.docx" }
|
146
|
+
let(:render_options) {
|
147
|
+
{
|
148
|
+
special_variable_replacements: { "(date|sig|sigfirm|text|check|initial|initials)\\|(req|noreq)\\|(.+?)" => false }.freeze,
|
149
|
+
hide_custom_tags: ['(?:sig|sigfirm|date|check|text|initial)\|(?:req|noreq)\|.+?']
|
150
|
+
}
|
151
|
+
}
|
152
|
+
let(:document) { LMDocstache::Document.new(input_file) }
|
153
|
+
|
154
|
+
it 'should have content replacement aligned with hide custom tags' do
|
155
|
+
doc = document
|
156
|
+
doc.fix_errors
|
157
|
+
new_file_path = "#{Time.now.to_i}-#{SecureRandom.uuid}.docx"
|
158
|
+
n = doc.render_file(new_file_path, { 'full_name' => 'fred document01' }, render_options)
|
159
|
+
noko = doc.render_xml({ 'full_name' => 'fred document01' }, render_options)
|
160
|
+
output = noko['word/document.xml'].to_xml
|
161
|
+
#puts output
|
162
|
+
#doc.render_file(new_file_path, { 'full_name' => 'fred document01' }, render_options)
|
163
|
+
#noko = doc.render_xml({ 'full_name' => 'fred document01' }, render_options)
|
164
|
+
#output = noko['word/document.xml'].to_xml
|
165
|
+
#puts output
|
166
|
+
end
|
167
|
+
end
|
142
168
|
end
|
143
169
|
end
|
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:
|
4
|
+
version: 3.0.3
|
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-04-27 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: nokogiri
|
@@ -121,7 +121,9 @@ files:
|
|
121
121
|
- spec/conditional_block_spec.rb
|
122
122
|
- spec/example_input/ExampleTemplate.docx
|
123
123
|
- spec/example_input/blank.docx
|
124
|
+
- spec/example_input/docx-no-rpr.docx
|
124
125
|
- spec/example_input/sample-signature-blue.docx
|
126
|
+
- spec/example_input/sample-signature-with-tabs-spacing.docx
|
125
127
|
- spec/example_input/sample-signature.docx
|
126
128
|
- spec/hide_custom_tags_spec.rb
|
127
129
|
- spec/integration_spec.rb
|
@@ -154,7 +156,9 @@ test_files:
|
|
154
156
|
- spec/conditional_block_spec.rb
|
155
157
|
- spec/example_input/ExampleTemplate.docx
|
156
158
|
- spec/example_input/blank.docx
|
159
|
+
- spec/example_input/docx-no-rpr.docx
|
157
160
|
- spec/example_input/sample-signature-blue.docx
|
161
|
+
- spec/example_input/sample-signature-with-tabs-spacing.docx
|
158
162
|
- spec/example_input/sample-signature.docx
|
159
163
|
- spec/hide_custom_tags_spec.rb
|
160
164
|
- spec/integration_spec.rb
|