docxify 0.0.7 → 0.0.9

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
  SHA256:
3
- metadata.gz: cb6c9076520d0776ed132e2abbc42166a6ff0106fce181d5d178ecf3abff1a96
4
- data.tar.gz: 042d1e27e5572cd0b4868eb9af065d1b3831e25d80569eafd2086bda130ab7b1
3
+ metadata.gz: c43501f65a265d3e8ce49ae0c5081d425c838465e31392b8e17c66c5d32214f9
4
+ data.tar.gz: efff6888d11c6ba8d9984150c0589d57e960b0e04fdc3daa79a2a458517b0fdd
5
5
  SHA512:
6
- metadata.gz: 83ee99a55280b56efd7f571c00c8cb09eb026345b821173c389f40f3bffb907bc2f4b57ce08d6d587a0bc653cdbc4efc630a1400dd48e3dee78f005bf574469e
7
- data.tar.gz: 62a9965149dde87c65db3877642fff5e6cf0cc8f02f373ab59422b78ca9a645522c2df45125268140f8b1e36b29a105b500ea06489f0fc5fee6056bff740bd6a
6
+ metadata.gz: c8dae5642665518f3c8be12d9a3d16a54ab626e3601b0b1073583f5c6f67cc80e31215e045f9ce593e520db7e35349d55363fee1f4713be4bb8eb7a704081780
7
+ data.tar.gz: 2d6aca43dba5dbe008f58e83a7bac426d6edb43aac83ba9812dc3eb1ab9bbe9218d128f88d6dfbd78fafaef38f8be32b1a97a47ba1519d13f4c86b81417c62c7
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.0.8
4
+
5
+ Features:
6
+
7
+ - Allow changing of following section's page layout
8
+
3
9
  ## 0.0.7
4
10
 
5
11
  Features:
data/README.md CHANGED
@@ -41,26 +41,26 @@ gem install docxify
41
41
 
42
42
  @docx.add_page_break
43
43
 
44
- @docx.container page_width: DocXify::A4_PORTRAIT_HEIGHT, page_height: DocXify::A4_PORTRAIT_WIDTH do |container|
45
- rows = []
46
- rows << [
47
- DocXify::Element::TableCell.new("<b>Header 1</b>"),
48
- DocXify::Element::TableCell.new("<b>Header 2</b>")
49
- DocXify::Element::TableCell.new("<b>Header 3</b>")
50
- DocXify::Element::TableCell.new("<b>Header 4</b>", width_cm: 10)
51
- ]
52
- rows << [
53
- DocXify::Element::TableCell.new("Content 1", valign: :center, align: :left, nowrap: true, colspan: 3),
54
- DocXify::Element::TableCell.new("Rowspan <b>here</b>", rowspan: true) # merges until a "nil" cell
55
- ]
56
- rows << [
57
- DocXify::Element::TableCell.new("Secondary 1")
58
- DocXify::Element::TableCell.new("Secondary 2")
59
- DocXify::Element::TableCell.new("Secondary 3")
60
- DocXify::Element::TableCell.new(nil) # Word requires that there's an empty cell merged with the previous rowspan
61
- ]
62
- container.add_table rows
63
- end
44
+ @docx.page_layout width: DocXify::A4_PORTRAIT_HEIGHT, height: DocXify::A4_PORTRAIT_WIDTH, orientation: :landscape
45
+
46
+ rows = []
47
+ rows << [
48
+ DocXify::Element::TableCell.new("<b>Header 1</b>"),
49
+ DocXify::Element::TableCell.new("<b>Header 2</b>")
50
+ DocXify::Element::TableCell.new("<b>Header 3</b>")
51
+ DocXify::Element::TableCell.new("<b>Header 4</b>", width_cm: 10)
52
+ ]
53
+ rows << [
54
+ DocXify::Element::TableCell.new("Content 1", valign: :center, align: :left, nowrap: true, colspan: 3),
55
+ DocXify::Element::TableCell.new("Rowspan <b>here</b>", rowspan: true) # merges until a "nil" cell
56
+ ]
57
+ rows << [
58
+ DocXify::Element::TableCell.new("Secondary 1")
59
+ DocXify::Element::TableCell.new("Secondary 2")
60
+ DocXify::Element::TableCell.new("Secondary 3")
61
+ DocXify::Element::TableCell.new(nil) # Word requires that there's an empty cell merged with the previous rowspan
62
+ ]
63
+ container.add_table rows
64
64
 
65
65
  docx_binary_data = @docx.render
66
66
  # or
@@ -1,6 +1,6 @@
1
1
  module DocXify
2
2
  class Document
3
- attr_accessor :font, :size, :color, :background
3
+ attr_accessor :font, :size, :color, :background, :margins, :page_layout
4
4
  attr_reader :content, :relationships, :width
5
5
 
6
6
  def initialize(options = {})
@@ -8,11 +8,12 @@ module DocXify
8
8
  @relationships = []
9
9
  @width = options[:width] || A4_PORTRAIT_WIDTH
10
10
  @height = options[:height] || A4_PORTRAIT_HEIGHT
11
+ @orientation = options[:orientation] || :portrait
11
12
  @font = options[:font] || "Times New Roman"
12
13
  @size = options[:size] || 12
13
14
  @color = options[:color] || 12
14
15
  @background = options[:background] if options[:background]
