lm_docstache 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f8d96c1d658331a8ef93e7ace164759a64e26f9fc6f579ccab6aaf37032ad6fd
4
+ data.tar.gz: abaf62fe5a057c4adc66cbf2ed57ec87c75b55d349259953b02bba568b55dd35
5
+ SHA512:
6
+ metadata.gz: e1b98280644b99d84502cf026887d6be64b038950afe96cefb97ce5364de9b7b760c577ea052a377e4a3e7cf1af6b743a1d3d8fcbe8837b7d8a47d83b75f7625
7
+ data.tar.gz: 694650b1c8b433244cfc7ea891de75d77ab70816aaa69442faee72f8efc3c8de71c9201d096c24fbdcf8a02ed32b47fa471a08d2a890be11c60380d5011f1d94
@@ -0,0 +1,5 @@
1
+ .DS_Store
2
+ spec/example_input/tmp
3
+ .idea/
4
+ *.gem
5
+ Gemfile.lock
@@ -0,0 +1,58 @@
1
+ # Changelog
2
+
3
+ ## 1.0.0
4
+ Fork the Docstache codebase and rename all references to LMDocstache.
5
+
6
+ ## 0.3.2
7
+ * [b3b66a0cd7ae67834cdbe7c18cefdb1498bcc5ab] I'm an idiot. Fixed an error in the `Document#unusable_tags` method
8
+
9
+ ## 0.3.1
10
+ * [2228ba727f8a2e3705fe223ce02b5e760c935572] Added `Document#unusable_tags` to list out tags that are not able to be interpolated due to being split across tags in the Document's XML
11
+
12
+ ## 0.3.0
13
+ * [c841078f7d340c18323f05f0785e75cd560eed2c] Added ability to do tag interpolation in headers and footers. The header/footer used is the header or footer from the first document passed into `Document.new()`
14
+
15
+ ## 0.2.1
16
+ * [25b4c2e9e3dcb19b6fe9a255fec2c2bfd92029a6] Fixed a bug where a tag with regular expression characters could not be fixed with the `fix_errors` method
17
+
18
+ ## 0.2.0
19
+
20
+ * [902d9890bee1a20a90377a94c3096a51781e8a24] You can access array elements with `[]`s within a `{{tag}}`
21
+ * [a36c4fefed763b6a54d15564e887fbcf3c3421e3] You can use conditions on loops to only loop over specific elements
22
+
23
+ ## 0.1.0
24
+
25
+ * [08f09d1922e73ccaf17989a568deeb2ce0ebd4b8] Hash keys inside DataScope are symbolized
26
+
27
+ ## 0.0.6
28
+
29
+ * [f945bc70ebb684f21e2488a3ad30c205bc05ddbb] Fixed a bug When two tags were inside one xml node, it would mistakenly report that there was an error when there wasn't.
30
+
31
+ ## 0.0.5
32
+
33
+ * Fixed a bug that caused {{^tags}} to always show up as errors
34
+
35
+ ## 0.0.4
36
+
37
+ * Rewrite of the Renderer class
38
+ * Conditional Blocks now work
39
+ * Loops now work
40
+
41
+ ## 0.0.3
42
+
43
+ * Added ability to have nested tags like `{{purchase_order.number}}` through the
44
+ use of the `Docstache::DataScope` class
45
+
46
+ ## 0.0.2
47
+
48
+ * Fixed a bug that made appended documents not get appended to the *end* of a
49
+ document
50
+ * Fixed a bug that made merged documents not open in Word. This was caused due
51
+ to the `section properties` tag in the xml getting used more than once, since
52
+ each document had one. Now only the first `sectPr` tag is kept.
53
+
54
+ ## 0.0.1
55
+
56
+ * Initial release. Still not working:
57
+ * Nested tags `{{foo.bar}}`
58
+ * Blocks (loops and conditiontals) `{{#foo}} ... {{/foo}}`
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
@@ -0,0 +1,72 @@
1
+ [![Gem Version](https://badge.fury.io/rb/lm_docstache.svg)](http://badge.fury.io/rb/lm_docstache)
2
+
3
+ # LM-Docstache
4
+
5
+ Lawmatics' templating utility for generating dynamic docx files.
6
+
7
+ ## Features
8
+
9
+ * Mustache-like data interpolation into Microsoft Word .docx files
10
+ * This includes loops, and conditional blocks
11
+ * Merging together multiple documents (documents are joined by a page break)
12
+ * No more hassles with Word splitting up your tags across multiple XML tags. We gotcha.
13
+ * Error detection for word documents, just in case we don't gotcha.
14
+
15
+ ## Conditionals
16
+
17
+ ### Truthy
18
+ ```
19
+ {{#true_cond}}
20
+ SHOW ME
21
+ {{/true_cond}}
22
+ ```
23
+ ```
24
+ {{#false_cond}}
25
+ DONT SHOW ME
26
+ {{/false_cond}}
27
+ ```
28
+
29
+ ### Falsy
30
+ ```
31
+ {{^false_cond}}
32
+ SHOW ME
33
+ {{/false_cond}}
34
+ ```
35
+ ```
36
+ {{^true_cond}}
37
+ DONT SHOW ME
38
+ {{/true_cond}}
39
+ ```
40
+
41
+ ### If
42
+ Assume `classrooom = "Rm 202"`
43
+
44
+ ```
45
+ {{#classroom == “Rm 202”}}
46
+ SHOW ME
47
+ {{/classroom}}
48
+ ```
49
+ ```
50
+ {{#classroom == "NON EXISTANT"}}
51
+ DONT SHOW ME
52
+ {{/classroom}}
53
+ ```
54
+
55
+ Without Quotes:
56
+ ```
57
+ {{#classroom == Rm 202}}
58
+ SHOW ME
59
+ {{/classroom}}
60
+ ```
61
+
62
+ With Negation
63
+ ```
64
+ {{^classroom == "NON EXISTANT"}}
65
+ SHOW ME
66
+ {{/classroom}}
67
+ ```
68
+ ```
69
+ {{^classroom == Rm 202}}
70
+ DONT SHOW ME
71
+ {{/classroom}}
72
+ ```
@@ -0,0 +1,7 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ Bundler::GemHelper.install_tasks
6
+
7
+ task default: [:spec]
@@ -0,0 +1,9 @@
1
+ require 'nokogiri'
2
+ require 'zip'
3
+ require "lm_docstache/version"
4
+ require "lm_docstache/data_scope"
5
+ require "lm_docstache/document"
6
+ require "lm_docstache/block"
7
+ require "lm_docstache/renderer"
8
+
9
+ module LMDocstache; end
@@ -0,0 +1,62 @@
1
+ module LMDocstache
2
+ class Block
3
+ attr_reader :name, :opening_element, :content_elements, :closing_element, :inverted, :condition
4
+ def initialize(name:, data:, opening_element:, content_elements:, closing_element:, inverted:, condition: nil)
5
+ @name = name
6
+ @data = data
7
+ @opening_element = opening_element
8
+ @content_elements = content_elements
9
+ @closing_element = closing_element
10
+ @inverted = inverted
11
+ @condition = condition
12
+ end
13
+
14
+ def type
15
+ @type ||= if @inverted
16
+ :conditional
17
+ else
18
+ if @data.get(@name).is_a? Array
19
+ :loop
20
+ else
21
+ :conditional
22
+ end
23
+ end
24
+ end
25
+
26
+ def loop?
27
+ type == :loop
28
+ end
29
+
30
+ def conditional?
31
+ type == :conditional
32
+ end
33
+
34
+ def self.find_all(name:, data:, elements:, inverted:, condition: nil, ignore_missing: true)
35
+ inverted_op = inverted ? '\^' : '\#'
36
+ full_tag_regex = /\{\{#{inverted_op}(#{name})\s?#{condition}\}\}.+?\{\{\/\k<1>\}\}/m
37
+ start_tag_regex = /\{\{#{inverted_op}#{name}\s?#{condition}\}\}/m
38
+ close_tag_regex = /\{\{\/#{name}\}\}/s
39
+
40
+ if elements.text.match(full_tag_regex)
41
+ if elements.any? { |e| e.text.match(full_tag_regex) }
42
+ matches = elements.select { |e| e.text.match(full_tag_regex) }
43
+ return matches.flat_map do |match|
44
+ find_all(name: name, data: data, elements: match.elements, inverted: inverted, condition: condition)
45
+ end
46
+ else
47
+ opening = elements.find { |e| e.text.match(start_tag_regex) }
48
+ content = []
49
+ next_sibling = opening.next
50
+ while !next_sibling.text.match(close_tag_regex)
51
+ content << next_sibling
52
+ next_sibling = next_sibling.next
53
+ end
54
+ closing = next_sibling
55
+ return Block.new(name: name, data: data, opening_element: opening, content_elements: content, closing_element: closing, inverted: inverted, condition: condition)
56
+ end
57
+ else
58
+ raise "Block not found in given elements" unless ignore_missing
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,67 @@
1
+ module LMDocstache
2
+ class DataScope
3
+
4
+ def initialize(data, parent=EmptyDataScope.new)
5
+ @data = data
6
+ @parent = parent
7
+ end
8
+
9
+ def get(key, hash: @data, original_key: key, condition: nil)
10
+ symbolize_keys!(hash)
11
+ tokens = key.split('.')
12
+ if tokens.length == 1
13
+ result = hash.fetch(key.to_sym) { |_| @parent.get(original_key) }
14
+ unless result.respond_to?(:select)
15
+ return result if evaluate_condition(condition, result)
16
+ else
17
+ return result.select { |el| evaluate_condition(condition, el) }
18
+ end
19
+ elsif tokens.length > 1
20
+ key = tokens.shift
21
+ subhash = hash.fetch(key.to_sym) { |_| @parent.get(original_key) }
22
+ return get(tokens.join('.'), hash: subhash, original_key: original_key)
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def symbolize_keys!(hash)
29
+ hash.transform_keys!{ |key| key.to_sym rescue key }
30
+ end
31
+
32
+ def evaluate_condition(condition, data)
33
+ return true if condition.nil?
34
+ condition = condition.match(/(==|~=)\s*(.+)/)
35
+ operator = condition[1]
36
+ expression = condition[2]
37
+ case condition[1]
38
+ when "=="
39
+ # Equality condition
40
+ expression = evaluate_expression(expression, data)
41
+ return data == expression
42
+ else
43
+ # Matches condition
44
+ expression = evaluate_expression(expression, data)
45
+ right = Regex.new(expression.match(/\/(.+)\//)[1])
46
+ return data.match(right)
47
+ end
48
+ end
49
+
50
+ def evaluate_expression(expression, data)
51
+ if expression.match(/(["'“]?)(.+)(\k<1>|”)/)
52
+ $2
53
+ elsif data.respond_to?(:select)
54
+ get(expression, hash: data)
55
+ else
56
+ false
57
+ end
58
+ end
59
+ end
60
+
61
+ class EmptyDataScope
62
+ def get(_)
63
+ return nil
64
+ end
65
+ end
66
+
67
+ end