lm_docstache 3.0.5 → 3.0.10

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cc1a3839b3cfabfd78b144d3f2862aa4a8bf1650bc4037220b2e5af4feaa4a31
4
- data.tar.gz: d573c864a49f7e2dcc07122fc242664b597a588253408f065ad94cb0f2823f0a
3
+ metadata.gz: c0fd5eae0f079701b3e226ae771b1d155adb69e7766dc57e67f54574c3800663
4
+ data.tar.gz: 2142350801b95b13ce03d2d9d34d7e077e08b257d938b65c833d25f368530a12
5
5
  SHA512:
6
- metadata.gz: 7b0fb9ff483de6b2e4315206d1961f3ff847612519ed7ff11203f8ca9e7aabd35fc8a5f8730ff552c171082a0d29d2a1a126f86e89a3a117e3a10ab0a5fb5222
7
- data.tar.gz: a8c510d591b10ee2d66e6c3ac85995ee9eadb8d91f2178c127e70c12b4184d840cfa725929d4d02b40e62b2e2f9f2c7d629bf65866164833e421a5fdbb5e5c7b
6
+ metadata.gz: 716af477b2e3c4c9b85a968836c9af7e698a0d047fb31a2801cc139eebee35968e6e47447dfbda154c70263e7b253a3d1f06aedaf6589ef12c51d9258f9ddc61
7
+ data.tar.gz: a85b2b58ca5590a392ff65373250049987ca14c9894fe0ba18e3731d3cb74a706104046f03f5a9053402490c1d664b012ddb74790c30a8226a722e3bc891f5db
data/CHANGELOG.md CHANGED
@@ -1,5 +1,43 @@
1
1
  # Changelog
2
2
 
3
+ ## 3.0.10
4
+
5
+ #### Bug fixes
6
+
7
+ * Only try to access and set text node's "xml:space" attribute if the text node
8
+ exists (in `LMDocstache::Document#flatten_text_blocks` private method).
9
+
10
+ ## 3.0.9
11
+
12
+ #### Bug fixes
13
+
14
+ * Text nodes merged in paragraphs with problems through `fix_errors` private
15
+ method have now the "xml:space" attribute preserved from now on.
16
+
17
+ ## 3.0.8
18
+
19
+ #### Bug fixes
20
+
21
+ * Fix a bug on `usable_tags` method, so it now properly and expectedly
22
+ includes conditional tag names that have its opening tag markup as the sole
23
+ content of paragraphs (which represents conditional blocks where both
24
+ opening and closing tags are in separate parapraghs sorrounding one or more
25
+ paragraphs as its conditional block content).
26
+
27
+ ## 3.0.7
28
+
29
+ #### Bug fixes
30
+
31
+ * Fix a bug on `usable_tag_names` method, so it now properly and expectedly
32
+ includes conditional tag names as well, as before.
33
+
34
+ ## 3.0.6
35
+
36
+ #### Bug fixes
37
+
38
+ * Fix bug on `LMDocstache::Docstache#unusable_tags` method, where `nil` could be
39
+ passed to `broken_tags.deleted_at` call.
40
+
3
41
  ## 3.0.5
4
42
 
5
43
  #### Bug fixes and improvements
@@ -6,6 +6,8 @@ module LMDocstache
6
6
  BLOCK_CHILDREN_ELEMENTS = 'w|r,w|hyperlink,w|ins,w|del'
7
7
  RUN_LIKE_ELEMENTS = 'w|r,w|ins'
8
8
 
9
+ attr_reader :document
10
+
9
11
  def initialize(*paths)
10
12
  raise ArgumentError if paths.empty?
11
13
 
@@ -38,7 +40,7 @@ module LMDocstache
38
40
  def tags
39
41
  @documents.values.flat_map do |document|
40
42
  document_text = document.text
41
- extract_tag_names(document_text) + extract_tag_names(document_text, true)
43
+ extract_tag_names(document_text) + extract_tag_names(document_text, :full_block)
42
44
  end
43
45
  end
44
46
 
@@ -47,7 +49,8 @@ module LMDocstache
47
49
  document.css('w|t').reduce(tags) do |document_tags, text_node|
48
50
  text = text_node.text
49
51
  document_tags.push(*extract_tag_names(text))
50
- document_tags.push(*extract_tag_names(text, true))
52
+ document_tags.push(*extract_tag_names(text, :start_block))
53
+ document_tags.push(*extract_tag_names(text, :full_block))
51
54
  end
52
55
  end
53
56
  end
@@ -56,24 +59,16 @@ module LMDocstache
56
59
  usable_tags.reduce([]) do |memo, tag|
57
60
  next memo if !tag.is_a?(Regexp) && tag =~ ROLES_REGEXP
58
61
 
59
- tag = tag.source if tag.is_a?(Regexp)
62
+ tag = unescape_escaped_start_block(tag.source) if tag.is_a?(Regexp)
60
63
  memo << (tag.scan(GENERAL_TAG_REGEX) && $1)
61
64
  end.compact.uniq
62
65
  end
63
66
 
64
67
  def unusable_tags
65
- conditional_start_tags = text_nodes_containing_only_starting_conditionals.map(&:text)
66
-
67
68
  usable_tags.reduce(tags) do |broken_tags, usable_tag|
