Class: Burr::Book

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, format) ⇒ Book

Returns a new instance of Book.



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
38
# File 'lib/burr/book.rb', line 10

def initialize(config, format)
  @config = config
  @format = format
  @ui = Burr::UI.new

  # directory location
  @gem_dir       = File.expand_path('../../../' ,__FILE__)
  @root_dir      = Dir.pwd
  @outputs_dir   = "#{@root_dir}/outputs"
  @caches_dir    = "#{@root_dir}/caches"
  @contents_dir  = "#{@root_dir}/contents"
  @plugins_dir   = "#{@root_dir}/plugins"
  @templates_dir = "#{@root_dir}/templates"

  # publishing process variables
  @items  = []
  @toc    = ''
  @images = []
  @tables = []
  @current_item = []

  # labels and titles
  book_labels  # @labels = {}, @ids = []
  book_titles  # @titles = {}

  # book information
  book_uid  # @uid  = ''
  book_slug # @slug = ''
end

Instance Attribute Details

#caches_dirObject

Returns the value of attribute caches_dir.



5
6
7
# File 'lib/burr/book.rb', line 5

def caches_dir
  @caches_dir
end

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/burr/book.rb', line 4

def config
  @config
end

#contents_dirObject

Returns the value of attribute contents_dir.



5
6
7
# File 'lib/burr/book.rb', line 5

def contents_dir
  @contents_dir
end

#current_itemObject

Returns the value of attribute current_item.



6
7
8
# File 'lib/burr/book.rb', line 6

def current_item
  @current_item
end

#formatObject

Returns the value of attribute format.



4
5
6
# File 'lib/burr/book.rb', line 4

def format
  @format
end

#gem_dirObject

Returns the value of attribute gem_dir.



5
6
7
# File 'lib/burr/book.rb', line 5

def gem_dir
  @gem_dir
end

#idsObject

Returns the value of attribute ids.



7
8
9
# File 'lib/burr/book.rb', line 7

def ids
  @ids
end

#imagesObject

Returns the value of attribute images.



6
7
8
# File 'lib/burr/book.rb', line 6

def images
  @images
end

#itemsObject

Returns the value of attribute items.



6
7
8
# File 'lib/burr/book.rb', line 6

def items
  @items
end

#labelsObject

Returns the value of attribute labels.



7
8
9
# File 'lib/burr/book.rb', line 7

def labels
  @labels
end

#outputs_dirObject

Returns the value of attribute outputs_dir.



5
6
7
# File 'lib/burr/book.rb', line 5

def outputs_dir
  @outputs_dir
end

#plugins_dirObject

Returns the value of attribute plugins_dir.



5
6
7
# File 'lib/burr/book.rb', line 5

def plugins_dir
  @plugins_dir
end

#root_dirObject

Returns the value of attribute root_dir.



5
6
7
# File 'lib/burr/book.rb', line 5

def root_dir
  @root_dir
end

#slugObject

Returns the value of attribute slug.



8
9
10
# File 'lib/burr/book.rb', line 8

def slug
  @slug
end

#tablesObject

Returns the value of attribute tables.



6
7
8
# File 'lib/burr/book.rb', line 6

def tables
  @tables
end

#templates_dirObject

Returns the value of attribute templates_dir.



5
6
7
# File 'lib/burr/book.rb', line 5

def templates_dir
  @templates_dir
end

#titlesObject

Returns the value of attribute titles.



7
8
9
# File 'lib/burr/book.rb', line 7

def titles
  @titles
end

#tocObject

Returns the value of attribute toc.



6
7
8
# File 'lib/burr/book.rb', line 6

def toc
  @toc
end

#uiObject

Returns the value of attribute ui.



4
5
6
# File 'lib/burr/book.rb', line 4

def ui
  @ui
end

#uidObject

Returns the value of attribute uid.



8
9
10
# File 'lib/burr/book.rb', line 8

def uid
  @uid
end

Instance Method Details

#export_allObject

Export all formats



94
95
96
97
98
# File 'lib/burr/book.rb', line 94

def export_all
  self.export_pdf
  self.export_epub
  self.export_mobi
end

#export_epubObject

Export Epub files in outputs/epub



60
61
62
63
64
65
# File 'lib/burr/book.rb', line 60

def export_epub
  exporter = Burr::Epub.new(self)
  self.ui.confirm "Start exporting epub file...."
  exporter.run
  self.ui.confirm "Exported Epub!"
end

#export_mobiObject

Export Mobi files in outputs/mobi



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/burr/book.rb', line 69

def export_mobi
  dest = File.join(self.outputs_dir, 'mobi')
  FileUtils.mkdir_p(dest) unless File.exist?(dest)

  self.ui.confirm "Start exporting mobi file...."

  FileUtils.cd(File.join(self.outputs_dir, 'epub')) do
    base = "#{self.config['slug']}-#{Time.new.strftime('%Y%m%d')}"
    epub = "#{base}.epub"
    mobi = "#{base}.mobi"
    unless File.exist?(epub)
      self.ui.error('Please export Epub first!')
      exit 1
    end

    system "kindlegen #{epub} -c2"
    FileUtils.cp(mobi, dest)
    FileUtils.rm(mobi)
  end

  self.ui.confirm "Exported Mobi!"
