docx_generator 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,52 +1,52 @@
1
- module DocxGenerator
2
-
3
- # Represent an XML element. This class should not be used directly by the users of the library.
4
- class Element
5
-
6
- # Create a new XML element.
7
- # @param name [String] The name of the XML element.
8
- # @param attributes [Hash] The attributes of the XML element.
9
- # @param content [Array] An array of the children of the XML element (other XML elements).
10
- def initialize(name, attributes = {}, content = [])
11
- @name = name
12
- @attributes = attributes
13
- @content = content
14
- end
15
-
16
- # Add an XML element in the content of this XML element.
17
- # @param element[DocxGenerator::Element] The XML element to add.
18
- def add(element)
19
- @content << element
20
- end
21
-
22
- # Generate the XML for the element.
23
- # @return [String] The XML produced for the element.
24
- def generate
25
- output = ""
26
- if @content.length != 0
27
- output += "<#{@name}#{generate_attributes}>"
28
- @content.each do |element|
29
- if element.respond_to?(:generate)
30
- output += element.generate
31
- else
32
- output += element.to_s
33
- end
34
- end
35
- output += "</#{@name}>"
36
- else
37
- output += "<#{@name}#{generate_attributes} />"
38
- end
39
- output
40
- end
41
- alias :to_s :generate
42
-
43
- private
44
- def generate_attributes
45
- output = ""
46
- @attributes.each do |name, value|
47
- output += " #{name}=\"#{value}\""
48
- end
49
- output
50
- end
51
- end
52
- end
1
+ module DocxGenerator
2
+
3
+ # Represent an XML element. This class should not be used directly by the users of the library.
4
+ class Element
5
+
6
+ # Create a new XML element.
7
+ # @param name [String] The name of the XML element.
8
+ # @param attributes [Hash] The attributes of the XML element.
9
+ # @param content [Array] An array of the children of the XML element (other XML elements).
10
+ def initialize(name, attributes = {}, content = [])
11
+ @name = name
12
+ @attributes = attributes
13
+ @content = content
14
+ end
15
+
16
+ # Add an XML element in the content of this XML element.
17
+ # @param element[DocxGenerator::Element] The XML element to add.
18
+ def add(element)
19
+ @content << element
20
+ end
21
+
22
+ # Generate the XML for the element.
23
+ # @return [String] The XML produced for the element.
24
+ def generate
25
+ output = ""
26
+ if @content.length != 0
27
+ output += "<#{@name}#{generate_attributes}>"
28
+ @content.each do |element|
29
+ if element.respond_to?(:generate)
30
+ output += element.generate
31
+ else
32
+ output += element.to_s
33
+ end
34
+ end
35
+ output += "</#{@name}>"
36
+ else
37
+ output += "<#{@name}#{generate_attributes} />"
38
+ end
39
+ output
40
+ end
41
+ alias :to_s :generate
42
+
43
+ private
44
+ def generate_attributes
45
+ output = ""
46
+ @attributes.each do |name, value|
47
+ output += " #{name}=\"#{value}\""
48
+ end
49
+ output
50
+ end
51
+ end
52
+ end
@@ -1,4 +1,4 @@
1
- module DocxGenerator
2
- # Version of DocxGenerator.
3
- VERSION = "0.1.0"
4
- end
1
+ module DocxGenerator
2
+ # Version of DocxGenerator.
3
+ VERSION = "0.1.1"
4
+ end
@@ -1,9 +1,9 @@
1
- require 'docx_generator/word/base'
2
- require 'docx_generator/word/extensions'
3
- require 'docx_generator/word/formatting'
4
-
5
- module DocxGenerator
6
- # Namesapce for all XML Elements defined in the standard.
7
- module Word
8
- end
1
+ require 'docx_generator/word/base'
2
+ require 'docx_generator/word/extensions'
3
+ require 'docx_generator/word/formatting'
4
+
5
+ module DocxGenerator
6
+ # Namesapce for all XML Elements defined in the standard.
7
+ module Word
8
+ end
9
9
  end
