docx_generator 0.0.1 → 0.0.2
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 +4 -4
- data/CHANGES +6 -0
- data/README.md +4 -0
- data/basic_paragraph.docx +0 -0
- data/examples/basic_paragraph.rb +2 -1
- data/lib/docx_generator/document.rb +24 -4
- data/lib/docx_generator/version.rb +1 -1
- data/lib/docx_generator/word/base.rb +6 -0
- data/lib/docx_generator/word/formatting.rb +26 -0
- data/spec/docx_generator/document_spec.rb +15 -0
- data/spec/docx_generator/word/base_spec.rb +7 -0
- data/spec/docx_generator/word/formatting_spec.rb +19 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 24ad7dac74a46bba80074d78c61c3036b3edc4b0
|
4
|
+
data.tar.gz: 04ac19428d41ec7102c40f7906483b624f1c776b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1cae7f9f66f6db08762573726408bf48ec5133e59e67a27f75c757a5d2c03f53c173303714c639d8b5033638a8a1af1ac55db729a7188660550249d33738d301
|
7
|
+
data.tar.gz: bc8f8b9857b4e64168da542135aaab693bab4105b0ed28ddf06d2f8d3d3bdd0d9c3f6c8cc61bab214eeafd8e26e0f36e358d5b26dd6abe19e6f16f588e9c1060
|
data/CHANGES
CHANGED
data/README.md
CHANGED
@@ -36,6 +36,8 @@ document.add_paragraph("Simple string of text and", document.text("some formatte
|
|
36
36
|
document.save
|
37
37
|
```
|
38
38
|
|
39
|
+
You can see more examples in the `examples` directory.
|
40
|
+
|
39
41
|
## Contributing
|
40
42
|
|
41
43
|
1. Fork it
|
@@ -43,3 +45,5 @@ document.save
|
|
43
45
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
44
46
|
4. Push to the branch (`git push origin my-new-feature`)
|
45
47
|
5. Create new Pull Request
|
48
|
+
|
49
|
+
[](http://githalytics.com/magicienap/docx_generator)
|
Binary file
|
data/examples/basic_paragraph.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'docx_generator'
|
2
2
|
|
3
3
|
document = DocxGenerator::Document.new("basic_paragraph")
|
4
|
-
document.add_paragraph("
|
4
|
+
document.add_paragraph(document.text("Title", underline: { style: "double" }, size: 20), alignment: "center")
|
5
|
+
document.add_paragraph("Simple string of text and", document.text("some formatted text", bold: true, italics: true, underline: { style: "single" }))
|
5
6
|
document.add_paragraph(document.text("Antoine", bold: true), "How are you today?")
|
6
7
|
document.add_paragraph(document.text("John", bold: true), document.text("(whispering)", bold: true, italics: true), "How are you today?")
|
7
8
|
document.save
|
@@ -24,12 +24,24 @@ module DocxGenerator
|
|
24
24
|
# text_fragments : Word::Text or String
|
25
25
|
# Later: Paragraph Object, with Text Object
|
26
26
|
def add_paragraph(*text_fragments)
|
27
|
+
arguments = {}
|
27
28
|
content = []
|
28
29
|
text_fragments.each do |text_fragment|
|
29
|
-
|
30
|
-
|
30
|
+
if text_fragment.respond_to?(:keys)
|
31
|
+
arguments = text_fragment
|
32
|
+
else
|
33
|
+
content.push(text_fragment.respond_to?(:generate) ? text_fragment : text(text_fragment))
|
34
|
+
content.push Word::Extensions.space
|
35
|
+
end
|
31
36
|
end
|
32
37
|
content.pop
|
38
|
+
if arguments.length != 0
|
39
|
+
options = []
|
40
|
+
arguments.each do |option, value|
|
41
|
+
options.push parse_paragraph_option(option, value)
|
42
|
+
end
|
43
|
+
content.unshift Word::ParagraphProperties.new({}, options)
|
44
|
+
end
|
33
45
|
@content.push Word::Paragraph.new({}, content)
|
34
46
|
self
|
35
47
|
end
|
@@ -39,7 +51,7 @@ module DocxGenerator
|
|
39
51
|
if arguments.length != 0
|
40
52
|
options = []
|
41
53
|
arguments.each do |option, value|
|
42
|
-
options.push
|
54
|
+
options.push parse_text_option(option, value)
|
43
55
|
end
|
44
56
|
content.push Word::RunProperties.new({}, options)
|
45
57
|
end
|
@@ -75,10 +87,18 @@ EOF
|
|
75
87
|
[ Word::Body.new({}, @content) ]).to_s
|
76
88
|
end
|
77
89
|
|
78
|
-
def
|
90
|
+
def parse_text_option(option, value)
|
79
91
|
case option
|
80
92
|
when :bold then Word::Bold.new(value)
|
81
93
|
when :italics then Word::Italics.new(value)
|
94
|
+
when :underline then Word::Underline.new(value)
|
95
|
+
when :size then Word::Size.new(value)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def parse_paragraph_option(option, value)
|
100
|
+
case option
|
101
|
+
when :alignment then Word::Alignment.new(value)
|
82
102
|
end
|
83
103
|
end
|
84
104
|
end
|
@@ -17,6 +17,12 @@ module DocxGenerator
|
|
17
17
|
super("w:p", attributes, content)
|
18
18
|
end
|
19
19
|
end
|
20
|
+
|
21
|
+
class ParagraphProperties < Element
|
22
|
+
def initialize(attributes = {}, content = [])
|
23
|
+
super("w:pPr", attributes, content)
|
24
|
+
end
|
25
|
+
end
|
20
26
|
|
21
27
|
class Run < Element
|
22
28
|
def initialize(attributes = {}, content = [])
|
@@ -13,5 +13,31 @@ module DocxGenerator
|
|
13
13
|
super("w:i", arguments)
|
14
14
|
end
|
15
15
|
end
|
16
|
+
|
17
|
+
class Underline < Element
|
18
|
+
def initialize(arguments = { "w:val" => "single" })
|
19
|
+
final_arguments = {}
|
20
|
+
arguments.each do |option, value|
|
21
|
+
case option
|
22
|
+
when :style then final_arguments["w:val"] = value
|
23
|
+
else final_arguments[option] = value
|
24
|
+
end
|
25
|
+
end
|
26
|
+
super("w:u", final_arguments)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class Size < Element
|
31
|
+
# size : The font size in points
|
32
|
+
def initialize(size)
|
33
|
+
super("w:sz", { "w:val" => size*2 })
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class Alignment < Element
|
38
|
+
def initialize(value)
|
39
|
+
super("w:jc", { "w:val" => value })
|
40
|
+
end
|
41
|
+
end
|
16
42
|
end
|
17
43
|
end
|
@@ -62,6 +62,13 @@ describe DocxGenerator::Document do
|
|
62
62
|
it "should return the current document" do
|
63
63
|
document.add_paragraph(["The first characters", "and the last ones."]).should be(document)
|
64
64
|
end
|
65
|
+
|
66
|
+
context "with styles" do
|
67
|
+
it "should align the paragraph" do
|
68
|
+
document.add_paragraph(document.text("The first characters"), "and the last ones.", alignment: "center").save
|
69
|
+
open_file("word/document.xml").should include("<w:p><w:pPr><w:jc w:val=\"center\" /></w:pPr><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>")
|
70
|
+
end
|
71
|
+
end
|
65
72
|
end
|
66
73
|
|
67
74
|
describe "#text" do
|
@@ -77,6 +84,14 @@ describe DocxGenerator::Document do
|
|
77
84
|
it "should return a text in italics" do
|
78
85
|
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
86
|
end
|
87
|
+
|
88
|
+
it "should return an underlined text" do
|
89
|
+
DocxGenerator::Document.new("word").text("Text", underline: { style: "single" }).to_s.should eq("<w:r><w:rPr><w:u w:val=\"single\" /></w:rPr><w:t>Text</w:t></w:r>")
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should return a text with a font size" do
|
93
|
+
DocxGenerator::Document.new("word").text("Text", size: 20).to_s.should eq("<w:r><w:rPr><w:sz w:val=\"40\" /></w:rPr><w:t>Text</w:t></w:r>")
|
94
|
+
end
|
80
95
|
end
|
81
96
|
end
|
82
97
|
end
|
@@ -22,6 +22,13 @@ describe DocxGenerator::Word::Paragraph do
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
+
describe DocxGenerator::Word::ParagraphProperties do
|
26
|
+
it "should render a w:pPr element" do
|
27
|
+
DocxGenerator::Word::ParagraphProperties.new.to_s.should eq("<w:pPr />")
|
28
|
+
DocxGenerator::Word::ParagraphProperties.new({}, ["Text"]).to_s.should eq("<w:pPr>Text</w:pPr>")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
25
32
|
describe DocxGenerator::Word::Run do
|
26
33
|
it "should render a w:r element" do
|
27
34
|
DocxGenerator::Word::Run.new.to_s.should eq("<w:r />")
|
@@ -15,3 +15,22 @@ describe DocxGenerator::Word::Italics do
|
|
15
15
|
DocxGenerator::Word::Italics.new(false).to_s.should eq("<w:i w:val=\"false\" />")
|
16
16
|
end
|
17
17
|
end
|
18
|
+
|
19
|
+
describe DocxGenerator::Word::Underline do
|
20
|
+
it "should render a w:u element" do
|
21
|
+
DocxGenerator::Word::Underline.new.to_s.should eq("<w:u w:val=\"single\" />")
|
22
|
+
DocxGenerator::Word::Underline.new(style: "double").to_s.should eq("<w:u w:val=\"double\" />")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe DocxGenerator::Word::Size do
|
27
|
+
it "should render a w:sz element" do
|
28
|
+
DocxGenerator::Word::Size.new(20).to_s.should eq("<w:sz w:val=\"40\" />")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe DocxGenerator::Word::Alignment do
|
33
|
+
it "should render a w:jc element" do
|
34
|
+
DocxGenerator::Word::Alignment.new("center").to_s.should eq("<w:jc w:val=\"center\" />")
|
35
|
+
end
|
36
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: docx_generator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Antoine Proulx
|
@@ -137,6 +137,7 @@ files:
|
|
137
137
|
- LICENSE.txt
|
138
138
|
- README.md
|
139
139
|
- Rakefile
|
140
|
+
- basic_paragraph.docx
|
140
141
|
- docx_generator.gemspec
|
141
142
|
- examples/basic_paragraph.rb
|
142
143
|
- lib/docx_generator.rb
|