Class: Asciidoctor::Html::Book

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor/html/book.rb

Overview

A book is a collection of documents with cross referencing supported via the cref macro.

Defined Under Namespace

Classes: TData

Constant Summary collapse

DOCATTRS =
{
  "sectids" => false,
  "stem" => "latexmath",
  "hide-uri-scheme" => true,
  "source-highlighter" => "highlight.js",
  "imagesdir" => IMG_PATH
}.freeze
DEFAULT_OPTS =
{
  title: "Untitled Book",
  author: "Anonymous Author",
  chapname: "Chapter"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Book

opts:

  • title

  • short_title

  • author

  • date

  • se_id

  • chapname



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/asciidoctor/html/book.rb', line 51

def initialize(opts = {})
  opts = DEFAULT_OPTS.merge opts
  @title = ERB::Escape.html_escape opts[:title]
  @short_title = ERB::Escape.html_escape opts[:short_title]
  @author = ERB::Escape.html_escape opts[:author]
  @date = opts.include?(:date) ? Date.parse(opts[:date]) : Date.today
  @se_id = opts[:se_id]
  @base_url = opts[:base_url]
  @chapname = opts[:chapname]
  @refs = {} # Hash(docname => Hash(id => reftext))
  @templates = {} # Hash(docname => TData)
end

Instance Attribute Details

#authorObject (readonly)

Returns the value of attribute author.



18
19
20
# File 'lib/asciidoctor/html/book.rb', line 18

def author
  @author
end

#chapnameObject (readonly)

Returns the value of attribute chapname.



18
19
20
# File 'lib/asciidoctor/html/book.rb', line 18

def chapname
  @chapname
end

#dateObject (readonly)

Returns the value of attribute date.



18
19
20
# File 'lib/asciidoctor/html/book.rb', line 18

def date
  @date
end

#refsObject (readonly)

Returns the value of attribute refs.



18
19
20
# File 'lib/asciidoctor/html/book.rb', line 18

def refs
  @refs
end

#templatesObject (readonly)

Returns the value of attribute templates.



18
19
20
# File 'lib/asciidoctor/html/book.rb', line 18

def templates
  @templates
end

#titleObject (readonly)

Returns the value of attribute title.



18
19
20
# File 'lib/asciidoctor/html/book.rb', line 18

def title
  @title
end

Instance Method Details

#read(chapters = [], appendices = []) ⇒ Object

params:

  • chapters: array of filenames

  • appendices: array of filenames

returns: Hash(file_basename_without_ext => html)



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/asciidoctor/html/book.rb', line 68

def read(chapters = [], appendices = [])
  docs = {} # Hash(docname => document)
  chapters.each_with_index do |filename, idx|
    doc = chapter filename, idx
    register! docs, filename, doc
  end
  appendices.each_with_index do |filename, idx|
    doc = appendix filename, idx, appendices.size
    register! docs, filename, doc
  end
  html docs
end

#write(chapters, appendices, outdir, sitemap: false) ⇒ Object

params:

  • chapters: array of filenames

  • appendices: array of filenames

  • outdir: directory to write the converted html files to



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/asciidoctor/html/book.rb', line 85

def write(chapters, appendices, outdir, sitemap: false)
  needs_sitemap = sitemap && @base_url
  entries = [] # for sitemap
  read(chapters, appendices).each do |name, html|
    filename = "#{name}.html"
    File.write("#{outdir}/#{filename}", html)
    entries << Template.sitemap_entry("#{@base_url}#{filename}") if needs_sitemap
  end
  File.write("#{outdir}/#{SEARCH_PAGE}", search_page(@se_id)) if @se_id
  File.write("#{outdir}/sitemap.xml", Template.sitemap(entries)) if needs_sitemap
end