Class: Jekyll::Epub

Inherits:
Object
  • Object
show all
Includes:
Filters
Defined in:
lib/jekyll/epub.rb,
lib/jekyll/epub/tasks.rb

Defined Under Namespace

Classes: Tasks

Constant Summary collapse

DEFAULTS =
Jekyll::DEFAULTS.merge( {
  'destination' => File.join('.', '_epub', 'src'),
  'permalink'   => '/:title.html',
  'epub' => {
    'title' => "My Jekyll Blog",
    'language' => 'en',
    'identifier' => UUID.generate
  }
} )

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configuration(override = {}) ⇒ Object

Generate a Jekyll::Epub configuration Hash by merging the default options with anything in _epub.yml, and adding the given options on top override is a Hash of config directives

Returns Hash



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/jekyll/epub.rb', line 209

def self.configuration(override = {})
  # _epub.yml may override default source location, but until
  # then, we need to know where to look for _config.yml
  source = override['source'] || Jekyll::Epub::DEFAULTS['source']

  # Get configuration from <source>/_epub.yml
  config_file = File.join(source, '_epub.yml')
  begin
    config = YAML.load_file(config_file)
    raise "Invalid configuration - #{config_file}" if !config.is_a?(Hash)
    $stdout.puts "** Configuration from #{config_file}"
  rescue => err
    $stderr.puts "** WARNING: Could not read configuration. Using defaults (and options)."
    $stderr.puts "\t" + err.to_s
    config = {}
  end

  # Merge DEFAULTS < _config.yml < override
  Jekyll::Epub::DEFAULTS.deep_merge(config).deep_merge(override)
end

Instance Method Details

#create(override) ⇒ Object

Generate the epub! All in one!



231
232
233
234
235
236
237
238
239
240
# File 'lib/jekyll/epub.rb', line 231

def create( override )
  options = Jekyll::Epub.configuration( override )
  
  site = Jekyll::Site.new(options)
  site.epub
  
  if options['epub']['validate'] == true
    self.validate( site )
  end
end

#validate(site) ⇒ Object

A tiny XHTML validator.

If you want to validate your xhtml files, juste add

epub:
  validate: true

in your _epub.yml file.



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/jekyll/epub.rb', line 250

def validate( site )
  begin
    require 'xml/libxml'
  rescue => e
    $stderr.puts "** WARNING: libxml-ruby is not installed. Can't validate!"
    return
  end
  
  dtd_file = File.join( File.expand_path( File.dirname( __FILE__ ) ), "epub", "dtd", "xhtml1-strict.dtd" )
  dtd = XML::Dtd.new("-//W3C//DTD XHTML 1.0 Strict//EN", dtd_file)
  
  (site.posts + site.pages).each do |path|
    file = File.join( site.dest, path.url )
    $stderr.puts "** Validate #{file}."
    begin
      doc = XML::Document.file(file)
      doc.validate(dtd)
    rescue => e
      $stderr.puts e.message
    end
  end
end