Sha256: 371b767edf66b25bdfdcd24819baf8a2504cd75f2ad7568c64c193c5055b4191

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

require "jekyll-books/books_parser"
require "util"

module Jekyll
  class BooksGenerator < Generator
    def generate(site)
      params = get_params(site)

      parser = BooksParser.new(params["source"], site)
      parser.parse(params)
      site.pages += parser.pages

      books = parser.books
      books += get_books_from_data(site)
      sort_books_by_date(books)
      site.config["books"] = books
    end

    def get_params(site)
      default_setting = {
        "source" => "_books",
        "destination" => "/",
        "book" => {
          "layout" => "book"
        },
        "chapter" => {
          "layout" => "chapter"
        }
      }

      default_setting.merge(site.config["books_settings"] || {})
    end

    private
      def get_books_from_data(site)
        books = site.data["books"]
        return [] unless books

        books.each do |book|
          Util.init_date_of_book(book)
        end
      end

      def sort_books_by_date(books)
        books.sort_by! do |book|
          if book.respond_to? "data"
            [book.data["end"], book.data["start"]]
          else
            [book["end"], book["start"]]
          end
        end
        books.reverse!
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jekyll-books-0.2.1 lib/jekyll/generators/book.rb
jekyll-books-0.2.0 lib/jekyll/generators/book.rb