Class: Asciibook::Book
- Inherits:
-
Object
- Object
- Asciibook::Book
- Defined in:
- lib/asciibook/book.rb
Instance Attribute Summary collapse
-
#base_dir ⇒ Object
readonly
Returns the value of attribute base_dir.
-
#basename ⇒ Object
readonly
Returns the value of attribute basename.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#dest_dir ⇒ Object
readonly
Returns the value of attribute dest_dir.
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#pages ⇒ Object
readonly
Returns the value of attribute pages.
-
#template_dir ⇒ Object
readonly
Returns the value of attribute template_dir.
-
#theme_dir ⇒ Object
readonly
Returns the value of attribute theme_dir.
Class Method Summary collapse
Instance Method Summary collapse
- #append_page(path, node) ⇒ Object
- #assets ⇒ Object
- #build ⇒ Object
- #cover_image_path ⇒ Object
- #find_page_node(node) ⇒ Object
-
#initialize(data, options = {}) ⇒ Book
constructor
A new instance of Book.
- #outline ⇒ Object
-
#outline_node(node) ⇒ Object
book outline only list sections that split as page.
- #process ⇒ Object
- #process_page(node) ⇒ Object
- #process_pages ⇒ Object
- #title ⇒ Object
- #to_hash ⇒ Object
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, = {}) @data = data @options = @basename = [:basename] || 'output' @base_dir = [:base_dir] || '.' @dest_dir = [:dest_dir] || File.join(@base_dir, 'build') @theme_dir = [:theme_dir] || File.('../../../theme', __FILE__) @formats = [:formats] || %w(html pdf epub) @template_dir = [:template_dir] @page_level = @options[:page_level] || 1 @logger = @options[:logger] || Logger.new(STDERR, level: :warn) @exclude_patterns = ["build/**/*"] end |
Instance Attribute Details
#base_dir ⇒ Object (readonly)
Returns the value of attribute base_dir.
3 4 5 |
# File 'lib/asciibook/book.rb', line 3 def base_dir @base_dir end |
#basename ⇒ Object (readonly)
Returns the value of attribute basename.
3 4 5 |
# File 'lib/asciibook/book.rb', line 3 def basename @basename end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
3 4 5 |
# File 'lib/asciibook/book.rb', line 3 def data @data end |
#dest_dir ⇒ Object (readonly)
Returns the value of attribute dest_dir.
3 4 5 |
# File 'lib/asciibook/book.rb', line 3 def dest_dir @dest_dir end |
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
3 4 5 |
# File 'lib/asciibook/book.rb', line 3 def doc @doc end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/asciibook/book.rb', line 3 def @options end |
#pages ⇒ Object (readonly)
Returns the value of attribute pages.
3 4 5 |
# File 'lib/asciibook/book.rb', line 3 def pages @pages end |
#template_dir ⇒ Object (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_dir ⇒ Object (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, = {}) .merge!( basename: File.basename(path, '.*'), base_dir: File.dirname(path) ) if File.exist?(path) new(File.open(path, 'r:utf-8').read, ) 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 |
#assets ⇒ Object
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 |
#build ⇒ Object
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_path ⇒ Object
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 |
#outline ⇒ Object
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 |
#process ⇒ Object
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_pages ⇒ Object
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 |
#title ⇒ Object
41 42 43 |
# File 'lib/asciibook/book.rb', line 41 def title doc.attributes['doctitle'] end |
#to_hash ⇒ Object
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 |