docx_generator 0.0.1

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: 0e7df426c133c37b61b38793acb9170ea8a679c1
4
+ data.tar.gz: 280949c5112c2173d160605878d8166b563eae30
5
+ SHA512:
6
+ metadata.gz: 53c4c1b3963c15d5cbd56025f38d0ff2b25e3ac2744f01d148d64db0fa7effe51b37e591be3d73a777fe035aa9c581b0d03603bcecc062d79664c1682ad019b7
7
+ data.tar.gz: 020ccf60bc99bc5a032fd7ab7f5ed31a0372572483d484086f736ceb926bdffce209c81451e6208960049aa3b66f73113492b03a55ea1cd01666a7a70b7d0e9e
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/.rvmrc ADDED
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 2.0.0" > .rvmrc
9
+ environment_id="ruby-2.0.0-p0@docx_generator"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.20.5 (stable)" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ for __hook in "${rvm_path:-$HOME/.rvm}/hooks/after_use"*
27
+ do
28
+ if [[ -f "${__hook}" && -x "${__hook}" && -s "${__hook}" ]]
29
+ then \. "${__hook}" || true
30
+ fi
31
+ done
32
+ unset __hook
33
+ if (( ${rvm_use_flag:=1} >= 2 )) # display only when forced
34
+ then
35
+ if [[ $- == *i* ]] # check for interactive shells
36
+ then echo "Using: \E[32m$GEM_HOME\E[0m" # show the user the ruby and gemset they are using in green
37
+ else echo "Using: $GEM_HOME" # don't use colors in non-interactive shells
38
+ fi
39
+ fi
40
+ else
41
+ # If the environment file has not yet been created, use the RVM CLI to select.
42
+ rvm --create "$environment_id" || {
43
+ echo "Failed to create RVM environment '${environment_id}'."
44
+ return 1
45
+ }
46
+ fi
47
+
48
+ # If you use bundler, this might be useful to you:
49
+ # if [[ -s Gemfile ]] && {
50
+ # ! builtin command -v bundle >/dev/null ||
51
+ # builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
52
+ # }
53
+ # then
54
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
55
+ # gem install bundler
56
+ # fi
57
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
58
+ # then
59
+ # bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
60
+ # fi
data/CHANGES ADDED
@@ -0,0 +1,5 @@
1
+ # Version 0.0.1 (2013-05-11)
2
+
3
+ Initial version
4
+
5
+ - Create a basic docx document with paragraphs with text in bold and/or italics.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in docx_generator.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,11 @@
1
+ guard 'bundler' do
2
+ watch('Gemfile')
3
+ watch(/^.+\.gemspec/)
4
+ end
5
+
6
+ guard :rspec, cli: "--color" do
7
+ watch(%r{^spec/.+_spec\.rb$})
8
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
9
+ watch('spec/spec_helper.rb') { "spec" }
10
+ end
11
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Antoine Proulx
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # DocxGenerator
2
+
3
+ A gem to generate docx files.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'docx_generator'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install docx_generator
18
+
19
+ ## Usage
20
+
21
+ To create a new docx file and save it, just type:
22
+
23
+ ```ruby
24
+ require 'docx_generator'
25
+
26
+ DocxGenerator::Document.new("filename").save # Will save the document to filename.docx
27
+ ```
28
+
29
+ To create a paragraph and add formatted text to it:
30
+
31
+ ```ruby
32
+ require 'docx_generator'
33
+
34
+ document = DocxGenerator::Document.new("filename")
35
+ document.add_paragraph("Simple string of text and", document.text("some formatted text", bold: true, italics: true)) # The fragments will be separated by a space when they will be rendered
36
+ document.save
37
+ ```
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task default: :spec
7
+ task test: :spec
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'docx_generator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "docx_generator"
8
+ spec.version = DocxGenerator::VERSION
9
+ spec.authors = ["Antoine Proulx"]
10
+ spec.email = ["proulx.antoine@gmail.com"]
11
+ spec.description = %q{A gem to generate docx files.}
12
+ spec.summary = %q{A gem to generate docx files.}
13
+ spec.homepage = "http://magicienap.github.io/docx_generator"
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_runtime_dependency "zipruby"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "guard"
27
+ spec.add_development_dependency "guard-bundler"
28
+ spec.add_development_dependency "guard-rspec"
29
+ spec.add_development_dependency "libnotify"
30
+ end
@@ -0,0 +1,7 @@
1
+ require 'docx_generator'
2
+
3
+ document = DocxGenerator::Document.new("basic_paragraph")
4
+ document.add_paragraph("Simple string of text and", document.text("some formatted text", bold: true, italics: true))
5
+ document.add_paragraph(document.text("Antoine", bold: true), "How are you today?")
6
+ document.add_paragraph(document.text("John", bold: true), document.text("(whispering)", bold: true, italics: true), "How are you today?")
7
+ document.save
@@ -0,0 +1,8 @@
1
+ require 'docx_generator/version'
2
+ require 'docx_generator/element'
3
+ require 'docx_generator/document'
4
+ require 'docx_generator/word'
5
+ require 'zipruby'
6
+
7
+ module DocxGenerator
8
+ end
@@ -0,0 +1,85 @@
1
+ module DocxGenerator
2
+ class Document
3
+ attr_reader :filename
4
+
5
+ def initialize(filename)
6
+ @filename = filename
7
+ @content = []
8
+ end
9
+
10
+ def save
11
+ content_types = generate_content_types
12
+ rels = generate_rels
13
+ document = generate_document
14
+
15
+ Zip::Archive.open(@filename + ".docx", Zip::CREATE | Zip::TRUNC) do |docx|
16
+ docx.add_dir('_rels')
17
+ docx.add_dir('word')
18
+ docx.add_buffer('[Content_Types].xml', content_types)
19
+ docx.add_buffer('_rels/.rels', rels)
20
+ docx.add_buffer('word/document.xml', document)
21
+ end
22
+ end
23
+
24
+ # text_fragments : Word::Text or String
25
+ # Later: Paragraph Object, with Text Object
26
+ def add_paragraph(*text_fragments)
27
+ content = []
28
+ text_fragments.each do |text_fragment|
29
+ content.push(text_fragment.respond_to?(:generate) ? text_fragment : text(text_fragment))
30
+ content.push Word::Extensions.space
31
+ end
32
+ content.pop
33
+ @content.push Word::Paragraph.new({}, content)
34
+ self
35
+ end
36
+
37
+ def text(text_fragment, arguments = {})
38
+ content = []
39
+ if arguments.length != 0
40
+ options = []
41
+ arguments.each do |option, value|
42
+ options.push parse_option(option, value)
43
+ end
44
+ content.push Word::RunProperties.new({}, options)
45
+ end
46
+ content.push Word::Text.new({}, [text_fragment])
47
+ Word::Run.new({}, content)
48
+ end
49
+
50
+ private
51
+
52
+ def generate_content_types
53
+ <<EOF
54
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
55
+ <Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
56
+ <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
57
+ <Default Extension="xml" ContentType="application/xml"/>
58
+ <Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
59
+ </Types>
60
+ EOF
61
+ end
62
+
63
+ def generate_rels
64
+ <<EOF
65
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
66
+ <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
67
+ <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
68
+ </Relationships>
69
+ EOF
70
+ end
71
+
72
+ def generate_document
73
+ '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
74
+ Word::Document.new({ "xmlns:w" => "http://schemas.openxmlformats.org/wordprocessingml/2006/main" },
75
+ [ Word::Body.new({}, @content) ]).to_s
76
+ end
77
+
78
+ def parse_option(option, value)
79
+ case option
80
+ when :bold then Word::Bold.new(value)
81
+ when :italics then Word::Italics.new(value)
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,41 @@
1
+ module DocxGenerator
2
+ class Element
3
+ def initialize(name, attributes = {}, content = [])
4
+ @name = name
5
+ @attributes = attributes
6
+ @content = content
7
+ end
8
+
9
+ def add(element)
10
+ @content << element
11
+ end
12
+
13
+ def generate
14
+ output = ""
15
+ if @content.length != 0
16
+ output += "<#{@name}#{generate_attributes}>"
17
+ @content.each do |element|
18
+ if element.respond_to?(:generate)
19
+ output += element.generate
20
+ else
21
+ output += element.to_s
22
+ end
23
+ end
24
+ output += "</#{@name}>"
25
+ else
26
+ output += "<#{@name}#{generate_attributes} />"
27
+ end
28
+ output
29
+ end
30
+ alias :to_s :generate
31
+
32
+ private
33
+ def generate_attributes
34
+ output = ""
35
+ @attributes.each do |name, value|
36
+ output += " #{name}=\"#{value}\""
37
+ end
38
+ output
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module DocxGenerator
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,3 @@
1
+ require 'docx_generator/word/base'
2
+ require 'docx_generator/word/extensions'
3
+ require 'docx_generator/word/formatting'
@@ -0,0 +1,39 @@
1
+ module DocxGenerator
2
+ module Word
3
+ class Document < Element
4
+ def initialize(attributes = {}, content = [])
5
+ super("w:document", attributes, content)
6
+ end
7
+ end
8
+
9
+ class Body < Element
10
+ def initialize(attributes = {}, content = [])
11
+ super("w:body", attributes, content)
12
+ end
13
+ end
14
+
15
+ class Paragraph < Element
16
+ def initialize(attributes = {}, content = [])
17
+ super("w:p", attributes, content)
18
+ end
19
+ end
20
+
21
+ class Run < Element
22
+ def initialize(attributes = {}, content = [])
23
+ super("w:r", attributes, content)
24
+ end
25
+ end
26
+
27
+ class RunProperties < Element
28
+ def initialize(attributes = {}, content = [])
29
+ super("w:rPr", attributes, content)
30
+ end
31
+ end
32
+
33
+ class Text < Element
34
+ def initialize(attributes = {}, content = [])
35
+ super("w:t", attributes, content)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,9 @@
1
+ module DocxGenerator
2
+ module Word
3
+ module Extensions
4
+ def self.space
5
+ DocxGenerator::Word::Run.new({}, [DocxGenerator::Word::Text.new({ "xml:space" => "preserve" }, [" "])])
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ module DocxGenerator
2
+ module Word
3
+ class Bold < Element
4
+ def initialize(present = nil)
5
+ arguments = (present == nil ? {} : { "w:val" => present })
6
+ super("w:b", arguments)
7
+ end
8
+ end
9
+
10
+ class Italics < Element
11
+ def initialize(present = nil)
12
+ arguments = (present == nil ? {} : { "w:val" => present })
13
+ super("w:i", arguments)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ describe DocxGenerator::Document do
4
+ it "should create a new docx document with the filename specified" do
5
+ document = DocxGenerator::Document.new("word")
6
+ document.filename.should eq("word")
7
+ end
8
+
9
+ describe "#save" do
10
+ let(:document) { DocxGenerator::Document.new("word") }
11
+
12
+ after do
13
+ File.delete("word.docx")
14
+ end
15
+
16
+ it "should save the document" do
17
+ document.save
18
+ File.exists?("word.docx").should be_true
19
+ end
20
+
21
+ describe "required documents" do
22
+ before { DocxGenerator::Document.new("word").save }
23
+
24
+ it "should generate a [Content_Types].xml file" do
25
+ Zip::Archive.open("word.docx") do |docx|
26
+ expect { docx.fopen("[Content_Types].xml") }.to_not raise_error
27
+ end
28
+ end
29
+
30
+ it "should generate a _rels/.rels file" do
31
+ Zip::Archive.open("word.docx") do |docx|
32
+ expect { docx.fopen("_rels/.rels") }.to_not raise_error
33
+ end
34
+ end
35
+
36
+ it "should generate a word/document.xml" do
37
+ Zip::Archive.open("word.docx") do |docx|
38
+ expect { docx.fopen("word/document.xml") }.to_not raise_error
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ describe "#add_paragraph" do
45
+ let(:document) { DocxGenerator::Document.new("word") }
46
+
47
+ after do
48
+ File.delete("word.docx") if File.exists?("word.docx")
49
+ end
50
+
51
+ it "should add a paragraph with the fragments supplied separated by a space" do # Space : an option
52
+ document.add_paragraph("The first characters", "and the last ones.").save
53
+ open_file("word/document.xml").should include("<w:p><w:r><w:t>The first characters</w:t></w:r><w:r><w:t xml:space=\"preserve\"> </w:t></w:r><w:r><w:t>and the last ones.</w:t></w:r></w:p>")
54
+ end
55
+
56
+ # To be modified with bold and italics
57
+ it "should add a paragraph with a formatted text" do
58
+ document.add_paragraph(document.text("The first characters"), "and the last ones.").save
59
+ open_file("word/document.xml").should include("<w:p><w:r><w:t>The first characters</w:t></w:r><w:r><w:t xml:space=\"preserve\"> </w:t></w:r><w:r><w:t>and the last ones.</w:t></w:r></w:p>")
60
+ end
61
+
62
+ it "should return the current document" do
63
+ document.add_paragraph(["The first characters", "and the last ones."]).should be(document)
64
+ end
65
+ end
66
+
67
+ describe "#text" do
68
+ it "should return a new Run with text in it" do
69
+ DocxGenerator::Document.new("word").text("Text").to_s.should eq("<w:r><w:t>Text</w:t></w:r>")
70
+ end
71
+
72
+ context "with styles" do
73
+ it "should return a text in bold" do
74
+ DocxGenerator::Document.new("word").text("Text", bold: true).to_s.should eq("<w:r><w:rPr><w:b w:val=\"true\" /></w:rPr><w:t>Text</w:t></w:r>")
75
+ end
76
+
77
+ it "should return a text in italics" do
78
+ DocxGenerator::Document.new("word").text("Text", italics: true).to_s.should eq("<w:r><w:rPr><w:i w:val=\"true\" /></w:rPr><w:t>Text</w:t></w:r>")
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe DocxGenerator::Element do
4
+ it "should create a new XML element given a name, optional arguments and an optional content" do
5
+ DocxGenerator::Element.new("w:document")
6
+ DocxGenerator::Element.new("w:document", { "xmlns:w" => "http://schemas.openxmlformats.org/wordprocessingml/2006/main" })
7
+ DocxGenerator::Element.new("w:document", { "xmlns:w" => "http://schemas.openxmlformats.org/wordprocessingml/2006/main" }, [DocxGenerator::Element.new("w:body")])
8
+ end
9
+
10
+ describe "#add" do
11
+ it "should add a child to the element" do
12
+ element = DocxGenerator::Element.new("w:document")
13
+ element.add DocxGenerator::Element.new("w:body")
14
+ end
15
+ end
16
+
17
+ describe "#generate" do
18
+ context "without arguments and children" do
19
+ it "should render a self-closing XML tag" do
20
+ DocxGenerator::Element.new("w:document").generate.should eq("<w:document />")
21
+ end
22
+ end
23
+
24
+ context "with arguments" do
25
+ it "should render a self-closing XML tag with the arguments" do
26
+ DocxGenerator::Element.new("w:document", { "xmlns:w" => "http://schemas.openxmlformats.org/wordprocessingml/2006/main" }).generate.should eq("<w:document xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" />")
27
+ end
28
+ end
29
+
30
+ context "with arguments and children" do
31
+ it "should render the XML element with the arguments and the generated children" do
32
+ DocxGenerator::Element.new("w:document", { "xmlns:w" => "http://schemas.openxmlformats.org/wordprocessingml/2006/main" }, [DocxGenerator::Element.new("w:body")]).generate.should eq("<w:document xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:body /></w:document>")
33
+
34
+ DocxGenerator::Element.new("w:document", { "xmlns:w" => "http://schemas.openxmlformats.org/wordprocessingml/2006/main" }, ["Text"]).generate.should eq("<w:document xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">Text</w:document>")
35
+ end
36
+ end
37
+ end
38
+
39
+ describe "#to_s" do
40
+ it "should render the XML element in a string" do
41
+ DocxGenerator::Element.new("w:document", { "xmlns:w" => "http://schemas.openxmlformats.org/wordprocessingml/2006/main" }, [DocxGenerator::Element.new("w:body")]).to_s.should eq("<w:document xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:body /></w:document>")
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe DocxGenerator::Word::Document do
4
+ it "should render a w:document element" do
5
+ DocxGenerator::Word::Document.new.to_s.should eq("<w:document />")
6
+ DocxGenerator::Word::Document.new({}, ["Text"]).to_s.should eq("<w:document>Text</w:document>")
7
+ DocxGenerator::Word::Document.new({ "w:conformance" => "strict" }, ["Text"]).to_s.should eq("<w:document w:conformance=\"strict\">Text</w:document>")
8
+ end
9
+ end
10
+
11
+ describe DocxGenerator::Word::Body do
12
+ it "should render a w:body element" do
13
+ DocxGenerator::Word::Body.new.to_s.should eq("<w:body />")
14
+ DocxGenerator::Word::Body.new({}, ["Text"]).to_s.should eq("<w:body>Text</w:body>")
15
+ end
16
+ end
17
+
18
+ describe DocxGenerator::Word::Paragraph do
19
+ it "should render a w:p element" do
20
+ DocxGenerator::Word::Paragraph.new.to_s.should eq("<w:p />")
21
+ DocxGenerator::Word::Paragraph.new({}, ["Text"]).to_s.should eq("<w:p>Text</w:p>")
22
+ end
23
+ end
24
+
25
+ describe DocxGenerator::Word::Run do
26
+ it "should render a w:r element" do
27
+ DocxGenerator::Word::Run.new.to_s.should eq("<w:r />")
28
+ DocxGenerator::Word::Run.new({}, ["Text"]).to_s.should eq("<w:r>Text</w:r>")
29
+ end
30
+ end
31
+
32
+ describe DocxGenerator::Word::RunProperties do
33
+ it "should render a w:rPr element" do
34
+ DocxGenerator::Word::RunProperties.new.to_s.should eq("<w:rPr />")
35
+ DocxGenerator::Word::RunProperties.new({}, ["Text"]).to_s.should eq("<w:rPr>Text</w:rPr>")
36
+ end
37
+ end
38
+
39
+ describe DocxGenerator::Word::Text do
40
+ it "should render a w:t element" do
41
+ DocxGenerator::Word::Text.new.to_s.should eq("<w:t />")
42
+ DocxGenerator::Word::Text.new({}, ["Text"]).to_s.should eq("<w:t>Text</w:t>")
43
+ end
44
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe DocxGenerator::Word::Extensions do
4
+ describe "space" do
5
+ it "should render a space" do
6
+ DocxGenerator::Word::Extensions.space.to_s.should eq("<w:r><w:t xml:space=\"preserve\"> </w:t></w:r>")
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe DocxGenerator::Word::Bold do
4
+ it "should render a w:b element" do
5
+ DocxGenerator::Word::Bold.new.to_s.should eq("<w:b />")
6
+ DocxGenerator::Word::Bold.new(true).to_s.should eq("<w:b w:val=\"true\" />")
7
+ DocxGenerator::Word::Bold.new(false).to_s.should eq("<w:b w:val=\"false\" />")
8
+ end
9
+ end
10
+
11
+ describe DocxGenerator::Word::Italics do
12
+ it "should render a w:i element" do
13
+ DocxGenerator::Word::Italics.new.to_s.should eq("<w:i />")
14
+ DocxGenerator::Word::Italics.new(true).to_s.should eq("<w:i w:val=\"true\" />")
15
+ DocxGenerator::Word::Italics.new(false).to_s.should eq("<w:i w:val=\"false\" />")
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ require 'docx_generator'
2
+
3
+ def open_file(file)
4
+ content = nil
5
+ Zip::Archive.open("word.docx") do |docx|
6
+ docx.fopen("word/document.xml") { |f| content = f.read }
7
+ end
8
+ content
9
+ end
metadata ADDED
@@ -0,0 +1,186 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: docx_generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Antoine Proulx
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: zipruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
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: rspec
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: guard
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: guard-bundler
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
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: libnotify
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: A gem to generate docx files.
126
+ email:
127
+ - proulx.antoine@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - .gitignore
133
+ - .rvmrc
134
+ - CHANGES
135
+ - Gemfile
136
+ - Guardfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - docx_generator.gemspec
141
+ - examples/basic_paragraph.rb
142
+ - lib/docx_generator.rb
143
+ - lib/docx_generator/document.rb
144
+ - lib/docx_generator/element.rb
145
+ - lib/docx_generator/version.rb
146
+ - lib/docx_generator/word.rb
147
+ - lib/docx_generator/word/base.rb
148
+ - lib/docx_generator/word/extensions.rb
149
+ - lib/docx_generator/word/formatting.rb
150
+ - spec/docx_generator/document_spec.rb
151
+ - spec/docx_generator/element_spec.rb
152
+ - spec/docx_generator/word/base_spec.rb
153
+ - spec/docx_generator/word/extensions_spec.rb
154
+ - spec/docx_generator/word/formatting_spec.rb
155
+ - spec/spec_helper.rb
156
+ homepage: http://magicienap.github.io/docx_generator
157
+ licenses:
158
+ - MIT
159
+ metadata: {}
160
+ post_install_message:
161
+ rdoc_options: []
162
+ require_paths:
163
+ - lib
164
+ required_ruby_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - '>='
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ required_rubygems_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ requirements: []
175
+ rubyforge_project:
176
+ rubygems_version: 2.0.3
177
+ signing_key:
178
+ specification_version: 4
179
+ summary: A gem to generate docx files.
180
+ test_files:
181
+ - spec/docx_generator/document_spec.rb
182
+ - spec/docx_generator/element_spec.rb
183
+ - spec/docx_generator/word/base_spec.rb
184
+ - spec/docx_generator/word/extensions_spec.rb
185
+ - spec/docx_generator/word/formatting_spec.rb
186
+ - spec/spec_helper.rb