docx_generator 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0e7df426c133c37b61b38793acb9170ea8a679c1
4
- data.tar.gz: 280949c5112c2173d160605878d8166b563eae30
3
+ metadata.gz: 24ad7dac74a46bba80074d78c61c3036b3edc4b0
4
+ data.tar.gz: 04ac19428d41ec7102c40f7906483b624f1c776b
5
5
  SHA512:
6
- metadata.gz: 53c4c1b3963c15d5cbd56025f38d0ff2b25e3ac2744f01d148d64db0fa7effe51b37e591be3d73a777fe035aa9c581b0d03603bcecc062d79664c1682ad019b7
7
- data.tar.gz: 020ccf60bc99bc5a032fd7ab7f5ed31a0372572483d484086f736ceb926bdffce209c81451e6208960049aa3b66f73113492b03a55ea1cd01666a7a70b7d0e9e
6
+ metadata.gz: 1cae7f9f66f6db08762573726408bf48ec5133e59e67a27f75c757a5d2c03f53c173303714c639d8b5033638a8a1af1ac55db729a7188660550249d33738d301
7
+ data.tar.gz: bc8f8b9857b4e64168da542135aaab693bab4105b0ed28ddf06d2f8d3d3bdd0d9c3f6c8cc61bab214eeafd8e26e0f36e358d5b26dd6abe19e6f16f588e9c1060
data/CHANGES CHANGED
@@ -1,3 +1,9 @@
1
+ # Version 0.0.2
2
+
3
+ - Underline text
4
+ - Change the font size
5
+ - Align paragraphs
6
+
1
7
  # Version 0.0.1 (2013-05-11)
2
8
 
3
9
  Initial version
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
+ [![githalytics.com alpha](https://cruel-carlota.pagodabox.com/557c5048ed09993a48f60e0aa0869ab3 "githalytics.com")](http://githalytics.com/magicienap/docx_generator)
Binary file
@@ -1,7 +1,8 @@
1
1
  require 'docx_generator'
2
2
 
3
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))
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
- content.push(text_fragment.respond_to?(:generate) ? text_fragment : text(text_fragment))
30
- content.push Word::Extensions.space
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 parse_option(option, value)
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 parse_option(option, value)
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
@@ -1,3 +1,3 @@
1
1
  module DocxGenerator
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  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.1
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