open_xml 0.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: 4d5a61d483d830ba65ff655b1109cfd6c6658df0
4
+ data.tar.gz: f912f15f3ae8988ebd3630fe1eca1b7d42cc2468
5
+ SHA512:
6
+ metadata.gz: e458a1b9edab91f74a06d5998cc65578453981bcc6dc89a07bfe6e2d5782311a30416f75c2bfe2c961cd070ffd224bc0c8eb350c5e2fd445f40bd3cac9cf3df7
7
+ data.tar.gz: 1526980394d5a3adf23e9c372a3f4948d21ca9386cc70e47f7a09d91c165f591176f4a26d2eef27cd1eda675e670c393179c443fbd52a022653d3699afa5153f
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## v0.0.1
2
+
3
+ * initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in open_xml.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Carlos Espejo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ 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, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ OpenXml
2
+ ========
3
+
4
+ Library for reading and writing to open xml documents (*but at the moment you can generate word docs from a template*)
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'open_xml'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install open_xml
19
+
20
+
21
+ ## Usage
22
+ Provide a path to a docx with the word [SUPERPOWER] placed anywhere.
23
+
24
+ ```ruby
25
+ require 'open_xml'
26
+
27
+ doc = OpenXml::TemplateDocument.new(path: "[path to template]", data: {"[SUPERPOWER]" => "Bug Fixing!!!!"})
28
+ doc.process
29
+
30
+ IO.write "./powers.docx", doc.to_zip_buffer.string
31
+ ```
32
+
33
+ ## Todo
34
+ * ~~Implement reading and writing the word zip files~~
35
+ * ~~Create a template word document with formatted key words (bold, 14pt).~~
36
+ * ~~Replace the key words with the supplied plain text content but maintain all the formatting.~~
37
+ * ~~Handle replacing a key with multiple content~~
38
+ * Extract these features into a gem
39
+ * Format html content for wordprocessingML e.x. bold, italic,
40
+ underline
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "rake/testtask"
2
+ require "bundler/gem_tasks"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "lib"
6
+ t.libs << "spec"
7
+ t.pattern = "spec/**/*_spec.rb"
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,50 @@
1
+ require 'zip'
2
+ require 'nokogiri'
3
+
4
+ module OpenXml
5
+
6
+
7
+ class TemplateDocument
8
+ attr_reader :template_path, :parts, :data
9
+
10
+ def initialize(options)
11
+ @template_path = options[:path]
12
+ @parts = {}
13
+ @data = options[:data]
14
+ split_parts
15
+ end
16
+
17
+ def to_zip_buffer
18
+ Zip::OutputStream.write_buffer do |w|
19
+ parts.each do |k, v|
20
+ w.put_next_entry k
21
+ w.write v
22
+ end
23
+ end
24
+ end
25
+
26
+ def process
27
+ doc = Nokogiri::XML(parts["word/document.xml"])
28
+ doc.xpath("//w:t").each do |node|
29
+ data.each do |k, v|
30
+ node.content = node.content.gsub(k, Array(v).join("\n")) if node.content[/#{k}/]
31
+ end
32
+ end
33
+
34
+ parts["word/document.xml"] = doc.to_xml
35
+ end
36
+
37
+ private
38
+
39
+ def split_parts
40
+ Zip::File.new(template_path).each do |f|
41
+ parts[f.name] = f.get_input_stream.read
42
+ end
43
+
44
+ parts
45
+ end
46
+
47
+
48
+ end
49
+
50
+ end
@@ -0,0 +1,3 @@
1
+ module OpenXml
2
+ VERSION = "0.0.1"
3
+ end
data/lib/open_xml.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'open_xml/version'
2
+ require_relative 'open_xml/template_document'
data/open_xml.gemspec ADDED
@@ -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 'open_xml/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "open_xml"
8
+ spec.version = OpenXml::VERSION
9
+ spec.authors = ["Carlos Espejo"]
10
+ spec.email = ["carlosespejo@gmail.com"]
11
+ spec.description = %q{Currently you can only generate word documents from a template word document.}
12
+ spec.summary = %q{Library for reading and writing to open xml documents (*but at the moment you can generate word docs from a template*)}
13
+ spec.homepage = "https://github.com/CarlosEspejo/open_xml"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rubyzip"
24
+ spec.add_development_dependency "nokogiri"
25
+ spec.add_development_dependency "minitest"
26
+ spec.add_development_dependency "pry"
27
+ end
28
+
Binary file
@@ -0,0 +1,6 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require 'open_xml'
4
+ require 'pry'
5
+
6
+ include OpenXml
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+ require 'tempfile'
3
+ require 'nokogiri'
4
+
5
+ describe TemplateDocument do
6
+
7
+ it "should read the template file and split it into parts" do
8
+ t = TemplateDocument.new(path: template_path)
9
+ t.parts.keys.must_equal template_parts.keys
10
+
11
+ end
12
+
13
+ it "should create a new file from the template parts" do
14
+ t = Tempfile.new(['output','.docx'])
15
+
16
+ temp_doc = TemplateDocument.new(path: template_path)
17
+
18
+ IO.write t.path, temp_doc.to_zip_buffer.string
19
+
20
+ Zip::File.new(t.path).each do |f|
21
+ temp_doc.parts[f.name].must_equal f.get_input_stream.read
22
+ end
23
+
24
+ end
25
+
26
+ it "should replace key words in the document xml" do
27
+ temp = Tempfile.new(['output','.docx'])
28
+
29
+ t = TemplateDocument.new(path: template_path, data: {"[NAME]" => "<b>Carlos</b>", "[AGE]" => 30 })
30
+ t.process
31
+ IO.write temp.path, t.to_zip_buffer.string
32
+
33
+ processed = TemplateDocument.new(path: temp.path)
34
+ doc = Nokogiri::XML(processed.parts["word/document.xml"])
35
+
36
+ doc.xpath('//w:t').text[/\[NAME\]/].must_be_nil
37
+ doc.xpath('//w:t').text[/\[AGE\]/].must_be_nil
38
+ end
39
+
40
+ it "should replace one key word with many items" do
41
+ data = {
42
+ "[LIST]" => [
43
+ "list 1",
44
+ "list 2",
45
+ "list 3",
46
+ "list 4",
47
+ "list 5"
48
+ ]
49
+ }
50
+
51
+ t = TemplateDocument.new(path: template_path, data: data)
52
+ t.process
53
+ doc = Nokogiri::XML(t.parts["word/document.xml"])
54
+
55
+ text = doc.xpath('//w:t').text
56
+
57
+ text[/list 1\nlist 2/].wont_be_nil
58
+ end
59
+
60
+ let(:template_path){"#{File.expand_path('samples', File.dirname(__FILE__))}/template_sample.docx"}
61
+
62
+ let(:template_parts) do
63
+ {
64
+ "_rels/.rels" => '',
65
+ "docProps/core.xml" => '',
66
+ "docProps/app.xml" => '',
67
+ "word/document.xml" => '',
68
+ "word/styles.xml" => '',
69
+ "word/fontTable.xml" => '',
70
+ "word/header.xml" => '',
71
+ "word/footer.xml" => '',
72
+ "word/settings.xml" => '',
73
+ "word/_rels/document.xml.rels" => '',
74
+ "[Content_Types].xml" => ''
75
+ }
76
+ end
77
+
78
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open_xml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Carlos Espejo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-04 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
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
+ - !ruby/object:Gem::Dependency
42
+ name: rubyzip
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Currently you can only generate word documents from a template word document.
98
+ email:
99
+ - carlosespejo@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - CHANGELOG.md
106
+ - Gemfile
107
+ - LICENSE
108
+ - README.md
109
+ - Rakefile
110
+ - lib/open_xml.rb
111
+ - lib/open_xml/template_document.rb
112
+ - lib/open_xml/version.rb
113
+ - open_xml.gemspec
114
+ - spec/samples/template_sample.docx
115
+ - spec/spec_helper.rb
116
+ - spec/template_document_spec.rb
117
+ homepage: https://github.com/CarlosEspejo/open_xml
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.1.11
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Library for reading and writing to open xml documents (*but at the moment
141
+ you can generate word docs from a template*)
142
+ test_files:
143
+ - spec/samples/template_sample.docx
144
+ - spec/spec_helper.rb
145
+ - spec/template_document_spec.rb