# frozen_string_literal: true module Ezdoc class Document include Builder attr_reader :nodes 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 = {} 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 base = lists.any? ? 2 : 1 "rId#{base + @images.size}" end end end