class Verku::Exporter::Base

Constants

EXTENSIONS

Attributes

root_dir[RW]
source[RW]

Public Class Methods

export!(root_dir) click to toggle source
# File lib/verku/exporter/base.rb, line 17
def self.export!(root_dir)
  new(root_dir).export!
end
new(root_dir) click to toggle source
# File lib/verku/exporter/base.rb, line 21
def initialize(root_dir)
  @root_dir = Pathname.new(root_dir)
  @source = root_dir.join("text")
end

Public Instance Methods

base_name(ext='') click to toggle source
# File lib/verku/exporter/base.rb, line 38
def base_name(ext='')
  oname = "#{name}#{git_branch}"
end
build_data() click to toggle source
# File lib/verku/exporter/base.rb, line 49
def build_data
  source_list.each do |file|
    data = read_content(file)[1]
  end
end
config() click to toggle source

Return the configuration file.

# File lib/verku/exporter/base.rb, line 46
def config
  Verku.config(root_dir)
end
epub_file() click to toggle source
# File lib/verku/exporter/base.rb, line 71
def epub_file
  output_name("epub")
end
git_branch() click to toggle source
# 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
    ""
  end
  return branch
end
handle_error(error) click to toggle source
# 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_file() click to toggle source
# File lib/verku/exporter/base.rb, line 68
def html_file
  output_name("html")
end
name() click to toggle source

Return directory's basename.

# File lib/verku/exporter/base.rb, line 27
def name
  File.basename(root_dir)
end
output_name(ext='txt') click to toggle source
# File lib/verku/exporter/base.rb, line 41
def output_name(ext='txt')
  root_dir.join("builds/#{base_name}.#{ext}")
end
read_content(file) click to toggle source
# File lib/verku/exporter/base.rb, line 77
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 = {}) click to toggle source
# File lib/verku/exporter/base.rb, line 62
def render_template(file, locals = {})
  ERB.new(File.read(file)).result OpenStruct.new(locals).instance_eval{ binding }
end
source_list() click to toggle source
# 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(",")}}")
                   .sort
  # ap @source_list
  return @source_list
end
spawn_command(cmd) click to toggle source
# File lib/verku/exporter/base.rb, line 93
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_file() click to toggle source
# File lib/verku/exporter/base.rb, line 74
def tex_file
  output_name("tex")
end
ui() click to toggle source
# File lib/verku/exporter/base.rb, line 65
def ui
  @ui ||= Thor::Base.shell.new
end