Class: EpubForge::Utils::HtmlTranslator

Inherits:
Object
  • Object
show all
Includes:
HtmlTranslatorQueue
Defined in:
lib/epubforge/utils/html_translator.rb

Overview

An individual translator, which receives a filename, determines if it’s up to the job then returns the resulting HTML translation.

Constant Summary

Constants included from HtmlTranslatorQueue

EpubForge::Utils::HtmlTranslatorQueue::GROUP_NAMES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HtmlTranslatorQueue

included

Constructor Details

#initialize(&block) ⇒ HtmlTranslator

Returns a new instance of HtmlTranslator.



41
42
43
44
45
46
47
48
# File 'lib/epubforge/utils/html_translator.rb', line 41

def initialize( &block )
  # set some defaults
  group( :user )
  opts( "" )
  
  self.instance_exec( &block ) if block_given?
  self      # explicitly return.  Thought that wasn't necessary, but...
end

Class Method Details

.translate(filename, opts = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/epubforge/utils/html_translator.rb', line 10

def self.translate( filename, opts = {} )
  translator  = opts[:translator]
  translator  = self.named( translator ) if translator.is_a?( Symbol )
  opts      = opts[:opts] || ""
  
  if translator
    if result = translator.translate( filename, {opts: opts } )
      return result
    else
      puts "Named HtmlTranslator #{translator} did not return html. Falling back on other html translators"
    end
  end

  for translator in HtmlTranslator.each_translator
    if result = translator.translate( filename, opts )
      return result
    end
  end
  
  "<!-- COULD NOT FIND HTML TRANSLATOR FOR FORMAT (#{filename}) -->"
end

Instance Method Details

#can_do_job?(f) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/epubforge/utils/html_translator.rb', line 85

def can_do_job?( f )
  handles_format?( f ) && ( has_executable_installed? || has_custom_proc? )
end

#custom_proc(*args, &block) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/epubforge/utils/html_translator.rb', line 67

def custom_proc( *args, &block )
  if block_given?
    @custom_proc = block
  elsif args.first.is_a?(Proc)
    @custom_proc = args.first
  end

  @custom_proc
end

#executable(executable_name = nil) ⇒ Object



59
60
61
62
63
64
# File 'lib/epubforge/utils/html_translator.rb', line 59

def executable( executable_name = nil )
  if executable_name
    @executable_name = HtmlTranslator.location( executable_name ) || `which #{executable_name}`.strip
  end
  @executable_name || ""
end

#get_file_format(file) ⇒ Object



121
122
123
# File 'lib/epubforge/utils/html_translator.rb', line 121

def get_file_format( file )
  file.fwf_filepath.ext.to_sym
end

#group(g = nil) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/epubforge/utils/html_translator.rb', line 50

def group( g = nil )
  if g
    raise "group must be one of the following symbols: #{HtmlTranslatorQueue::GROUP_NAMES.inspect}" unless HtmlTranslatorQueue::GROUP_NAMES.include?(g)
    @group = g 
  end

  @group
end

#handles_format?(f) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/epubforge/utils/html_translator.rb', line 81

def handles_format?( f )
  @format == get_file_format( f ) || @format == :unknown
end

#has_custom_proc?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/epubforge/utils/html_translator.rb', line 93

def has_custom_proc?
  @custom_proc.is_a?( Proc )
end

#has_executable_installed?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/epubforge/utils/html_translator.rb', line 89

def has_executable_installed?
  executable.is_a?(String) && ! executable.fwf_blank?     # 'which will return an empty string if the given executable isn't in the path'
end

#installed?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/epubforge/utils/html_translator.rb', line 77

def installed?
  executable.length > 0
end

#translate(filename, opts = "") ⇒ Object

opts allows you to override the normal command line arguments Maybe a description of the job’s requirements should be more elaborate than just a filename. OTOH, simple can have its advantages.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/epubforge/utils/html_translator.rb', line 100

def translate( filename, opts = "" )
  return false unless can_do_job?( filename )

  result =  ""
  if @custom_proc
    result += @custom_proc.call( filename, *opts )
  elsif @cmd
    exec_string = cmd.gsub( /\{\{f\}\}/, "\"#{filename.to_s}\"" )
    opts = @opts if opts.fwf_blank?
    exec_string.gsub!( /\{\{o\}\}/, opts )
    exec_string.gsub!( /\{\{x\}\}/, executable )
  
    result += `#{exec_string}`
  else
    return false
  end

  result += "\n\n<!-- generated from #{@format} by html translator #{@name} -->\n" 
  result
end