Class: BookWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-books/book_writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, root, params) ⇒ BookWriter

Returns a new instance of BookWriter.



6
7
8
9
10
11
# File 'lib/jekyll-books/book_writer.rb', line 6

def initialize(name, root, params)
  @name   = name
  @root   = root
  @params = params
  @summary = Summary.new(book_folder)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/jekyll-books/book_writer.rb', line 5

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/jekyll-books/book_writer.rb', line 5

def params
  @params
end

#rootObject (readonly)

Returns the value of attribute root.



5
6
7
# File 'lib/jekyll-books/book_writer.rb', line 5

def root
  @root
end

#summaryObject (readonly)

Returns the value of attribute summary.



5
6
7
# File 'lib/jekyll-books/book_writer.rb', line 5

def summary
  @summary
end

Instance Method Details

#book_folderObject



19
20
21
# File 'lib/jekyll-books/book_writer.rb', line 19

def book_folder
  File.join(root, name)
end

#contentObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/jekyll-books/book_writer.rb', line 50

def content
  front_matter = YAML.dump(
    Util.cover_default({
      "title"  => name.capitalize,
      "start"  => default_year,
      "end"    => default_year,
      "img"    => img_url
    }, params)
  )

  front_matter + "---\n"
end

#create_indexObject



37
38
39
40
41
42
# File 'lib/jekyll-books/book_writer.rb', line 37

def create_index
  return if File.file?(index_file)

  f = File.new(index_file, "w+")
  f.write(content)
end

#create_summaryObject



44
45
46
47
48
# File 'lib/jekyll-books/book_writer.rb', line 44

def create_summary
  return if File.file?(summary_file) && !params["forced"]

  summary.write(true)
end

#default_yearObject



63
64
65
# File 'lib/jekyll-books/book_writer.rb', line 63

def default_year
  Date.today.year
end

#img_urlObject



67
68
69
# File 'lib/jekyll-books/book_writer.rb', line 67

def img_url
  "/img/home/#{name}.jpg"
end

#index_fileObject



27
28
29
# File 'lib/jekyll-books/book_writer.rb', line 27

def index_file
  summary.index_path
end

#mkdirObject



31
32
33
34
35
# File 'lib/jekyll-books/book_writer.rb', line 31

def mkdir
  unless File.directory?(book_folder)
    FileUtils.mkdir_p(book_folder)
  end
end

#runObject



13
14
15
16
17
# File 'lib/jekyll-books/book_writer.rb', line 13

def run
  mkdir
  create_index
  create_summary
end

#summary_fileObject



23
24
25
# File 'lib/jekyll-books/book_writer.rb', line 23

def summary_file
  summary.file_path
end