This commit is contained in:
69
lib/notare/xml/relationships.rb
Normal file
69
lib/notare/xml/relationships.rb
Normal file
@@ -0,0 +1,69 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Notare
|
||||
module Xml
|
||||
class Relationships
|
||||
NAMESPACE = "http://schemas.openxmlformats.org/package/2006/relationships"
|
||||
|
||||
def to_xml
|
||||
builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
|
||||
xml.Relationships(xmlns: NAMESPACE) do
|
||||
xml.Relationship(
|
||||
Id: "rId1",
|
||||
Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
|
||||
Target: "word/document.xml"
|
||||
)
|
||||
end
|
||||
end
|
||||
builder.to_xml
|
||||
end
|
||||
end
|
||||
|
||||
class DocumentRelationships
|
||||
NAMESPACE = "http://schemas.openxmlformats.org/package/2006/relationships"
|
||||
STYLES_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"
|
||||
NUMBERING_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering"
|
||||
IMAGE_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
|
||||
|
||||
def initialize(has_numbering: false, images: [], has_styles: false)
|
||||
@has_numbering = has_numbering
|
||||
@images = images
|
||||
@has_styles = has_styles
|
||||
end
|
||||
|
||||
def to_xml
|
||||
builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
|
||||
xml.Relationships(xmlns: NAMESPACE) do
|
||||
# rId1 = styles.xml (always first when present)
|
||||
if @has_styles
|
||||
xml.Relationship(
|
||||
Id: "rId1",
|
||||
Type: STYLES_TYPE,
|
||||
Target: "styles.xml"
|
||||
)
|
||||
end
|
||||
|
||||
# rId2 = numbering.xml (if lists present)
|
||||
if @has_numbering
|
||||
xml.Relationship(
|
||||
Id: "rId2",
|
||||
Type: NUMBERING_TYPE,
|
||||
Target: "numbering.xml"
|
||||
)
|
||||
end
|
||||
|
||||
# Images start at rId2 or rId3 depending on numbering
|
||||
@images.each do |image|
|
||||
xml.Relationship(
|
||||
Id: image.rid,
|
||||
Type: IMAGE_TYPE,
|
||||
Target: "media/#{image.filename}"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
builder.to_xml
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user