docx_template 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +22 -1
- data/README.md +45 -2
- data/docx_template.gemspec +28 -0
- data/lib/docx_template/docx.rb +25 -5
- data/lib/docx_template/version.rb +1 -1
- data/spec/docx_spec.rb +7 -0
- data/spec/unit/templates/docx_template.docx +0 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8de0ab802b5846e6c2c6ef7d65a254b6515803cc
|
4
|
+
data.tar.gz: 4240ab789b88a6fc281e4217b897c7effed15a2e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 825187871496b970ea789fda0a11416b13b6f66e08269dfe3b1cc930689b3f9bee3cfd48474e8d11a1550f490b8c06501885627cc0b143242bb90f4f81397229
|
7
|
+
data.tar.gz: cd89852f2474baa12dedacaf31e74e4d8c9465915693cb2519cdf1d464005460e262c434594fcb1399c6a6adbbe6b3cdfaf9f42d4659be755c0bd9463f623489
|
data/LICENSE
CHANGED
@@ -1,3 +1,24 @@
|
|
1
1
|
== docx_template
|
2
2
|
|
3
|
-
|
3
|
+
Copyright (c) 2014 Maniankara
|
4
|
+
|
5
|
+
Maniankara Inc. License
|
6
|
+
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
8
|
+
a copy of this software and associated documentation files (the
|
9
|
+
"Software"), to deal in the Software without restriction, including
|
10
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
11
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
12
|
+
permit persons to whom the Software is furnished to do so, subject to
|
13
|
+
the following conditions:
|
14
|
+
|
15
|
+
The above copyright notice and this permission notice shall be
|
16
|
+
included in all copies or substantial portions of the Software.
|
17
|
+
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
20
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
22
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
23
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
24
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,9 +1,30 @@
|
|
1
|
-
docx_template
|
1
|
+
docx_template - Templating library for docx files
|
2
2
|
=============
|
3
3
|
|
4
4
|
Docx templating library which could replace docx contents such as text, images etc. in one go.
|
5
5
|
|
6
|
+
### Installation
|
7
|
+
docx_template can be used from the command line or as part of a Ruby web framework. To install the gem using terminal, run the following command:
|
8
|
+
```
|
9
|
+
gem install docx_template
|
10
|
+
```
|
11
|
+
To use it in Rails, add this line to your Gemfile:
|
12
|
+
```
|
13
|
+
gem 'docx_template'
|
14
|
+
```
|
15
|
+
|
6
16
|
#### Sample usage
|
17
|
+
```
|
18
|
+
require 'docx_template'
|
19
|
+
template = DocxTemplate::Docx.new "/opt/docx_template.docx"
|
20
|
+
template.replace_text("##SINGLE_REPLACE_TEXT##", "ACTUAL TEXT HERE")
|
21
|
+
template.replace_text("##MULTI_REPLACE_TEXT##", "ACTUAL TEXT HERE", true)
|
22
|
+
template.replace_image("image1.jpeg", "/opt/image5.jpg")
|
23
|
+
template.replace_header("##HEADER_REPLACE_TEXT##", "ACTUAL TEXT HERE", true)
|
24
|
+
template.save('/opt/final.docx')
|
25
|
+
```
|
26
|
+
###### Line by line explanation is given here:
|
27
|
+
|
7
28
|
```
|
8
29
|
template = DocxTemplate::Docx.new "/opt/docx_template.docx"
|
9
30
|
```
|
@@ -21,6 +42,28 @@ template.replace_image("image1.jpeg", "/opt/image5.jpg")
|
|
21
42
|
```
|
22
43
|
The first param defines the name of the image to be replaced. Can be checked by opening the template as archive
|
23
44
|
```
|
45
|
+
template.replace_header("##HEADER_REPLACE_TEXT##", "ACTUAL TEXT HERE", true)
|
46
|
+
```
|
47
|
+
This does the same as before but in document headers
|
48
|
+
```
|
24
49
|
template.save('/opt/final.docx')
|
25
50
|
```
|
26
|
-
The actual
|
51
|
+
The actual file processing begins and ends here
|
52
|
+
|
53
|
+
### Contributing
|
54
|
+
Contributions are welcomed. You can fork a repository, add your code changes to the forked branch, ensure all existing unit tests pass, create new unit tests cover your new changes and finally create a pull request.
|
55
|
+
|
56
|
+
#### Testing
|
57
|
+
Once this is complete, you should be able to run the test suite:
|
58
|
+
```
|
59
|
+
rspec
|
60
|
+
```
|
61
|
+
### Release test setup
|
62
|
+
The following test setup was used when released:
|
63
|
+
* ruby2.1.2
|
64
|
+
* rails4
|
65
|
+
* Ubuntu14.1 Linux
|
66
|
+
* Microsoft word 2007+
|
67
|
+
|
68
|
+
### License
|
69
|
+
docx_template has been published under https://github.com/maniankara/docx_template/blob/master/LICENSE under Maniankara Inc.
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'docx_template/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "docx_template"
|
8
|
+
spec.version = "0.2"
|
9
|
+
spec.authors = ["Maniankara"]
|
10
|
+
spec.email = ["Maniankara@gmail.com"]
|
11
|
+
spec.summary = %q{Docx templating library which could replace docx contents such as text, images etc. in one go.}
|
12
|
+
spec.description = %q{
|
13
|
+
template = DocxTemplate::Docx.new "/opt/docx_template.docx"
|
14
|
+
template.replace_text("##SINGLE_REPLACE_TEXT##", "ACTUAL TEXT HERE")
|
15
|
+
template.replace_text("##MULTI_REPLACE_TEXT##", "ACTUAL TEXT HERE", true)
|
16
|
+
template.replace_image("image1.jpeg", "/opt/image5.jpg")
|
17
|
+
template.save('/opt/final.docx')
|
18
|
+
}
|
19
|
+
spec.homepage = "https://www.facebook.com/maniankara"
|
20
|
+
spec.license = "Maniankara Inc."
|
21
|
+
spec.files = `git ls-files -z`.split("\x0")
|
22
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
23
|
+
spec.test_files = spec.files.grep(%r{^(spec)/})
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
27
|
+
spec.add_development_dependency "rake"
|
28
|
+
end
|
data/lib/docx_template/docx.rb
CHANGED
@@ -3,11 +3,17 @@ require 'zip'
|
|
3
3
|
module DocxTemplate
|
4
4
|
|
5
5
|
class DocxTemplate::Docx
|
6
|
-
attr_reader :dest_path,
|
6
|
+
attr_reader :dest_path,
|
7
|
+
:file_path,
|
8
|
+
:text_replacer_list,
|
9
|
+
:image_replacer_list,
|
10
|
+
:header_replacer_list
|
11
|
+
|
7
12
|
def initialize(file_path)
|
8
13
|
@file_path = file_path
|
9
14
|
@text_replacer_list = []
|
10
15
|
@image_replacer_list = []
|
16
|
+
@header_replacer_list = []
|
11
17
|
end
|
12
18
|
|
13
19
|
def replace_text(src_text, dest_text, multiple_occurances=false)
|
@@ -20,6 +26,10 @@ module DocxTemplate
|
|
20
26
|
@image_replacer_list << replacer
|
21
27
|
end
|
22
28
|
|
29
|
+
def replace_header(src_text, dest_text, multiple_occurances=false)
|
30
|
+
@header_replacer_list << EntityReplacer.new(src_text, dest_text, multiple_occurances)
|
31
|
+
end
|
32
|
+
|
23
33
|
def save(dest_path=Dir.mktmpdir)
|
24
34
|
@dest_path = dest_path
|
25
35
|
buffer = nil
|
@@ -29,15 +39,17 @@ module DocxTemplate
|
|
29
39
|
prepare_rest_of_archive(zip_file, out, exclusion_files_list)
|
30
40
|
# text part
|
31
41
|
unless @text_replacer_list.empty?
|
32
|
-
out
|
33
|
-
replacer = @text_replacer_list.first
|
34
|
-
out.write zip_file.read(DOCUMENT_FILE_PATH).gsub(replacer.src_entity,replacer.dest_entity)
|
42
|
+
replace_content(out, DOCUMENT_FILE_PATH, zip_file, @text_replacer_list)
|
35
43
|
end
|
36
44
|
# image part
|
37
45
|
@image_replacer_list.each do |replacer|
|
38
46
|
out.put_next_entry("#{IMAGES_DIR_PATH}/#{replacer.src_entity}")
|
39
47
|
out.write File.read(replacer.dest_entity)
|
40
48
|
end
|
49
|
+
# header part
|
50
|
+
unless @header_replacer_list.empty?
|
51
|
+
replace_content(out, HEADER_FILE_PATH, zip_file, @header_replacer_list)
|
52
|
+
end
|
41
53
|
end
|
42
54
|
end
|
43
55
|
|
@@ -48,7 +60,7 @@ module DocxTemplate
|
|
48
60
|
|
49
61
|
DOCUMENT_FILE_PATH = "word/document.xml"
|
50
62
|
IMAGES_DIR_PATH = "word/media"
|
51
|
-
|
63
|
+
HEADER_FILE_PATH = "word/header1.xml"
|
52
64
|
|
53
65
|
def prepare_rest_of_archive(zip_file, out, exclude_list)
|
54
66
|
zip_file.entries.each do |e|
|
@@ -70,6 +82,14 @@ module DocxTemplate
|
|
70
82
|
e_list
|
71
83
|
end
|
72
84
|
|
85
|
+
def replace_content(out, dest_file, zip_file, list)
|
86
|
+
out.put_next_entry(dest_file)
|
87
|
+
string_buffer = zip_file.read(dest_file)
|
88
|
+
list.each do |replacer|
|
89
|
+
string_buffer.gsub!(replacer.src_entity,replacer.dest_entity)
|
90
|
+
end
|
91
|
+
out.write string_buffer
|
92
|
+
end
|
73
93
|
|
74
94
|
class EntityReplacer
|
75
95
|
attr_reader :src_entity, :dest_entity, :occurances
|
data/spec/docx_spec.rb
CHANGED
@@ -26,4 +26,11 @@ RSpec.describe DocxTemplate::Docx do
|
|
26
26
|
template.save("/tmp/a.docx")
|
27
27
|
expect(@docx_verifier.verify_image_exists?("/tmp/a.docx", "image1.jpeg", "spec/unit/images/image5.jpg")).to be(true)
|
28
28
|
end
|
29
|
+
|
30
|
+
it "Replaces text in header" do
|
31
|
+
template = DocxTemplate::Docx.new "spec/unit/templates/docx_template.docx"
|
32
|
+
template.replace_header("##HEADER_REPLACE_TEXT##", "here")
|
33
|
+
template.save("/tmp/a.docx")
|
34
|
+
expect(@docx_verifier.verify_text_exists?("/tmp/a.docx", "##HEADER_REPLACE_TEXT##")).to be(false)
|
35
|
+
end
|
29
36
|
end
|
Binary file
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: docx_template
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maniankara
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -53,6 +53,7 @@ files:
|
|
53
53
|
- README
|
54
54
|
- README.md
|
55
55
|
- Rakefile
|
56
|
+
- docx_template.gemspec
|
56
57
|
- lib/docx_template.rb
|
57
58
|
- lib/docx_template/docx.rb
|
58
59
|
- lib/docx_template/version.rb
|