Class: Bookpress::Book

Inherits:
Object
  • Object
show all
Defined in:
lib/bookpress.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory) ⇒ Book

Returns a new instance of Book.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bookpress.rb', line 24

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

Instance Attribute Details

#pagesObject

Returns the value of attribute pages.



22
23
24
# File 'lib/bookpress.rb', line 22

def pages
  @pages
end

#titleObject

Returns the value of attribute title.



22
23
24
# File 'lib/bookpress.rb', line 22

def title
  @title
end

#treeObject

Returns the value of attribute tree.



22
23
24
# File 'lib/bookpress.rb', line 22

def tree
  @tree
end

Instance Method Details

#to_htmlObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bookpress.rb', line 47

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