Module: RtfFilter

Defined in:
lib/rtf_filter.rb,
lib/rtf_filter/version.rb

Constant Summary collapse

VERSION =
"0.0.4"

Class Method Summary collapse

Class Method Details

.allow_tags(yes) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/rtf_filter.rb', line 40

def self.allow_tags(yes)
  if yes
    " --allowTags"
  else
    ""
  end
end

.format_txt(txt) ⇒ Object



48
49
50
51
52
53
# File 'lib/rtf_filter.rb', line 48

def self.format_txt(txt)
  txt.strip! #remove blank lines at begining and end
  txt.gsub!(/(^|\n)[ \t\r]+/, "\\1") #delete leading whitespace (spaces/tabs) from beginning of each line
  txt.gsub!(/\n{3,}/, "\n\n") #replace all occurrences of 3 or more line breaks with just two
  return txt
end

.to_txt(filename, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rtf_filter.rb', line 4

def self.to_txt(filename, options={})
  allow_tags = options[:allow_tags] || false
  begin
    txt = ""
  	IO.popen("rtffilter --source '"+filename+"' --toStdOut"+self.allow_tags(allow_tags)).each_line do |l|
      txt += l
    end
    self.format_txt(txt) if options[:format_txt]
    raise IOError if txt == ""
    return txt
  rescue IOError => e 
    if options[:io_exception]
      raise e
    else
  	  return nil
    end
  end
end

.to_txt_file(filename, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rtf_filter.rb', line 23

def self.to_txt_file(filename, options={})
  destination_folder = options[:destination_folder] || ""
  allow_tags = options[:allow_tags] || false
  begin
    destination_folder += "/"unless destination_folder.empty?
    destination = destination_folder + filename.sub(/.{3}$/,'txt')
    txt = self.to_txt(filename, options)
    File.open(destination, 'w') {|f| f.write(txt)}
  rescue IOError => e 
    if options[:io_exception]
      raise e
    else
  	  return nil
    end
 end  
end