Class: Verku::Exporter::Base

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

Direct Known Subclasses

Epub, HTML, Mobi, PDF

Constant Summary collapse

EXTENSIONS =
%w[markdown mkdown mkdn mkd md text]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_dir) ⇒ Base

Returns a new instance of Base.



21
22
23
24
# File 'lib/verku/exporter/base.rb', line 21

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/verku/exporter/base.rb', line 8

def root_dir
  @root_dir
end

#sourceObject

Where the text files are stored.



9
10
11
# File 'lib/verku/exporter/base.rb', line 9

def source
  @source
end

Class Method Details

.export!(root_dir) ⇒ Object



17
18
19
# File 'lib/verku/exporter/base.rb', line 17

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

Instance Method Details

#base_name(ext = '') ⇒ Object



38
39
40
# File 'lib/verku/exporter/base.rb', line 38

def base_name(ext='')
  oname = "#{name}-#{git_branch}"
end

#build_dataObject



49
50
51
52
53
# File 'lib/verku/exporter/base.rb', line 49

def build_data
  source_list.each do |file|
    data = read_content(file)[1]
  end
end

#configObject

Return the configuration file.



46
47
48
# File 'lib/verku/exporter/base.rb', line 46

def config
  Verku.config(root_dir)
end

#epub_fileObject



68
69
70
# File 'lib/verku/exporter/base.rb', line 68

def epub_file
  output_name("epub")
end

#git_branchObject



30
31
32
33
34
35
36
37
# File 'lib/verku/exporter/base.rb', line 30

def git_branch
  branch = if (File.exist?(root_dir.join(".git/HEAD")))
     File.read( root_dir.join(".git/HEAD") ).sub("ref: refs/heads/","").sub(/\n/,'')
  else
    "none"
  end
  return branch
end

#handle_error(error) ⇒ Object



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

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

#html_fileObject



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

def html_file
  output_name("html")
end

#nameObject

Return directory’s basename.



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

def name
  File.basename(root_dir)
end

#output_name(ext = 'txt') ⇒ Object



41
42
43
# File 'lib/verku/exporter/base.rb', line 41

def output_name(ext='txt')
  root_dir.join("builds/#{base_name}.#{ext}")
end

#read_content(file) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/verku/exporter/base.rb', line 74

def read_content(file)
  content = File.read(file)
  data = {}
  begin
    # YAML_FRONT_MATTER_REGEXP = /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m
    if content =~ /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m
      content = $POSTMATCH
      data = YAML.load($1, :safe => true)
    end
    return [content, data]
  rescue SyntaxError => e
    puts "YAML Exception reading #{file}: #{e.message}"
  rescue Exception => e
    puts "Error reading file #{file}: #{e.message}"
  end
end

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



59
60
61
# File 'lib/verku/exporter/base.rb', line 59

def render_template(file, locals = {})
  ERB.new(File.read(file)).result OpenStruct.new(locals).instance_eval{ binding }
end

#source_listObject



55
56
57
58
# File 'lib/verku/exporter/base.rb', line 55

def source_list
  # @source_list ||= SourceList.new(root_dir)
  @source_list ||= Dir.glob("#{@root_dir.join('text')}/**/*.{#{EXTENSIONS.join(",")}}")
end

#spawn_command(cmd) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/verku/exporter/base.rb', line 90

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

#tex_fileObject



71
72
73
# File 'lib/verku/exporter/base.rb', line 71

def tex_file
  output_name("tex")
end

#uiObject



62
63
64
# File 'lib/verku/exporter/base.rb', line 62

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