Class: EpubForge::Utils::Converter

Inherits:
Object
  • Object
show all
Includes:
FunWith::Patterns::Loader
Defined in:
lib/epubforge/utils/converter.rb

Overview

TODO: Starting to get the feeling that an HtmlTranslator is just a subtype of Converter.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Converter

Returns a new instance of Converter.



27
28
29
30
31
# File 'lib/epubforge/utils/converter.rb', line 27

def initialize( &block )
  instance_exec( &block ) 
  @command ||= "{{x}} {{o}} '{{src}}' '{{dst}}'"   # default
  self
end

Class Method Details

.allObject



13
14
15
# File 'lib/epubforge/utils/converter.rb', line 13

def self.all
  self.loader_pattern_registry
end

.converts(input_format, output_format = :any) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/epubforge/utils/converter.rb', line 17

def self.converts( input_format, output_format = :any )
  self.all.select{ |k,v|
    if v.input_format == input_format
      output_format == :any || v.output_format == output_format
    else
      false
    end
  }.values
end

Instance Method Details

#convert(src, opts = {}) ⇒ Object

opts

:dest => Destination (output) file,
:command_line_options =>  Arguments to feed to the executable. Just a string, replaces the {{o} in @command

}



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/epubforge/utils/converter.rb', line 67

def convert( src, opts = {} )
  src = src.fwf_filepath
  dst = opts[:dest].is_a?(String) ? opts[:dest].fwf_filepath : opts[:dest]

  if dst.nil? 
    dst = src.gsub( /#{@input_format}$/, @output_format.to_s )
  end
  
  if is_executable_installed?
    if src.file?
      cmd = command.gsub("{{x}}", executable.to_s).gsub("{{o}}", opts[:command_line_options]).gsub("{{src}}", src).gsub("{{dst}}", dst)
      puts "running command : #{cmd}"
      
      

      `#{cmd}`
      $?.success?
    else
      warn( "Source file #{src} does not exist.".paint(:red,:bold))
      false
    end
  else
    false
  end
end

#is_executable_installed?Boolean

def label( lbl = nil )

@label = lbl unless lbl.nil?
@label

end

def from( fmt = nil)

@src_format = fmt unless fmt.nil?
@src_format

end

def to( fmt = nil)

@dst_format = fmt unless fmt.nil?
@dst_format

end

def executable( executable_name = nil )

@executable_name = executable_name unless executable_name.nil?
@executable_name

end

Returns:

  • (Boolean)


53
54
55
# File 'lib/epubforge/utils/converter.rb', line 53

def is_executable_installed?
  @executable && `which #{executable}`.strip.fwf_blank? == false
end