Class: DocXify::Document
- Inherits:
-
Object
- Object
- DocXify::Document
- Defined in:
- lib/docxify/document.rb
Instance Attribute Summary collapse
-
#background ⇒ Object
Returns the value of attribute background.
-
#color ⇒ Object
Returns the value of attribute color.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#font ⇒ Object
Returns the value of attribute font.
-
#margins ⇒ Object
Returns the value of attribute margins.
-
#page_layout ⇒ Object
Returns the value of attribute page_layout.
-
#relationships ⇒ Object
readonly
Returns the value of attribute relationships.
-
#size ⇒ Object
Returns the value of attribute size.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
-
#add(element) ⇒ Object
MARK: Elements.
- #add_divider ⇒ Object
- #add_file(file_path_or_data) ⇒ Object
- #add_image(file_path_or_data, options = {}) ⇒ Object
- #add_page_break ⇒ Object
- #add_page_layout(options = {}) ⇒ Object
- #add_paragraph(text, options = {}) ⇒ Object
- #add_table(rows, options = {}) ⇒ Object
- #bounds_height ⇒ Object
- #bounds_width ⇒ Object
- #build_xml(container) ⇒ Object
- #default_styling(options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Document
constructor
A new instance of Document.
-
#render(path = nil) ⇒ Object
MARK: Rendering.
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( = {}) @content = [] @relationships = [] @width = [:width] || A4_PORTRAIT_WIDTH @height = [:height] || A4_PORTRAIT_HEIGHT @orientation = [:orientation] @font = [:font] || "Times New Roman" @size = [:size] || 12 @color = [:color] || 12 @background = [:background] if [:background] @margins = { top: 2, bottom: 2, left: 2, right: 2 }.merge([:margins] || {}) end |
Instance Attribute Details
#background ⇒ Object
Returns the value of attribute background.
3 4 5 |
# File 'lib/docxify/document.rb', line 3 def background @background end |
#color ⇒ Object
Returns the value of attribute color.
3 4 5 |
# File 'lib/docxify/document.rb', line 3 def color @color end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
4 5 6 |
# File 'lib/docxify/document.rb', line 4 def content @content end |
#font ⇒ Object
Returns the value of attribute font.
3 4 5 |
# File 'lib/docxify/document.rb', line 3 def font @font end |
#margins ⇒ Object
Returns the value of attribute margins.
3 4 5 |
# File 'lib/docxify/document.rb', line 3 def margins @margins end |
#page_layout ⇒ Object
Returns the value of attribute page_layout.
3 4 5 |
# File 'lib/docxify/document.rb', line 3 def page_layout @page_layout end |
#relationships ⇒ Object (readonly)
Returns the value of attribute relationships.
4 5 6 |
# File 'lib/docxify/document.rb', line 4 def relationships @relationships end |
#size ⇒ Object
Returns the value of attribute size.
3 4 5 |
# File 'lib/docxify/document.rb', line 3 def size @size end |
#width ⇒ Object (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_divider ⇒ Object
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, = {}) 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, ) end |
#add_page_break ⇒ Object
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( = {}) [:document] = self @page_layout = DocXify::Element::PageLayout.new() add @page_layout end |
#add_paragraph(text, options = {}) ⇒ Object
108 109 110 111 |
# File 'lib/docxify/document.rb', line 108 def add_paragraph(text, = {}) [:document] = self add DocXify::Element::Paragraph.new(text, ) end |
#add_table(rows, options = {}) ⇒ Object
113 114 115 116 |
# File 'lib/docxify/document.rb', line 113 def add_table(rows, = {}) [:document] = self add DocXify::Element::Table.new(rows, ) end |
#bounds_height ⇒ Object
23 24 25 |
# File 'lib/docxify/document.rb', line 23 def bounds_height @page_layout&.bounds_height || (@height - @margins[:top] - @margins[:bottom]) end |
#bounds_width ⇒ Object
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( = {}) @font = [:font] if [:font] @size = [:size] if [:size] @color = [:color] if [:color] @background = [:background] if [:background] end |