docxi 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.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +22 -0
  3. data/README.md +119 -0
  4. data/Rakefile +10 -0
  5. data/lib/docxer.rb +29 -0
  6. data/lib/docxer/content_types.rb +68 -0
  7. data/lib/docxer/content_types/default.rb +18 -0
  8. data/lib/docxer/content_types/override.rb +18 -0
  9. data/lib/docxer/document.rb +36 -0
  10. data/lib/docxer/properties.rb +29 -0
  11. data/lib/docxer/properties/app.rb +37 -0
  12. data/lib/docxer/properties/core.rb +32 -0
  13. data/lib/docxer/relationships.rb +49 -0
  14. data/lib/docxer/relationships/relationship.rb +19 -0
  15. data/lib/docxer/version.rb +3 -0
  16. data/lib/docxer/word.rb +16 -0
  17. data/lib/docxer/word/contents.rb +6 -0
  18. data/lib/docxer/word/contents/break.rb +33 -0
  19. data/lib/docxer/word/contents/image.rb +38 -0
  20. data/lib/docxer/word/contents/paragraph.rb +59 -0
  21. data/lib/docxer/word/contents/table.rb +223 -0
  22. data/lib/docxer/word/contents/table_of_content.rb +75 -0
  23. data/lib/docxer/word/contents/text.rb +34 -0
  24. data/lib/docxer/word/document.rb +144 -0
  25. data/lib/docxer/word/effects.rb +210 -0
  26. data/lib/docxer/word/endnotes.rb +39 -0
  27. data/lib/docxer/word/fonts.rb +42 -0
  28. data/lib/docxer/word/fonts/font.rb +25 -0
  29. data/lib/docxer/word/footers.rb +30 -0
  30. data/lib/docxer/word/footers/footer.rb +118 -0
  31. data/lib/docxer/word/footnotes.rb +40 -0
  32. data/lib/docxer/word/headers.rb +30 -0
  33. data/lib/docxer/word/headers/header.rb +142 -0
  34. data/lib/docxer/word/helpers.rb +37 -0
  35. data/lib/docxer/word/medias.rb +31 -0
  36. data/lib/docxer/word/medias/media.rb +37 -0
  37. data/lib/docxer/word/numbering.rb +141 -0
  38. data/lib/docxer/word/relationships.rb +57 -0
  39. data/lib/docxer/word/relationships/relationship.rb +21 -0
  40. data/lib/docxer/word/settings.rb +61 -0
  41. data/lib/docxer/word/styles.rb +235 -0
  42. data/lib/docxer/word/themes.rb +30 -0
  43. data/lib/docxer/word/themes/theme.rb +313 -0
  44. data/lib/docxer/word/web_settings.rb +29 -0
  45. metadata +120 -0
