docx_template 0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 25e7e959da3023c6e56466f20de720fd04b7c3a7
4
+ data.tar.gz: 33b39c4f911c116c28a8fb7be8b6689ca43edb9e
5
+ SHA512:
6
+ metadata.gz: c7badb132b8de0afd35cf2c03d9a0665303598e226ce50ef64d7c8efa116b46935c539a09c7835a80a4e98970d67130fd5f6d84fd447aca928b06cf5e85441a7
7
+ data.tar.gz: ee443021f0c0c7d2bf1d69d186798cc8c57207a4125b60aa1ac85a398bb1d776e81aada7496d73f217a4e473c36fd8b0523d9fd44c2c8ea35e0e7c7706604c8f
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ #Unit testing
4
+ gem 'rspec'
5
+
6
+ #ruby official zip
7
+ gem 'rubyzip'
data/LICENSE ADDED
@@ -0,0 +1,3 @@
1
+ == docx_template
2
+
3
+ Put appropriate LICENSE for your project here.
data/README ADDED
@@ -0,0 +1,3 @@
1
+ == docx_template
2
+
3
+ You should document your project here.
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ docx_template
2
+ =============
3
+
4
+ Docx templating library which could replace docx contents such as text, images etc. in one go.
5
+
6
+ #### Sample usage
7
+ ```
8
+ template = DocxTemplate::Docx.new "/opt/docx_template.docx"
9
+ ```
10
+ The template document contains notations and other things which will be replaced
11
+ ```
12
+ template.replace_text("##SINGLE_REPLACE_TEXT##", "ACTUAL TEXT HERE")
13
+ ```
14
+ The replace text can be anything inside the document
15
+ ```
16
+ template.replace_text("##MULTI_REPLACE_TEXT##", "ACTUAL TEXT HERE", true)
17
+ ```
18
+ The given text can also be replaced across the whole document
19
+ ```
20
+ template.replace_image("image1.jpeg", "/opt/image5.jpg")
21
+ ```
22
+ The first param defines the name of the image to be replaced. Can be checked by opening the template as archive
23
+ ```
24
+ template.save('/opt/final.docx')
25
+ ```
26
+ The actual process begins and ends here
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ #Rake::TestTask.new do |t|
5
+ # t.libs << "lib/docx_template"
6
+ # t.libs << "test"
7
+ # t.test_files = FileList["test/**/*_test.rb"]
8
+ # t.verbose = true
9
+ #end
@@ -0,0 +1,90 @@
1
+ require 'zip'
2
+
3
+ module DocxTemplate
4
+
5
+ class DocxTemplate::Docx
6
+ attr_reader :dest_path, :file_path, :text_replacer_list, :image_replacer_list
7
+ def initialize(file_path)
8
+ @file_path = file_path
9
+ @text_replacer_list = []
10
+ @image_replacer_list = []
11
+ end
12
+
13
+ def replace_text(src_text, dest_text, multiple_occurances=false)
14
+ @text_replacer_list << EntityReplacer.new(src_text, dest_text, multiple_occurances)
15
+ end
16
+
17
+ def replace_image(src_image_file_name, dest_image_file)
18
+ replacer = EntityReplacer.new(src_image_file_name, dest_image_file, false)
19
+ replacer.type = "IMAGE"
20
+ @image_replacer_list << replacer
21
+ end
22
+
23
+ def save(dest_path=Dir.mktmpdir)
24
+ @dest_path = dest_path
25
+ buffer = nil
26
+ Zip::File.open(@file_path) do |zip_file|
27
+ buffer = Zip::OutputStream.write_buffer do |out|
28
+ exclusion_files_list = derive_exclusion_file_list
29
+ prepare_rest_of_archive(zip_file, out, exclusion_files_list)
30
+ # text part
31
+ unless @text_replacer_list.empty?
32
+ out.put_next_entry(DOCUMENT_FILE_PATH)
33
+ replacer = @text_replacer_list.first
34
+ out.write zip_file.read(DOCUMENT_FILE_PATH).gsub(replacer.src_entity,replacer.dest_entity)
35
+ end
36
+ # image part
37
+ @image_replacer_list.each do |replacer|
38
+ out.put_next_entry("#{IMAGES_DIR_PATH}/#{replacer.src_entity}")
39
+ out.write File.read(replacer.dest_entity)
40
+ end
41
+ end
42
+ end
43
+
44
+ File.open(dest_path, "w") {|f| f.write(buffer.string) }
45
+ end
46
+
47
+ private
48
+
49
+ DOCUMENT_FILE_PATH = "word/document.xml"
50
+ IMAGES_DIR_PATH = "word/media"
51
+ RELS_FILE_PATH = "word"
52
+
53
+ def prepare_rest_of_archive(zip_file, out, exclude_list)
54
+ zip_file.entries.each do |e|
55
+ unless exclude_list.include?(e.name)
56
+ out.put_next_entry(e.name)
57
+ out.write e.get_input_stream.read
58
+ end
59
+ end
60
+ end
61
+
62
+ def derive_exclusion_file_list
63
+ e_list = []
64
+ unless @text_replacer_list.empty?
65
+ e_list << DOCUMENT_FILE_PATH
66
+ end
67
+ @image_replacer_list.each do |replacer|
68
+ e_list << "#{IMAGES_DIR_PATH}/#{replacer.src_entity}"
69
+ end
70
+ e_list
71
+ end
72
+
73
+
74
+ class EntityReplacer
75
+ attr_reader :src_entity, :dest_entity, :occurances
76
+ attr_writer :type
77
+ def initialize(src, dest, occurances)
78
+ @src_entity = src
79
+ @dest_entity = dest
80
+ @occurances = occurances
81
+ end
82
+
83
+ def type
84
+ @type || "FILE"
85
+ end
86
+ end
87
+
88
+ end
89
+
90
+ end
@@ -0,0 +1,3 @@
1
+ module DocxTemplate
2
+ VERISON = "0.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ require 'docx_template/version'
2
+ require 'docx_template/docx'
3
+
4
+ module DocxTemplate
5
+
6
+ end
data/spec/docx_spec.rb ADDED
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe DocxTemplate::Docx do
4
+
5
+ before(:all) do
6
+ @docx_verifier = SpecHelper::DocxVerifier.new
7
+ end
8
+
9
+ it "Replaces text once" do
10
+ template = DocxTemplate::Docx.new "spec/unit/templates/docx_template.docx"
11
+ template.replace_text("##SINGLE_REPLACE_TEXT##", "here")
12
+ template.save("/tmp/a.docx")
13
+ expect(@docx_verifier.verify_text_exists?("/tmp/a.docx", "##SINGLE_REPLACE_TEXT##")).to be(false)
14
+ end
15
+
16
+ it "Replaces text multiple occurances" do
17
+ template = DocxTemplate::Docx.new "spec/unit/templates/docx_template.docx"
18
+ template.replace_text("##MULTIPLE_REPLACE_TEXT##", "here")
19
+ template.save("/tmp/a.docx")
20
+ expect(@docx_verifier.verify_text_exists?("/tmp/a.docx", "##MULTIPLE_REPLACE_TEXT##")).to be(false)
21
+ end
22
+
23
+ it "Replaces an image" do
24
+ template = DocxTemplate::Docx.new "spec/unit/templates/docx_template.docx"
25
+ template.replace_image("image1.jpeg", "spec/unit/images/image5.jpg")
26
+ template.save("/tmp/a.docx")
27
+ expect(@docx_verifier.verify_image_exists?("/tmp/a.docx", "image1.jpeg", "spec/unit/images/image5.jpg")).to be(true)
28
+ end
29
+ end
@@ -0,0 +1,35 @@
1
+ require 'docx_template/docx'
2
+
3
+ module SpecHelper
4
+
5
+ class DocxVerifier
6
+
7
+ def verify_text_exists?(docx_file_path, search_text)
8
+ Zip::File.open(docx_file_path) do |zip_file|
9
+ zip_file.entries.each do |entry|
10
+ next if entry.name != DOCUMENT_FILE_PATH
11
+ file_contents = entry.get_input_stream.read
12
+ return true if file_contents.include?(search_text)
13
+ end
14
+ end
15
+ return false
16
+ end
17
+
18
+ # verifies if the sizes of file are same
19
+ def verify_image_exists?(docx_file_path, src_image, expected_image)
20
+ Zip::File.open(docx_file_path) do |zip_file|
21
+ zip_file.entries.each do |entry|
22
+ next if entry.name != "#{IMAGE_DIR_PATH}/#{src_image}"
23
+ archive_file_contents = entry.get_input_stream.read
24
+ return archive_file_contents.size == File.size(expected_image)
25
+ end
26
+ end
27
+ return false
28
+ end
29
+
30
+ private
31
+
32
+ DOCUMENT_FILE_PATH = 'word/document.xml'
33
+ IMAGE_DIR_PATH = 'word/media'
34
+ end
35
+ end
Binary file
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: docx_template
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Maniankara
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: "\n template = DocxTemplate::Docx.new \"/opt/docx_template.docx\"\n
42
+ \ template.replace_text(\"##SINGLE_REPLACE_TEXT##\", \"ACTUAL TEXT HERE\")\n template.replace_text(\"##MULTI_REPLACE_TEXT##\",
43
+ \"ACTUAL TEXT HERE\", true)\n template.replace_image(\"image1.jpeg\", \"/opt/image5.jpg\")\n
44
+ \ template.save('/opt/final.docx')\n "
45
+ email:
46
+ - Maniankara@gmail.com
47
+ executables: []
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - Gemfile
52
+ - LICENSE
53
+ - README
54
+ - README.md
55
+ - Rakefile
56
+ - lib/docx_template.rb
57
+ - lib/docx_template/docx.rb
58
+ - lib/docx_template/version.rb
59
+ - spec/docx_spec.rb
60
+ - spec/spec_helper.rb
61
+ - spec/unit/images/image5.jpg
62
+ - spec/unit/templates/docx_template.docx
63
+ homepage: https://www.facebook.com/maniankara
64
+ licenses:
65
+ - Maniankara Inc.
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.2.2
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Docx templating library which could replace docx contents such as text, images
87
+ etc. in one go.
88
+ test_files:
89
+ - spec/docx_spec.rb
90
+ - spec/spec_helper.rb
91
+ - spec/unit/images/image5.jpg
92
+ - spec/unit/templates/docx_template.docx