lm_docstache 2.1.2 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f57ee42bd7804fc41051ca123be801e909acfee0b0406a5eac91e8f562827f11
4
- data.tar.gz: 21ab27cac4e621eb3d3c391b71945a9f29dac1343d790ccb4026a191dbee62ae
3
+ metadata.gz: f59dcaa224f4e8e899df674bc5bd296432ebb24e8e4f929be6b414b8f3a8e0a9
4
+ data.tar.gz: b05e837bccad1978807e82bd7d13da786d24fc796213f4f2859f6ebf023522f1
5
5
  SHA512:
6
- metadata.gz: 642af591848de049083886277afa1fcd082cf150785d7610ebc15ff328ddc53527bc373e4ab18f910eabffe16b747d8f2191332a968b9e5716a52b4a2e5b2a62
7
- data.tar.gz: e5d7b66bdb616221ccb789841ce6d84eb7ce76b44c2319ebf522c0f8cd4fb53069fbe93df745724c34ec450884b7b462932624dc2bc10a85216dd1c655bf00d5
6
+ metadata.gz: 900ff62d4ada49c3645abfd6fbb64f30b6d7f7e0bf0d619d378c1702890dc25d15685e0c950b152242ca3438d9c4e09658788cc36171d0dacad57c37046eb545
7
+ data.tar.gz: abd44d43c73c3012b1512e56c2c0fa985295876217d66d86a83670c73e23054362a29899608a86e90893cc1b82ce5306770a57e45fdbf71f0bec5cacf6518900
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 3.0.0
4
+
5
+ ## Breaking Changes
6
+ * Replaced Renderer `hide_custom_tags` options to be a `Hash` instead of `Array`.
7
+ There are are edge cases which we want to replace the content on hide custom tags.
8
+ All documentations can be followed on `Renderer` and `HideCustomTag` classes.
9
+
3
10
  ## 2.1.2
4
11
 
5
12
  ### Bugfix
@@ -2,21 +2,27 @@ module LMDocstache
2
2
  class HideCustomTags
3
3
  attr_reader :document, :hide_custom_tags
4
4
 
5
- # The +hide_custom_tags+ options is an +Array+ of +Regexp+ or +String+ representing
6
- # the pattern you expect to keep at the document but with white font color.
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
- # You have to remember is not acceptable to have capture groups in your +Regexp's+.
9
- # We don't accept because we need to find all parts of your text, split it in multiple runs
10
- # and add document background color or white font color to matching custom tags.
11
- def initialize(document:, hide_custom_tags: [])
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
@@ -33,7 +39,13 @@ module LMDocstache
33
39
  nodes_list = [remainder_run_node]
34
40
  if matched_tag
35
41
  replace_style(run_node_with_match)
36
- replace_content(run_node_with_match, matched_tag)
42
+ matched_content = matched_tag
43
+ if value
44
+ matched_content = value.is_a?(Proc) ?
45
+ value.call(matched_tag) :
46
+ value.to_s
47
+ end
48
+ replace_content(run_node_with_match, matched_content)
37
49
  nodes_list << run_node_with_match
38
50
  end
39
51
  paragraph << Nokogiri::XML::NodeSet.new(document, nodes_list)
@@ -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 an +Array+ of +Regexp+ or +String+ representing
38
- # the pattern you expect to keep at the document but with white font color.
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
- # You have to remember is not acceptable to have capture groups in your +Regexp's+.
41
- # We don't accept because we need to find all parts of your text, split it in multiple runs
42
- # and add document background color or white font color to matching custom tags.
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 = load_hide_custom_tags(options)
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
- def load_hide_custom_tags(options)
51
- options.fetch(:hide_custom_tags, []).map do |regexp_str|
52
- regexp_str.is_a?(String) ? Regexp.new("{{#{regexp_str}}}") : /{{#{regexp_str.source}}/
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
- @parser = Parser.new(xml, data, options.slice(:special_variable_replacements, :hide_custom_tags))
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
@@ -1,3 +1,3 @@
1
1
  module LMDocstache
2
- VERSION = "2.1.2"
2
+ VERSION = "3.0.0"
3
3
  end
@@ -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,10 +15,18 @@ describe LMDocstache::HideCustomTags do
16
15
  end
17
16
 
18
17
  let(:base_path) { SPEC_BASE_PATH.join('example_input') }
19
- let(:document) { LMDocstache::Document.new(input_file).instance_variable_get(:@document) }
20
- let(:regexp_tag) { /{{(?:sig|sigfirm|date|check|text|initial)\|(?:req|noreq)\|.+?}}/ }
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: [ regexp_tag ])
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
@@ -30,10 +37,9 @@ describe LMDocstache::HideCustomTags do
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
- if run_node.text =~ regexp_tag
34
- expect(run_node.at_css('w|rPr w|color').first[1]).to eq('4472C4')
35
- expect(run_node.children.first.name).to eq('rPr')
36
- end
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')
37
43
  end
38
44
  end
39
45
  end
@@ -46,14 +52,13 @@ describe LMDocstache::HideCustomTags do
46
52
  d = hide_custom_tags.document
47
53
  run_nodes = d.css('w|p w|r')
48
54
  while run_node = run_nodes.shift
49
- if run_node.text =~ regexp_tag
50
- expect(run_node.at_css('w|rPr w|color').first[1]).to eq('FFFFFF')
51
- expect(run_node.children.first.name).to eq('rPr')
52
- end
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')
53
58
  end
54
59
  end
55
60
  end
56
- context 'giving a document without rpr' do
61
+ context 'giving a document without rpr and block tags on the left' do
57
62
  let(:input_file) { "#{base_path}/docx-no-rpr.docx" }
58
63
 
59
64
  it 'expect to have a white color on all hide custom tags matching and have first child node equal rPr tag' do
@@ -61,11 +66,23 @@ describe LMDocstache::HideCustomTags do
61
66
  d = hide_custom_tags.document
62
67
  run_nodes = d.css('w|p w|r')
63
68
  while run_node = run_nodes.shift
64
- if run_node.text =~ regexp_tag
65
- expect(run_node.at_css('w|rPr w|color').first[1]).to eq('FFFFFF')
66
- expect(run_node.children.first.name).to eq('rPr')
67
- end
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')
68
84
  end
85
+ expect(total_replacement).to eq(2)
69
86
  end
70
87
  end
71
88
  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: 2.1.2
4
+ version: 3.0.0
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-03-22 00:00:00.000000000 Z
15
+ date: 2021-03-23 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: nokogiri