@@ -0,0 +1,25 @@
1
+ module Docxer
2
+ module Word
3
+ class Fonts
4
+ class Font
5
+
6
+ attr_accessor :name, :options
7
+ def initialize(name, options)
8
+ @name = name
9
+ @options = options
10
+ end
11
+
12
+ def build(xml)
13
+ xml['w'].font('w:name' => @name) do
14
+ xml['w'].panose1('w:val' => @options[:panose] )
15
+ xml['w'].charset('w:val' => @options[:charset] )
16
+ xml['w'].family('w:val' => @options[:family] )
17
+ xml['w'].pitch('w:val' => @options[:pitch] )
18
+ xml['w'].sig( @options[:sig] )
19
+ end
20
+ end
21
+
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,30 @@
1
+ require 'docxer/word/footers/footer'
2
+
3
+ module Docxer
4
+ module Word
5
+ class Footers
6
+
7
+ attr_accessor :footers, :counter
8
+ def initialize
9
+ @footers = []
10
+ @counter = 0
11
+ end
12
+
13
+ def add(footer)
14
+ @footers << footer
15
+ footer
16
+ end
17
+
18
+ def sequence
19
+ @counter += 1
20
+ end
21
+
22
+ def render(zip)
23
+ @footers.each do |footer|
24
+ footer.render(zip)
25
+ end
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,118 @@
1
+ #encoding: utf-8
2
+
3
+ module Docxer
4
+ module Word
5
+ class Footers
6
+ class Footer
7
+
8
+ attr_accessor :id, :sequence, :options, :content, :relationships
9
+ def initialize(options={})
10
+ @options = options
11
+ @content = []
12
+ @relationships = []
13
+
14
+ if block_given?
15
+ yield self
16
+ else
17
+
18
+ end
19
+ end
20
+
21
+ def content_type
22
+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer"
23
+ end
24
+
25
+ def target
26
+ "footer#{id}.xml"
27
+ end
28
+
29
+ def render(zip)
30
+ zip.put_next_entry("word/#{target}")
31
+ zip.write(Docxer.to_xml(document))
32
+
33
+ if !@relationships.empty?
34
+ zip.put_next_entry("word/_rels/#{target}.rels")
35
+ zip.write(Docxer.to_xml(relationships))
36
+ end
37
+ end
38
+
39
+ def text(text, options={})
40
+ options = @options.merge(options)
41
+ element = Docxer::Word::Contents::Paragraph.new(options) do |p|
42
+ p.text(text)
43
+ end
44
+ @content << element
45
+ element
46
+ end
47
+
48
+ def page_numbers
49
+ numbers = PageNumbers.new
50
+ @content << numbers
51
+ numbers
52
+ end
53
+
54
+ class PageNumbers
55
+
56
+ attr_accessor :options
57
+ def initialize(options={})
58
+ @options = options
59
+ end
60
+
61
+ def render(xml)
62
+ xml['w'].sdt do
63
+ xml['w'].sdtPr do
64
+ xml['w'].id( 'w:val' => "-472213903" )
65
+ xml['w'].docPartObj do
66
+ xml['w'].docPartGallery( 'w:val' => "Page Numbers (Bottom of Page)" )
67
+ xml['w'].docPartUnique
68
+ end
69
+ end
70
+ xml['w'].sdtContent do
71
+ xml['w'].p do
72
+ xml['w'].pPr do
73
+ xml['w'].jc( 'w:val' => @options[:align] || 'right' )
74
+ end
75
+ xml['w'].r do
76
+ xml['w'].fldChar( 'w:fldCharType' => "begin" )
77
+ end
78
+ xml['w'].r do
79
+ xml['w'].instrText "PAGE \* MERGEFORMAT"
80
+ end
81
+ xml['w'].r do
82
+ xml['w'].fldChar( 'w:fldCharType' => "separate" )
83
+ end
84
+ xml['w'].r do
85
+ xml['w'].fldChar( 'w:fldCharType' => "end" )
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+ private
94
+ def document
95
+ Nokogiri::XML::Builder.with(Nokogiri::XML('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>')) do |xml|
96
+ xml.ftr( 'xmlns:wpc' => "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas", 'xmlns:mc' => "http://schemas.openxmlformats.org/markup-compatibility/2006", 'xmlns:o' => "urn:schemas-microsoft-com:office:office", 'xmlns:r' => "http://schemas.openxmlformats.org/officeDocument/2006/relationships", 'xmlns:m' => "http://schemas.openxmlformats.org/officeDocument/2006/math", 'xmlns:v' => "urn:schemas-microsoft-com:vml", 'xmlns:wp14' => "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing", 'xmlns:wp' => "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", 'xmlns:w10' => "urn:schemas-microsoft-com:office:word", 'xmlns:w' => "http://schemas.openxmlformats.org/wordprocessingml/2006/main", 'xmlns:w14' => "http://schemas.microsoft.com/office/word/2010/wordml", 'xmlns:wpg' => "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup", 'xmlns:wpi' => "http://schemas.microsoft.com/office/word/2010/wordprocessingInk", 'xmlns:wne' => "http://schemas.microsoft.com/office/word/2006/wordml", 'xmlns:wps' => "http://schemas.microsoft.com/office/word/2010/wordprocessingShape", 'mc:Ignorable' => "w14 wp14" ) do
97
+ xml.parent.namespace = xml.parent.namespace_definitions.find{|ns| ns.prefix == "w" }
98
+ @content.each do |element|
99
+ element.render(xml)
100
+ end
101
+ end
102
+ end
103
+ end
104
+
105
+ def relationships
106
+ Nokogiri::XML::Builder.with(Nokogiri::XML('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>')) do |xml|
107
+ xml.Relationships(xmlns: 'http://schemas.openxmlformats.org/package/2006/relationships') do
108
+ @relationships.each do |relationship|
109
+ xml.Relationship('Id' => relationship.sequence, 'Type' => relationship.content_type, 'Target' => relationship.target)
110
+ end
111
+ end
112
+ end
113
+ end
114
+
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,40 @@
1
+ module Docxer
2
+ module Word
3
+ class Footnotes
4
+
5
+ attr_accessor :options
6
+ def initialize(options={})
7
+ @options = options
8
+ end
9
+
10
+ def render(zip)
11
+ zip.put_next_entry('word/footnotes.xml')
12
+ zip.write(Docxer.to_xml(document))
13
+ end
14
+
15
+ private
16
+ def document
17
+ Nokogiri::XML::Builder.with(Nokogiri::XML('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>')) do |xml|
18
+ xml.footnotes( 'xmlns:wpc' => "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas", 'xmlns:mc' => "http://schemas.openxmlformats.org/markup-compatibility/2006", 'xmlns:o' => "urn:schemas-microsoft-com:office:office", 'xmlns:r' => "http://schemas.openxmlformats.org/officeDocument/2006/relationships", 'xmlns:m' => "http://schemas.openxmlformats.org/officeDocument/2006/math", 'xmlns:v' => "urn:schemas-microsoft-com:vml", 'xmlns:wp14' => "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing", 'xmlns:wp' => "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", 'xmlns:w10' => "urn:schemas-microsoft-com:office:word", 'xmlns:w' => "http://schemas.openxmlformats.org/wordprocessingml/2006/main", 'xmlns:w14' => "http://schemas.microsoft.com/office/word/2010/wordml", 'xmlns:wpg' => "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup", 'xmlns:wpi' => "http://schemas.microsoft.com/office/word/2010/wordprocessingInk", 'xmlns:wne' => "http://schemas.microsoft.com/office/word/2006/wordml", 'xmlns:wps' => "http://schemas.microsoft.com/office/word/2010/wordprocessingShape", 'mc:Ignorable' => "w14 wp14" ) do
19
+ xml.parent.namespace = xml.parent.namespace_definitions.find{|ns| ns.prefix == "w" }
20
+ xml['w'].footnote( 'w:type' => "separator", 'w:id' => "-1") do
21
+ xml['w'].p do
22
+ xml['w'].r do
23
+ xml['w'].separator
24
+ end
25
+ end
26
+ end
27
+ xml['w'].footnote( 'w:type' => "continuationSeparator", 'w:id' => "0" ) do
28
+ xml['w'].p do
29
+ xml['w'].r do
30
+ xml['w'].continuationSeparator
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,30 @@
1
+ require 'docxer/word/headers/header'
2
+
3
+ module Docxer
4
+ module Word
5
+ class Headers
6
+
7
+ attr_accessor :headers, :counter
8
+ def initialize
9
+ @headers = []
10
+ @counter = 0
11
+ end
12
+
13
+ def add(header)
14
+ @headers << header
15
+ header
16
+ end
17
+
18
+ def sequence
19
+ @counter += 1
20
+ end
21
+
22
+ def render(zip)
23
+ @headers.each do |header|
24
+ header.render(zip)
25
+ end
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,142 @@
1
+ #encoding: utf-8
2
+
3
+ module Docxer
4
+ module Word
5
+ class Headers
6
+ class Header
7
+
8
+ attr_accessor :id, :sequence, :options, :content, :relationships
9
+ def initialize(options={})
10
+ @options = options
11
+ @content = []
12
+ @relationships = []
13
+
14
+ if block_given?
15
+ yield self
16
+ else
17
+
18
+ end
19
+ end
20
+
21
+ def content_type
22
+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header"
23
+ end
24
+
25
+ def target
26
+ "header#{id}.xml"
27
+ end
28
+
29
+ def render(zip)
30
+ zip.put_next_entry("word/#{target}")
31
+ zip.write(Docxer.to_xml(document))
32
+
33
+ if !@relationships.empty?
34
+ zip.put_next_entry("word/_rels/#{target}.rels")
35
+ zip.write(Docxer.to_xml(relationships))
36
+ end
37
+ end
38
+
39
+ def image(image, options={})
40
+ img = Image.new(image, options)
41
+ @content << img
42
+ @relationships << image
43
+ img
44
+ end
45
+
46
+ class Image
47
+ attr_accessor :media, :options
48
+ def initialize(media, options={})
49
+ @media = media
50
+ @media.file.rewind
51
+ @options = options
52
+ end
53
+
54
+ def width
55
+ @options[:width]
56
+ end
57
+
58
+ def height
59
+ @options[:height]
60
+ end
61
+
62
+ def render(xml)
63
+ xml['w'].p do
64
+ xml['w'].pPr do
65
+ xml['w'].jc( 'w:val' => @options[:align] ) if @options[:align]
66
+ end
67
+ xml['w'].r do
68
+ xml['w'].rPr do
69
+ xml['w'].noProof
70
+ end
71
+ xml['w'].drawing do
72
+ xml['wp'].inline( 'distT' => 0, 'distB' => 0, 'distL' => 0, 'distR' => 0 ) do
73
+ xml['wp'].extent( 'cx' => ( width * 9250 ).to_i, 'cy' => ( height * 9250 ).to_i )
74
+ xml['wp'].effectExtent( 'l' => 0, 't' => 0, 'r' => 0, 'b' => 1905 )
75
+ xml['wp'].docPr( 'id' => 1, 'name'=> "Image", 'descr' => "image")
76
+ xml['wp'].cNvGraphicFramePr do
77
+ xml.graphicFrameLocks( 'xmlns:a' => "http://schemas.openxmlformats.org/drawingml/2006/main", 'noChangeAspect' => 1 ) do
78
+ xml.parent.namespace = xml.parent.namespace_definitions.find{|ns| ns.prefix == "a" }
79
+ end
80
+ end
81
+ xml.graphic( 'xmlns:a' => "http://schemas.openxmlformats.org/drawingml/2006/main" ) do
82
+ xml.parent.namespace = xml.parent.namespace_definitions.find{|ns| ns.prefix == "a" }
83
+ xml['a'].graphicData( 'uri' => "http://schemas.openxmlformats.org/drawingml/2006/picture") do
84
+ xml.pic( 'xmlns:pic' => "http://schemas.openxmlformats.org/drawingml/2006/picture" ) do
85
+ xml.parent.namespace = xml.parent.namespace_definitions.find{|ns| ns.prefix == "pic" }
86
+ xml['pic'].nvPicPr do
87
+ xml['pic'].cNvPr( 'id' => 0, 'name' => "Image" )
88
+ xml['pic'].cNvPicPr
89
+ end
90
+ xml['pic'].blipFill do
91
+ xml['a'].blip( 'r:embed' => @media.sequence ) do
92
+ xml['a'].extLst
93
+ end
94
+ xml['a'].stretch do
95
+ xml['a'].fillRect
96
+ end
97
+ end
98
+ xml['pic'].spPr do
99
+ xml['a'].xfrm do
100
+ xml['a'].off( 'x' => 0, 'y' => 0 )
101
+ xml['a'].ext( 'cx' => ( width * 50 ).to_i, 'cy' => ( height * 50 ).to_i )
102
+ end
103
+ xml['a'].prstGeom( 'prst' => "rect" ) do
104
+ xml['a'].avLst
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+
117
+ private
118
+ def document
119
+ Nokogiri::XML::Builder.with(Nokogiri::XML('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>')) do |xml|
120
+ xml.hdr( 'xmlns:wpc' => "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas", 'xmlns:mc' => "http://schemas.openxmlformats.org/markup-compatibility/2006", 'xmlns:o' => "urn:schemas-microsoft-com:office:office", 'xmlns:r' => "http://schemas.openxmlformats.org/officeDocument/2006/relationships", 'xmlns:m' => "http://schemas.openxmlformats.org/officeDocument/2006/math", 'xmlns:v' => "urn:schemas-microsoft-com:vml", 'xmlns:wp14' => "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing", 'xmlns:wp' => "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", 'xmlns:w10' => "urn:schemas-microsoft-com:office:word", 'xmlns:w' => "http://schemas.openxmlformats.org/wordprocessingml/2006/main", 'xmlns:w14' => "http://schemas.microsoft.com/office/word/2010/wordml", 'xmlns:wpg' => "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup", 'xmlns:wpi' => "http://schemas.microsoft.com/office/word/2010/wordprocessingInk", 'xmlns:wne' => "http://schemas.microsoft.com/office/word/2006/wordml", 'xmlns:wps' => "http://schemas.microsoft.com/office/word/2010/wordprocessingShape", 'mc:Ignorable' => "w14 wp14" ) do
121
+ xml.parent.namespace = xml.parent.namespace_definitions.find{|ns| ns.prefix == "w" }
122
+ @content.each do |element|
123
+ element.render(xml)
124
+ end
125
+ end
126
+ end
127
+ end
128
+
129
+ def relationships
130
+ Nokogiri::XML::Builder.with(Nokogiri::XML('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>')) do |xml|
131
+ xml.Relationships(xmlns: 'http://schemas.openxmlformats.org/package/2006/relationships') do
132
+ @relationships.each do |relationship|
133
+ xml.Relationship('Id' => relationship.sequence, 'Type' => relationship.content_type, 'Target' => relationship.target)
134
+ end
135
+ end
136
+ end
137
+ end
138
+
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,37 @@
1
+ module Docxer
2
+ module Word
3
+ module Helpers
4
+
5
+ def text(text, options={})
6
+ p(options) do |p|
7
+ p.text text
8
+ end
9
+ end
10
+
11
+ def br(options={})
12
+ element = Docxer::Word::Contents::Break.new(options)
13
+ @content << element
14
+ element
15
+ end
16
+
17
+ def p(options={}, &block)
18
+ element = Docxer::Word::Contents::Paragraph.new(options, &block)
19
+ @content << element
20
+ element
21
+ end
22
+
23
+ def table_of_content(options={}, &block)
24
+ toc = Docxer::Word::Contents::TableOfContent.new(options, &block)
25
+ @content << toc
26
+ toc
27
+ end
28
+
29
+ def table(options={}, &block)
30
+ table = Docxer::Word::Contents::Table.new(options, &block)
31
+ @content << table
32
+ table
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,31 @@
1
+ require 'docxer/word/medias/media'
2
+
3
+ module Docxer
4
+ module Word
5
+ class Medias
6
+
7
+ attr_accessor :medias, :counter
8
+ def initialize(options={})
9
+ @medias = []
10
+ @counter = 0
11
+ end
12
+
13
+ def add(media)
14
+ @medias << media
15
+ media
16
+ end
17
+
18
+ def sequence
19
+ @counter += 1
20
+ end
21
+
22
+ def render(zip)
23
+ @medias.each do |media|
24
+ zip.put_next_entry("word/#{media.target}")
25
+ zip.write(media.file.read)
26
+ end
27
+ end
28
+
29
+ end
30
+ end
31
+ end