Implement table styles
All checks were successful
CI Pipeline / build (pull_request) Successful in 14s

This commit is contained in:
2025-12-03 12:14:31 +01:00
parent 67a60c8c6e
commit 9a70d91fd5
12 changed files with 555 additions and 13 deletions

View File

@@ -12,8 +12,15 @@ module Notare
justify: "both"
}.freeze
def initialize(styles)
TABLE_ALIGNMENT_MAP = {
left: "left",
center: "center",
right: "right"
}.freeze
def initialize(styles, table_styles = {})
@styles = styles
@table_styles = table_styles
end
def to_xml
@@ -24,6 +31,10 @@ module Notare
@styles.each_value do |style|
render_style(xml, style)
end
@table_styles.each_value do |style|
render_table_style(xml, style)
end
end
end
builder.to_xml
@@ -63,6 +74,59 @@ module Notare
xml["w"].highlight("w:val" => style.highlight) if style.highlight
end
end
def render_table_style(xml, style)
xml["w"].style("w:type" => "table", "w:styleId" => style.style_id) do
xml["w"].name("w:val" => style.display_name)
xml["w"].tblPr do
render_table_borders(xml, style.borders) if style.borders
render_table_shading(xml, style.shading) if style.shading
render_table_cell_margins(xml, style.cell_margins) if style.cell_margins
xml["w"].jc("w:val" => TABLE_ALIGNMENT_MAP[style.align]) if style.align
end
end
end
def render_table_borders(xml, borders)
xml["w"].tblBorders do
%i[top left bottom right insideH insideV].each do |pos|
border = borders == :none ? :none : (borders[pos] || borders)
render_single_border(xml, pos, border)
end
end
end
def render_single_border(xml, position, border)
if border == :none
xml["w"].send(position, "w:val" => "nil")
else
xml["w"].send(position,
"w:val" => border[:style],
"w:sz" => border[:size].to_s,
"w:space" => "0",
"w:color" => border[:color])
end
end
def render_table_shading(xml, color)
xml["w"].shd("w:val" => "clear", "w:color" => "auto", "w:fill" => color)
end
def render_table_cell_margins(xml, margins)
xml["w"].tblCellMar do
if margins.is_a?(Hash)
xml["w"].top("w:w" => margins[:top].to_s, "w:type" => "dxa") if margins[:top]
xml["w"].left("w:w" => margins[:left].to_s, "w:type" => "dxa") if margins[:left]
xml["w"].bottom("w:w" => margins[:bottom].to_s, "w:type" => "dxa") if margins[:bottom]
xml["w"].right("w:w" => margins[:right].to_s, "w:type" => "dxa") if margins[:right]
else
%i[top left bottom right].each do |side|
xml["w"].send(side, "w:w" => margins.to_s, "w:type" => "dxa")
end
end
end
end
end
end
end