49 lines
1.2 KiB
Ruby
49 lines
1.2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "zip"
|
|
|
|
module Ezdoc
|
|
class Package
|
|
def initialize(document)
|
|
@document = document
|
|
end
|
|
|
|
def save(path)
|
|
Zip::File.open(path, Zip::File::CREATE) do |zipfile|
|
|
zipfile.get_output_stream("[Content_Types].xml") { |f| f.write(content_types_xml) }
|
|
zipfile.get_output_stream("_rels/.rels") { |f| f.write(relationships_xml) }
|
|
zipfile.get_output_stream("word/_rels/document.xml.rels") { |f| f.write(document_relationships_xml) }
|
|
zipfile.get_output_stream("word/document.xml") { |f| f.write(document_xml) }
|
|
|
|
zipfile.get_output_stream("word/numbering.xml") { |f| f.write(numbering_xml) } if lists?
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def lists?
|
|
@document.lists.any?
|
|
end
|
|
|
|
def content_types_xml
|
|
Xml::ContentTypes.new(has_numbering: lists?).to_xml
|
|
end
|
|
|
|
def relationships_xml
|
|
Xml::Relationships.new.to_xml
|
|
end
|
|
|
|
def document_relationships_xml
|
|
Xml::DocumentRelationships.new(has_numbering: lists?).to_xml
|
|
end
|
|
|
|
def document_xml
|
|
Xml::DocumentXml.new(@document.nodes).to_xml
|
|
end
|
|
|
|
def numbering_xml
|
|
Xml::Numbering.new(@document.lists).to_xml
|
|
end
|
|
end
|
|
end
|