Sha256: 3af24e0bc3ee0e0e679bb3a43cd47e60f570029f2e8c13a49929c92661071989

Contents?: true

Size: 1.79 KB

Versions: 28

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

module OoxmlParser
  # Class Header Footer classes
  class HeaderFooter < OOXMLDocumentObject
    # @return [Integer] id of header-footer
    attr_reader :id
    # @return [Array<OOXMLDocumentObject>] list of elements if object
    attr_reader :elements
    # @return [Symbol] `:header` or `:footer`
    attr_reader :type
    # @return [String] suffix for object files
    attr_reader :path_suffix

    def initialize(type = :header, parent: nil)
      @type = type
      @elements = []
      super(parent: parent)
    end

    # @return [String] string for search of xpath
    def xpath_for_search
      "//w:#{path_suffix}"
    end

    # @return [String] string with xml path
    def xml_path
      "word/#{path_suffix}s.xml"
    end

    # Parse type and path suffix by node type
    # @param [Nokogiri::XML:Node] node with HeaderFooter
    def parse_type(node)
      case node.name
      when 'footnoteReference'
        @path_suffix = 'footnote'
        @type = :header
      when 'endnoteReference'
        @path_suffix = 'endnote'
        @type = :footer
      end
    end

    # Parse HeaderFooter
    # @param [Nokogiri::XML:Node] node with HeaderFooter
    # @return [HeaderFooter] result of parsing
    def parse(node)
      @id = node.attribute('id').value.to_i
      parse_type(node)
      doc = parse_xml(OOXMLDocumentObject.path_to_folder + xml_path)
      doc.search(xpath_for_search).each do |footnote|
        next unless footnote.attribute('id').value.to_i == @id

        paragraph_number = 0
        footnote.xpath('w:p').each do |paragraph|
          @elements << DocumentStructure.default_paragraph_style.dup.parse(paragraph, paragraph_number, DocumentStructure.default_run_style, parent: self)
          paragraph_number += 1
        end
      end
      self
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
ooxml_parser-0.29.0 lib/ooxml_parser/docx_parser/document_structure/header_footer.rb
ooxml_parser-0.28.0 lib/ooxml_parser/docx_parser/document_structure/header_footer.rb
ooxml_parser-0.27.0 lib/ooxml_parser/docx_parser/document_structure/header_footer.rb
ooxml_parser-0.26.0 lib/ooxml_parser/docx_parser/document_structure/header_footer.rb
ooxml_parser-0.25.0 lib/ooxml_parser/docx_parser/document_structure/header_footer.rb
ooxml_parser-0.24.0 lib/ooxml_parser/docx_parser/document_structure/header_footer.rb
ooxml_parser-0.23.0 lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb
ooxml_parser-0.22.0 lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb
ooxml_parser-0.21.0 lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb
ooxml_parser-0.20.0 lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb
ooxml_parser-0.19.0 lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb
ooxml_parser-0.18.1 lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb
ooxml_parser-0.18.0 lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb
ooxml_parser-0.17.0 lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb
ooxml_parser-0.16.0 lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb
ooxml_parser-0.15.0 lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb
ooxml_parser-0.14.2 lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb
ooxml_parser-0.14.1-mingw32 lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb
ooxml_parser-0.14.1 lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb
ooxml_parser-0.14.0 lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb