Class: Verkilo::Book

Inherits:
Object
  • Object
show all
Defined in:
lib/verkilo/book.rb

Instance Method Summary collapse

Constructor Details

#initialize(title, root_dir, repo = "none") ⇒ Book

Returns a new instance of Book.



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/verkilo/book.rb', line 3

def initialize(title, root_dir, repo="none")
  @title = title
  @root_dir = root_dir
  @resource_dir = File.join(File.dirname(File.expand_path(__FILE__)), '../../resources')
  @contents = nil
  @wc = nil
  @files = nil
  @today = Time.now.strftime("%F")
  @repo = repo
  @bib = Dir["#{@root_dir}/**/*.bib"].first || nil
  @csl = Dir["#{@root_dir}/**/*.csl"].first || nil
end

Instance Method Details

#compile(dir = ".") ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/verkilo/book.rb', line 30

def compile(dir=".")
  @build_dir = File.join(dir, 'build', @title)
  FileUtils.mkdir_p(@build_dir)
  src = File.join("/tmp", "#{@title}.md")
  f = File.new(src,'w')
  f.write(self.contents)
  f.close
  %w(frontmatter).each {|action|
    dst = File.join(["/tmp", "#{@title}-#{action}.tex"])
    pandoc(action, src, dst)
  }
  %w(yaml tex pdf docx html epub docbook).each do |action|
    pandoc(action,src)
  end
end

#contentsObject



16
17
18
# File 'lib/verkilo/book.rb', line 16

def contents
  @contents ||= files.map { |f| File.open(f,'r').read }.join("\n\n")
end

#to_iObject Also known as: wordcount



25
26
27
# File 'lib/verkilo/book.rb', line 25

def to_i
  @wc ||= self.contents.gsub(/[^a-zA-Z\s\d]/,"").split(/\W+/).count
end

#to_sObject Also known as: title



20
21
22
# File 'lib/verkilo/book.rb', line 20

def to_s
  @title
end