class DocxGenerator::DSL::Document
Represent the docx document.
Attributes
filename[R]
Filename of the document (without the docx extension).
Public Class Methods
new(filename) { |self| ... }
click to toggle source
Create a new docx document. @param filename [String] The filename of the docx file, without the docx extension.
# File lib/docx_generator/dsl/document.rb, line 10 def initialize(filename, &block) @filename = filename + ".docx" @objects = [] # It contains all the DSL elements yield self if block end
Public Instance Methods
add(*objects)
click to toggle source
Add other objects to the document. @param objects [Object] Objects (like paragraphs). @return [DocxGenerator::DSL::Document] The document object.
# File lib/docx_generator/dsl/document.rb, line 32 def add(*objects) objects.each do |object| @objects << object end self end
paragraph(options = {}) { |par| ... }
click to toggle source
Add a new paragraph to the document. @param options [Hash] Formatting options for the paragraph. See the full list in DocxGenerator::DSL::Paragraph
.
# File lib/docx_generator/dsl/document.rb, line 23 def paragraph(options = {}, &block) par = DocxGenerator::DSL::Paragraph.new(options) yield par if block @objects << par end
save()
click to toggle source
Save the docx document to the target location.
# File lib/docx_generator/dsl/document.rb, line 17 def save generate_archive(generate_content_types, generate_rels, generate_document) end
Private Instance Methods
generate_archive(content_types, rels, document)
click to toggle source
# File lib/docx_generator/dsl/document.rb, line 72 def generate_archive(content_types, rels, document) File.delete(@filename) if File.exists?(@filename) Zip::File.open(@filename, Zip::File::CREATE) do |docx| docx.mkdir('_rels') docx.mkdir('word') docx.get_output_stream('[Content_Types].xml') { |f| f.puts content_types } docx.get_output_stream('_rels/.rels') { |f| f.puts rels } docx.get_output_stream('word/document.xml') { |f| f.puts document } end end
generate_content_types()
click to toggle source
# File lib/docx_generator/dsl/document.rb, line 41 def generate_content_types <<EOF <Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"> <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/> <Default Extension="xml" ContentType="application/xml"/> <Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/> </Types> EOF end
generate_document()
click to toggle source
# File lib/docx_generator/dsl/document.rb, line 61 def generate_document content = [] @objects.each do |object| content << object.generate end '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + Word::Document.new({ "xmlns:w" => "http://schemas.openxmlformats.org/wordprocessingml/2006/main" }, [ Word::Body.new(content) ]).to_s end
generate_rels()
click to toggle source
# File lib/docx_generator/dsl/document.rb, line 52 def generate_rels <<EOF <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/> </Relationships> EOF end