Module: Documentalist::OpenOffice

Includes:
Dependencies
Defined in:
lib/backends/open_office.rb

Defined Under Namespace

Modules: Server

Class Method Summary collapse

Methods included from Dependencies

included

Class Method Details

.convert(origin, options) ⇒ Object

Converts documents



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/backends/open_office.rb', line 15

def self.convert(origin, options)
  Documentalist.logger.debug("Going to convert #{origin} to #{options[:to]}")

  # See how to make OpenOffice startup as smooth as possible and not on first conversion
  # OO auto-start option if in Rails app ?
  Server.ensure_available

  # TODO : manage multi OO instances : http://code.google.com/p/jodconverter/wiki/GettingStarted

  Documentalist.timeout(Documentalist.config[:open_office][:max_conversion_time], :attempts => Documentalist.config[:open_office][:max_conversion_attempts]) do
    if Documentalist.config[:open_office][:bridge] == 'JOD'
      command = "#{Documentalist.config[:java][:path]} -jar #{File.join(File.dirname(__FILE__), %w{open_office bridges jodconverter-2.2.2 lib jodconverter-cli-2.2.2.jar})} #{origin} #{options[:to]}"          
    elsif Documentalist.config[:open_office][:bridge] == 'PYOD'
      command = "#{Documentalist.config[:python][:path]} #{File.join(File.dirname(__FILE__), %w{open_office bridges pyodconverter.py})} #{origin} #{options[:to]}"
    end

    if Documentalist.config[:log_file] and !Documentalist.config[:log_file].empty?
      command += " >> #{Documentalist.config[:log_file]} 2>&1"
    end

    Documentalist.logger.debug("Going to run #{Documentalist.config[:open_office][:bridge]} bridge with command -- #{command}")
    system(command)

    self.convert_txt_to_utf8(options[:to]) if options[:to_format] == :txt
    
    options[:to]
  end
end

.convert_txt_to_utf8(file_path) ⇒ Object

HACK : convert ISO-8859-1 files back to UTF-8 when OpenOffice messes up and outputs the wrong encoding



46
47
48
49
50
# File 'lib/backends/open_office.rb', line 46

def self.convert_txt_to_utf8(file_path)
  if `file #{file_path}` =~ /ISO/
    system("iconv --from-code ISO-8859-1 --to-code UTF-8 #{file_path} > tmp_iconv.txt && mv tmp_iconv.txt #{file_path}")
  end
end