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

@@ -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