This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
|
||||
module Ezdoc
|
||||
module Builder
|
||||
def p(text = nil, &block)
|
||||
para = Nodes::Paragraph.new
|
||||
def p(text = nil, style: nil, &block)
|
||||
para = Nodes::Paragraph.new(style: resolve_style(style))
|
||||
if block
|
||||
with_target(para, &block)
|
||||
elsif text
|
||||
@@ -12,8 +12,34 @@ module Ezdoc
|
||||
@nodes << para
|
||||
end
|
||||
|
||||
def text(value)
|
||||
@current_target.add_run(Nodes::Run.new(value, **current_formatting))
|
||||
def text(value, style: nil)
|
||||
formatting = current_formatting.merge(style: resolve_style(style))
|
||||
@current_target.add_run(Nodes::Run.new(value, **formatting))
|
||||
end
|
||||
|
||||
# Heading shortcuts
|
||||
def h1(text = nil, &block)
|
||||
p(text, style: :heading1, &block)
|
||||
end
|
||||
|
||||
def h2(text = nil, &block)
|
||||
p(text, style: :heading2, &block)
|
||||
end
|
||||
|
||||
def h3(text = nil, &block)
|
||||
p(text, style: :heading3, &block)
|
||||
end
|
||||
|
||||
def h4(text = nil, &block)
|
||||
p(text, style: :heading4, &block)
|
||||
end
|
||||
|
||||
def h5(text = nil, &block)
|
||||
p(text, style: :heading5, &block)
|
||||
end
|
||||
|
||||
def h6(text = nil, &block)
|
||||
p(text, style: :heading6, &block)
|
||||
end
|
||||
|
||||
def image(path, width: nil, height: nil)
|
||||
@@ -125,5 +151,12 @@ module Ezdoc
|
||||
|
||||
raise ArgumentError, "Unsupported image format: #{ext}. Use PNG or JPEG."
|
||||
end
|
||||
|
||||
def resolve_style(style_or_name)
|
||||
return nil if style_or_name.nil?
|
||||
return style_or_name if style_or_name.is_a?(Style)
|
||||
|
||||
style(style_or_name) || raise(ArgumentError, "Unknown style: #{style_or_name}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,7 +4,7 @@ module Ezdoc
|
||||
class Document
|
||||
include Builder
|
||||
|
||||
attr_reader :nodes
|
||||
attr_reader :nodes, :styles
|
||||
|
||||
def self.create(path, &block)
|
||||
doc = new
|
||||
@@ -22,6 +22,16 @@ module Ezdoc
|
||||
@current_row = nil
|
||||
@num_id_counter = 0
|
||||
@images = {}
|
||||
@styles = {}
|
||||
register_built_in_styles
|
||||
end
|
||||
|
||||
def define_style(name, **properties)
|
||||
@styles[name] = Style.new(name, **properties)
|
||||
end
|
||||
|
||||
def style(name)
|
||||
@styles[name]
|
||||
end
|
||||
|
||||
def save(path)
|
||||
@@ -49,8 +59,27 @@ module Ezdoc
|
||||
private
|
||||
|
||||
def next_image_rid
|
||||
base = lists.any? ? 2 : 1
|
||||
# rId1 = styles.xml (always present)
|
||||
# rId2 = numbering.xml (if lists present)
|
||||
# rId3+ = images
|
||||
base = lists.any? ? 3 : 2
|
||||
"rId#{base + @images.size}"
|
||||
end
|
||||
|
||||
def register_built_in_styles
|
||||
# Headings
|
||||
define_style :heading1, size: 24, bold: true
|
||||
define_style :heading2, size: 18, bold: true
|
||||
define_style :heading3, size: 14, bold: true
|
||||
define_style :heading4, size: 12, bold: true
|
||||
define_style :heading5, size: 11, bold: true, italic: true
|
||||
define_style :heading6, size: 10, bold: true, italic: true
|
||||
|
||||
# Other built-in styles
|
||||
define_style :title, size: 26, bold: true, align: :center
|
||||
define_style :subtitle, size: 15, italic: true, color: "666666"
|
||||
define_style :quote, italic: true, color: "666666", indent: 720
|
||||
define_style :code, font: "Courier New", size: 10
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,11 +3,12 @@
|
||||
module Ezdoc
|
||||
module Nodes
|
||||
class Paragraph < Base
|
||||
attr_reader :runs
|
||||
attr_reader :runs, :style
|
||||
|
||||
def initialize(runs = [])
|
||||
def initialize(runs = [], style: nil)
|
||||
super()
|
||||
@runs = runs
|
||||
@style = style
|
||||
end
|
||||
|
||||
def add_run(run)
|
||||
|
||||
@@ -3,14 +3,15 @@
|
||||
module Ezdoc
|
||||
module Nodes
|
||||
class Run < Base
|
||||
attr_reader :text, :bold, :italic, :underline
|
||||
attr_reader :text, :bold, :italic, :underline, :style
|
||||
|
||||
def initialize(text, bold: false, italic: false, underline: false)
|
||||
def initialize(text, bold: false, italic: false, underline: false, style: nil)
|
||||
super()
|
||||
@text = text
|
||||
@bold = bold
|
||||
@italic = italic
|
||||
@underline = underline
|
||||
@style = style
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -14,6 +14,7 @@ module Ezdoc
|
||||
zipfile.get_output_stream("_rels/.rels") { |f| f.write(relationships_xml) }
|
||||
zipfile.get_output_stream("word/_rels/document.xml.rels") { |f| f.write(document_relationships_xml) }
|
||||
zipfile.get_output_stream("word/document.xml") { |f| f.write(document_xml) }
|
||||
zipfile.get_output_stream("word/styles.xml") { |f| f.write(styles_xml) }
|
||||
|
||||
zipfile.get_output_stream("word/numbering.xml") { |f| f.write(numbering_xml) } if lists?
|
||||
|
||||
@@ -36,7 +37,7 @@ module Ezdoc
|
||||
end
|
||||
|
||||
def content_types_xml
|
||||
Xml::ContentTypes.new(has_numbering: lists?, images: images).to_xml
|
||||
Xml::ContentTypes.new(has_numbering: lists?, images: images, has_styles: true).to_xml
|
||||
end
|
||||
|
||||
def relationships_xml
|
||||
@@ -44,13 +45,17 @@ module Ezdoc
|
||||
end
|
||||
|
||||
def document_relationships_xml
|
||||
Xml::DocumentRelationships.new(has_numbering: lists?, images: images).to_xml
|
||||
Xml::DocumentRelationships.new(has_numbering: lists?, images: images, has_styles: true).to_xml
|
||||
end
|
||||
|
||||
def document_xml
|
||||
Xml::DocumentXml.new(@document.nodes).to_xml
|
||||
end
|
||||
|
||||
def styles_xml
|
||||
Xml::StylesXml.new(@document.styles).to_xml
|
||||
end
|
||||
|
||||
def numbering_xml
|
||||
Xml::Numbering.new(@document.lists).to_xml
|
||||
end
|
||||
|
||||
65
lib/ezdoc/style.rb
Normal file
65
lib/ezdoc/style.rb
Normal file
@@ -0,0 +1,65 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Ezdoc
|
||||
class Style
|
||||
attr_reader :name, :bold, :italic, :underline, :color, :size, :font,
|
||||
:align, :indent, :spacing_before, :spacing_after
|
||||
|
||||
ALIGNMENTS = %i[left center right justify].freeze
|
||||
|
||||
def initialize(name, bold: nil, italic: nil, underline: nil, color: nil,
|
||||
size: nil, font: nil, align: nil, indent: nil,
|
||||
spacing_before: nil, spacing_after: nil)
|
||||
@name = name
|
||||
@bold = bold
|
||||
@italic = italic
|
||||
@underline = underline
|
||||
@color = normalize_color(color)
|
||||
@size = size
|
||||
@font = font
|
||||
@align = validate_align(align)
|
||||
@indent = indent
|
||||
@spacing_before = spacing_before
|
||||
@spacing_after = spacing_after
|
||||
end
|
||||
|
||||
def style_id
|
||||
name.to_s.split("_").map(&:capitalize).join
|
||||
end
|
||||
|
||||
def display_name
|
||||
name.to_s.split("_").map(&:capitalize).join(" ")
|
||||
end
|
||||
|
||||
def paragraph_properties?
|
||||
!!(align || indent || spacing_before || spacing_after)
|
||||
end
|
||||
|
||||
def text_properties?
|
||||
!!(bold || italic || underline || color || size || font)
|
||||
end
|
||||
|
||||
# Size in half-points for OOXML (14pt = 28 half-points)
|
||||
def size_half_points
|
||||
size ? (size * 2).to_i : nil
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def normalize_color(color)
|
||||
return nil if color.nil?
|
||||
|
||||
hex = color.to_s.sub(/^#/, "").upcase
|
||||
return hex if hex.match?(/\A[0-9A-F]{6}\z/)
|
||||
|
||||
raise ArgumentError, "Invalid color: #{color}. Use 6-digit hex (e.g., 'FF0000')"
|
||||
end
|
||||
|
||||
def validate_align(align)
|
||||
return nil if align.nil?
|
||||
return align if ALIGNMENTS.include?(align)
|
||||
|
||||
raise ArgumentError, "Invalid alignment: #{align}. Use #{ALIGNMENTS.join(", ")}"
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -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",
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
66
lib/ezdoc/xml/styles_xml.rb
Normal file
66
lib/ezdoc/xml/styles_xml.rb
Normal 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
|
||||
Reference in New Issue
Block a user