Class: EpubForge::Utils::HtmlTranslator

Inherits:
Object
  • Object
show all
Defined in:
lib/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 collapse

GROUP_NAMES =
[:preferred, :user, :default, :fallback]

Instance Method Summary collapse

Constructor Details

#initializeHtmlTranslator

Returns a new instance of HtmlTranslator.



8
9
10
11
# File 'lib/utils/html_translator.rb', line 8

def initialize
  group( :user )
  opts( "" )
end

Instance Method Details

#can_do_job?(f) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/utils/html_translator.rb', line 67

def can_do_job?( f )
  installed? && handles_format?( f )
end

#cmd(c = nil) ⇒ Object



39
40
41
42
# File 'lib/utils/html_translator.rb', line 39

def cmd c = nil
  @cmd = c if c
  @cmd
end

#custom_proc(p = nil, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/utils/html_translator.rb', line 44

def custom_proc( p = nil, &block )
  if block_given?
    @custom_proc = block
  else
    @custom_proc = c if c
  end

  @custom_proc
end

#determine_file_format(file) ⇒ Object



95
96
97
# File 'lib/utils/html_translator.rb', line 95

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

#executable(executable_name = nil) ⇒ Object



27
28
29
30
31
32
# File 'lib/utils/html_translator.rb', line 27

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

#format(f = nil) ⇒ Object



34
35
36
37
# File 'lib/utils/html_translator.rb', line 34

def format f = nil
  @format = f if f
  @format
end

#group(g = nil) ⇒ Object



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

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

  @group
end

#handles_format?(f) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/utils/html_translator.rb', line 63

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

#installed?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/utils/html_translator.rb', line 59

def installed?
  executable.length > 0
end

#name(n = nil) ⇒ Object



13
14
15
16
# File 'lib/utils/html_translator.rb', line 13

def name( n = nil )
  @name = n if n
  @name
end

#opts(o = nil) ⇒ Object



54
55
56
57
# File 'lib/utils/html_translator.rb', line 54

def opts o = nil
  @opts = o if o
  @opts
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.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/utils/html_translator.rb', line 74

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.epf_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 htmlizer #{@name} -->\n" 
  result
end