docxgen 0.1.0
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 +7 -0
- data/.gitignore +20 -0
- data/.rspec +3 -0
- data/.rubocop.yml +244 -0
- data/.travis.yml +22 -0
- data/CHANGELOG.md +6 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +66 -0
- data/LICENSE.txt +21 -0
- data/README.md +108 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/docxgen.gemspec +28 -0
- data/lib/docxgen.rb +10 -0
- data/lib/docxgen/generator.rb +64 -0
- data/lib/docxgen/templates/parser.rb +42 -0
- data/lib/docxgen/version.rb +5 -0
- metadata +63 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 33216029e3163b1ccb7bfb7dbfe4511efb1978ce526d89b37e5b970ab4c9bc31
|
4
|
+
data.tar.gz: 7e688cae088640535ad46f016df63682e79d5ec4a51d73b18da02fd026bfefe8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0df11f03ebd605943e971dd9431c52ff30268d93375255b6807e2d7f41b36626be09a1deec9a67ce36d9cd636e27f3925192a24e2b416470f18282876680d90d
|
7
|
+
data.tar.gz: a01a03568bee419fc6b0ba827cfe5cf7c2c53d76739a30bf357670a1ec4e628801b96f2915c37448dac5428253d212254af922e8950c4afc4c4e92e07e752f9e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,244 @@
|
|
1
|
+
#
|
2
|
+
# Copied from Rails official rubocop config, with minor changes
|
3
|
+
#
|
4
|
+
|
5
|
+
require:
|
6
|
+
- rubocop-performance
|
7
|
+
|
8
|
+
AllCops:
|
9
|
+
TargetRubyVersion: 2.4
|
10
|
+
# RuboCop has a bunch of cops enabled by default. This setting tells RuboCop
|
11
|
+
# to ignore them, so only the ones explicitly set in this file are enabled.
|
12
|
+
DisabledByDefault: true
|
13
|
+
# Exclude:
|
14
|
+
# - '**/templates/**/*'
|
15
|
+
# - '**/vendor/**/*'
|
16
|
+
# - 'actionpack/lib/action_dispatch/journey/parser.rb'
|
17
|
+
# - 'railties/test/fixtures/tmp/**/*'
|
18
|
+
# - 'actionmailbox/test/dummy/**/*'
|
19
|
+
# - 'actiontext/test/dummy/**/*'
|
20
|
+
# - '**/node_modules/**/*'
|
21
|
+
# - 'db/schema.rb'
|
22
|
+
|
23
|
+
Performance:
|
24
|
+
Exclude:
|
25
|
+
- '**/test/**/*'
|
26
|
+
|
27
|
+
# Prefer &&/|| over and/or.
|
28
|
+
Style/AndOr:
|
29
|
+
Enabled: true
|
30
|
+
|
31
|
+
# Align `when` with `case`.
|
32
|
+
Layout/CaseIndentation:
|
33
|
+
Enabled: true
|
34
|
+
|
35
|
+
# Align comments with method definitions.
|
36
|
+
Layout/CommentIndentation:
|
37
|
+
Enabled: true
|
38
|
+
|
39
|
+
Layout/ElseAlignment:
|
40
|
+
Enabled: true
|
41
|
+
|
42
|
+
# Align `end` with the matching keyword or starting expression except for
|
43
|
+
# assignments, where it should be aligned with the LHS.
|
44
|
+
Layout/EndAlignment:
|
45
|
+
Enabled: true
|
46
|
+
EnforcedStyleAlignWith: variable
|
47
|
+
AutoCorrect: true
|
48
|
+
|
49
|
+
Layout/EmptyLineAfterMagicComment:
|
50
|
+
Enabled: true
|
51
|
+
|
52
|
+
Layout/EmptyLinesAroundBlockBody:
|
53
|
+
Enabled: true
|
54
|
+
|
55
|
+
# In a regular class definition, no empty lines around the body.
|
56
|
+
Layout/EmptyLinesAroundClassBody:
|
57
|
+
Enabled: true
|
58
|
+
|
59
|
+
# In a regular method definition, no empty lines around the body.
|
60
|
+
Layout/EmptyLinesAroundMethodBody:
|
61
|
+
Enabled: true
|
62
|
+
|
63
|
+
# In a regular module definition, no empty lines around the body.
|
64
|
+
Layout/EmptyLinesAroundModuleBody:
|
65
|
+
Enabled: true
|
66
|
+
|
67
|
+
Layout/FirstArgumentIndentation:
|
68
|
+
Enabled: true
|
69
|
+
|
70
|
+
# Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
|
71
|
+
Style/HashSyntax:
|
72
|
+
Enabled: true
|
73
|
+
|
74
|
+
# Method definitions after `private` or `protected` isolated calls need one
|
75
|
+
# extra level of indentation.
|
76
|
+
Layout/IndentationConsistency:
|
77
|
+
Enabled: true
|
78
|
+
EnforcedStyle: indented_internal_methods
|
79
|
+
|
80
|
+
# Two spaces, no tabs (for indentation).
|
81
|
+
Layout/IndentationWidth:
|
82
|
+
Enabled: true
|
83
|
+
|
84
|
+
Layout/LeadingCommentSpace:
|
85
|
+
Enabled: true
|
86
|
+
|
87
|
+
Layout/SpaceAfterColon:
|
88
|
+
Enabled: true
|
89
|
+
|
90
|
+
Layout/SpaceAfterComma:
|
91
|
+
Enabled: true
|
92
|
+
|
93
|
+
Layout/SpaceAfterSemicolon:
|
94
|
+
Enabled: true
|
95
|
+
|
96
|
+
Layout/SpaceAroundEqualsInParameterDefault:
|
97
|
+
Enabled: true
|
98
|
+
|
99
|
+
Layout/SpaceAroundKeyword:
|
100
|
+
Enabled: true
|
101
|
+
|
102
|
+
Layout/SpaceAroundOperators:
|
103
|
+
Enabled: true
|
104
|
+
|
105
|
+
Layout/SpaceBeforeComma:
|
106
|
+
Enabled: true
|
107
|
+
|
108
|
+
Layout/SpaceBeforeComment:
|
109
|
+
Enabled: true
|
110
|
+
|
111
|
+
Layout/SpaceBeforeFirstArg:
|
112
|
+
Enabled: true
|
113
|
+
|
114
|
+
Style/DefWithParentheses:
|
115
|
+
Enabled: true
|
116
|
+
|
117
|
+
# Defining a method with parameters needs parentheses.
|
118
|
+
Style/MethodDefParentheses:
|
119
|
+
Enabled: true
|
120
|
+
|
121
|
+
Style/FrozenStringLiteralComment:
|
122
|
+
Enabled: true
|
123
|
+
EnforcedStyle: always
|
124
|
+
Exclude:
|
125
|
+
- 'actionview/test/**/*.builder'
|
126
|
+
- 'actionview/test/**/*.ruby'
|
127
|
+
- 'actionpack/test/**/*.builder'
|
128
|
+
- 'actionpack/test/**/*.ruby'
|
129
|
+
- 'activestorage/db/migrate/**/*.rb'
|
130
|
+
- 'activestorage/db/update_migrate/**/*.rb'
|
131
|
+
- 'actionmailbox/db/migrate/**/*.rb'
|
132
|
+
- 'actiontext/db/migrate/**/*.rb'
|
133
|
+
|
134
|
+
Style/RedundantFreeze:
|
135
|
+
Enabled: true
|
136
|
+
|
137
|
+
# Use `foo {}` not `foo{}`.
|
138
|
+
Layout/SpaceBeforeBlockBraces:
|
139
|
+
Enabled: true
|
140
|
+
|
141
|
+
# Use `foo { bar }` not `foo {bar}`.
|
142
|
+
Layout/SpaceInsideBlockBraces:
|
143
|
+
Enabled: true
|
144
|
+
EnforcedStyleForEmptyBraces: space
|
145
|
+
|
146
|
+
# Use `{ a: 1 }` not `{a:1}`.
|
147
|
+
Layout/SpaceInsideHashLiteralBraces:
|
148
|
+
Enabled: true
|
149
|
+
|
150
|
+
Layout/SpaceInsideParens:
|
151
|
+
Enabled: true
|
152
|
+
|
153
|
+
# Check quotes usage according to lint rule below.
|
154
|
+
Style/StringLiterals:
|
155
|
+
Enabled: true
|
156
|
+
EnforcedStyle: double_quotes
|
157
|
+
|
158
|
+
# Detect hard tabs, no hard tabs.
|
159
|
+
Layout/IndentationStyle:
|
160
|
+
Enabled: true
|
161
|
+
EnforcedStyle: spaces
|
162
|
+
IndentationWidth: 2
|
163
|
+
|
164
|
+
# Blank lines should not have any spaces.
|
165
|
+
Layout/TrailingEmptyLines:
|
166
|
+
Enabled: true
|
167
|
+
|
168
|
+
# No trailing whitespace.
|
169
|
+
Layout/TrailingWhitespace:
|
170
|
+
Enabled: true
|
171
|
+
|
172
|
+
# Use quotes for string literals when they are enough.
|
173
|
+
Style/RedundantPercentQ:
|
174
|
+
Enabled: true
|
175
|
+
|
176
|
+
Lint/AmbiguousOperator:
|
177
|
+
Enabled: true
|
178
|
+
|
179
|
+
Lint/AmbiguousRegexpLiteral:
|
180
|
+
Enabled: true
|
181
|
+
|
182
|
+
Lint/ErbNewArguments:
|
183
|
+
Enabled: true
|
184
|
+
|
185
|
+
# Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
|
186
|
+
Lint/RequireParentheses:
|
187
|
+
Enabled: true
|
188
|
+
|
189
|
+
Lint/ShadowingOuterLocalVariable:
|
190
|
+
Enabled: true
|
191
|
+
|
192
|
+
Lint/RedundantStringCoercion:
|
193
|
+
Enabled: true
|
194
|
+
|
195
|
+
Lint/UriEscapeUnescape:
|
196
|
+
Enabled: true
|
197
|
+
|
198
|
+
Lint/UselessAssignment:
|
199
|
+
Enabled: true
|
200
|
+
|
201
|
+
Lint/DeprecatedClassMethods:
|
202
|
+
Enabled: true
|
203
|
+
|
204
|
+
Style/ParenthesesAroundCondition:
|
205
|
+
Enabled: true
|
206
|
+
|
207
|
+
Style/RedundantBegin:
|
208
|
+
Enabled: true
|
209
|
+
|
210
|
+
Style/RedundantReturn:
|
211
|
+
Enabled: true
|
212
|
+
AllowMultipleReturnValues: true
|
213
|
+
|
214
|
+
Style/Semicolon:
|
215
|
+
Enabled: true
|
216
|
+
AllowAsExpressionSeparator: true
|
217
|
+
|
218
|
+
# Prefer Foo.method over Foo::method
|
219
|
+
Style/ColonMethodCall:
|
220
|
+
Enabled: true
|
221
|
+
|
222
|
+
Style/TrivialAccessors:
|
223
|
+
Enabled: true
|
224
|
+
|
225
|
+
Performance/FlatMap:
|
226
|
+
Enabled: true
|
227
|
+
|
228
|
+
Performance/RedundantMerge:
|
229
|
+
Enabled: true
|
230
|
+
|
231
|
+
Performance/StartWith:
|
232
|
+
Enabled: true
|
233
|
+
|
234
|
+
Performance/EndWith:
|
235
|
+
Enabled: true
|
236
|
+
|
237
|
+
Performance/RegexpMatch:
|
238
|
+
Enabled: true
|
239
|
+
|
240
|
+
Performance/ReverseEach:
|
241
|
+
Enabled: true
|
242
|
+
|
243
|
+
Performance/UnfreezeString:
|
244
|
+
Enabled: true
|
data/.travis.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
language: ruby
|
2
|
+
dist: trusty
|
3
|
+
sudo: false
|
4
|
+
rvm:
|
5
|
+
- 2.4
|
6
|
+
- 2.5
|
7
|
+
- 2.6
|
8
|
+
- 2.7
|
9
|
+
- ruby-head
|
10
|
+
|
11
|
+
matrix:
|
12
|
+
allow_failures:
|
13
|
+
- rvm: ruby-head
|
14
|
+
|
15
|
+
cache: bundler
|
16
|
+
bundler_args: --jobs=2
|
17
|
+
|
18
|
+
before_install:
|
19
|
+
- gem update --system
|
20
|
+
|
21
|
+
script:
|
22
|
+
- bundle exec rake spec
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in docxgen.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "docx", "~> 0.5"
|
9
|
+
|
10
|
+
group :development, :test do
|
11
|
+
gem "rake", "~> 12.0"
|
12
|
+
gem "rspec", "~> 3.0"
|
13
|
+
gem "rubocop"
|
14
|
+
gem "rubocop-performance"
|
15
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
docxgen (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.0)
|
10
|
+
diff-lcs (1.3)
|
11
|
+
docx (0.5.0)
|
12
|
+
nokogiri (~> 1.10, >= 1.10.4)
|
13
|
+
rubyzip (~> 2.0)
|
14
|
+
mini_portile2 (2.4.0)
|
15
|
+
nokogiri (1.10.9)
|
16
|
+
mini_portile2 (~> 2.4.0)
|
17
|
+
parallel (1.19.1)
|
18
|
+
parser (2.7.1.3)
|
19
|
+
ast (~> 2.4.0)
|
20
|
+
rainbow (3.0.0)
|
21
|
+
rake (12.3.3)
|
22
|
+
regexp_parser (1.7.1)
|
23
|
+
rexml (3.2.4)
|
24
|
+
rspec (3.9.0)
|
25
|
+
rspec-core (~> 3.9.0)
|
26
|
+
rspec-expectations (~> 3.9.0)
|
27
|
+
rspec-mocks (~> 3.9.0)
|
28
|
+
rspec-core (3.9.2)
|
29
|
+
rspec-support (~> 3.9.3)
|
30
|
+
rspec-expectations (3.9.2)
|
31
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
32
|
+
rspec-support (~> 3.9.0)
|
33
|
+
rspec-mocks (3.9.1)
|
34
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
35
|
+
rspec-support (~> 3.9.0)
|
36
|
+
rspec-support (3.9.3)
|
37
|
+
rubocop (0.85.1)
|
38
|
+
parallel (~> 1.10)
|
39
|
+
parser (>= 2.7.0.1)
|
40
|
+
rainbow (>= 2.2.2, < 4.0)
|
41
|
+
regexp_parser (>= 1.7)
|
42
|
+
rexml
|
43
|
+
rubocop-ast (>= 0.0.3)
|
44
|
+
ruby-progressbar (~> 1.7)
|
45
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
46
|
+
rubocop-ast (0.0.3)
|
47
|
+
parser (>= 2.7.0.1)
|
48
|
+
rubocop-performance (1.6.1)
|
49
|
+
rubocop (>= 0.71.0)
|
50
|
+
ruby-progressbar (1.10.1)
|
51
|
+
rubyzip (2.3.0)
|
52
|
+
unicode-display_width (1.7.0)
|
53
|
+
|
54
|
+
PLATFORMS
|
55
|
+
ruby
|
56
|
+
|
57
|
+
DEPENDENCIES
|
58
|
+
docx (~> 0.5)
|
59
|
+
docxgen!
|
60
|
+
rake (~> 12.0)
|
61
|
+
rspec (~> 3.0)
|
62
|
+
rubocop
|
63
|
+
rubocop-performance
|
64
|
+
|
65
|
+
BUNDLED WITH
|
66
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Andrey Viktorov
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
# docxgen
|
2
|
+
|
3
|
+
[](https://travis-ci.org/4ndv/docxgen)
|
4
|
+
|
5
|
+
Docxgen is a Ruby library for templating docx files based on [ruby-docx](https://github.com/ruby-docx/docx).
|
6
|
+
|
7
|
+
## Features
|
8
|
+
|
9
|
+
- Validates if there is any variables without provided values
|
10
|
+
- Supports arrays
|
11
|
+
- Preserves formatting
|
12
|
+
- Supports non-latin variable names (cyrillic, for example)
|
13
|
+
|
14
|
+
## Requirements
|
15
|
+
|
16
|
+
Ruby 2.4+
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem "docxgen"
|
24
|
+
```
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
```bash
|
29
|
+
bundle install
|
30
|
+
```
|
31
|
+
|
32
|
+
Or install it yourself as:
|
33
|
+
|
34
|
+
```bash
|
35
|
+
gem install docxgen
|
36
|
+
```
|
37
|
+
|
38
|
+
## Usage
|
39
|
+
|
40
|
+
### Variables
|
41
|
+
|
42
|
+
Example of variables and corresponding hashes:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
# {{ hello }} => world
|
46
|
+
|
47
|
+
{ hello: "world" }
|
48
|
+
|
49
|
+
# {{ hello.world }} => wow
|
50
|
+
|
51
|
+
{ hello: { world: "wow" } }
|
52
|
+
|
53
|
+
# {{ array.0.name }} => Wow
|
54
|
+
|
55
|
+
{ array: [ { name: "Wow" } ]}
|
56
|
+
|
57
|
+
# {{ Привет.Мир }} => 123
|
58
|
+
|
59
|
+
{ "Привет": { "Мир": "123" } }
|
60
|
+
```
|
61
|
+
|
62
|
+
### Reading file
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
generator = Docxgen::Generator.new("template.docx")
|
66
|
+
```
|
67
|
+
|
68
|
+
### Rendering
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
generator.render({ variable: "Value" }, remove_missing: true)
|
72
|
+
```
|
73
|
+
|
74
|
+
Options:
|
75
|
+
|
76
|
+
`remove_missing` [default: true] - Replaces variables without provided values with empty string
|
77
|
+
|
78
|
+
### Checking render results
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
generator.valid? # Returns true if there is no errors
|
82
|
+
|
83
|
+
puts generator.errors # Returns array of the errors
|
84
|
+
```
|
85
|
+
|
86
|
+
### Saving file
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
generator.save("result.docx")
|
90
|
+
```
|
91
|
+
|
92
|
+
## Development
|
93
|
+
|
94
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
95
|
+
|
96
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
97
|
+
|
98
|
+
## TODO
|
99
|
+
|
100
|
+
- Add error objects
|
101
|
+
|
102
|
+
## Contributing
|
103
|
+
|
104
|
+
Bug reports and pull requests are welcome on GitHub at [https://github.com/4ndv/docxgen](https://github.com/4ndv/docxgen).
|
105
|
+
|
106
|
+
## License
|
107
|
+
|
108
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "docxgen"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/docxgen.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/docxgen/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "docxgen"
|
7
|
+
spec.version = Docxgen::VERSION
|
8
|
+
spec.authors = ["Andrey Viktorov"]
|
9
|
+
spec.email = ["andv@outlook.com"]
|
10
|
+
|
11
|
+
spec.summary = "docxgen allows generating docx files based on templates with variables"
|
12
|
+
spec.homepage = "https://github.com/4ndv/docxgen"
|
13
|
+
spec.license = "MIT"
|
14
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = "https://github.com/4ndv/docxgen"
|
18
|
+
spec.metadata["changelog_uri"] = "https://github.com/4ndv/docxgen/blob/master/CHANGELOG.md"
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
end
|
data/lib/docxgen.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "json"
|
4
|
+
require "docx"
|
5
|
+
|
6
|
+
module Docxgen
|
7
|
+
class Generator
|
8
|
+
attr_reader :doc
|
9
|
+
|
10
|
+
def initialize(document)
|
11
|
+
@doc = Docx::Document.open(document)
|
12
|
+
@errors = []
|
13
|
+
@rendered = false
|
14
|
+
end
|
15
|
+
|
16
|
+
def render(data, remove_missing: true)
|
17
|
+
raise "Already rendered" if @rendered
|
18
|
+
|
19
|
+
variables = JSON.parse(data.to_json, symbolize_names: true)
|
20
|
+
|
21
|
+
paragraphs = @doc.paragraphs + collect_table_paragraphs
|
22
|
+
|
23
|
+
paragraphs.each do |p|
|
24
|
+
p.each_text_run do |tr|
|
25
|
+
_, sub_errors = Templates::Parser.tr_substitute!(tr, variables, remove_missing: remove_missing)
|
26
|
+
|
27
|
+
@errors.push(*sub_errors)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
@rendered = true
|
32
|
+
|
33
|
+
valid?
|
34
|
+
end
|
35
|
+
|
36
|
+
def errors
|
37
|
+
raise "Wasn't rendered" unless @rendered
|
38
|
+
|
39
|
+
@errors
|
40
|
+
end
|
41
|
+
|
42
|
+
def valid?
|
43
|
+
errors.size.zero?
|
44
|
+
end
|
45
|
+
|
46
|
+
def save(to)
|
47
|
+
raise "Wasn't rendered" unless @rendered
|
48
|
+
|
49
|
+
@doc.save(to)
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def collect_table_paragraphs
|
55
|
+
# TODO: Refactor, maybe?
|
56
|
+
|
57
|
+
@doc
|
58
|
+
.tables
|
59
|
+
.map(&:rows).flatten
|
60
|
+
.map(&:cells).flatten
|
61
|
+
.map(&:paragraphs).flatten
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Docxgen
|
4
|
+
module Templates
|
5
|
+
class Parser
|
6
|
+
VARIABLE_REGEX = /({{[[:space:]]?(([[:alpha:]]|_)+([[:alnum:]]|_|\.)*(?<!\.))[[:space:]]?}})/
|
7
|
+
|
8
|
+
def self.tr_substitute!(tr, data, remove_missing: true)
|
9
|
+
found_variables = self.find_variables(tr)
|
10
|
+
|
11
|
+
errors = []
|
12
|
+
|
13
|
+
found_variables.each do |var|
|
14
|
+
value = data.dig(*var[:path])
|
15
|
+
|
16
|
+
errors.push("No value provided for variable: #{var[:src]}") if value.nil?
|
17
|
+
|
18
|
+
next if value.nil? && !remove_missing # Don't replace variables with empty string if remove_missing is false
|
19
|
+
|
20
|
+
tr.substitute(var[:src], value || "")
|
21
|
+
end
|
22
|
+
|
23
|
+
[tr, errors]
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.find_variables(tr)
|
27
|
+
tr.to_s.scan(VARIABLE_REGEX).map do |match|
|
28
|
+
src, path, *_ = match
|
29
|
+
|
30
|
+
{
|
31
|
+
src: src,
|
32
|
+
path: path.split(".").reject(&:empty?).map do |p|
|
33
|
+
Integer(p) rescue next p.to_sym
|
34
|
+
|
35
|
+
p.to_i
|
36
|
+
end
|
37
|
+
}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: docxgen
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrey Viktorov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-06-11 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- andv@outlook.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".rspec"
|
22
|
+
- ".rubocop.yml"
|
23
|
+
- ".travis.yml"
|
24
|
+
- CHANGELOG.md
|
25
|
+
- Gemfile
|
26
|
+
- Gemfile.lock
|
27
|
+
- LICENSE.txt
|
28
|
+
- README.md
|
29
|
+
- Rakefile
|
30
|
+
- bin/console
|
31
|
+
- bin/setup
|
32
|
+
- docxgen.gemspec
|
33
|
+
- lib/docxgen.rb
|
34
|
+
- lib/docxgen/generator.rb
|
35
|
+
- lib/docxgen/templates/parser.rb
|
36
|
+
- lib/docxgen/version.rb
|
37
|
+
homepage: https://github.com/4ndv/docxgen
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
metadata:
|
41
|
+
homepage_uri: https://github.com/4ndv/docxgen
|
42
|
+
source_code_uri: https://github.com/4ndv/docxgen
|
43
|
+
changelog_uri: https://github.com/4ndv/docxgen/blob/master/CHANGELOG.md
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 2.4.0
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubygems_version: 3.0.3
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: docxgen allows generating docx files based on templates with variables
|
63
|
+
test_files: []
|