Class: Doc2Text::XmlBasedDocument::DocumentFile

Inherits:
Object
  • Object
show all
Defined in:
lib/doc2text/xml_based_document_file.rb

Direct Known Subclasses

Docx::Document, Odt::Document, Pptx::Document

Instance Method Summary collapse

Constructor Details

#initialize(document_path) ⇒ DocumentFile

Returns a new instance of DocumentFile.



6
7
8
# File 'lib/doc2text/xml_based_document_file.rb', line 6

def initialize(document_path)
  @document_path = document_path
end

Instance Method Details

#cleanObject



26
27
28
29
30
31
32
# File 'lib/doc2text/xml_based_document_file.rb', line 26

def clean
  if File.exist?(extract_path) and contains_extracted_files?
    FileUtils.rm_r extract_path
  else
    puts 'Failed to clean temp files'
  end
end

#contains_extracted_files?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/doc2text/xml_based_document_file.rb', line 22

def contains_extracted_files?
  false
end

#extract_extensionObject



39
40
41
# File 'lib/doc2text/xml_based_document_file.rb', line 39

def extract_extension
  'unpacked'
end

#extract_pathObject



43
44
45
# File 'lib/doc2text/xml_based_document_file.rb', line 43

def extract_path
  File.join File.dirname(@document_path), ".#{File.basename(@document_path)}_#{extract_extension}"
end

#open(filename) ⇒ Object

Open file from the current odt



35
36
37
# File 'lib/doc2text/xml_based_document_file.rb', line 35

def open(filename)
  File.open File.join(extract_path, filename), 'r'
end

#unpackObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/doc2text/xml_based_document_file.rb', line 10

def unpack
  Zip::File.open(@document_path) {
      |zip_file|
    Dir.mkdir(extract_path)
    zip_file.each do |entry|
      zipped_file_extract_path = File.join extract_path, entry.name
      FileUtils.mkdir_p File.dirname(zipped_file_extract_path)
      zip_file.extract entry, zipped_file_extract_path
    end
  }
end