Class: Docbook::Output::Formats::PagedFormat

Inherits:
BaseFormat
  • Object
show all
Defined in:
lib/docbook/output/formats/paged_format.rb

Constant Summary

Constants inherited from BaseFormat

BaseFormat::DEFAULT_DIST_DIR

Instance Attribute Summary

Attributes inherited from BaseFormat

#dist_dir

Instance Method Summary collapse

Methods inherited from BaseFormat

default_dist_dir, #initialize

Constructor Details

This class inherits a constructor from Docbook::Output::Formats::BaseFormat

Instance Method Details

#write(output_path, guide, title: "DocBook", _manifest: nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/docbook/output/formats/paged_format.rb', line 9

def write(output_path, guide, title: "DocBook", _manifest: nil)
  dir = ensure_directory(output_path)
  FileUtils.mkdir_p(File.join(dir, "pages"))

  toc = guide.dig("toc", "sections") || []
  sections = split_into_pages(guide["content"] || [])

  page_map = {}
  sections.each_with_index do |(section_id, nodes), i|
    renderer = HtmlRenderer.new(guide)
    page_html = renderer.render_nodes(nodes)
    filename = section_id ? "pages/#{section_id}.html" : "pages/intro.html"
    page_key = section_id || "intro"

    page_full = html_boilerplate(
      title: "#{title} — Page #{i + 1}",
      body_content: %(<div id="docbook-content">#{page_html}</div>\n<div id="docbook-app"></div>),
      script_data: "window.DOCBOOK_FORMAT = 'paged'; window.DOCBOOK_PAGE_ID = '#{page_key}';",
    )
    File.write(File.join(dir, filename), page_full)
    page_map[page_key] = filename
  end

  data_script = "window.DOCBOOK_PAGES = #{safe_json(page_map)}; window.DOCBOOK_TOC = #{safe_json(toc)}; window.DOCBOOK_FORMAT = 'paged';"
  html = html_boilerplate(title: title, body_content: '<div id="docbook-app"></div>',
                          script_data: data_script)
  File.write(File.join(dir, "index.html"), html)
  dir
end

#write_library(output_path, guides, manifest:, title: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/docbook/output/formats/paged_format.rb', line 39

def write_library(output_path, guides, manifest:, title: nil)
  dir = ensure_directory(output_path)
  title ||= manifest.name || "DocBook Library"

  books_meta = manifest.books.each_with_index.map do |book, i|
    book_dir = File.join(dir, book.id)
    write(File.join(book_dir, "index.html"), guides[i],
          title: book.title || book.id)

    {
      "id" => book.id,
      "title" => book.title || guides[i].dig("meta",
                                             "title") || book.id,
      "source" => "#{book.id}/",
    }
  end

  collection = { "name" => title, "books" => books_meta }
  data_script = "window.DOCBOOK_COLLECTION = #{safe_json(collection)}; window.DOCBOOK_FORMAT = 'paged';"
  html = html_boilerplate(title: title, body_content: '<div id="docbook-app"></div>',
                          script_data: data_script)
  File.write(File.join(dir, "index.html"), html)
  dir
end