Add builder pattern for paragraph, lists, tables

This commit is contained in:
2025-12-02 10:57:54 +01:00
parent 50c9c20eca
commit b602b2a2ff
25 changed files with 1248 additions and 47 deletions

View File

@@ -5,6 +5,10 @@ module Ezdoc
class ContentTypes
NAMESPACE = "http://schemas.openxmlformats.org/package/2006/content-types"
def initialize(has_numbering: false)
@has_numbering = has_numbering
end
def to_xml
builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
xml.Types(xmlns: NAMESPACE) do
@@ -14,6 +18,12 @@ module Ezdoc
PartName: "/word/document.xml",
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"
)
if @has_numbering
xml.Override(
PartName: "/word/numbering.xml",
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml"
)
end
end
end
builder.to_xml

View File

@@ -8,8 +8,8 @@ module Ezdoc
"xmlns:r" => "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
}.freeze
def initialize(content)
@content = content
def initialize(nodes)
@nodes = nodes
end
def to_xml
@@ -17,18 +17,88 @@ module Ezdoc
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
@nodes.each { |node| render_node(xml, node) }
end
end
end
builder.to_xml
end
private
def render_node(xml, node)
case node
when Nodes::Paragraph
render_paragraph(xml, node)
when Nodes::List
render_list(xml, node)
when Nodes::Table
render_table(xml, node)
end
end
def render_paragraph(xml, para)
xml["w"].p do
para.runs.each { |run| render_run(xml, run) }
end
end
def render_list(xml, list)
list.items.each { |item| render_list_item(xml, item) }
end
def render_list_item(xml, item)
xml["w"].p do
xml["w"].pPr do
xml["w"].numPr do
xml["w"].ilvl("w:val" => "0")
xml["w"].numId("w:val" => item.num_id.to_s)
end
end
item.runs.each { |run| render_run(xml, run) }
end
end
def render_run(xml, run)
xml["w"].r do
if run.bold || run.italic || run.underline
xml["w"].rPr do
xml["w"].b if run.bold
xml["w"].i if run.italic
xml["w"].u("w:val" => "single") if run.underline
end
end
xml["w"].t(run.text, "xml:space" => "preserve")
end
end
def render_table(xml, table)
xml["w"].tbl do
xml["w"].tblPr do
xml["w"].tblW("w:w" => "0", "w:type" => "auto")
xml["w"].tblBorders do
%w[top left bottom right insideH insideV].each do |border|
xml["w"].send(border, "w:val" => "single", "w:sz" => "4", "w:color" => "000000")
end
end
end
table.rows.each { |row| render_table_row(xml, row) }
end
end
def render_table_row(xml, row)
xml["w"].tr do
row.cells.each { |cell| render_table_cell(xml, cell) }
end
end
def render_table_cell(xml, cell)
xml["w"].tc do
xml["w"].p do
cell.runs.each { |run| render_run(xml, run) }
end
end
end
end
end
end

View File

@@ -0,0 +1,58 @@
# frozen_string_literal: true
module Ezdoc
module Xml
class Numbering
NAMESPACE = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
def initialize(lists)
@lists = lists
end
def to_xml
builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
xml.numbering("xmlns:w" => NAMESPACE) do
xml.parent.namespace = xml.parent.namespace_definitions.find { |ns| ns.prefix == "w" }
@lists.each do |list|
render_abstract_num(xml, list)
render_num(xml, list)
end
end
end
builder.to_xml
end
private
def render_abstract_num(xml, list)
xml["w"].abstractNum("w:abstractNumId" => list.num_id.to_s) do
xml["w"].lvl("w:ilvl" => "0") do
xml["w"].start("w:val" => "1")
xml["w"].numFmt("w:val" => num_format(list.type))
xml["w"].lvlText("w:val" => lvl_text(list.type))
xml["w"].lvlJc("w:val" => "left")
xml["w"].pPr do
xml["w"].ind("w:left" => "720", "w:hanging" => "360")
end
end
end
end
def render_num(xml, list)
xml["w"].num("w:numId" => list.num_id.to_s) do
xml["w"].abstractNumId("w:val" => list.num_id.to_s)
end
end
def num_format(type)
type == :bullet ? "bullet" : "decimal"
end
def lvl_text(type)
type == :bullet ? "" : "%1."
end
end
end
end

View File

@@ -22,9 +22,21 @@ module Ezdoc
class DocumentRelationships
NAMESPACE = "http://schemas.openxmlformats.org/package/2006/relationships"
def initialize(has_numbering: false)
@has_numbering = has_numbering
end
def to_xml
builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
xml.Relationships(xmlns: NAMESPACE)
xml.Relationships(xmlns: NAMESPACE) do
if @has_numbering
xml.Relationship(
Id: "rId1",
Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering",
Target: "numbering.xml"
)
end
end
end
builder.to_xml
end