end

#export_pdfObject

Export PDF files in outputs/pdf



51
52
53
54
55
56
# File 'lib/burr/book.rb', line 51

def export_pdf
  exporter = Burr::PDF.new(self)
  self.ui.confirm "Start exporting pdf file...."
  exporter.run
  self.ui.confirm "Exported PDF!"
end

#export_siteObject

Export site files in outputs/site



42
43
44
45
46
47
# File 'lib/burr/book.rb', line 42

def export_site
  exporter = Burr::Site.new(self)
  self.ui.confirm "Start exporting site files...."
  exporter.run
  self.ui.confirm "Exported site!"
end

#render(template, parameters = {}, target = nil) ⇒ String

Renders any template (currently only supports Liquid templates).

Parameters:

  • template (String)

    The template name, without the extension

  • parameters (Hash) (defaults to: {})

    Optional variables passed to the template

  • target (String) (defaults to: nil)

    Optional output file path. If set, the rendered template is saved in this file.

Returns:

  • (String)

    The rendered content



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/burr/book.rb', line 196

def render(template, parameters = {}, target = nil)
  defaults = {
    'config'    => self.config,
    'format'    => self.format,
    'generator' => { 'name' => 'Burr', 'version' => Burr::Version::STRING }
  }
  text = File.read(template)
  registers = { :registers => { :book => self } }
  content = Liquid::Template.parse(text).render(defaults.merge(parameters), registers)

  if target
    File.open(target, 'wb') { |f| f.puts content }
  end

  content
end

#render_id(variables = {}) ⇒ Object

Shortcut method to get the id of headings.

variables - A variables Hash used to render the id.

Returns the id String of the heading.



169
170
171
172
173
174
175
176
177
178
# File 'lib/burr/book.rb', line 169

def render_id(variables = {})
  index = variables['item']['level'] - 1
  if index == 0
    id = self.ids[0]
  else
    id = self.ids[1]
  end

  self.render_string(id, variables)
end

#render_label(element, variables = {}) ⇒ Object

Shortcut method to get the label of any element type.

element - The element type (‘chapter’, ‘foreword’, …) in String format. variables - A variables Hash used to render the label.

Returns the label String of the element or an empty String.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/burr/book.rb', line 147

def render_label(element, variables = {})
  c_labels = self.labels.include?(element) ? self.labels[element] : ''
  # some elements (mostly chapters and appendices) have a different label for each level (h1, ..., h6)
  if c_labels.is_a? Array
    index = variables['item']['level'] - 1
    if index == 0
      label = c_labels[0]
    else
      label = c_labels[1]
    end
  else
    label = c_labels
  end

  self.render_string(label, variables)
end

#render_string(text, variables = {}) ⇒ Object

Renders any string as a Liquid template.

Parameters:

  • text (String)

    The original content to render

  • variables (Array) (defaults to: {})

    Optional variables passed to the template



184
185
186
187
# File 'lib/burr/book.rb', line 184

def render_string(text, variables = {})
  registers = { :registers => { :book => self } }
  Liquid::Template.parse(text).render(variables, registers)
end

#stylesheet_for(format) ⇒ String

Get the stylesheet file for a format.

Parameters:

  • format (String)

    The format name, could be ‘pdf’, ‘epub’, ‘site’ and ‘mobi’

Returns:

  • (String)

    The absolute path to this format’s stylesheet



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

def stylesheet_for(format)
  if %w(pdf epub site mobi).include?(format)
    css = File.join(self.outputs_dir, format, "style.css")

    if File.exist?(css)
      css
    else
      self.ui.error("ERROR: Not found stylesheet for format #{format}.")
      exit 1
    end
  else
    self.ui.error("ERROR: #{format} is not support!")
    exit 1
  end
end

#template_for(element) ⇒ Object

Gets the template file for an element.

  • element The element name, such as ‘chapter’, ‘appendix’

Returns The absolute path of this element’s template file.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/burr/book.rb', line 106

def template_for(element)
  base = File.join('templates', self.format, "#{element}.liquid")
  default = File.join(self.gem_dir, 'resources', base)
  custom = File.join(self.root_dir, base)

  if File.exist?(custom)
    custom
  elsif !File.exist?(custom) && File.exist?(default)
    default
  else
    self.ui.error("ERROR: Template #{self.format}/#{element}.liquid not found!")
    exit 1
  end
end

#to_liquidObject

Makes the liquid tags live.

Returns Hash.



216
217
218
# File 'lib/burr/book.rb', line 216

def to_liquid
  #{ 'book' => self }
end