15
- @margins = { top: 2, bottom: 2, left: 2, right: 2 }
16
+ @margins = { top: 2, bottom: 2, left: 2, right: 2 }.merge(options[:margins] || {})
16
17
  end
17
18
 
18
19
  def default_styling(options = {})
@@ -41,17 +42,21 @@ module DocXify
41
42
  <w:body>
42
43
  XML
43
44
 
45
+ # See the note in DocXify::Element::PageLayout for why it's not just handled the same as any other element
46
+ @page_layout = DocXify::Element::PageLayout.new(width: @width, height: @height, orientation: @orientatation, document: self)
47
+
44
48
  @content.each do |element|
45
- xml << element.to_s(container)
49
+ if element.is_a?(DocXify::Element::PageLayout)
50
+ xml << @page_layout.to_s
51
+ @page_layout = element
52
+ else
53
+ xml << element.to_s(container)
54
+ end
46
55
  end
47
56
 
57
+ xml << @page_layout.to_s
58
+
48
59
  xml << <<~XML
49
- <w:sectPr >
50
- <w:pgSz w:h="#{@height}" w:w="#{@width}"/>
51
- <w:pgMar w:bottom="#{DocXify.cm2dxa @margins[:bottom]}" w:footer="708" w:gutter="0" w:header="708" w:left="#{DocXify.cm2dxa @margins[:left]}" w:right="#{DocXify.cm2dxa @margins[:right]}" w:top="#{DocXify.cm2dxa @margins[:top]}"/>
52
- <w:cols w:space="708"/>
53
- <w:docGrid w:linePitch="360"/>
54
- </w:sectPr>
55
60
  </w:body>
56
61
  </w:document>
57
62
  XML
@@ -82,6 +87,11 @@ module DocXify
82
87
  add DocXify::Element::PageBreak.new
83
88
  end
84
89
 
90
+ def add_page_layout(options = {})
91
+ options[:document] = self
92
+ add DocXify::Element::PageLayout.new(options)
93
+ end
94
+
85
95
  def add_divider
86
96
  add DocXify::Element::Divider.new
87
97
  end
@@ -8,7 +8,6 @@ module DocXify
8
8
  <w:br w:type="page"/>
9
9
  </w:r>
10
10
  </w:p>
11
- <w:p/>
12
11
  XML
13
12
  end
14
13
  end
@@ -0,0 +1,34 @@
1
+ # Word PageLayouts are a weird concept; instead of a wrapping element or a
2
+ # "this applies to content from here on down", it's applied retrospectively
3
+ # to the content that came before it.
4
+ module DocXify
5
+ module Element
6
+ class PageLayout < Base
7
+ attr_accessor :width, :height, :orientation
8
+
9
+ def initialize(options = {})
10
+ super()
11
+ @document = options[:document]
12
+ @margins = @document.margins
13
+ @width = options[:width] || @document&.width || DocXify::A4_PORTRAIT_WIDTH
14
+ @height = options[:height] || @document&.height || DocXify::A4_PORTRAIT_HEIGHT
15
+ @orientation = options[:orientation] || :portrait
16
+ end
17
+
18
+ def to_s(_container = nil)
19
+ <<~XML
20
+ <w:p>
21
+ <w:pPr>
22
+ <w:sectPr>
23
+ <w:pgSz w:w="#{@width}" w:h="#{@height}" w:orient="#{@orientation}"/>
24
+ <w:pgMar w:bottom="#{DocXify.cm2dxa @margins[:bottom]}" w:footer="708" w:gutter="0" w:header="708" w:left="#{DocXify.cm2dxa @margins[:left]}" w:right="#{DocXify.cm2dxa @margins[:right]}" w:top="#{DocXify.cm2dxa @margins[:top]}"/>
25
+ <w:cols w:space="708"/>
26
+ <w:docGrid w:linePitch="360"/>
27
+ </w:sectPr>
28
+ </w:pPr>
29
+ </w:p>
30
+ XML
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,3 +1,3 @@
1
1
  module DocXify
2
- VERSION = "0.0.7".freeze
2
+ VERSION = "0.0.9".freeze
3
3
  end
data/lib/docxify.rb CHANGED
@@ -8,6 +8,7 @@ require_relative "docxify/element/divider"
8
8
  require_relative "docxify/element/file"
9
9
  require_relative "docxify/element/image"
10
10
  require_relative "docxify/element/page_break"
11
+ require_relative "docxify/element/page_layout"
11
12
  require_relative "docxify/element/paragraph"
12
13
  require_relative "docxify/element/table"
13
14
  require_relative "docxify/element/table_cell"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docxify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Jeffries
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-04-30 00:00:00.000000000 Z
11
+ date: 2024-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubyzip
@@ -46,6 +46,7 @@ files:
46
46
  - lib/docxify/element/file.rb
47
47
  - lib/docxify/element/image.rb
48
48
  - lib/docxify/element/page_break.rb
49
+ - lib/docxify/element/page_layout.rb
49
50
  - lib/docxify/element/paragraph.rb
50
51
  - lib/docxify/element/table.rb
51
52
  - lib/docxify/element/table_cell.rb
@@ -74,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
75
  - !ruby/object:Gem::Version
75
76
  version: '0'
76
77
  requirements: []
77
- rubygems_version: 3.5.6
78
+ rubygems_version: 3.5.17
78
79
  signing_key:
79
80
  specification_version: 4
80
81
  summary: DocXify is a gem to help you generate Word documents from Ruby.