Class: DocXify::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/docxify/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Document

Returns a new instance of Document.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/docxify/document.rb', line 6

def initialize(options = {})
  @content = []
  @relationships = []
  @width = options[:width] || A4_PORTRAIT_WIDTH
  @height = options[:height] || A4_PORTRAIT_HEIGHT
  @orientation = options[:orientation]
  @font = options[:font] || "Times New Roman"
  @size = options[:size] || 12
  @color = options[:color] || 12
  @background = options[:background] if options[:background]
  @margins = { top: 2, bottom: 2, left: 2, right: 2 }.merge(options[:margins] || {})
end

Instance Attribute Details

#backgroundObject

Returns the value of attribute background.



3
4
5
# File 'lib/docxify/document.rb', line 3

def background
  @background
end

#colorObject

Returns the value of attribute color.



3
4
5
# File 'lib/docxify/document.rb', line 3

def color
  @color
end

#contentObject (readonly)

Returns the value of attribute content.



4
5
6
# File 'lib/docxify/document.rb', line 4

def content
  @content
end

#fontObject

Returns the value of attribute font.



3
4
5
# File 'lib/docxify/document.rb', line 3

def font
  @font
end

#marginsObject

Returns the value of attribute margins.



3
4
5
# File 'lib/docxify/document.rb', line 3

def margins
  @margins
end

#page_layoutObject

Returns the value of attribute page_layout.



3
4
5
# File 'lib/docxify/document.rb', line 3

def page_layout
  @page_layout
end

#relationshipsObject (readonly)

Returns the value of attribute relationships.



4
5
6
# File 'lib/docxify/document.rb', line 4

def relationships
  @relationships
end

#sizeObject

Returns the value of attribute size.



3
4
5
# File 'lib/docxify/document.rb', line 3

def size
  @size
end

#widthObject (readonly)

Returns the value of attribute width.



4
5
6
# File 'lib/docxify/document.rb', line 4

def width
  @width
end

Instance Method Details

#add(element) ⇒ Object

MARK: Elements



75
76
77
# File 'lib/docxify/document.rb', line 75

def add(element)
  @content << element
end

#add_dividerObject



104
105
106
# File 'lib/docxify/document.rb', line 104

def add_divider
  add DocXify::Element::Divider.new
end

#add_file(file_path_or_data) ⇒ Object



88
89
90
91
92
# File 'lib/docxify/document.rb', line 88

def add_file(file_path_or_data)
  file = DocXify::Element::File.new(file_path_or_data)
  @relationships << file
  file
end

#add_image(file_path_or_data, options = {}) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/docxify/document.rb', line 79

def add_image(file_path_or_data, options = {})
  file = if file_path_or_data.is_a?(DocXify::Element::File)
    file_path_or_data
  else
    add_file(file_path_or_data)
  end
  add DocXify::Element::Image.new(file, options)
end

#add_page_breakObject



94
95
96
# File 'lib/docxify/document.rb', line 94

def add_page_break
  add DocXify::Element::PageBreak.new
end

#add_page_layout(options = {}) ⇒ Object



98
99
100
101
102
# File 'lib/docxify/document.rb', line 98

def add_page_layout(options = {})
  options[:document] = self
  @page_layout = DocXify::Element::PageLayout.new(options)
  add @page_layout
end

#add_paragraph(text, options = {}) ⇒ Object



108
109
110
111
# File 'lib/docxify/document.rb', line 108

def add_paragraph(text, options = {})
  options[:document] = self
  add DocXify::Element::Paragraph.new(text, options)
end

#add_table(rows, options = {}) ⇒ Object



113
114
115
116
# File 'lib/docxify/document.rb', line 113

def add_table(rows, options = {})
  options[:document] = self
  add DocXify::Element::Table.new(rows, options)
end

#bounds_heightObject



23
24
25
# File 'lib/docxify/document.rb', line 23

def bounds_height
  @page_layout&.bounds_height || (@height - @margins[:top] - @margins[:bottom])
end

#bounds_widthObject



19
20
21
# File 'lib/docxify/document.rb', line 19

def bounds_width
  @page_layout&.bounds_width || (@width - @margins[:left] - @margins[:right])
end

#build_xml(container) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/docxify/document.rb', line 46

def build_xml(container)
  xml = +<<~XML
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <w:document mc:Ignorable="w14 w15 w16se w16cid w16 w16cex w16sdtdh w16du wp14" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:oel="http://schemas.microsoft.com/office/2019/extlst" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml" xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16du="http://schemas.microsoft.com/office/word/2023/wordml/word16du" xmlns:w16sdtdh="http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
      <w:body>
  XML

  # See the note in DocXify::Element::PageLayout for why it's not just handled the same as any other element
  @page_layout = DocXify::Element::PageLayout.new(width: @width, height: @height, orientation: @orientatation, document: self)

  @content.each do |element|
    if element.is_a?(DocXify::Element::PageLayout)
      xml << @page_layout.to_s
      @page_layout = element
    else
      xml << element.to_s(container)
    end
  end

  xml << @page_layout.to_s

  xml << <<~XML
      </w:body>
    </w:document>
  XML
end

#default_styling(options = {}) ⇒ Object



27
28
29
30
31
32
# File 'lib/docxify/document.rb', line 27

def default_styling(options = {})
  @font = options[:font] if options[:font]
  @size = options[:size] if options[:size]
  @color = options[:color] if options[:color]
  @background = options[:background] if options[:background]
end

#render(path = nil) ⇒ Object

MARK: Rendering



36
37
38
39
40
41
42
43
44
# File 'lib/docxify/document.rb', line 36

def render(path = nil)
  container = DocXify::Container.new(self)

  if path
    File.write(path, container.render)
  else
    container.render
  end
end