xtotxt 0.7 → 0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/xtotxt.rb +22 -13
  2. metadata +4 -4
@@ -3,8 +3,9 @@ require 'yaml'
3
3
  class XtotxtError < StandardError; end
4
4
 
5
5
  class Xtotxt
6
- VERSION = 0.7
6
+ VERSION = 0.8
7
7
  SUPPORTED_EXTENSIONS = %w{txt pdf doc docx odt rtf html}
8
+ TMP_DIR = "/tmp"
8
9
 
9
10
  @@config_file_name = "xtotxt.yml"
10
11
  @@dirs_to_check = %w{. ~ /etc}
@@ -23,14 +24,14 @@ class Xtotxt
23
24
  @@ext = @ext_default
24
25
  end
25
26
 
26
- def convert(input_file_name,tmp_dir = "/tmp",retain_output=false)
27
- dot_ext = File.extname(input_file_name)
27
+ def convert_file(input_file_name, tmp_dir = TMP_DIR)
28
+ dot_ext = File.extname(input_file_name)
28
29
  file_ext = dot_ext.slice(1,dot_ext.length)
29
30
  raise XtotxtError.new("not a supported document extension: #{file_ext}") unless SUPPORTED_EXTENSIONS.member?(file_ext)
30
31
 
31
- file_base = File.basename(input_file_name, file_ext)
32
+ file_base = File.basename(input_file_name, dot_ext)
32
33
 
33
- output_file_name = "#{tmp_dir}/#{file_base}txt"
34
+ output_file_name = "#{tmp_dir}/#{file_base}.txt"
34
35
 
35
36
  command_line = case file_ext
36
37
  when "txt"
@@ -55,22 +56,30 @@ class Xtotxt
55
56
  #puts "executing: #{command_line}"
56
57
 
57
58
  command_output = `#{command_line} 2>/dev/null` if command_line and not command_line.empty?
58
- text = if $? == 0
59
- File.read(output_file_name)
59
+ if $? == 0 && File.exists?(output_file_name)
60
+ File.new(output_file_name,"r")
60
61
  else
61
62
  raise XtotxtError.new("Failed to convert #{input_file_name}. Exit status: #{$?.exitstatus}. Output: #{command_output}")
62
63
  end
64
+ end
63
65
 
64
- File.delete(output_file_name) unless retain_output
65
66
 
66
- case file_ext
67
- when "rtf"
68
- skip_unrtf_header(text)
69
- else
70
- text
67
+ def convert(input_file_name,tmp_dir = TMP_DIR,retain_output=false)
68
+
69
+ file = convert_file(input_file_name, tmp_dir)
70
+ text = file.read
71
+ file.close
72
+ File.delete(file.path) unless retain_output
73
+
74
+ case File.extname(input_file_name)
75
+ when ".rtf"
76
+ skip_unrtf_header(text)
77
+ else
78
+ text
71
79
  end
72
80
  end
73
81
 
82
+
74
83
  def initialize(ext=nil)
75
84
  @ext =
76
85
  case
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xtotxt
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 7
9
- version: "0.7"
8
+ - 8
9
+ version: "0.8"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Alexy Khrabrov
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-10-03 00:00:00 -07:00
17
+ date: 2011-10-12 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies: []
20
20