Class: OoxmlParser::Note

Inherits:
OOXMLDocumentObject show all
Defined in:
lib/ooxml_parser/docx_parser/document_structure/page_properties/note.rb

Overview

Class with data of Note

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: 'unknown') ⇒ Note

Returns a new instance of Note.



8
9
10
11
12
# File 'lib/ooxml_parser/docx_parser/document_structure/page_properties/note.rb', line 8

def initialize(type: 'unknown')
  @elements = []
  @type = type
  super(parent: nil)
end

Instance Attribute Details

#assigned_toObject

Returns the value of attribute assigned_to.



6
7
8
# File 'lib/ooxml_parser/docx_parser/document_structure/page_properties/note.rb', line 6

def assigned_to
  @assigned_to
end

#elementsObject

Returns the value of attribute elements.



6
7
8
# File 'lib/ooxml_parser/docx_parser/document_structure/page_properties/note.rb', line 6

def elements
  @elements
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/ooxml_parser/docx_parser/document_structure/page_properties/note.rb', line 6

def type
  @type
end

Instance Method Details

#file_path(target) ⇒ String

Returns path to note xml file.

Parameters:

  • target (String)

    name of target

Returns:

  • (String)

    path to note xml file



51
52
53
54
55
56
# File 'lib/ooxml_parser/docx_parser/document_structure/page_properties/note.rb', line 51

def file_path(target)
  file = "#{root_object.unpacked_folder}word/#{target}"
  return file if File.exist?(file)

  "#{root_object.unpacked_folder}#{target}" unless File.exist?(file)
end

#note_base_xpathString

Returns xpath for note in xml object.

Returns:

  • (String)

    xpath for note in xml object



15
16
17
18
19
20
21
22
23
# File 'lib/ooxml_parser/docx_parser/document_structure/page_properties/note.rb', line 15

def note_base_xpath
  @note_base_xpath ||= if @type.include?('footer')
                         '//w:ftr'
                       elsif @type.include?('header')
                         '//w:hdr'
                       else
                         raise NameError, "Unknown note type: #{@type}"
                       end
end

#parse(params) ⇒ Note

Parse note data

Parameters:

  • params (Hash)

    data to parse

Returns:

  • (Note)

    result of parsing



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ooxml_parser/docx_parser/document_structure/page_properties/note.rb', line 28

def parse(params)
  @type = params[:type]
  @assigned_to = params[:assigned_to]
  @parent = params[:parent]
  doc = parse_xml(file_path(params[:target]))
  doc.search(note_base_xpath).each do |ftr|
    number = 0
    ftr.xpath('*').each do |sub_element|
      case sub_element.name
      when 'p'
        @elements << params[:default_paragraph].dup.parse(sub_element, number, params[:default_character], parent: self)
        number += 1
      when 'tbl'
        @elements << Table.new(parent: self).parse(sub_element, number)
        number += 1
      end
    end
  end
  self
end