Class: Asciibook::Book

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, options = {}) ⇒ Book

Returns a new instance of Book.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/asciibook/book.rb', line 5

def initialize(data, options = {})
  @data = data
  @options = options
  @basename = options[:basename] || 'output'
  @base_dir = options[:base_dir] || '.'
  @dest_dir = options[:dest_dir] || File.join(@base_dir, 'build')
  @theme_dir = options[:theme_dir] || File.expand_path('../../../theme', __FILE__)
  @formats = options[:formats] || %w(html pdf epub)
  @template_dir = options[:template_dir]

  @page_level = @options[:page_level] || 1

  @logger = @options[:logger] || Logger.new(STDERR, level: :warn)

  @exclude_patterns = ["build/**/*"]
end

Instance Attribute Details

#base_dirObject (readonly)

Returns the value of attribute base_dir.



3
4
5
# File 'lib/asciibook/book.rb', line 3

def base_dir
  @base_dir
end

#basenameObject (readonly)

Returns the value of attribute basename.



3
4
5
# File 'lib/asciibook/book.rb', line 3

def basename
  @basename
end

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/asciibook/book.rb', line 3

def data
  @data
end

#dest_dirObject (readonly)

Returns the value of attribute dest_dir.



3
4
5
# File 'lib/asciibook/book.rb', line 3

def dest_dir
  @dest_dir
end

#docObject (readonly)

Returns the value of attribute doc.



3
4
5
# File 'lib/asciibook/book.rb', line 3

def doc
  @doc
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/asciibook/book.rb', line 3

def options
  @options
end

#pagesObject (readonly)

Returns the value of attribute pages.



3
4
5
# File 'lib/asciibook/book.rb', line 3

def pages
  @pages
end

#template_dirObject (readonly)

Returns the value of attribute template_dir.



3
4
5
# File 'lib/asciibook/book.rb', line 3

def template_dir
  @template_dir
end

#theme_dirObject (readonly)

Returns the value of attribute theme_dir.



3
4
5
# File 'lib/asciibook/book.rb', line 3

def theme_dir
  @theme_dir
end

Class Method Details

.load_file(path, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/asciibook/book.rb', line 22

def self.load_file(path, options = {})
  options.merge!(
    basename: File.basename(path, '.*'),
    base_dir: File.dirname(path)
  )

  if File.exist?(path)
    new(File.open(path, 'r:utf-8').read, options)
  else
    raise "File not exists #{path}"
  end
end

Instance Method Details

#append_page(path, node) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/asciibook/book.rb', line 125

def append_page(path, node)
  if @pages.map(&:path).include?(path)
    @logger.warn("Page path already in use: #{path}")
  end

  page = Page.new(
    path: path,
    node: node
  )

  if last_page = @pages.last
    page.prev_page = last_page
    last_page.next_page = page
  end

  node.page = page
  @pages << page
end

#assetsObject



144
145
146
147
148
149
150
# File 'lib/asciibook/book.rb', line 144

def assets
  Dir.glob('**/*.{jpg,png,svg,gif,mp3,mp4,ogg,wav}', File::FNM_CASEFOLD, base: @base_dir).reject do |path|
    @exclude_patterns.any? do |pattern|
      File.fnmatch?(pattern, path)
    end
  end
end

#buildObject



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/asciibook/book.rb', line 89

def build
  if @formats.include?('html')
    Builders::HtmlBuilder.new(self).build
  end

  if @formats.include?('pdf')
    Builders::PdfBuilder.new(self).build
  end

  if @formats.include?('epub')
    Builders::EpubBuilder.new(self).build
  end
end

#cover_image_pathObject



45
46
47
# File 'lib/asciibook/book.rb', line 45

def cover_image_path
  doc.attributes['cover-image']
end

#find_page_node(node) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/asciibook/book.rb', line 71

def find_page_node(node)
  page_node = node

  until page_node.page or page_node.parent.nil?
    page_node = page_node.parent
  end

  page_node
end

#outlineObject



49
50
51
# File 'lib/asciibook/book.rb', line 49

def outline
  outline_node(doc)
end

#outline_node(node) ⇒ Object

book outline only list sections that split as page



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/asciibook/book.rb', line 54

def outline_node(node)
  data = []
  node.sections.each do |section|
    if section.page
      section_data = {
        'title' => section.xreftext,
        'path' => section.page.path
      }
      if section.sections.count > 0 and section.level < @page_level
        section_data['items'] = outline_node(section)
      end
      data << section_data
    end
  end
  data
end

#processObject



35
36
37
38
39
# File 'lib/asciibook/book.rb', line 35

def process
  @doc = Asciidoctor.load(@data, base_dir: @base_dir, backend: 'asciibook', logger: @logger, safe: :unsafe, template_dirs: [@template_dir].compact)
  @toc = nil
  process_pages
end

#process_page(node) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/asciibook/book.rb', line 115

def process_page(node)
  append_page("#{node.id}.html", node)

  if node.level < @page_level
    node.sections.each do |section|
      process_page(section)
    end
  end
end

#process_pagesObject



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/asciibook/book.rb', line 103

def process_pages
  @pages = []

  append_page('index.html', doc)

  if @page_level > 0
    doc.sections.each do |section|
      process_page(section)
    end
  end
end

#titleObject



41
42
43
# File 'lib/asciibook/book.rb', line 41

def title
  doc.attributes['doctitle']
end

#to_hashObject



81
82
83
84
85
86
87
# File 'lib/asciibook/book.rb', line 81

def to_hash
  {
    'title' => doc.attributes['doctitle'],
    'attributes' => doc.attributes,
    'outline' => outline
  }
end