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

114
lib/ezdoc/builder.rb Normal file
View File

@@ -0,0 +1,114 @@
# frozen_string_literal: true
module Ezdoc
module Builder
def p(text = nil, &block)
para = Nodes::Paragraph.new
if block
with_target(para, &block)
elsif text
para.add_run(Nodes::Run.new(text, **current_formatting))
end
@nodes << para
end
def text(value)
@current_target.add_run(Nodes::Run.new(value, **current_formatting))
end
def b(&block)
with_format(:bold, &block)
end
def i(&block)
with_format(:italic, &block)
end
def u(&block)
with_format(:underline, &block)
end
def ul(&block)
list(:bullet, &block)
end
def ol(&block)
list(:number, &block)
end
def li(text = nil, &block)
item = Nodes::ListItem.new([], list_type: @current_list.type, num_id: @current_list.num_id)
if block
with_target(item, &block)
elsif text
item.add_run(Nodes::Run.new(text, **current_formatting))
end
@current_list.add_item(item)
end
def table(&block)
tbl = Nodes::Table.new
previous_table = @current_table
@current_table = tbl
block.call
@current_table = previous_table
@nodes << tbl
end
def tr(&block)
row = Nodes::TableRow.new
previous_row = @current_row
@current_row = row
block.call
@current_row = previous_row
@current_table.add_row(row)
end
def td(text = nil, &block)
cell = Nodes::TableCell.new
if block
with_target(cell, &block)
elsif text
cell.add_run(Nodes::Run.new(text, **current_formatting))
end
@current_row.add_cell(cell)
end
private
def list(type, &block)
@num_id_counter ||= 0
@num_id_counter += 1
list_node = Nodes::List.new(type: type, num_id: @num_id_counter)
previous_list = @current_list
@current_list = list_node
block.call
@current_list = previous_list
@nodes << list_node
end
def with_format(format, &block)
@format_stack ||= []
@format_stack.push(format)
block.call
@format_stack.pop
end
def with_target(target, &block)
previous_target = @current_target
@current_target = target
block.call
@current_target = previous_target
end
def current_formatting
@format_stack ||= []
{
bold: @format_stack.include?(:bold),
italic: @format_stack.include?(:italic),
underline: @format_stack.include?(:underline)
}
end
end
end

View File

@@ -2,7 +2,9 @@
module Ezdoc
class Document
attr_reader :content
include Builder
attr_reader :nodes
def self.create(path, &block)
doc = new
@@ -12,15 +14,21 @@ module Ezdoc
end
def initialize
@content = []
end
def text(value)
@content << { text: value }
@nodes = []
@format_stack = []
@current_target = nil
@current_list = nil
@current_table = nil
@current_row = nil
@num_id_counter = 0
end
def save(path)
Package.new(self).save(path)
end
def lists
@nodes.select { |n| n.is_a?(Nodes::List) }
end
end
end

9
lib/ezdoc/nodes/base.rb Normal file
View File

@@ -0,0 +1,9 @@
# frozen_string_literal: true
module Ezdoc
module Nodes
class Base
# Base class for all document nodes
end
end
end

20
lib/ezdoc/nodes/list.rb Normal file
View File

@@ -0,0 +1,20 @@
# frozen_string_literal: true
module Ezdoc
module Nodes
class List < Base
attr_reader :items, :type, :num_id
def initialize(type:, num_id:)
super()
@type = type
@num_id = num_id
@items = []
end
def add_item(item)
@items << item
end
end
end
end

View File

@@ -0,0 +1,20 @@
# frozen_string_literal: true
module Ezdoc
module Nodes
class ListItem < Base
attr_reader :runs, :list_type, :num_id
def initialize(runs = [], list_type:, num_id:)
super()
@runs = runs
@list_type = list_type
@num_id = num_id
end
def add_run(run)
@runs << run
end
end
end
end

View File

@@ -0,0 +1,18 @@
# frozen_string_literal: true
module Ezdoc
module Nodes
class Paragraph < Base
attr_reader :runs
def initialize(runs = [])
super()
@runs = runs
end
def add_run(run)
@runs << run
end
end
end
end

17
lib/ezdoc/nodes/run.rb Normal file
View File

@@ -0,0 +1,17 @@
# frozen_string_literal: true
module Ezdoc
module Nodes
class Run < Base
attr_reader :text, :bold, :italic, :underline
def initialize(text, bold: false, italic: false, underline: false)
super()
@text = text
@bold = bold
@italic = italic
@underline = underline
end
end
end
end

18
lib/ezdoc/nodes/table.rb Normal file
View File

@@ -0,0 +1,18 @@
# frozen_string_literal: true
module Ezdoc
module Nodes
class Table < Base
attr_reader :rows
def initialize
super
@rows = []
end
def add_row(row)
@rows << row
end
end
end
end

View File

@@ -0,0 +1,18 @@
# frozen_string_literal: true
module Ezdoc
module Nodes
class TableCell < Base
attr_reader :runs
def initialize
super
@runs = []
end
def add_run(run)
@runs << run
end
end
end
end

View File

@@ -0,0 +1,18 @@
# frozen_string_literal: true
module Ezdoc
module Nodes
class TableRow < Base
attr_reader :cells
def initialize
super
@cells = []
end
def add_cell(cell)
@cells << cell
end
end
end
end

View File

@@ -14,13 +14,19 @@ module Ezdoc
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.to_xml
Xml::ContentTypes.new(has_numbering: lists?).to_xml
end
def relationships_xml
@@ -28,11 +34,15 @@ module Ezdoc
end
def document_relationships_xml
Xml::DocumentRelationships.new.to_xml
Xml::DocumentRelationships.new(has_numbering: lists?).to_xml
end
def document_xml
Xml::DocumentXml.new(@document.content).to_xml
Xml::DocumentXml.new(@document.nodes).to_xml
end
def numbering_xml
Xml::Numbering.new(@document.lists).to_xml
end
end
end

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