class Caracal::Document
Public Class Methods
new(name = nil, &block)
click to toggle source
This method instantiates a new word document.
# File lib/caracal/document.rb, line 110 def initialize(name = nil, &block) file_name(name) page_size page_margins top: 1440, bottom: 1440, left: 1440, right: 1440 page_numbers [:font, :list_style, :namespace, :relationship, :style].each do |method| collection = self.class.send("default_#{ method }s") collection.each do |item| send(method, item) end end if block_given? (block.arity < 1) ? instance_eval(&block) : block[self] end end
render(f_name = nil, &block)
click to toggle source
This method renders a new Word document and returns it as a a string.
# File lib/caracal/document.rb, line 81 def self.render(f_name = nil, &block) docx = new(f_name, &block) buffer = docx.render buffer.rewind buffer.sysread end
save(f_name = nil, &block)
click to toggle source
This method renders a new Word document and saves it to the file system.
# File lib/caracal/document.rb, line 92 def self.save(f_name = nil, &block) docx = new(f_name, &block) docx.save # buffer = docx.render # # File.open(docx.path, 'wb') { |f| f.write(buffer.string) } end
Public Instance Methods
contents()
click to toggle source
This method returns an array of models which constitute the set of instructions for producing the document content.
# File lib/caracal/document.rb, line 135 def contents @contents ||= [] end
header()
click to toggle source
# File lib/caracal/document.rb, line 100 def header @header ||= Header.new end
render()
click to toggle source
This method renders the word document instance into a string buffer. Order is important!
# File lib/caracal/document.rb, line 144 def render buffer = ::Zip::OutputStream.write_buffer do |zip| render_package_relationships(zip) render_content_types(zip) render_app(zip) render_core(zip) render_custom(zip) render_fonts(zip) render_header(zip) render_footer(zip) render_settings(zip) render_styles(zip) render_document(zip) render_relationships(zip) # Must go here: Depends on document renderer render_header_relationships(zip) # Must go here: Depends on document renderer render_media(zip) # Must go here: Depends on document renderer render_numbering(zip) # Must go here: Depends on document renderer end end
Private Instance Methods
render_app(zip)
click to toggle source
render_content_types(zip)
click to toggle source
# File lib/caracal/document.rb, line 188 def render_content_types(zip) content = ::Caracal::Renderers::ContentTypesRenderer.render(self) zip.put_next_entry('[Content_Types].xml') zip.write(content) end
render_core(zip)
click to toggle source
# File lib/caracal/document.rb, line 195 def render_core(zip) content = ::Caracal::Renderers::CoreRenderer.render(self) zip.put_next_entry('docProps/core.xml') zip.write(content) end
render_custom(zip)
click to toggle source
# File lib/caracal/document.rb, line 202 def render_custom(zip) content = ::Caracal::Renderers::CustomRenderer.render(self) zip.put_next_entry('docProps/custom.xml') zip.write(content) end
render_document(zip)
click to toggle source
# File lib/caracal/document.rb, line 209 def render_document(zip) content = ::Caracal::Renderers::DocumentRenderer.render(self) zip.put_next_entry('word/document.xml') zip.write(content) end
render_fonts(zip)
click to toggle source
# File lib/caracal/document.rb, line 216 def render_fonts(zip) content = ::Caracal::Renderers::FontsRenderer.render(self) zip.put_next_entry('word/fontTable.xml') zip.write(content) end
render_header(zip)
click to toggle source
# File lib/caracal/document.rb, line 223 def render_header(zip) content = ::Caracal::Renderers::HeaderRenderer.render(header) zip.put_next_entry('word/header1.xml') zip.write(content) end
render_header_relationships(zip)
click to toggle source
# File lib/caracal/document.rb, line 273 def render_header_relationships(zip) if header.relationships.any? content = ::Caracal::Renderers::RelationshipsRenderer.render(header) zip.put_next_entry('word/_rels/header1.xml.rels') zip.write(content) end end
render_media(zip)
click to toggle source
# File lib/caracal/document.rb, line 237 def render_media(zip) images = relationships.select { |r| r.relationship_type == :image } images.concat(header.relationships.select { |r| r.relationship_type == :image }) images.each do |rel| if rel.relationship_data.to_s.size > 0 content = rel.relationship_data else content = open(rel.relationship_target).read end zip.put_next_entry("word/#{ rel.formatted_target }") zip.write(content) end end
render_numbering(zip)
click to toggle source
# File lib/caracal/document.rb, line 252 def render_numbering(zip) content = ::Caracal::Renderers::NumberingRenderer.render(self) zip.put_next_entry('word/numbering.xml') zip.write(content) end
render_package_relationships(zip)
click to toggle source
# File lib/caracal/document.rb, line 259 def render_package_relationships(zip) content = ::Caracal::Renderers::PackageRelationshipsRenderer.render(self) zip.put_next_entry('_rels/.rels') zip.write(content) end
render_relationships(zip)
click to toggle source
# File lib/caracal/document.rb, line 266 def render_relationships(zip) content = ::Caracal::Renderers::RelationshipsRenderer.render(self) zip.put_next_entry('word/_rels/document.xml.rels') zip.write(content) end
render_settings(zip)
click to toggle source
# File lib/caracal/document.rb, line 282 def render_settings(zip) content = ::Caracal::Renderers::SettingsRenderer.render(self) zip.put_next_entry('word/settings.xml') zip.write(content) end
render_styles(zip)
click to toggle source
# File lib/caracal/document.rb, line 289 def render_styles(zip) content = ::Caracal::Renderers::StylesRenderer.render(self) zip.put_next_entry('word/styles.xml') zip.write(content) end