Class: Pocketbook::BookRenderer::Chapter

Inherits:
Object
  • Object
show all
Defined in:
lib/pocketbook/book_renderer/chapter.rb

Defined Under Namespace

Classes: Compiled, TocHeading

Instance Method Summary collapse

Constructor Details

#initialize(front_matter: FrontMatter.new) ⇒ Chapter

Returns a new instance of Chapter.



20
21
22
# File 'lib/pocketbook/book_renderer/chapter.rb', line 20

def initialize(front_matter: FrontMatter.new)
  @front_matter = front_matter
end

Instance Method Details

#compile(inputs:) ⇒ Object



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

def compile(inputs:)
  front_matter = {}

  chapters = inputs.map.with_index do |path, index|
    raw = File.read(path)

    if index.zero?
      front_matter, raw = @front_matter.extract(raw)
    end

    title = detect_title(raw, path)
    chapter_html = Kramdown::Document.new(raw, input: "GFM").to_html

    Compiled.new(
      id: chapter_id(title, index),
      source: path,
      title: title,
      html: chapter_html,
      toc_headings: extract_toc_headings(chapter_html)
    )
  end

  [chapters, front_matter]
end