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

This commit is contained in:
2025-12-02 12:02:51 +01:00
parent 1fffecf0eb
commit 58492e9ef6
16 changed files with 786 additions and 23 deletions

View File

@@ -5,9 +5,10 @@ module Ezdoc
class ContentTypes
NAMESPACE = "http://schemas.openxmlformats.org/package/2006/content-types"
def initialize(has_numbering: false, images: [])
def initialize(has_numbering: false, images: [], has_styles: false)
@has_numbering = has_numbering
@images = images
@has_styles = has_styles
end
def to_xml
@@ -24,6 +25,12 @@ module Ezdoc
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",

View File

@@ -42,6 +42,11 @@ module Ezdoc
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
@@ -73,8 +78,9 @@ module Ezdoc
def render_text_run(xml, run)
xml["w"].r do
if run.bold || run.italic || run.underline
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

View File

@@ -21,24 +21,38 @@ module Ezdoc
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: [])
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
if @has_numbering
# rId1 = styles.xml (always first when present)
if @has_styles
xml.Relationship(
Id: "rId1",
Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering",
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,

View File

@@ -0,0 +1,66 @@
# frozen_string_literal: true
module Ezdoc
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