class Sablon::DOM::Numbering

Manages the creation of new list definitions

Constants

Definition

Public Class Methods

extend_model(model_klass, &block) click to toggle source

extends the Model class using instance eval with a block argument

Calls superclass method Sablon::DOM::FileHandler::extend_model
# File lib/sablon/document_object_model/numbering.rb, line 10
def self.extend_model(model_klass, &block)
  super do
    #
    # adds a list definition to the numbering.xml file
    define_method(:add_list_definition) do |style|
      @dom['word/numbering.xml'].add_list_definition(style)
    end
  end
end
new(xml_node) click to toggle source

Sets up the class to add new list definitions to the number.xml file

Calls superclass method Sablon::DOM::FileHandler::new
# File lib/sablon/document_object_model/numbering.rb, line 22
def initialize(xml_node)
  super
  #
  @numbering = xml_node.root
  #
  @max_numid = max_attribute_value('//w:num', 'w:numId')
  #
  selector = '//w:abstractNum'
  @max_abstract_id = max_attribute_value(selector, 'w:abstractNumId')
end

Public Instance Methods

add_list_definition(style) click to toggle source

adds a new relationship and returns the corresponding rId for it

# File lib/sablon/document_object_model/numbering.rb, line 34
def add_list_definition(style)
  definition = create_definition(style)
  #
  # update numbering file with new definitions
  node = @numbering.xpath('//w:abstractNum').last
  node.add_next_sibling(abstract_tag(definition))
  #
  node = @numbering.xpath('//w:num').last
  node.add_next_sibling(definition_tag(definition))
  #
  definition
end

Private Instance Methods

abstract_tag(definition) click to toggle source

Creates a new abstract numbering definition tag to style a list

# File lib/sablon/document_object_model/numbering.rb, line 65
def abstract_tag(definition)
  abstract_num = find_abstract_definition(definition.style)
  abstract_num['w:abstractNumId'] = definition.abstract_id
  abstract_num.xpath('./w:nsid').each(&:remove)
  #
  abstract_num
end
create_definition(style) click to toggle source

Creates a new instance of the Definition struct, after incrementing the max id values

# File lib/sablon/document_object_model/numbering.rb, line 87
def create_definition(style)
  @max_numid += 1
  @max_abstract_id += 1
  Definition.new(@max_numid, @max_abstract_id, style)
end
definition_tag(definition) click to toggle source

Creates a new list definition tag to define a list

# File lib/sablon/document_object_model/numbering.rb, line 56
      def definition_tag(definition)
        <<-XML.gsub(/^\s+|\n/, '')
          <w:num w:numId="#{definition.numid}">
            <w:abstractNumId w:val="#{definition.abstract_id}" />
          </w:num>
        XML
      end
find_abstract_definition(style) click to toggle source

Locates and copies the first abstract numbering definition with the expected style. If one can not be found an error is raised.

# File lib/sablon/document_object_model/numbering.rb, line 75
def find_abstract_definition(style)
  path = "//w:abstractNum[descendant-or-self::*[w:pStyle[@w:val='#{style}']]]"
  unless (abstract_num = @numbering.at_xpath(path))
    msg = "Could not find w:abstractNum definition for style: '#{style}'"
    raise ArgumentError, msg
  end
  #
  abstract_num.dup
end
max_attribute_value(selector, attr_name) click to toggle source

Finds the maximum value of an attribute by converting it to an integer. Non numeric portions of values are ignored.

# File lib/sablon/document_object_model/numbering.rb, line 51
def max_attribute_value(selector, attr_name)
  super(@numbering, selector, attr_name)
end