template_docx 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e83a00f787254eadc60f298a9a4c0ffa25b5e3cf
4
+ data.tar.gz: 2df41e4ca1e89b5e8e3db65aa0916c2c73738cba
5
+ SHA512:
6
+ metadata.gz: 40f9f1eb8bb3c42027c84a07064ac3983803cbfeb605e6a021017442e6bbfbb2c5f7e0f38381a53fdac93c290963072e02ecec1866c453d07fb06345381a570f
7
+ data.tar.gz: 64d4898ddf60c4076e4ac77fd9e8e5fdabfacf2f3f001d4c64d98a37b22af755a210ea21e5b03529caecd7ead2374cb84615236c9918857a514e0c97ae440a77
data/.gitignore ADDED
@@ -0,0 +1,53 @@
1
+ .bundle/
2
+ .yardoc
3
+ Gemfile.lock
4
+ _yardoc/
5
+ coverage/
6
+ doc/
7
+ pkg/
8
+ spec/reports/
9
+ tmp/
10
+
11
+ # Ignore IDE
12
+ .idea/
13
+
14
+ # Ignore bundler config
15
+ .bundle
16
+
17
+ #mac os
18
+ .DS_Store
19
+ ._.DS_Store
20
+
21
+ /.bundle/
22
+ /.yardoc
23
+ /Gemfile.lock
24
+ /_yardoc/
25
+ /coverage/
26
+ /doc/
27
+ /pkg/
28
+ /spec/reports/
29
+ /tmp/
30
+ spec/example_input/tmp
31
+ docx_templater*gem
32
+ Gemfile.lock
33
+
34
+ # 特定的 docx 文件
35
+ ~*.docx
36
+ **/~*.docx
37
+ Template.docx
38
+ Example.docx
39
+ OutputTemplate.docx
40
+ OutputTemplate-*.docx
41
+ ExampleTemplate_RenderedOutput.docx
42
+
43
+ *.swo
44
+ *.swp
45
+
46
+ **/*.swo
47
+ **/*.swp
48
+
49
+ *.gem
50
+
51
+ # 忽略 rvm gemset 的文件
52
+ .ruby-gemset
53
+ .ruby-version
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -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
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ {<img src#"https://travis-ci.org/jawspeak/ruby-docx-templater.png" />}[https://travis-ci.org/jawspeak/ruby-docx-templater]
2
+
3
+
4
+ # Roughly, this takes a .docx file and uses it as a template to create a new docx, with your data
5
+
6
+ ## Features
7
+
8
+ * All manipulation in memory (great if you have sensitive data)
9
+ * Global key/value substitutions by entering a `$KEY$` anywhere in the word document, with whatever formatting you want.
10
+ * Multi-row loops inside tables, also with whatever your formatting you wish. `#BEGIN_ROW:XYZ#`... `#END_ROW:XYZ#` see tests/example
11
+ * Summation formula for row count `#SUM:XYZ_LIST#`
12
+
13
+ ## Usage
14
+
15
+ * Create your docx "template" in Word
16
+ * Install rvm and bundler
17
+ * Run `bundle install`.
18
+ * Render new docx files from the template, ex: `ruby render_docx_template.rb`. Look for an output_*.docx file.
19
+ * Sample output with tests: `rake spec` (unit tests) `rake integration` (opens word)
20
+
21
+ __TECHNICAL NOTE:__ You will probably need to extract and edit the xml template manually after creating in Word, to prepare the ruby script template with it. This is very fiddly / hacky. You can’t have Word’s ugly markup around the fields to template. It will try to break up fields with markup. Test the template, fixing it until the extracted word/document.xml as needed.
22
+
23
+ I frequently use this workflow:
24
+ `rm -rf word/; unzip ExampleTemplate.docx word/document.xml; mate word/document.xml`
25
+ Grep/search for all $ and # lines and ensure no templating is split with xml markup.
26
+ And then re-add the edited document:
27
+ `zip ExampleTemplate.docx word/document.xml`
28
+
29
+ __PRO TIP:__ You don’t want any grammar errors on the template Keys, or they will not substitute. (Grammar suggestion markup splits up the tokens in the xml). Right click and choose Ignore each grammar error.
30
+
31
+ [中文文档](https://github.com/Yundianjia/ruby-docx-templater/blob/master/README_cn.md)
32
+
33
+
34
+ ## Development
35
+
36
+ * Install any missing dependencies, then run the test suite: `script/ci`
37
+ * Run the integration test suite (on mac, with word installed): `rake integration`
38
+ * Build the gem file: `gem build docx_templater.gemspec`
39
+
40
+
41
+ ## Future ideas
42
+
43
+ * We could in the future use Word's MailMerge fields instead of using our own style `$KEY_ABC$` keys. We would then not have to worry about Word munging our text by inserting XML markup in-between parts of a template key #BEGIN_ROW:BLAH#. This is probably a really good idea. Also we would need to delete these other nodes that get added when Fields are added.
44
+ ** See also: {http://tomasvarsavsky.com/2009/04/04/simple-word-document-templating-using-ruby-and-xml/} and {https://github.com/bagilevi/docx_builder/tree/master/example/plan_report_template},
45
+ * Possibly, also use Fields to create the looping constructs. That would be a big win for preventing word adding markup between words in by text.
46
+ * Make rendering from the template more efficient.
47
+ * Try templating header/footer (I haven't attempted that yet because I do not have a need for it.)
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'rubocop/rake_task'
3
+ require 'bundler/gem_tasks'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: [:spec, :rubocop]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "docx_templater"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'json'
5
+ require 'bundler/setup'
6
+ require 'docx_templater'
7
+
8
+ options = {}
9
+
10
+ OptionParser.new do |opts|
11
+ opts.banner = "Usage: docx-templater [options]"
12
+
13
+ opts.on_tail('-h', '--help', 'Show this message') do
14
+ puts opts
15
+ exit
16
+ end
17
+
18
+ opts.on_tail('-v', '--version', 'Print version') do
19
+ puts "docx-templater #{DocxTemplater::VERSION}"
20
+ exit
21
+ end
22
+
23
+ opts.on('-i', '--input-file [file]', 'Default: Example.docx') do |file|
24
+ puts "input file: #{file}"
25
+ options[:input_file] = file || 'Example.docx'
26
+ end
27
+
28
+ opts.on('-d', '--format-data [data]', 'Default: data format file, json format') do |data|
29
+ unless data
30
+ options[:data] = {
31
+ company: '伟大的邪王真眼',
32
+ shop_name: '斜阳西下,三百二十七',
33
+ shop_id: 122323232323,
34
+ shop_url: 'http://blog.csdn.net/ruixj/article/details/3765385',
35
+ master_company: '紫电清爽公司',
36
+ brand_name: '邪王正眼',
37
+ start_date: (Time.now - 3600 * 24).strftime('%Y年-%m月-%d日'),
38
+ end_date: Time.now.strftime('%Y年-%m月-%d日'),
39
+ license_number: 1212121212,
40
+ authorized_party: '夏健的夏天',
41
+ authorized_date: Time.now.strftime('%Y年-%m月-%d日')
42
+ }
43
+ else
44
+ # 可以选择的 YML 文件,或者 json 文件之类格式化的文件,或者直接就是 Ruby 的 Hash 对象
45
+ end
46
+ end
47
+
48
+ opts.on('-o', '--output-file [file]', 'Default: OutputTemplate.docx') do |file|
49
+ puts "input file: #{file}"
50
+
51
+ options[:output_file] = file || "OutputTemplate-#{Time.now.strftime('%Y-%m-%d')}.docx"
52
+ end
53
+ end.parse!
54
+
55
+ DocxTemplater::Command.generate_doc options
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'docx_templater/version'
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = 'template_docx'
9
+ s.version = DocxTemplater::VERSION
10
+ s.authors = ['Jonathan Andrew Wolter', 'xiajian']
11
+ s.email = ['jaw@jawspeak.com', 'jhqy2011@gmail.com']
12
+ s.description = 'A Ruby library to template Microsoft Word .docx files. Uses a .docx file with keyword tags within "$$" as a template. '
13
+ s.summary = 'Generates new Word .docx files based on a template file.'
14
+ s.homepage = 'https://github.com/Yundianjia/ruby-docx-templater'
15
+ s.license = 'MIT'
16
+
17
+ if s.respond_to?(:metadata)
18
+ # spec.metadata['allowed_push_host'] = 'http://mygemserver.com'
19
+ else
20
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
21
+ end
22
+
23
+ s.require_paths = ['lib']
24
+ root_files = %w(docx_templater.gemspec LICENSE.txt Rakefile README.md .gitignore Gemfile)
25
+ s.files = Dir['{lib,script,spec,bin}/**/*'] + root_files
26
+ s.test_files = Dir['spec/**/*']
27
+ s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ s.required_ruby_version = '>= 1.9.3'
29
+
30
+ s.add_dependency 'nokogiri'
31
+ s.add_dependency 'rubyzip', '>= 1.1.1'
32
+
33
+ s.add_development_dependency 'rake'
34
+ s.add_development_dependency 'rspec'
35
+ s.add_development_dependency 'bundler', '~> 1.8'
36
+ s.add_development_dependency 'rubocop'
37
+ end
@@ -0,0 +1,24 @@
1
+ require 'docx_templater/version'
2
+ require 'docx_templater/template_processor'
3
+
4
+ module DocxTemplater
5
+ module_function
6
+
7
+ def log(str)
8
+ # braindead logging
9
+ puts str if ENV['DEBUG']
10
+ end
11
+
12
+ def open_output_file(output_file)
13
+ puts "\n************************************"
14
+ puts ' >>> Only will work on mac <<<'
15
+ puts 'NOW attempting to open created file in Word.'
16
+ cmd = "open #{output_file}"
17
+ puts " will run '#{cmd}'"
18
+ puts '************************************'
19
+
20
+ system cmd
21
+ end
22
+ end
23
+
24
+ Gem.find_files('docx_templater/*.rb').each { |path| require path }