This commit is contained in:
162
lib/notare/builder.rb
Normal file
162
lib/notare/builder.rb
Normal file
@@ -0,0 +1,162 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Notare
|
||||
module Builder
|
||||
def p(text = nil, style: nil, &block)
|
||||
para = Nodes::Paragraph.new(style: resolve_style(style))
|
||||
if block
|
||||
with_target(para, &block)
|
||||
elsif text
|
||||
para.add_run(Nodes::Run.new(text, **current_formatting))
|
||||
end
|
||||
@nodes << para
|
||||
end
|
||||
|
||||
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)
|
||||
validate_image_path!(path)
|
||||
img = register_image(path, width: width, height: height)
|
||||
@current_target.add_run(img)
|
||||
end
|
||||
|
||||
def b(&block)
|
||||
with_format(:bold, &block)
|
||||
end
|
||||
|
||||
def i(&block)
|
||||
with_format(:italic, &block)
|
||||
end
|
||||
|
||||
def u(&block)
|
||||
with_format(:underline, &block)
|
||||
end
|
||||
|
||||
def ul(&block)
|
||||
list(:bullet, &block)
|
||||
end
|
||||
|
||||
def ol(&block)
|
||||
list(:number, &block)
|
||||
end
|
||||
|
||||
def li(text = nil, &block)
|
||||
item = Nodes::ListItem.new([], list_type: @current_list.type, num_id: @current_list.num_id)
|
||||
if block
|
||||
with_target(item, &block)
|
||||
elsif text
|
||||
item.add_run(Nodes::Run.new(text, **current_formatting))
|
||||
end
|
||||
@current_list.add_item(item)
|
||||
end
|
||||
|
||||
def table(&block)
|
||||
tbl = Nodes::Table.new
|
||||
previous_table = @current_table
|
||||
@current_table = tbl
|
||||
block.call
|
||||
@current_table = previous_table
|
||||
@nodes << tbl
|
||||
end
|
||||
|
||||
def tr(&block)
|
||||
row = Nodes::TableRow.new
|
||||
previous_row = @current_row
|
||||
@current_row = row
|
||||
block.call
|
||||
@current_row = previous_row
|
||||
@current_table.add_row(row)
|
||||
end
|
||||
|
||||
def td(text = nil, &block)
|
||||
cell = Nodes::TableCell.new
|
||||
if block
|
||||
with_target(cell, &block)
|
||||
elsif text
|
||||
cell.add_run(Nodes::Run.new(text, **current_formatting))
|
||||
end
|
||||
@current_row.add_cell(cell)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def list(type, &block)
|
||||
@num_id_counter ||= 0
|
||||
@num_id_counter += 1
|
||||
|
||||
list_node = Nodes::List.new(type: type, num_id: @num_id_counter)
|
||||
previous_list = @current_list
|
||||
@current_list = list_node
|
||||
block.call
|
||||
@current_list = previous_list
|
||||
@nodes << list_node
|
||||
end
|
||||
|
||||
def with_format(format, &block)
|
||||
@format_stack ||= []
|
||||
@format_stack.push(format)
|
||||
block.call
|
||||
@format_stack.pop
|
||||
end
|
||||
|
||||
def with_target(target, &block)
|
||||
previous_target = @current_target
|
||||
@current_target = target
|
||||
block.call
|
||||
@current_target = previous_target
|
||||
end
|
||||
|
||||
def current_formatting
|
||||
@format_stack ||= []
|
||||
{
|
||||
bold: @format_stack.include?(:bold),
|
||||
italic: @format_stack.include?(:italic),
|
||||
underline: @format_stack.include?(:underline)
|
||||
}
|
||||
end
|
||||
|
||||
def validate_image_path!(path)
|
||||
raise ArgumentError, "Image file not found: #{path}" unless File.exist?(path)
|
||||
|
||||
ext = File.extname(path).downcase
|
||||
return if %w[.png .jpg .jpeg].include?(ext)
|
||||
|
||||
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
|
||||
85
lib/notare/document.rb
Normal file
85
lib/notare/document.rb
Normal file
@@ -0,0 +1,85 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Notare
|
||||
class Document
|
||||
include Builder
|
||||
|
||||
attr_reader :nodes, :styles
|
||||
|
||||
def self.create(path, &block)
|
||||
doc = new
|
||||
block.call(doc)
|
||||
doc.save(path)
|
||||
doc
|
||||
end
|
||||
|
||||
def initialize
|
||||
@nodes = []
|
||||
@format_stack = []
|
||||
@current_target = nil
|
||||
@current_list = nil
|
||||
@current_table = nil
|
||||
@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)
|
||||
Package.new(self).save(path)
|
||||
end
|
||||
|
||||
def lists
|
||||
@nodes.select { |n| n.is_a?(Nodes::List) }
|
||||
end
|
||||
|
||||
def images
|
||||
@images.values
|
||||
end
|
||||
|
||||
def register_image(path, width: nil, height: nil)
|
||||
return @images[path] if @images[path]
|
||||
|
||||
rid = next_image_rid
|
||||
width_emu, height_emu = ImageDimensions.calculate_emus(path, width: width, height: height)
|
||||
image = Nodes::Image.new(path, rid: rid, width_emu: width_emu, height_emu: height_emu)
|
||||
@images[path] = image
|
||||
image
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def next_image_rid
|
||||
# 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 (spacing_before ensures they're rendered as paragraph styles)
|
||||
define_style :heading1, size: 24, bold: true, spacing_before: 240, spacing_after: 120
|
||||
define_style :heading2, size: 18, bold: true, spacing_before: 200, spacing_after: 100
|
||||
define_style :heading3, size: 14, bold: true, spacing_before: 160, spacing_after: 80
|
||||
define_style :heading4, size: 12, bold: true, spacing_before: 120, spacing_after: 60
|
||||
define_style :heading5, size: 11, bold: true, italic: true, spacing_before: 100, spacing_after: 40
|
||||
define_style :heading6, size: 10, bold: true, italic: true, spacing_before: 80, spacing_after: 40
|
||||
|
||||
# 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
|
||||
55
lib/notare/image_dimensions.rb
Normal file
55
lib/notare/image_dimensions.rb
Normal file
@@ -0,0 +1,55 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "fastimage"
|
||||
|
||||
module Notare
|
||||
class ImageDimensions
|
||||
EMUS_PER_INCH = 914_400
|
||||
DEFAULT_DPI = 96
|
||||
|
||||
def self.read(path)
|
||||
FastImage.size(path) || raise(ArgumentError, "Could not read image dimensions: #{path}")
|
||||
end
|
||||
|
||||
def self.calculate_emus(path, width: nil, height: nil)
|
||||
native_width, native_height = read(path)
|
||||
calculate_dimensions_emu(native_width, native_height, width, height)
|
||||
end
|
||||
|
||||
def self.calculate_dimensions_emu(native_width, native_height, width, height)
|
||||
if width && height
|
||||
[parse_dimension(width), parse_dimension(height)]
|
||||
elsif width
|
||||
w = parse_dimension(width)
|
||||
ratio = native_height.to_f / native_width
|
||||
[w, (w * ratio).to_i]
|
||||
elsif height
|
||||
h = parse_dimension(height)
|
||||
ratio = native_width.to_f / native_height
|
||||
[(h * ratio).to_i, h]
|
||||
else
|
||||
pixels_to_emus(native_width, native_height)
|
||||
end
|
||||
end
|
||||
|
||||
def self.parse_dimension(value)
|
||||
case value
|
||||
when Integer
|
||||
pixels_to_emus(value, 0).first
|
||||
when /\A(\d+(?:\.\d+)?)\s*in\z/i
|
||||
(::Regexp.last_match(1).to_f * EMUS_PER_INCH).to_i
|
||||
when /\A(\d+(?:\.\d+)?)\s*cm\z/i
|
||||
(::Regexp.last_match(1).to_f * 360_000).to_i
|
||||
when /\A(\d+(?:\.\d+)?)\s*px\z/i, /\A(\d+)\z/
|
||||
pixels_to_emus(::Regexp.last_match(1).to_i, 0).first
|
||||
else
|
||||
raise ArgumentError, "Invalid dimension format: #{value}. Use '2in', '5cm', '100px', or integer pixels."
|
||||
end
|
||||
end
|
||||
|
||||
def self.pixels_to_emus(width_px, height_px)
|
||||
emu_per_pixel = EMUS_PER_INCH / DEFAULT_DPI
|
||||
[(width_px * emu_per_pixel).to_i, (height_px * emu_per_pixel).to_i]
|
||||
end
|
||||
end
|
||||
end
|
||||
9
lib/notare/nodes/base.rb
Normal file
9
lib/notare/nodes/base.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Notare
|
||||
module Nodes
|
||||
class Base
|
||||
# Base class for all document nodes
|
||||
end
|
||||
end
|
||||
end
|
||||
38
lib/notare/nodes/image.rb
Normal file
38
lib/notare/nodes/image.rb
Normal file
@@ -0,0 +1,38 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Notare
|
||||
module Nodes
|
||||
class Image < Base
|
||||
attr_reader :path, :width_emu, :height_emu, :rid, :filename
|
||||
|
||||
EMUS_PER_INCH = 914_400
|
||||
EMUS_PER_CM = 360_000
|
||||
DEFAULT_DPI = 96
|
||||
|
||||
def initialize(path, rid:, width_emu:, height_emu:)
|
||||
super()
|
||||
@path = path
|
||||
@rid = rid
|
||||
@width_emu = width_emu
|
||||
@height_emu = height_emu
|
||||
@filename = "image#{rid.delete_prefix("rId")}.#{extension}"
|
||||
end
|
||||
|
||||
def extension
|
||||
case File.extname(@path).downcase
|
||||
when ".png" then "png"
|
||||
when ".jpg", ".jpeg" then "jpeg"
|
||||
else raise ArgumentError, "Unsupported image format: #{File.extname(@path)}"
|
||||
end
|
||||
end
|
||||
|
||||
def content_type
|
||||
extension == "png" ? "image/png" : "image/jpeg"
|
||||
end
|
||||
|
||||
def doc_pr_id
|
||||
rid.delete_prefix("rId").to_i
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
20
lib/notare/nodes/list.rb
Normal file
20
lib/notare/nodes/list.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Notare
|
||||
module Nodes
|
||||
class List < Base
|
||||
attr_reader :items, :type, :num_id
|
||||
|
||||
def initialize(type:, num_id:)
|
||||
super()
|
||||
@type = type
|
||||
@num_id = num_id
|
||||
@items = []
|
||||
end
|
||||
|
||||
def add_item(item)
|
||||
@items << item
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
20
lib/notare/nodes/list_item.rb
Normal file
20
lib/notare/nodes/list_item.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Notare
|
||||
module Nodes
|
||||
class ListItem < Base
|
||||
attr_reader :runs, :list_type, :num_id
|
||||
|
||||
def initialize(runs = [], list_type:, num_id:)
|
||||
super()
|
||||
@runs = runs
|
||||
@list_type = list_type
|
||||
@num_id = num_id
|
||||
end
|
||||
|
||||
def add_run(run)
|
||||
@runs << run
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
19
lib/notare/nodes/paragraph.rb
Normal file
19
lib/notare/nodes/paragraph.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Notare
|
||||
module Nodes
|
||||
class Paragraph < Base
|
||||
attr_reader :runs, :style
|
||||
|
||||
def initialize(runs = [], style: nil)
|
||||
super()
|
||||
@runs = runs
|
||||
@style = style
|
||||
end
|
||||
|
||||
def add_run(run)
|
||||
@runs << run
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
18
lib/notare/nodes/run.rb
Normal file
18
lib/notare/nodes/run.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Notare
|
||||
module Nodes
|
||||
class Run < Base
|
||||
attr_reader :text, :bold, :italic, :underline, :style
|
||||
|
||||
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
|
||||
end
|
||||
18
lib/notare/nodes/table.rb
Normal file
18
lib/notare/nodes/table.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Notare
|
||||
module Nodes
|
||||
class Table < Base
|
||||
attr_reader :rows
|
||||
|
||||
def initialize
|
||||
super
|
||||
@rows = []
|
||||
end
|
||||
|
||||
def add_row(row)
|
||||
@rows << row
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
18
lib/notare/nodes/table_cell.rb
Normal file
18
lib/notare/nodes/table_cell.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Notare
|
||||
module Nodes
|
||||
class TableCell < Base
|
||||
attr_reader :runs
|
||||
|
||||
def initialize
|
||||
super
|
||||
@runs = []
|
||||
end
|
||||
|
||||
def add_run(run)
|
||||
@runs << run
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
18
lib/notare/nodes/table_row.rb
Normal file
18
lib/notare/nodes/table_row.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Notare
|
||||
module Nodes
|
||||
class TableRow < Base
|
||||
attr_reader :cells
|
||||
|
||||
def initialize
|
||||
super
|
||||
@cells = []
|
||||
end
|
||||
|
||||
def add_cell(cell)
|
||||
@cells << cell
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
63
lib/notare/package.rb
Normal file
63
lib/notare/package.rb
Normal file
@@ -0,0 +1,63 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "zip"
|
||||
|
||||
module Notare
|
||||
class Package
|
||||
def initialize(document)
|
||||
@document = document
|
||||
end
|
||||
|
||||
def save(path)
|
||||
Zip::File.open(path, Zip::File::CREATE) do |zipfile|
|
||||
zipfile.get_output_stream("[Content_Types].xml") { |f| f.write(content_types_xml) }
|
||||
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?
|
||||
|
||||
images.each do |image|
|
||||
zipfile.get_output_stream("word/media/#{image.filename}") do |f|
|
||||
f.write(File.binread(image.path))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def lists?
|
||||
@document.lists.any?
|
||||
end
|
||||
|
||||
def images
|
||||
@document.images
|
||||
end
|
||||
|
||||
def content_types_xml
|
||||
Xml::ContentTypes.new(has_numbering: lists?, images: images, has_styles: true).to_xml
|
||||
end
|
||||
|
||||
def relationships_xml
|
||||
Xml::Relationships.new.to_xml
|
||||
end
|
||||
|
||||
def document_relationships_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
|
||||
end
|
||||
end
|
||||
65
lib/notare/style.rb
Normal file
65
lib/notare/style.rb
Normal file
@@ -0,0 +1,65 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Notare
|
||||
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
lib/notare/version.rb
Normal file
5
lib/notare/version.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Notare
|
||||
VERSION = "0.0.1"
|
||||
end
|
||||
56
lib/notare/xml/content_types.rb
Normal file
56
lib/notare/xml/content_types.rb
Normal 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
|
||||
172
lib/notare/xml/document_xml.rb
Normal file
172
lib/notare/xml/document_xml.rb
Normal 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
|
||||
58
lib/notare/xml/numbering.rb
Normal file
58
lib/notare/xml/numbering.rb
Normal 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
|
||||
69
lib/notare/xml/relationships.rb
Normal file
69
lib/notare/xml/relationships.rb
Normal 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
|
||||
66
lib/notare/xml/styles_xml.rb
Normal file
66
lib/notare/xml/styles_xml.rb
Normal 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
|
||||
Reference in New Issue
Block a user