Project rename
All checks were successful
CI Pipeline / build (push) Successful in 12s

This commit is contained in:
2025-12-02 13:21:13 +01:00
parent 29ebb9a8d1
commit dec346254c
36 changed files with 114 additions and 114 deletions

View File

@@ -0,0 +1,56 @@
# frozen_string_literal: true
module Notare
module Xml
class ContentTypes
NAMESPACE = "http://schemas.openxmlformats.org/package/2006/content-types"
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.Types(xmlns: NAMESPACE) do
xml.Default(Extension: "rels", ContentType: "application/vnd.openxmlformats-package.relationships+xml")
xml.Default(Extension: "xml", ContentType: "application/xml")
image_extensions.each do |ext, content_type|
xml.Default(Extension: ext, ContentType: content_type)
end
xml.Override(
PartName: "/word/document.xml",
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"
)
if @has_styles
xml.Override(
PartName: "/word/styles.xml",
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"
)
end
if @has_numbering
xml.Override(
PartName: "/word/numbering.xml",
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml"
)
end
end
end
builder.to_xml
end
private
def image_extensions
extensions = {}
@images.each do |image|
extensions[image.extension] ||= image.content_type
end
extensions
end
end
end
end

View File

@@ -0,0 +1,172 @@
# frozen_string_literal: true
module Notare
module Xml
class DocumentXml
NAMESPACES = {
"xmlns:w" => "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
"xmlns:r" => "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
"xmlns:wp" => "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
"xmlns:a" => "http://schemas.openxmlformats.org/drawingml/2006/main",
"xmlns:pic" => "http://schemas.openxmlformats.org/drawingml/2006/picture"
}.freeze
def initialize(nodes)
@nodes = nodes
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
@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
if para.style
xml["w"].pPr do
xml["w"].pStyle("w:val" => para.style.style_id)
end
end
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)
case run
when Nodes::Image
render_image(xml, run)
when Nodes::Run
render_text_run(xml, run)
end
end
def render_text_run(xml, run)
xml["w"].r do
if run.bold || run.italic || run.underline || run.style
xml["w"].rPr do
xml["w"].rStyle("w:val" => run.style.style_id) if run.style
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_image(xml, image)
xml["w"].r do
xml["w"].drawing do
xml["wp"].inline(distT: "0", distB: "0", distL: "0", distR: "0") do
xml["wp"].extent(cx: image.width_emu.to_s, cy: image.height_emu.to_s)
xml["wp"].docPr(id: image.doc_pr_id.to_s, name: image.filename)
xml["wp"].cNvGraphicFramePr do
xml["a"].graphicFrameLocks(noChangeAspect: "1")
end
xml["a"].graphic do
xml["a"].graphicData(uri: "http://schemas.openxmlformats.org/drawingml/2006/picture") do
xml["pic"].pic do
xml["pic"].nvPicPr do
xml["pic"].cNvPr(id: "0", name: image.filename)
xml["pic"].cNvPicPr
end
xml["pic"].blipFill do
xml["a"].blip("r:embed" => image.rid)
xml["a"].stretch do
xml["a"].fillRect
end
end
xml["pic"].spPr do
xml["a"].xfrm do
xml["a"].off(x: "0", y: "0")
xml["a"].ext(cx: image.width_emu.to_s, cy: image.height_emu.to_s)
end
xml["a"].prstGeom(prst: "rect") do
xml["a"].avLst
end
end
end
end
end
end
end
end
end
def render_table(xml, table)
column_count = table.rows.first&.cells&.size || 1
col_width = 5000 / column_count
xml["w"].tbl do
xml["w"].tblPr do
xml["w"].tblW("w:w" => "5000", "w:type" => "pct")
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:space" => "0", "w:color" => "000000")
end
end
end
xml["w"].tblGrid do
column_count.times do
xml["w"].gridCol("w:w" => col_width.to_s)
end
end
table.rows.each { |row| render_table_row(xml, row, col_width) }
end
end
def render_table_row(xml, row, col_width)
xml["w"].tr do
row.cells.each { |cell| render_table_cell(xml, cell, col_width) }
end
end
def render_table_cell(xml, cell, col_width)
xml["w"].tc do
xml["w"].tcPr do
xml["w"].tcW("w:w" => col_width.to_s, "w:type" => "pct")
end
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 Notare
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

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

View File

@@ -0,0 +1,66 @@
# frozen_string_literal: true
module Notare
module Xml
class StylesXml
NAMESPACE = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
ALIGNMENT_MAP = {
left: "left",
center: "center",
right: "right",
justify: "both"
}.freeze
def initialize(styles)
@styles = styles
end
def to_xml
builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
xml.styles("xmlns:w" => NAMESPACE) do
xml.parent.namespace = xml.parent.namespace_definitions.find { |ns| ns.prefix == "w" }
@styles.each_value do |style|
render_style(xml, style)
end
end
end
builder.to_xml
end
private
def render_style(xml, style)
style_type = style.paragraph_properties? ? "paragraph" : "character"
xml["w"].style("w:type" => style_type, "w:styleId" => style.style_id) do
xml["w"].name("w:val" => style.display_name)
render_paragraph_properties(xml, style) if style.paragraph_properties?
render_run_properties(xml, style) if style.text_properties?
end
end
def render_paragraph_properties(xml, style)
xml["w"].pPr do
xml["w"].jc("w:val" => ALIGNMENT_MAP[style.align]) if style.align
xml["w"].ind("w:left" => style.indent.to_s) if style.indent
xml["w"].spacing("w:before" => style.spacing_before.to_s) if style.spacing_before
xml["w"].spacing("w:after" => style.spacing_after.to_s) if style.spacing_after
end
end
def render_run_properties(xml, style)
xml["w"].rPr do
xml["w"].rFonts("w:ascii" => style.font, "w:hAnsi" => style.font) if style.font
xml["w"].sz("w:val" => style.size_half_points.to_s) if style.size
xml["w"].color("w:val" => style.color) if style.color
xml["w"].b if style.bold
xml["w"].i if style.italic
xml["w"].u("w:val" => "single") if style.underline
end
end
end
end
end