Sha256: b555f7fdc77020e4d73e9f3dc3acd1c43119ef626cd2ffc6341ed0a0dd74328d

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

require "jekyll/pages/chapter"
require "jekyll/pages/book"
require_relative "summary"

class BookReader
  attr_reader :root, :site, :name, :destination, :params
  attr_accessor :index, :pages

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

  def read
    @pages = [book_page]
    index = 0

    recursive_read_chapters(summary.read, 1, book_page)
    add_prev_and_next

    pages
  end

  def recursive_read_chapters(chapters, level, parent)
    page = parent
    chapter = parent

    begin
      chapter = chapters[index]
      return unless chapter

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

      @index += 1
    end while chapter["level"] >= level
  end

  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

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

  def summary
    Summary.new(book_folder)
  end

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jekyll-books-0.2.0 lib/jekyll-books/book_reader.rb