@@ -1,82 +1,83 @@
1
- module DocxGenerator
2
- module Word
3
- # Represent the `w:document` element from Office Open XML specification. This class should not be used directly by the users of the library.
4
- class Document < Element
5
- # Create a new `w:document` element.
6
- # @param attributes [Hash] The attributes of the XML element.
7
- # In the case of the `w:document` element, the only attribute should be `w:conformance`. If ommited, the value is `transitional`. If not, the two accepted values are `strict` and `transitional`.
8
- # @param content [Array] An array of the children of the XML element (other XML elements).
9
- def initialize(attributes = {}, content = [])
10
- super("w:document", attributes, content)
11
- end
12
- end
13
-
14
- # Represent the `w:body` element from Office Open XML specification. This class should not be used directly by the users of the library.
15
- class Body < Element
16
- # Create a new `w:body` XML element.
17
- # @param content [Array] An array of the children of the XML element (other XML elements).
18
- def initialize(content = [])
19
- super("w:body", {}, content)
20
- end
21
- end
22
-
23
- # Represent the `w:p` element from Office Open XML specification. This class should not be used directly by the users of the library.
24
- class Paragraph < Element
25
- # Create a new `w:p` element.
26
- # @param attributes [Hash] The attributes of the XML element. Check the specification of the `w:p` element for the possible attributes.
27
- # @param content [Array] An array of the children of the XML element (other XML elements).
28
- def initialize(attributes = {}, content = [])
29
- super("w:p", attributes, content)
30
- end
31
- end
32
-
33
- # Represent the `w:pPr` element from Office Open XML specification. This class should not be used directly by the users of the library.
34
- class ParagraphProperties < Element
35
- # Create a new `w:pPr` XML element.
36
- # @param content [Array] An array of the children of the XML element (other XML elements).
37
- def initialize(content = [])
38
- super("w:pPr", {}, content)
39
- end
40
- end
41
-
42
- # Represent the `w:r` element from Office Open XML specification. This class should not be used directly by the users of the library.
43
- class Run < Element
44
- # Create a new `w:r` element.
45
- # @param attributes [Hash] The attributes of the XML element. Check the specification of the `w:r` element for the possible attributes.
46
- # @param content [Array] An array of the children of the XML element (other XML elements).
47
- def initialize(attributes = {}, content = [])
48
- super("w:r", attributes, content)
49
- end
50
- end
51
-
52
- # Represent the `w:rPr` element from Office Open XML specification. This class should not be used directly by the users of the library.
53
- class RunProperties < Element
54
- # Create a new `w:rPr` XML element.
55
- # @param content [Array] An array of the children of the XML element (other XML elements).
56
- def initialize(content = [])
57
- super("w:rPr", {}, content)
58
- end
59
- end
60
-
61
- # Represent the `w:t` element from Office Open XML specification. This class should not be used directly by the users of the library.
62
- class Text < Element
63
- # Create a new `w:t` element.
64
- # @param attributes [Hash] The attributes of the XML element.
65
- # In the case of the `w:t` element, the only attribute should be `xml:space`. It means that the content contains significant whitespace.
66
- # @param content [Array] An array of the children of the XML element (other XML elements).
67
- def initialize(attributes = {}, content = [])
68
- super("w:t", attributes, content)
69
- end
70
- end
71
-
72
- # Represent the `w:br` element from Office Open XML specification. This class should not be used directly by the users of the library.
73
- class Break < Element
74
- # Create a new `w:br` element.
75
- # @param attributes [Hash] The attributes of the XML element. Check the specification of the `w:br` element for the possible attributes.
76
- # @param content [Array] An array of the children of the XML element (other XML elements).
77
- def initialize(attributes = {})
78
- super("w:br", attributes)
79
- end
80
- end
81
- end
82
- end
1
+ # Warning: This file has been automatically generated from generator/word_base.rb.
2
+ # It should not be edited by hand. Instead, modify the code generator.
3
+
4
+ module DocxGenerator
5
+ module Word
6
+ # Represent the `w:document` element from Office Open XML specification. This class should not be used directly by the users of the library.
7
+ class Document < Element
8
+ # Create a new `w:document` XML element.
9
+ # @param attributes [Hash] The attributes of the XML element. Check the specification of the `w:document` element for the possible attributes.
10
+ # @param content [Array] An array of the children of the XML element (other XML elements).
11
+ def initialize(attributes = {}, content = [])
12
+ super("w:document", attributes, content)
13
+ end
14
+ end
15
+
16
+ # Represent the `w:body` element from Office Open XML specification. This class should not be used directly by the users of the library.
17
+ class Body < Element
18
+ # Create a new `w:body` XML element.
19
+ # @param content [Array] An array of the children of the XML element (other XML elements).
20
+ def initialize(content = [])
21
+ super("w:body", {}, content)
22
+ end
23
+ end
24
+
25
+ # Represent the `w:p` element from Office Open XML specification. This class should not be used directly by the users of the library.
26
+ class Paragraph < Element
27
+ # Create a new `w:p` XML element.
28
+ # @param attributes [Hash] The attributes of the XML element. Check the specification of the `w:p` element for the possible attributes.
29
+ # @param content [Array] An array of the children of the XML element (other XML elements).
30
+ def initialize(attributes = {}, content = [])
31
+ super("w:p", attributes, content)
32
+ end
33
+ end
34
+
35
+ # Represent the `w:pPr` element from Office Open XML specification. This class should not be used directly by the users of the library.
36
+ class ParagraphProperties < Element
37
+ # Create a new `w:pPr` XML element.
38
+ # @param content [Array] An array of the children of the XML element (other XML elements).
39
+ def initialize(content = [])
40
+ super("w:pPr", {}, content)
41
+ end
42
+ end
43
+
44
+ # Represent the `w:r` element from Office Open XML specification. This class should not be used directly by the users of the library.
45
+ class Run < Element
46
+ # Create a new `w:r` XML element.
47
+ # @param attributes [Hash] The attributes of the XML element. Check the specification of the `w:r` element for the possible attributes.
48
+ # @param content [Array] An array of the children of the XML element (other XML elements).
49
+ def initialize(attributes = {}, content = [])
50
+ super("w:r", attributes, content)
51
+ end
52
+ end
53
+
54
+ # Represent the `w:rPr` element from Office Open XML specification. This class should not be used directly by the users of the library.
55
+ class RunProperties < Element
56
+ # Create a new `w:rPr` XML element.
57
+ # @param content [Array] An array of the children of the XML element (other XML elements).
58
+ def initialize(content = [])
59
+ super("w:rPr", {}, content)
60
+ end
61
+ end
62
+
63
+ # Represent the `w:t` element from Office Open XML specification. This class should not be used directly by the users of the library.
64
+ class Text < Element
65
+ # Create a new `w:t` XML element.
66
+ # @param attributes [Hash] The attributes of the XML element. Check the specification of the `w:t` element for the possible attributes.
67
+ # @param content [Array] An array of the children of the XML element (other XML elements).
68
+ def initialize(attributes = {}, content = [])
69
+ super("w:t", attributes, content)
70
+ end
71
+ end
72
+
73
+ # Represent the `w:br` element from Office Open XML specification. This class should not be used directly by the users of the library.
74
+ class Break < Element
75
+ # Create a new `w:br` XML element.
76
+ # @param attributes [Hash] The attributes of the XML element. Check the specification of the `w:br` element for the possible attributes.
77
+ def initialize(attributes = {})
78
+ super("w:br", attributes)
79
+ end
80
+ end
81
+
82
+ end
83
+ end
@@ -1,18 +1,30 @@
1
- module DocxGenerator
2
- module Word
3
-
4
- # Extensions to create some common elements easily.
5
- module Extensions
6
- def self.space
7
- DocxGenerator::Word::Run.new({}, [DocxGenerator::Word::Text.new({ "xml:space" => "preserve" }, [" "])])
8
- end
9
-
10
- def self.newline
11
- DocxGenerator::Word::Run.new({}, [DocxGenerator::Word::Break.new])
12
- end
13
-
14
- class NoSpace
15
- end
16
- end
17
- end
18
- end
1
+ module DocxGenerator
2
+ module Word
3
+
4
+ # Extensions to create some common elements easily.
5
+ module Extensions
6
+ def self.space
7
+ DocxGenerator::Word::Run.new({}, [DocxGenerator::Word::Text.new({ "xml:space" => "preserve" }, [" "])])
8
+ end
9
+
10
+ def self.newline
11
+ DocxGenerator::Word::Run.new({}, [DocxGenerator::Word::Break.new])
12
+ end
13
+
14
+ class Newline
15
+ def generate
16
+ DocxGenerator::Word::Run.new({}, [DocxGenerator::Word::Break.new])
17
+ end
18
+ end
19
+
20
+ class Tab
21
+ def generate
22
+ DocxGenerator::Word::Run.new({}, [DocxGenerator::Word::Tab.new])
23
+ end
24
+ end
25
+
26
+ class NoSpace
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,58 +1,162 @@
1
- module DocxGenerator
2
- module Word
3
- # Represent the `w:b` element from Office Open XML specification. This class should not be used directly by the users of the library.
4
- class Bold < Element
5
- # Create a new `w:b` element.
6
- # @param present [Boolean] If bold should be applied to the text.
7
- def initialize(present = nil)
8
- arguments = (present == nil ? {} : { "w:val" => present })
9
- super("w:b", arguments)
10
- end
11
- end
12
-
13
- # Represent the `w:i` element from Office Open XML specification. This class should not be used directly by the users of the library.
14
- class Italics < Element
15
- # Create a new `w:i` element.
16
- # @param present [Boolean] If italics should be applied to the text.
17
- def initialize(present = nil)
18
- arguments = (present == nil ? {} : { "w:val" => present })
19
- super("w:i", arguments)
20
- end
21
- end
22
-
23
- # Represent the `w:u` element from Office Open XML specification. This class should not be used directly by the users of the library.
24
- class Underline < Element
25
- # Create a new `w:u` element.
26
- # @param arguments [Hash] Arguments for the `w:u` element. See the full list in the specification. You can use `style` instead of `w:val` if you want. The list of all underline patterns can be found in the specification.
27
- def initialize(arguments = { "w:val" => "single" })
28
- final_arguments = {}
29
- arguments.each do |option, value|
30
- case option
31
- when :style then final_arguments["w:val"] = value
32
- else final_arguments[option] = value
33
- end
34
- end
35
- super("w:u", final_arguments)
36
- end
37
- end
38
-
39
- class Size < Element
40
- # size : The font size in points
41
- def initialize(size)
42
- super("w:sz", { "w:val" => size*2 })
43
- end
44
- end
45
-
46
- class Alignment < Element
47
- def initialize(value)
48
- super("w:jc", { "w:val" => value })
49
- end
50
- end
51
-
52
- class VerticalAlign < Element
53
- def initialize(value)
54
- super("w:vertAlign", { "w:val" => value })
55
- end
56
- end
57
- end
58
- end
1
+ # This file should be standarized. All the parameters should be named parameters (or maybe only those who take a Hash).
2
+ # All they do is arguments tranformations and they document possible arguments from the specification.
3
+
4
+ module DocxGenerator
5
+ module Word
6
+ # Represent the `w:b` element from Office Open XML specification. This class should not be used directly by the users of the library.
7
+ class Bold < Element
8
+ # Create a new `w:b` element.
9
+ # @param present [Boolean] If bold should be applied to the text.
10
+ def initialize(present = nil)
11
+ arguments = (present == nil ? {} : { "w:val" => present })
12
+ super("w:b", arguments)
13
+ end
14
+ end
15
+
16
+ # Represent the `w:i` element from Office Open XML specification. This class should not be used directly by the users of the library.
17
+ class Italics < Element
18
+ # Create a new `w:i` element.
19
+ # @param present [Boolean] If italics should be applied to the text.
20
+ def initialize(present = nil)
21
+ arguments = (present == nil ? {} : { "w:val" => present })
22
+ super("w:i", arguments)
23
+ end
24
+ end
25
+
26
+ # Represent the `w:u` element from Office Open XML specification. This class should not be used directly by the users of the library.
27
+ class Underline < Element
28
+ # Create a new `w:u` element.
29
+ # @param arguments [Hash] Arguments for the `w:u` element. See the full list in the specification. You can use `style` instead of `w:val` if you want. The list of all underline patterns can be found in the specification.
30
+ def initialize(arguments = { "w:val" => "single" })
31
+ final_arguments = {}
32
+ arguments.each do |option, value|
33
+ case option
34
+ when :style then final_arguments["w:val"] = value
35
+ else final_arguments[option] = value
36
+ end
37
+ end
38
+ super("w:u", final_arguments)
39
+ end
40
+ end
41
+
42
+ # Represent the `w:sz` element from Office Open XML specification. This class should not be used directly by the users of the library.
43
+ class Size < Element
44
+ # Create a new `w:sz` element.
45
+ # @param size [Number] The size of the text (in points)
46
+ def initialize(size)
47
+ super("w:sz", { "w:val" => size*2 })
48
+ end
49
+ end
50
+
51
+ # Represent the `w:jc` element from Office Open XML specification. This class should not be used directly by the users of the library.
52
+ class Alignment < Element
53
+ # Create a new `w:jc` element.
54
+ # @param value [String] The type of alignment. The list of all types of alignment can be found in the specification.
55
+ def initialize(value)
56
+ super("w:jc", { "w:val" => value })
57
+ end
58
+ end
59
+
60
+ # Represent the `w:spacing` element from Office Open XML specification. This class should not be used directly by the users of the library.
61
+ class Spacing < Element
62
+ # Create a new `w:spacing` element.
63
+ # @param arguments [Hash] The different options for the spacing. They can be found in the specification.
64
+ def initialize(arguments = {})
65
+ final_arguments = {}
66
+ arguments.each do |name, value|
67
+ if name.to_s == "after" || name.to_s == "before"
68
+ final_arguments["w:" + name.to_s] = (value * 20).round
69
+ else
70
+ final_arguments["w:" + name.to_s] = (value * 20 * 12).round
71
+ final_arguments["w:lineRule"] = "auto"
72
+ end
73
+ end
74
+ super("w:spacing", final_arguments)
75
+ end
76
+ end
77
+
78
+ # Represent the `w:indent` element from Office Open XML specification. This class should not be used directly by the users of the library.
79
+ class Indentation < Element
80
+ # Create a new `w:indent` element.
81
+ # @param properties [Hash] The different properties for the indentation. They can be found in the specification.
82
+ def initialize(arguments = {})
83
+ final_arguments = {}
84
+ arguments.each do |name, value|
85
+ if name.to_s == "first_line"
86
+ final_arguments["w:firstLine"] = (value * 20).round
87
+ else
88
+ final_arguments["w:" + name.to_s] = (value * 20).round
89
+ end
90
+ end
91
+ super("w:ind", final_arguments)
92
+ end
93
+ end
94
+
95
+ # Represent the `w:tabs` element from Office Open XML specification. This class should not be used directly by the users of the library.
96
+ class Tabs < Element
97
+ # Create a new `w:tabs` element.
98
+ # @param tab_stops [Array] The tab stops.
99
+ def initialize(tab_stops)
100
+ tab_stop_elements = tab_stops.inject([]) do |tab_stop_elements, tab_stop|
101
+ tab_stop_elements << Tab.new(tab_stop)
102
+ end
103
+ super("w:tabs", {}, tab_stop_elements)
104
+ end
105
+ end
106
+
107
+ # Represent the `w:tab` element from Office Open XML specification. This class should not be used directly by the users of the library.
108
+ class Tab < Element
109
+ # Create a new `w:tab` element.
110
+ # @param options [Hash] Options for the tab stop. Must be `leader`, `pos` or `val`. See the specification for the possible values for each option.
111
+ def initialize(options = {})
112
+ final_arguments = {}
113
+ options.each do |name, value|
114
+ if name.to_s == "pos"
115
+ final_arguments["w:pos"] = (value * 20).round
116
+ else
117
+ final_arguments["w:" + name.to_s] = value
118
+ end
119
+ end
120
+ super("w:tab", final_arguments)
121
+ end
122
+ end
123
+
124
+ # Represent the `w:vertAlign` element from Office Open XML specification. This class should not be used directly by the users of the library.
125
+ class VerticalAlign < Element
126
+ # Create a new `w:vertAlign` element.
127
+ # @param value [String] The type of alignment. It should be `baseline`, `subscript` or `superscript`.
128
+ def initialize(value)
129
+ super("w:vertAlign", { "w:val" => value })
130
+ end
131
+ end
132
+
133
+ # Represent the `w:caps` element from Office Open XML specification. This class should not be used directly by the users of the library.
134
+ class CapitalLetters < Element
135
+ # Create a new `w:caps` element.
136
+ # @param present [Boolean] If the text should be displayed in capital letters.
137
+ def initialize(present = nil)
138
+ arguments = (present == nil ? {} : { "w:val" => present })
139
+ super("w:caps", arguments)
140
+ end
141
+ end
142
+
143
+ # Represent the `w:smallCaps` element from Office Open XML specification. This class should not be used directly by the users of the library.
144
+ class SmallCapitalLetters < Element
145
+ # Create a new `w:smallCaps` element.
146
+ # @param present [Boolean] If the text should be displayed in small capital letters.
147
+ def initialize(present = nil)
148
+ arguments = (present == nil ? {} : { "w:val" => present })
149
+ super("w:smallCaps", arguments)
150
+ end
151
+ end
152
+
153
+ # Represent the `w:rFonts` element from Office Open XML specification. This class should not be used directly by the users of the library.
154
+ class Font < Element
155
+ # Create a new `w:rFonts` element.
156
+ # @param name [String] The name of the font.
157
+ def initialize(name)
158
+ super("w:rFonts", { "w:ascii" => name })
159
+ end
160
+ end
161
+ end
162
+ end