Implement many more nodes
All checks were successful
CI Pipeline / build (pull_request) Successful in 12s
All checks were successful
CI Pipeline / build (pull_request) Successful in 12s
Adds these new styling and formatting nodes * strike * highlight * linebreaks * pagebreaks * Hyperlinks
This commit is contained in:
@@ -4,6 +4,8 @@ require "nokogiri"
|
||||
|
||||
require_relative "notare/version"
|
||||
require_relative "notare/nodes/base"
|
||||
require_relative "notare/nodes/break"
|
||||
require_relative "notare/nodes/hyperlink"
|
||||
require_relative "notare/nodes/run"
|
||||
require_relative "notare/nodes/image"
|
||||
require_relative "notare/nodes/paragraph"
|
||||
|
||||
@@ -60,6 +60,30 @@ module Notare
|
||||
with_format(:underline, &block)
|
||||
end
|
||||
|
||||
def s(&block)
|
||||
with_format(:strike, &block)
|
||||
end
|
||||
|
||||
def br
|
||||
@current_target.add_run(Nodes::Break.new(type: :line))
|
||||
end
|
||||
|
||||
def page_break
|
||||
@nodes << Nodes::Break.new(type: :page)
|
||||
end
|
||||
|
||||
def link(url, text = nil, &block)
|
||||
hyperlink = register_hyperlink(url)
|
||||
if block
|
||||
with_target(hyperlink, &block)
|
||||
elsif text
|
||||
hyperlink.add_run(Nodes::Run.new(text, underline: true, color: "0000FF"))
|
||||
else
|
||||
hyperlink.add_run(Nodes::Run.new(url, underline: true, color: "0000FF"))
|
||||
end
|
||||
@current_target.add_run(hyperlink)
|
||||
end
|
||||
|
||||
def ul(&block)
|
||||
list(:bullet, &block)
|
||||
end
|
||||
@@ -111,6 +135,7 @@ module Notare
|
||||
def list(type, &block)
|
||||
@num_id_counter ||= 0
|
||||
@num_id_counter += 1
|
||||
mark_has_lists!
|
||||
|
||||
list_node = Nodes::List.new(type: type, num_id: @num_id_counter)
|
||||
previous_list = @current_list
|
||||
@@ -139,7 +164,8 @@ module Notare
|
||||
{
|
||||
bold: @format_stack.include?(:bold),
|
||||
italic: @format_stack.include?(:italic),
|
||||
underline: @format_stack.include?(:underline)
|
||||
underline: @format_stack.include?(:underline),
|
||||
strike: @format_stack.include?(:strike)
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ module Notare
|
||||
class Document
|
||||
include Builder
|
||||
|
||||
attr_reader :nodes, :styles
|
||||
attr_reader :nodes, :styles, :hyperlinks
|
||||
|
||||
def self.create(path, &block)
|
||||
doc = new
|
||||
@@ -21,7 +21,9 @@ module Notare
|
||||
@current_table = nil
|
||||
@current_row = nil
|
||||
@num_id_counter = 0
|
||||
@has_lists = false
|
||||
@images = {}
|
||||
@hyperlinks = []
|
||||
@styles = {}
|
||||
register_built_in_styles
|
||||
end
|
||||
@@ -42,10 +44,25 @@ module Notare
|
||||
@nodes.select { |n| n.is_a?(Nodes::List) }
|
||||
end
|
||||
|
||||
def uses_lists?
|
||||
@has_lists
|
||||
end
|
||||
|
||||
def mark_has_lists!
|
||||
@has_lists = true
|
||||
end
|
||||
|
||||
def images
|
||||
@images.values
|
||||
end
|
||||
|
||||
def register_hyperlink(url)
|
||||
rid = next_hyperlink_rid
|
||||
hyperlink = Nodes::Hyperlink.new(url: url, rid: rid)
|
||||
@hyperlinks << hyperlink
|
||||
hyperlink
|
||||
end
|
||||
|
||||
def register_image(path, width: nil, height: nil)
|
||||
return @images[path] if @images[path]
|
||||
|
||||
@@ -61,11 +78,17 @@ module Notare
|
||||
def next_image_rid
|
||||
# rId1 = styles.xml (always present)
|
||||
# rId2 = numbering.xml (if lists present)
|
||||
# rId3+ = images
|
||||
base = lists.any? ? 3 : 2
|
||||
# rId3+ = images, then hyperlinks
|
||||
base = @has_lists ? 3 : 2
|
||||
"rId#{base + @images.size}"
|
||||
end
|
||||
|
||||
def next_hyperlink_rid
|
||||
# Hyperlinks come after images
|
||||
base = @has_lists ? 3 : 2
|
||||
"rId#{base + @images.size + @hyperlinks.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
|
||||
|
||||
18
lib/notare/nodes/break.rb
Normal file
18
lib/notare/nodes/break.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Notare
|
||||
module Nodes
|
||||
class Break < Base
|
||||
attr_reader :type
|
||||
|
||||
def initialize(type: :line)
|
||||
super()
|
||||
@type = type
|
||||
end
|
||||
|
||||
def page?
|
||||
type == :page
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
20
lib/notare/nodes/hyperlink.rb
Normal file
20
lib/notare/nodes/hyperlink.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Notare
|
||||
module Nodes
|
||||
class Hyperlink < Base
|
||||
attr_reader :url, :rid, :runs
|
||||
|
||||
def initialize(url:, rid:)
|
||||
super()
|
||||
@url = url
|
||||
@rid = rid
|
||||
@runs = []
|
||||
end
|
||||
|
||||
def add_run(run)
|
||||
@runs << run
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -3,14 +3,18 @@
|
||||
module Notare
|
||||
module Nodes
|
||||
class Run < Base
|
||||
attr_reader :text, :bold, :italic, :underline, :style
|
||||
attr_reader :text, :bold, :italic, :underline, :strike, :highlight, :color, :style
|
||||
|
||||
def initialize(text, bold: false, italic: false, underline: false, style: nil)
|
||||
def initialize(text, bold: false, italic: false, underline: false,
|
||||
strike: false, highlight: nil, color: nil, style: nil)
|
||||
super()
|
||||
@text = text
|
||||
@bold = bold
|
||||
@italic = italic
|
||||
@underline = underline
|
||||
@strike = strike
|
||||
@highlight = highlight
|
||||
@color = color
|
||||
@style = style
|
||||
end
|
||||
end
|
||||
|
||||
@@ -29,13 +29,17 @@ module Notare
|
||||
private
|
||||
|
||||
def lists?
|
||||
@document.lists.any?
|
||||
@document.uses_lists?
|
||||
end
|
||||
|
||||
def images
|
||||
@document.images
|
||||
end
|
||||
|
||||
def hyperlinks
|
||||
@document.hyperlinks
|
||||
end
|
||||
|
||||
def content_types_xml
|
||||
Xml::ContentTypes.new(has_numbering: lists?, images: images, has_styles: true).to_xml
|
||||
end
|
||||
@@ -45,7 +49,9 @@ module Notare
|
||||
end
|
||||
|
||||
def document_relationships_xml
|
||||
Xml::DocumentRelationships.new(has_numbering: lists?, images: images, has_styles: true).to_xml
|
||||
Xml::DocumentRelationships.new(
|
||||
has_numbering: lists?, images: images, hyperlinks: hyperlinks, has_styles: true
|
||||
).to_xml
|
||||
end
|
||||
|
||||
def document_xml
|
||||
|
||||
@@ -2,18 +2,24 @@
|
||||
|
||||
module Notare
|
||||
class Style
|
||||
attr_reader :name, :bold, :italic, :underline, :color, :size, :font,
|
||||
attr_reader :name, :bold, :italic, :underline, :strike, :highlight, :color, :size, :font,
|
||||
:align, :indent, :spacing_before, :spacing_after
|
||||
|
||||
ALIGNMENTS = %i[left center right justify].freeze
|
||||
HIGHLIGHT_COLORS = %w[
|
||||
black blue cyan darkBlue darkCyan darkGray darkGreen darkMagenta
|
||||
darkRed darkYellow green lightGray magenta red white yellow
|
||||
].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)
|
||||
def initialize(name, bold: nil, italic: nil, underline: nil, strike: nil,
|
||||
highlight: 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
|
||||
@strike = strike
|
||||
@highlight = validate_highlight(highlight)
|
||||
@color = normalize_color(color)
|
||||
@size = size
|
||||
@font = font
|
||||
@@ -36,7 +42,7 @@ module Notare
|
||||
end
|
||||
|
||||
def text_properties?
|
||||
!!(bold || italic || underline || color || size || font)
|
||||
!!(bold || italic || underline || strike || highlight || color || size || font)
|
||||
end
|
||||
|
||||
# Size in half-points for OOXML (14pt = 28 half-points)
|
||||
@@ -61,5 +67,14 @@ module Notare
|
||||
|
||||
raise ArgumentError, "Invalid alignment: #{align}. Use #{ALIGNMENTS.join(", ")}"
|
||||
end
|
||||
|
||||
def validate_highlight(highlight)
|
||||
return nil if highlight.nil?
|
||||
|
||||
color = highlight.to_s
|
||||
return color if HIGHLIGHT_COLORS.include?(color)
|
||||
|
||||
raise ArgumentError, "Invalid highlight color: #{highlight}. Use one of: #{HIGHLIGHT_COLORS.join(", ")}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -37,6 +37,16 @@ module Notare
|
||||
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
|
||||
|
||||
@@ -71,19 +81,42 @@ module Notare
|
||||
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.style
|
||||
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")
|
||||
|
||||
@@ -24,10 +24,12 @@ module Notare
|
||||
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"
|
||||
HYPERLINK_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
|
||||
|
||||
def initialize(has_numbering: false, images: [], has_styles: false)
|
||||
def initialize(has_numbering: false, images: [], hyperlinks: [], has_styles: false)
|
||||
@has_numbering = has_numbering
|
||||
@images = images
|
||||
@hyperlinks = hyperlinks
|
||||
@has_styles = has_styles
|
||||
end
|
||||
|
||||
@@ -60,6 +62,16 @@ module Notare
|
||||
Target: "media/#{image.filename}"
|
||||
)
|
||||
end
|
||||
|
||||
# Hyperlinks come after images
|
||||
@hyperlinks.each do |hyperlink|
|
||||
xml.Relationship(
|
||||
Id: hyperlink.rid,
|
||||
Type: HYPERLINK_TYPE,
|
||||
Target: hyperlink.url,
|
||||
TargetMode: "External"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
builder.to_xml
|
||||
|
||||
@@ -59,6 +59,8 @@ module Notare
|
||||
xml["w"].b if style.bold
|
||||
xml["w"].i if style.italic
|
||||
xml["w"].u("w:val" => "single") if style.underline
|
||||
xml["w"].strike if style.strike
|
||||
xml["w"].highlight("w:val" => style.highlight) if style.highlight
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user