Class: Burr::Book
Instance Attribute Summary collapse
-
#caches_dir ⇒ Object
Returns the value of attribute caches_dir.
-
#config ⇒ Object
Returns the value of attribute config.
-
#contents_dir ⇒ Object
Returns the value of attribute contents_dir.
-
#current_item ⇒ Object
Returns the value of attribute current_item.
-
#format ⇒ Object
Returns the value of attribute format.
-
#gem_dir ⇒ Object
Returns the value of attribute gem_dir.
-
#ids ⇒ Object
Returns the value of attribute ids.
-
#images ⇒ Object
Returns the value of attribute images.
-
#items ⇒ Object
Returns the value of attribute items.
-
#labels ⇒ Object
Returns the value of attribute labels.
-
#outputs_dir ⇒ Object
Returns the value of attribute outputs_dir.
-
#plugins_dir ⇒ Object
Returns the value of attribute plugins_dir.
-
#root_dir ⇒ Object
Returns the value of attribute root_dir.
-
#slug ⇒ Object
Returns the value of attribute slug.
-
#tables ⇒ Object
Returns the value of attribute tables.
-
#templates_dir ⇒ Object
Returns the value of attribute templates_dir.
-
#titles ⇒ Object
Returns the value of attribute titles.
-
#toc ⇒ Object
Returns the value of attribute toc.
-
#ui ⇒ Object
Returns the value of attribute ui.
-
#uid ⇒ Object
Returns the value of attribute uid.
Instance Method Summary collapse
-
#export_all ⇒ Object
Export all formats.
-
#export_epub ⇒ Object
Export Epub files in outputs/epub.
-
#export_mobi ⇒ Object
Export Mobi files in outputs/mobi.
-
#export_pdf ⇒ Object
Export PDF files in outputs/pdf.
-
#export_site ⇒ Object
Export site files in outputs/site.
-
#initialize(config, format) ⇒ Book
constructor
A new instance of Book.
-
#render(template, parameters = {}, target = nil) ⇒ String
Renders any template (currently only supports Liquid templates).
-
#render_id(variables = {}) ⇒ Object
Shortcut method to get the id of headings.
-
#render_label(element, variables = {}) ⇒ Object
Shortcut method to get the label of any element type.
-
#render_string(text, variables = {}) ⇒ Object
Renders any string as a Liquid template.
-
#stylesheet_for(format) ⇒ String
Get the stylesheet file for a format.
-
#template_for(element) ⇒ Object
Gets the template file for an element.
-
#to_liquid ⇒ Object
Makes the liquid tags live.
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.('../../../' ,__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_dir ⇒ Object
Returns the value of attribute caches_dir.
5 6 7 |
# File 'lib/burr/book.rb', line 5 def caches_dir @caches_dir end |
#config ⇒ Object
Returns the value of attribute config.
4 5 6 |
# File 'lib/burr/book.rb', line 4 def config @config end |
#contents_dir ⇒ Object
Returns the value of attribute contents_dir.
5 6 7 |
# File 'lib/burr/book.rb', line 5 def contents_dir @contents_dir end |
#current_item ⇒ Object
Returns the value of attribute current_item.
6 7 8 |
# File 'lib/burr/book.rb', line 6 def current_item @current_item end |
#format ⇒ Object
Returns the value of attribute format.
4 5 6 |
# File 'lib/burr/book.rb', line 4 def format @format end |
#gem_dir ⇒ Object
Returns the value of attribute gem_dir.
5 6 7 |
# File 'lib/burr/book.rb', line 5 def gem_dir @gem_dir end |
#ids ⇒ Object
Returns the value of attribute ids.
7 8 9 |
# File 'lib/burr/book.rb', line 7 def ids @ids end |
#images ⇒ Object
Returns the value of attribute images.
6 7 8 |
# File 'lib/burr/book.rb', line 6 def images @images end |
#items ⇒ Object
Returns the value of attribute items.
6 7 8 |
# File 'lib/burr/book.rb', line 6 def items @items end |
#labels ⇒ Object
Returns the value of attribute labels.
7 8 9 |
# File 'lib/burr/book.rb', line 7 def labels @labels end |
#outputs_dir ⇒ Object
Returns the value of attribute outputs_dir.
5 6 7 |
# File 'lib/burr/book.rb', line 5 def outputs_dir @outputs_dir end |
#plugins_dir ⇒ Object
Returns the value of attribute plugins_dir.
5 6 7 |
# File 'lib/burr/book.rb', line 5 def plugins_dir @plugins_dir end |
#root_dir ⇒ Object
Returns the value of attribute root_dir.
5 6 7 |
# File 'lib/burr/book.rb', line 5 def root_dir @root_dir end |
#slug ⇒ Object
Returns the value of attribute slug.
8 9 10 |
# File 'lib/burr/book.rb', line 8 def slug @slug end |
#tables ⇒ Object
Returns the value of attribute tables.
6 7 8 |
# File 'lib/burr/book.rb', line 6 def tables @tables end |
#templates_dir ⇒ Object
Returns the value of attribute templates_dir.
5 6 7 |
# File 'lib/burr/book.rb', line 5 def templates_dir @templates_dir end |
#titles ⇒ Object
Returns the value of attribute titles.
7 8 9 |
# File 'lib/burr/book.rb', line 7 def titles @titles end |
#toc ⇒ Object
Returns the value of attribute toc.
6 7 8 |
# File 'lib/burr/book.rb', line 6 def toc @toc end |
#ui ⇒ Object
Returns the value of attribute ui.
4 5 6 |
# File 'lib/burr/book.rb', line 4 def ui @ui end |
#uid ⇒ Object
Returns the value of attribute uid.
8 9 10 |
# File 'lib/burr/book.rb', line 8 def uid @uid end |
Instance Method Details
#export_all ⇒ Object
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_epub ⇒ Object
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_mobi ⇒ Object
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_pdf ⇒ Object
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_site ⇒ Object
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).
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.
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.
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_liquid ⇒ Object
Makes the liquid tags live.
Returns Hash.
216 217 218 |
# File 'lib/burr/book.rb', line 216 def to_liquid #{ 'book' => self } end |