Class: BookReader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, site, name) ⇒ BookReader

Returns a new instance of BookReader.



10
11
12
13
14
15
16
17
18
# File 'lib/jekyll-books/book_reader.rb', line 10

def initialize(params, site, name)
  @root        = params["source"]
  @destination = params["destination"]
  @params      = params
  @site        = site
  @name        = name
  @index       = 0
  @pages       = []
end

Instance Attribute Details

#destinationObject (readonly)

Returns the value of attribute destination.



7
8
9
# File 'lib/jekyll-books/book_reader.rb', line 7

def destination
  @destination
end

#indexObject

Returns the value of attribute index.



8
9
10
# File 'lib/jekyll-books/book_reader.rb', line 8

def index
  @index
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/jekyll-books/book_reader.rb', line 7

def name
  @name
end

#pagesObject

Returns the value of attribute pages.



8
9
10
# File 'lib/jekyll-books/book_reader.rb', line 8

def pages
  @pages
end

#paramsObject (readonly)

Returns the value of attribute params.



7
8
9
# File 'lib/jekyll-books/book_reader.rb', line 7

def params
  @params
end

#rootObject (readonly)

Returns the value of attribute root.



7
8
9
# File 'lib/jekyll-books/book_reader.rb', line 7

def root
  @root
end

#siteObject (readonly)

Returns the value of attribute site.



7
8
9
# File 'lib/jekyll-books/book_reader.rb', line 7

def site
  @site
end

Instance Method Details

#add_prev_and_nextObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/jekyll-books/book_reader.rb', line 57

def add_prev_and_next
  pages.each_with_index do |page, i|
    if i > 0
      page.data["prev"] = pages[i - 1]
    end
    if i < pages.size - 1
      page.data["next"] = pages[i + 1]
    end
  end
end

#book_folderObject



84
85
86
# File 'lib/jekyll-books/book_reader.rb', line 84

def book_folder
  File.join(site.source, root, name)
end

#book_pageObject



88
89
90
91
92
93
94
95
# File 'lib/jekyll-books/book_reader.rb', line 88

def book_page
  @book_page ||= Jekyll::BookPage.new(
    site,
    params.merge({
      "name" => name
    })
  )
end

#ebook_pageObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/jekyll-books/book_reader.rb', line 97

def ebook_page
  default_setting = {
    "layout" => "ebook",
    "destination" => "ebooks"
  }
  @ebook_page ||= Jekyll::EbookPage.new(
    site,
    default_setting
      .merge(params["ebook"])
      .merge({
      "name" => name
    })
  )
end

#readObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/jekyll-books/book_reader.rb', line 20

def read
  @pages = [book_page]
  index = 0

  recursive_read_chapters(summary.read, 1, book_page)
  add_prev_and_next

  if params["ebook"]["enabled"]
    pages << ebook_page

    ebook_page.data["book"] = book_page
  end

  pages
end

#read_chapter(chapter) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/jekyll-books/book_reader.rb', line 68

def read_chapter(chapter)
  page = Jekyll::ChapterPage.new(
    site,
    params.merge({
      "chapter" => chapter.merge(params["chapter"]),
      "name" => name
    }).merge(chapter)
  )
  page.data["book"] = book_page
  page
end

#recursive_read_chapters(chapters, level, parent) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/jekyll-books/book_reader.rb', line 36

def recursive_read_chapters(chapters, level, parent)
  page = parent
  chapter = chapters[index]

  while chapter do
    break if chapter["level"] < level

    if chapter["level"] > level
      recursive_read_chapters(chapters, chapter["level"], page)
      @index -= 1
    else
      page = read_chapter(chapter)
      pages << page
      parent.data["parts"] << page
    end

    @index += 1
    chapter = chapters[index]
  end
end

#summaryObject



80
81
82
# File 'lib/jekyll-books/book_reader.rb', line 80

def summary
  Summary.new(book_folder)
end