Class: OoxmlParser::HeaderFooter

Inherits:
OOXMLDocumentObject show all
Defined in:
lib/ooxml_parser/docx_parser/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

#==, #boolean_attribute_value, #parse_xml, #with_data?

Methods included from OoxmlObjectAttributeHelper

#attribute_enabled?, #option_enabled?

Methods included from OoxmlDocumentObjectHelper

#to_hash

Constructor Details

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

Returns a new instance of HeaderFooter.



15
16
17
18
19
# File 'lib/ooxml_parser/docx_parser/document_structure/header_footer.rb', line 15

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

Instance Attribute Details

#elementsArray<OOXMLDocumentObject> (readonly)

Returns list of elements if object.

Returns:



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

def elements
  @elements
end

#idInteger (readonly)

Returns id of header-footer.

Returns:

  • (Integer)

    id of header-footer



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

def id
  @id
end

#path_suffixString (readonly)

Returns suffix for object files.

Returns:

  • (String)

    suffix for object files



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

def path_suffix
  @path_suffix
end

#typeSymbol (readonly)

Returns ‘:header` or `:footer`.

Returns:

  • (Symbol)

    ‘:header` or `:footer`



11
12
13
# File 'lib/ooxml_parser/docx_parser/document_structure/header_footer.rb', line 11

def type
  @type
end

Instance Method Details

#parse(node) ⇒ HeaderFooter

Parse HeaderFooter

Parameters:

  • node (Nokogiri::XML:Node)

    with HeaderFooter

Returns:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ooxml_parser/docx_parser/document_structure/header_footer.rb', line 47

def parse(node)
  node.attributes.each do |key, value|
    case key
    when 'id'
      @id = value.value.to_i
    end
  end
  parse_type(node)
  doc = parse_xml(root_object.unpacked_folder + xml_path)
  doc.search(xpath_for_search).each do |footnote|
    footnote_id = nil
    footnote.attributes.each do |key, value|
      case key
      when 'id'
        footnote_id = value.value.to_i
      end
    end
    next unless footnote_id == @id

    paragraph_number = 0
    footnote.xpath('w:p').each do |paragraph|
      @elements << root_object.default_paragraph_style.dup.parse(paragraph, paragraph_number, root_object.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



33
34
35
36
37
38
39
40
41
42
# File 'lib/ooxml_parser/docx_parser/document_structure/header_footer.rb', line 33

def parse_type(node)
  case node.name
  when 'footnoteReference'
    @path_suffix = 'footnote'
    @type = :header
  when 'endnoteReference'
    @path_suffix = 'endnote'
    @type = :footer
  end
end

#xml_pathString

Returns string with xml path.

Returns:

  • (String)

    string with xml path



27
28
29
# File 'lib/ooxml_parser/docx_parser/document_structure/header_footer.rb', line 27

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



22
23
24
# File 'lib/ooxml_parser/docx_parser/document_structure/header_footer.rb', line 22

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