68
- broken_tags.delete_at(broken_tags.index(usable_tag)) && broken_tags
69
- end.reject do |broken_tag|
70
- operator = broken_tag.is_a?(Regexp) ? :=~ : :==
71
- start_tags_index = conditional_start_tags.find_index do |start_tag|
72
- broken_tag.send(operator, start_tag)
73
- end
69
+ next broken_tags unless index = broken_tags.index(usable_tag)
74
70
 
75
- conditional_start_tags.delete_at(start_tags_index) if start_tags_index
76
- !!start_tags_index
71
+ broken_tags.delete_at(index) && broken_tags
77
72
  end
78
73
  end
79
74
 
@@ -112,23 +107,26 @@ module LMDocstache
112
107
 
113
108
  private
114
109
 
115
- def text_nodes_containing_only_starting_conditionals
116
- @documents.values.flat_map do |document|
117
- document.css('w|t').select do |paragraph|
118
- paragraph.text =~ WHOLE_BLOCK_START_REGEX
110
+ def unescape_escaped_start_block(regex_source_string)
111
+ regex_source_string
112
+ .gsub('\\{', '{')
113
+ .gsub('\\#', '#')
114
+ .gsub('\\}', '}')
115
+ .gsub('\\^', '^')
116
+ .gsub('\\ ', ' ')
117
+ end
118
+
119
+ def extract_tag_names(text, tag_type = :variable)
120
+ text, regex, extractor =
121
+ if tag_type == :variable
122
+ [text, Parser::VARIABLE_MATCHER, ->(match) { "{{%s}}" % match }]
123
+ else
124
+ extractor = ->(match) { /#{Regexp.escape("{{%s%s %s %s}}" % match)}/ }
125
+ tag_type == :full_block ? [text, Parser::BLOCK_MATCHER, extractor] :
126
+ [text.strip, WHOLE_BLOCK_START_REGEX, extractor]
119
127
  end
120
- end
121
- end
122
128
 
123
- def extract_tag_names(text, conditional_tag = false)
124
- if conditional_tag
125
- text.scan(Parser::BLOCK_MATCHER).map do |match|
126
- start_block_tag = "{{#{match[0]}#{match[1]} #{match[2]} #{match[3]}}}"
127
- /#{Regexp.escape(start_block_tag)}/
128
- end
129
- else
130
- text.scan(Parser::VARIABLE_MATCHER).map { |match| "{{#{match[0]}}}" }
131
- end
129
+ text.scan(regex).map(&extractor)
132
130
  end
133
131
 
134
132
  def render_documents(data, text = nil, render_options = {})
@@ -180,7 +178,10 @@ module LMDocstache
180
178
  next if style_html != previous_style_html
181
179
  next if current_text_node.nil? || previous_text_node.nil?
182
180
 
181
+ whitespace_attr = current_text_node['xml:space']
182
+ previous_text_node['xml:space'] = whitespace_attr if whitespace_attr
183
183
  previous_text_node.content = previous_text_node.text + current_text_node.text
184
+
184
185
  node.unlink
185
186
  end
186
187
  end
@@ -1,3 +1,3 @@
1
1
  module LMDocstache
2
- VERSION = "3.0.5"
2
+ VERSION = "3.0.10"
3
3
  end
@@ -62,7 +62,7 @@ describe 'integration test', integration: true do
62
62
  it 'fixes nested xml errors breaking tags' do
63
63
  expect { document.fix_errors }.to change {
64
64
  document.send(:problem_paragraphs).size
65
- }.from(7).to(1)
65
+ }.from(10).to(1)
66
66
 
67
67
  expect(document.send(:problem_paragraphs).first.text).to eq(
68
68
  '{{TAG123-\\-//WITH WE👻IRD CHARS}}'
@@ -70,7 +70,18 @@ describe 'integration test', integration: true do
70
70
  end
71
71
 
72
72
  it 'has the expected amount of usable tags' do
73
- expect(document.usable_tags.count).to eq(21)
73
+ expect { document.fix_errors }.to change {
74
+ document.usable_tags.count
75
+ }.from(29).to(37)
76
+ end
77
+
78
+ it 'keeps "xml:space" attribute when fixing errors' do
79
+ document.fix_errors
80
+
81
+ text_node = document.document.css('w|p').last
82
+ .css('w|t').find { |node| node.text.include?('that occurred on') }
83
+
84
+ expect(text_node['xml:space']).to eq('preserve')
74
85
  end
75
86
 
76
87
  it 'has the expected amount of usable roles tags' do
@@ -79,13 +90,14 @@ describe 'integration test', integration: true do
79
90
  end
80
91
 
81
92
  it 'has the expected amount of unique tag names' do
82
- expect(document.usable_tag_names.count).to eq(14)
93
+ expect(document.usable_tag_names.count).to eq(20)
83
94
  end
84
95
 
85
96
  it 'renders file using data' do
86
97
  document.render_file(output_file, data)
87
98
  end
88
99
  end
100
+
89
101
  context "testing hide custom tags" do
90
102
  before do
91
103
  FileUtils.rm_rf(output_dir) if File.exist?(output_dir)
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.5
4
+ version: 3.0.10
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-06-04 00:00:00.000000000 Z
15
+ date: 2021-07-12 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: nokogiri
@@ -148,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  - !ruby/object:Gem::Version
149
149
  version: '0'
150
150
  requirements: []
151
- rubygems_version: 3.0.3
151
+ rubygems_version: 3.0.8
152
152
  signing_key:
153
153
  specification_version: 4
154
154
  summary: Merges Hash of Data into Word docx template files using mustache syntax