Class: Kitabu::Exporter::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/kitabu/exporter/base.rb

Direct Known Subclasses

CSS, Epub, HTML, Mobi, PDF

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_dir) ⇒ Base

Returns a new instance of Base.



18
19
20
21
# File 'lib/kitabu/exporter/base.rb', line 18

def initialize(root_dir)
  @root_dir = Pathname.new(root_dir)
  @source = root_dir.join("text")
end

Instance Attribute Details

#root_dirObject

The e-book directory.



8
9
10
# File 'lib/kitabu/exporter/base.rb', line 8

def root_dir
  @root_dir
end

#sourceObject

Where the text files are stored.



12
13
14
# File 'lib/kitabu/exporter/base.rb', line 12

def source
  @source
end

Class Method Details

.export(root_dir) ⇒ Object



14
15
16
# File 'lib/kitabu/exporter/base.rb', line 14

def self.export(root_dir)
  new(root_dir).export
end

Instance Method Details

#configObject

Return the configuration file.



39
40
41
# File 'lib/kitabu/exporter/base.rb', line 39

def config
  Kitabu.config(root_dir)
end

#copy_directory(source, target) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/kitabu/exporter/base.rb', line 70

def copy_directory(source, target)
  return unless root_dir.join(source).directory?

  source = root_dir.join("#{source}/.")
  target = root_dir.join(target)

  FileUtils.mkdir_p target
  FileUtils.cp_r source, target, remove_destination: true
end

#exportObject



23
24
25
# File 'lib/kitabu/exporter/base.rb', line 23

def export
  FileUtils.mkdir_p(root_dir.join("output"))
end

#handle_error(error) ⇒ Object



65
66
67
68
# File 'lib/kitabu/exporter/base.rb', line 65

def handle_error(error)
  ui.say "#{error.class}: #{error.message}", :red
  ui.say error.backtrace.join("\n"), :white
end

#nameObject

Return directory’s basename.



33
34
35
# File 'lib/kitabu/exporter/base.rb', line 33

def name
  File.basename(root_dir)
end

#render_template(file, locals = {}) ⇒ Object

Render a eRb template using locals as data seed.



45
46
47
48
49
50
# File 'lib/kitabu/exporter/base.rb', line 45

def render_template(file, locals = {})
  context = OpenStruct.new(locals).extend(Helpers)
  data = context.instance_eval { binding }
  ERB.new(File.read(file), trim_mode: "%<>",
                           eoutvar: "@_output").result(data)
end

#source_listObject



27
28
29
# File 'lib/kitabu/exporter/base.rb', line 27

def source_list
  @source_list ||= SourceList.new(root_dir)
end

#spawn_command(command) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/kitabu/exporter/base.rb', line 52

def spawn_command(command)
  stdout_and_stderr, status = Open3.capture2e(*command)
rescue Errno::ENOENT => error
  puts error.message
else
  puts stdout_and_stderr unless status.success?
  status.success?
end

#uiObject



61
62
63
# File 'lib/kitabu/exporter/base.rb', line 61

def ui
  @ui ||= Thor::Base.shell.new
end