All checks were successful
CI Pipeline / build (pull_request) Successful in 13s
206 lines
6.3 KiB
Ruby
206 lines
6.3 KiB
Ruby
# 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)
|
|
when Nodes::Break
|
|
render_page_break(xml, node)
|
|
end
|
|
end
|
|
|
|
def render_page_break(xml, _node)
|
|
xml["w"].p do
|
|
xml["w"].r do
|
|
xml["w"].br("w:type" => "page")
|
|
end
|
|
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" => item.level.to_s)
|
|
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::Break
|
|
render_break(xml, run)
|
|
when Nodes::Hyperlink
|
|
render_hyperlink(xml, run)
|
|
when Nodes::Run
|
|
render_text_run(xml, run)
|
|
end
|
|
end
|
|
|
|
def render_hyperlink(xml, hyperlink)
|
|
xml["w"].hyperlink("r:id" => hyperlink.rid) do
|
|
hyperlink.runs.each { |run| render_run(xml, run) }
|
|
end
|
|
end
|
|
|
|
def render_break(xml, break_node)
|
|
xml["w"].r do
|
|
if break_node.page?
|
|
xml["w"].br("w:type" => "page")
|
|
else
|
|
xml["w"].br
|
|
end
|
|
end
|
|
end
|
|
|
|
def render_text_run(xml, run)
|
|
xml["w"].r do
|
|
if run.bold || run.italic || run.underline || run.strike || run.highlight || run.color || 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
|
|
xml["w"].strike if run.strike
|
|
xml["w"].highlight("w:val" => run.highlight) if run.highlight
|
|
xml["w"].color("w:val" => run.color) if run.color
|
|
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
|