lib/goldendocx/docx.rb in goldendocx-0.2.2 vs lib/goldendocx/docx.rb in goldendocx-0.2.3

- old
+ new

@@ -1,34 +1,56 @@ # frozen_string_literal: true -require 'goldendocx/units' -require 'goldendocx/element' -require 'goldendocx/document' - +require 'goldendocx/models' require 'goldendocx/components' require 'goldendocx/documents' require 'goldendocx/parts' module Goldendocx class Docx - attr_reader :unstructured_entries, :documents, :content_types + include Goldendocx::HasAssociations + attr_reader :unstructured_entries, :documents + + RELATIONSHIPS_XML_PATH = '_rels/.rels' + STRUCTURED_ENTRIES = [ + RELATIONSHIPS_XML_PATH, + Goldendocx::Parts::ContentTypes::XML_PATH, + Goldendocx::Parts::App::XML_PATH, + Goldendocx::Parts::Core::XML_PATH, - Goldendocx::Documents::Body::XML_PATH, - Goldendocx::Documents::Relationships::XML_PATH, + Goldendocx::Parts::Documents::RELATIONSHIPS_XML_PATH, + + Goldendocx::Documents::Document::XML_PATH, + Goldendocx::Documents::Settings::XML_PATH, Goldendocx::Documents::Styles::XML_PATH ].freeze - def initialize(file_path) + relationships_at RELATIONSHIPS_XML_PATH + associate :app, class_name: 'Goldendocx::Parts::App' + associate :core, class_name: 'Goldendocx::Parts::Core' + associate :content_types, class_name: 'Goldendocx::Parts::ContentTypes', isolate: true + + def initialize(file_path = nil) + file_path.present? ? read_from(file_path) : build_default + end + + def read_from(file_path) docx_file = Zip::File.new(file_path) + + read_relationships(docx_file) + read_associations(docx_file) + @documents = Goldendocx::Parts::Documents.read_from(docx_file) - @content_types = Goldendocx::Parts::ContentTypes.read_from(docx_file) + @unstructured_entries = docx_file.entries.filter_map do |entry| UnStructuredEntry.new(entry) unless STRUCTURED_ENTRIES.include?(entry.name) end + + self end def write_to(new_path) File.binwrite(new_path, to_stream.string) end @@ -63,10 +85,29 @@ chart end private + def build_default + associations.each do |association, options| + association_class = options[:class_name].constantize + instance_variable_set("@#{association}", association_class.new) + add_relationship association_class::TYPE, association_class::XML_PATH unless options[:isolate] + end + + content_types.add_override "/#{Goldendocx::Parts::App::XML_PATH}", Goldendocx::Parts::App::CONTENT_TYPE + content_types.add_override "/#{Goldendocx::Parts::Core::XML_PATH}", Goldendocx::Parts::Core::CONTENT_TYPE + content_types.add_override "/#{Goldendocx::Documents::Styles::XML_PATH}", Goldendocx::Documents::Styles::CONTENT_TYPE + content_types.add_override "/#{Goldendocx::Documents::Settings::XML_PATH}", Goldendocx::Documents::Settings::CONTENT_TYPE + + @documents = Goldendocx::Parts::Documents.new + add_relationship Goldendocx::Documents::Document::TYPE, Goldendocx::Documents::Document::XML_PATH + content_types.add_override "/#{Goldendocx::Documents::Document::XML_PATH}", Goldendocx::Documents::Document::CONTENT_TYPE + + @unstructured_entries = [] + end + def ensure_image_content_type!(_image_data) extension = 'png' content_type = 'image/png' content_types.add_default(extension, content_type) end @@ -77,13 +118,15 @@ ) end def to_stream Zip::OutputStream.write_buffer do |zos| - zos.put_next_entry Goldendocx::Parts::ContentTypes::XML_PATH - zos.write content_types.to_document_xml + write_relationships(zos) - zos.put_next_entry Goldendocx::Parts::Documents::XML_PATH + associations.each_key do |association| + send(association).write_to(zos) + end + documents.write_stream(zos) @unstructured_entries.each { |unstructured_entry| unstructured_entry.write_to(zos) } end end