Class: OoxmlParser::HeaderFooter

Inherits:
OOXMLDocumentObject show all
Defined in:
lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb

Overview

Class Header Footer classes

Instance Attribute Summary collapse

Attributes inherited from OOXMLDocumentObject

#parent

Instance Method Summary collapse

Methods inherited from OOXMLDocumentObject

#==, add_to_xmls_stack, copy_file_and_rename_to_zip, current_xml, dir, encrypted_file?, get_link_from_rels, unzip_file, #with_data?

Methods included from OoxmlDocumentObjectHelper

#to_hash

Constructor Details

#initialize(type = :header, parent: nil) ⇒ HeaderFooter

Returns a new instance of HeaderFooter.



7
8
9
10
11
# File 'lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb', line 7

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

Instance Attribute Details

#elementsObject

Returns the value of attribute elements.



4
5
6
# File 'lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb', line 4

def elements
  @elements
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb', line 4

def id
  @id
end

#path_suffixObject

Returns the value of attribute path_suffix.



5
6
7
# File 'lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb', line 5

def path_suffix
  @path_suffix
end

#typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb', line 4

def type
  @type
end

Instance Method Details

#parse(node) ⇒ HeaderFooter

Parse HeaderFooter

Parameters:

  • node (Nokogiri::XML:Node)

    with HeaderFooter

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb', line 41

def parse(node)
  @id = node.attribute('id').value.to_i
  parse_type(node)
  doc = Nokogiri::XML(File.open(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

#parse_type(node) ⇒ Object

Parse type and path suffix by node type

Parameters:

  • node (Nokogiri::XML:Node)

    with HeaderFooter



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb', line 25

def parse_type(node)
  case node.name
  when 'footnoteReference'
    @path_suffix = 'footnote'
    @type = :header
  when 'endnoteReference'
    @path_suffix = 'endnote'
    @type = :footer
  else
    warn "Unknown HeaderFooter type: #{node.name}"
  end
end

#xml_pathString

Returns string with xml path.

Returns:

  • (String)

    string with xml path



19
20
21
# File 'lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb', line 19

def xml_path
  "word/#{path_suffix}s.xml"
end

#xpath_for_searchString

Returns string for search of xpath.

Returns:

  • (String)

    string for search of xpath



14
15
16
# File 'lib/ooxml_parser/docx_parser/docx_data/document_structure/header_footer.rb', line 14

def xpath_for_search
  "//w:#{path_suffix}"
end