class Bookpress::Book

Attributes

pages[RW]
title[RW]
tree[RW]

Public Class Methods

new(directory) click to toggle source
# File lib/bookpress.rb, line 25
def initialize(directory)
  # Get all markdown files
  @pages = Pathname.glob("#{directory}/" "**/*.{md,markdown}")
  
  # Create a Hash to store the book tree structure
  @tree = Hash.new { |h, k| h[k] = Hash.new &h.default_proc }

  @pages.each do |page|
   subdirectory = @tree
   page_name    = page.basename
   page.cleanpath.each_filename do |segment|
     subdirectory[segment]
     if segment.to_s == page_name.to_s
       subdirectory[segment] = Utility.markdown_renderer.render(page.read)
     end
     subdirectory = subdirectory[segment]
   end
  end

  # Sort the structure by key
  @tree = @tree.sort_by_key(true) { |x, y| Utility.orderify(x.to_s) <=> Utility.orderify(y.to_s) }
end

Public Instance Methods

to_html() click to toggle source
# File lib/bookpress.rb, line 48
def to_html
  # Build the document
  builder = Nokogiri::HTML::Builder.new do |doc|
   doc.html do
     doc.body do
       doc.cdata Utility.articlize(@tree)
     end
   end
  end
  
  builder.to_html
end