Module: Proselytism

Defined in:
lib/proselytism/engine.rb,
lib/proselytism/logger.rb,
lib/proselytism/version.rb,
lib/proselytism/converter.rb,
lib/proselytism/proselytism.rb,
lib/generators/proselytism/config_generator.rb,
lib/generators/proselytism/initializer_generator.rb

Defined Under Namespace

Modules: Converters Classes: Config, ConfigGenerator, Engine, InitializerGenerator, Logger

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.config {|@@config| ... } ⇒ Object

Yields:



6
7
8
9
10
# File 'lib/proselytism/proselytism.rb', line 6

def self.config(&block)
  @@config ||= Config.new
  yield @@config if block
  @@config
end

.convert(file_path, options = {}, &block) ⇒ Object

Converts a document from one format to another



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/proselytism/proselytism.rb', line 40

def self.convert(file_path, options={}, &block)
  options.symbolize_keys!
  options[:to]    ||= :txt

  # Tempfile don't have extension => let's make with the from option
  if options[:from] and file_path.match(/[^\.]*$/)[0] != options[:from].to_s
    file = Tempfile.new [File.basename(file_path), ".#{options[:from]}"]
    file.write File.read(file_path)
    file.close
    file_path = file.path
  else
    # if file_path has extension and no from option => set the option
    options[:from] ||= file_path.match(/[^\.]*$/)[0].downcase.to_sym
  end

  dir = Dir.mktmpdir(nil, options[:dir] || config.tmp_path)
  result = get_converter(options[:from], options[:to]).convert file_path, options.update(:dir => dir)

  if block_given?
    output = yield result
    FileUtils.remove_entry_secure dir
    output
  else
    result
  end
end

.extract_images(file_path, options = {}, &block) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/proselytism/proselytism.rb', line 67

def self.extract_images(file_path, options={}, &block)
  if File.extname(file_path).match(/pdf/)
    convert file_path, :to => :ppm do |files|
      output = files.map do |file|
        convert file, :to => :jpg, :dir => File.dirname(file)
      end
      #block.call(output)
      block_given? ? yield(output) : output
    end
  else
    convert file_path, :to => :html do |file|
      output = Dir.glob(File.join(File.dirname(file), "*.{jpg,jpeg,bmp,tif,tiff,gif,png}"))
      #block.call(output)
      block_given? ? yield(output) : output
    end
  end
end

.extract_text(file_path, options = {}, &block) ⇒ Object



85
86
87
88
89
90
# File 'lib/proselytism/proselytism.rb', line 85

def self.extract_text(file_path, options={}, &block)
  convert(file_path, options.update(:to => :txt)) do |file|
    text = File.read(file).toutf8
    block_given? ? yield(text) : text
  end
end

.get_converter(origin, destination) ⇒ Object

Finds the relevant converter



31
32
33
34
35
# File 'lib/proselytism/proselytism.rb', line 31

def self.get_converter(origin, destination)
  Converters::Base.subclasses.detect do |converter|
    converter.from.include?(origin) and converter.to.include?(destination)
  end.instance
end

.log(severity, message = nil, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/proselytism/logger.rb', line 18

def self.log(severity, message = nil, &block)
  if config.logger
    start_time = Time.now
    if block_given?
      result = yield
      config.logger.send(severity, "#{message} in #{((Time.now - start_time)*1000).to_i} ms")
    else
      config.logger.send(severity, message.strip)
    end
    block_given? ? result : true
  end
end