All checks were successful
CI Pipeline / build (pull_request) Successful in 12s
Adds these new styling and formatting nodes * strike * highlight * linebreaks * pagebreaks * Hyperlinks
70 lines
1.7 KiB
Ruby
70 lines
1.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "zip"
|
|
|
|
module Notare
|
|
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/styles.xml") { |f| f.write(styles_xml) }
|
|
|
|
zipfile.get_output_stream("word/numbering.xml") { |f| f.write(numbering_xml) } if lists?
|
|
|
|
images.each do |image|
|
|
zipfile.get_output_stream("word/media/#{image.filename}") do |f|
|
|
f.write(File.binread(image.path))
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def lists?
|
|
@document.uses_lists?
|
|
end
|
|
|
|
def images
|
|
@document.images
|
|
end
|
|
|
|
def hyperlinks
|
|
@document.hyperlinks
|
|
end
|
|
|
|
def content_types_xml
|
|
Xml::ContentTypes.new(has_numbering: lists?, images: images, has_styles: true).to_xml
|
|
end
|
|
|
|
def relationships_xml
|
|
Xml::Relationships.new.to_xml
|
|
end
|
|
|
|
def document_relationships_xml
|
|
Xml::DocumentRelationships.new(
|
|
has_numbering: lists?, images: images, hyperlinks: hyperlinks, has_styles: true
|
|
).to_xml
|
|
end
|
|
|
|
def document_xml
|
|
Xml::DocumentXml.new(@document.nodes).to_xml
|
|
end
|
|
|
|
def styles_xml
|
|
Xml::StylesXml.new(@document.styles).to_xml
|
|
end
|
|
|
|
def numbering_xml
|
|
Xml::Numbering.new(@document.lists).to_xml
|
|
end
|
|
end
|
|
end
|