Initial project setup
This commit is contained in:
26
lib/ezdoc/document.rb
Normal file
26
lib/ezdoc/document.rb
Normal file
@@ -0,0 +1,26 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Ezdoc
|
||||
class Document
|
||||
attr_reader :content
|
||||
|
||||
def self.create(path, &block)
|
||||
doc = new
|
||||
block.call(doc)
|
||||
doc.save(path)
|
||||
doc
|
||||
end
|
||||
|
||||
def initialize
|
||||
@content = []
|
||||
end
|
||||
|
||||
def text(value)
|
||||
@content << { text: value }
|
||||
end
|
||||
|
||||
def save(path)
|
||||
Package.new(self).save(path)
|
||||
end
|
||||
end
|
||||
end
|
||||
38
lib/ezdoc/package.rb
Normal file
38
lib/ezdoc/package.rb
Normal file
@@ -0,0 +1,38 @@
|
||||
# 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) }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def content_types_xml
|
||||
Xml::ContentTypes.new.to_xml
|
||||
end
|
||||
|
||||
def relationships_xml
|
||||
Xml::Relationships.new.to_xml
|
||||
end
|
||||
|
||||
def document_relationships_xml
|
||||
Xml::DocumentRelationships.new.to_xml
|
||||
end
|
||||
|
||||
def document_xml
|
||||
Xml::DocumentXml.new(@document.content).to_xml
|
||||
end
|
||||
end
|
||||
end
|
||||
5
lib/ezdoc/version.rb
Normal file
5
lib/ezdoc/version.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Ezdoc
|
||||
VERSION = "0.0.1"
|
||||
end
|
||||
23
lib/ezdoc/xml/content_types.rb
Normal file
23
lib/ezdoc/xml/content_types.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Ezdoc
|
||||
module Xml
|
||||
class ContentTypes
|
||||
NAMESPACE = "http://schemas.openxmlformats.org/package/2006/content-types"
|
||||
|
||||
def to_xml
|
||||
builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
|
||||
xml.Types(xmlns: NAMESPACE) do
|
||||
xml.Default(Extension: "rels", ContentType: "application/vnd.openxmlformats-package.relationships+xml")
|
||||
xml.Default(Extension: "xml", ContentType: "application/xml")
|
||||
xml.Override(
|
||||
PartName: "/word/document.xml",
|
||||
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"
|
||||
)
|
||||
end
|
||||
end
|
||||
builder.to_xml
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
34
lib/ezdoc/xml/document_xml.rb
Normal file
34
lib/ezdoc/xml/document_xml.rb
Normal file
@@ -0,0 +1,34 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Ezdoc
|
||||
module Xml
|
||||
class DocumentXml
|
||||
NAMESPACES = {
|
||||
"xmlns:w" => "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
|
||||
"xmlns:r" => "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
|
||||
}.freeze
|
||||
|
||||
def initialize(content)
|
||||
@content = content
|
||||
end
|
||||
|
||||
def to_xml
|
||||
builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
|
||||
xml.document(NAMESPACES) do
|
||||
xml.parent.namespace = xml.parent.namespace_definitions.find { |ns| ns.prefix == "w" }
|
||||
xml["w"].body do
|
||||
@content.each do |item|
|
||||
xml["w"].p do
|
||||
xml["w"].r do
|
||||
xml["w"].t item[:text]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
builder.to_xml
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
33
lib/ezdoc/xml/relationships.rb
Normal file
33
lib/ezdoc/xml/relationships.rb
Normal file
@@ -0,0 +1,33 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Ezdoc
|
||||
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"
|
||||
|
||||
def to_xml
|
||||
builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
|
||||
xml.Relationships(xmlns: NAMESPACE)
|
||||
end
|
||||
builder.to_xml
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user