jekyll-ebook 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # For use/testing when no gem is installed
4
+ $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
5
+
6
+ require 'jekyll-ebook'
7
+
8
+ # Generate an EPUB from manifests passed as command line arguments
9
+ ARGV.each { |m| Ebook.new(m).generate_epub }
@@ -0,0 +1,18 @@
1
+
2
+ # Name:: EPUB Generator for Jekyll
3
+ # Author:: Lincoln Mullen | lincoln@lincolnmullen.com
4
+ # Copyright:: Copyright (c) 2012 Lincoln Mullen
5
+ # License:: MIT License | http://lmullen.mit-license.org/
6
+
7
+ # A Ruby script to create EPUB books from Jekyll posts and pages using
8
+ # Pandoc.
9
+
10
+ $:.unshift File.dirname(__FILE__) # For use/testing when no gem is installed
11
+
12
+ # load the required files
13
+ require 'yaml'
14
+ require 'pandoc-ruby'
15
+ require 'jekyll-ebook/article'
16
+ require 'jekyll-ebook/ebook'
17
+
18
+
@@ -0,0 +1,63 @@
1
+ # The Article class loads a file that is formatted as a Jekyll post or
2
+ # page with metadata in a YAML block at the top and the content in
3
+ # Markdown or HTML. It has a method to export the metadata in a format
4
+ # suitable for conversion to EPUB using Pandoc.
5
+
6
+ class Article
7
+
8
+ # +filename+ is the name of the Jekyll post or page
9
+ # +metadata+ is the metadata read in from the post or page
10
+ # +content+ is the body of the post or page
11
+ # +required_fields+ is the list of metadata items to include
12
+ attr_accessor :filename, :metadata, :content, :required_fields
13
+
14
+ # Retrieve the filename then call the method to read its data. Pass
15
+ # required_fields along but delete 'title' since we're going to print
16
+ # it regardless.
17
+ def initialize( filename, required_fields )
18
+ @required_fields = required_fields.delete_if { |f| f == "title" }
19
+ @filename = filename
20
+ self.read_file
21
+ end
22
+
23
+ # Read the metadata and content from the Jekyll post or page
24
+ def read_file
25
+
26
+ self.content = File.read(@filename)
27
+
28
+ # Define a regular expression to find the YAML header Use the back
29
+ # reference to load the metadata and the postmatch to load the
30
+ # content.
31
+ begin
32
+ if /^(---\s*\n.*?\n?)^(---\s*$\n?)/m.match(self.content)
33
+ self.metadata = YAML.load($1)
34
+ self.content = $'
35
+ end
36
+ rescue => e
37
+ puts "YAML exception reading #{filename}: #{e.message}"
38
+ end
39
+
40
+ end
41
+
42
+ # Print the relevant metadata in a block with CSS selectors for
43
+ # formatting in the e-book, then print the content
44
+ def format_article
45
+
46
+ # an array to hold all our output
47
+ out = Array.new
48
+
49
+ out.push "# " + self.metadata['title'] + "\n\n"
50
+
51
+ # Loop through the required fields, printing them if they exist
52
+ self.required_fields.each do |f|
53
+ out.push "<p class='#{f}'>" + self.metadata[f] + "</p>\n\n" unless self.metadata[f].nil?
54
+ end
55
+
56
+ out.push self.content
57
+
58
+ # Return the contents of the array
59
+ return out.join("\n")
60
+
61
+ end
62
+
63
+ end
@@ -0,0 +1,67 @@
1
+ # The Ebook class reads a YAML manifest file that contains a list of
2
+ # Jekyll posts and pages to be included in the EPUB book. It then
3
+ # creates an article object for each file, writing the metadata to both
4
+ # a table of contents and a body file.
5
+
6
+ class Ebook
7
+
8
+ # +manifest+ is the filename of the YAML manifest
9
+ attr_accessor :manifest
10
+
11
+ # Initialize the object with the filename of the manifest
12
+ def initialize( manifest )
13
+
14
+ begin
15
+ @manifest = YAML.load(File.read(manifest))
16
+ rescue => e
17
+ puts "YAML exception reading #{manifest}: #{e.message}"
18
+ end
19
+
20
+ end
21
+
22
+ # Loop through the contents in the manifest to generate the body
23
+ # content and the table of contents
24
+ def generate_content
25
+
26
+ # an array to hold all our output
27
+ out = Array.new
28
+
29
+ # Generate front matter as Pandoc title block
30
+ out.push "% " + self.manifest['title']
31
+ out.push "% " + self.manifest['author']
32
+ out.push "% " + self.manifest['date'] + "\n" unless self.manifest['date'].nil?
33
+
34
+ # Loop through the sections in the manifest's list of contents
35
+ self.manifest['contents'].each do |section|
36
+
37
+ out.push "# " + section['section-title'] + "\n\n"
38
+
39
+ # Loop through the files in this section. Create an Article object
40
+ # for each file, to which we pass the directory plus filename and
41
+ # the list of required metadata items.
42
+ section['files'].each do |filename|
43
+ out.push Article.new( self.manifest['content-dir'] + filename , self.manifest['header-items'] ).format_article
44
+ end
45
+
46
+ end
47
+
48
+ # Return the contents of the array
49
+ return out.join("\n")
50
+
51
+ end
52
+
53
+ # Use PandocRuby to take the output of the `generate_content` method
54
+ # and create an EPUB file from it, using the settings in the manifest.
55
+ def generate_epub
56
+
57
+ Dir.chdir(self.manifest['epub-dir'])
58
+ PandocRuby.new( self.generate_content ,
59
+ {:f => :markdown, :to => :epub},
60
+ 'smart', 'o' => self.manifest['epub-filename'],
61
+ 'epub-cover-image' => self.manifest['epub-cover-image'],
62
+ 'epub-metadata' => self.manifest['epub-metadata'],
63
+ 'epub-stylesheet' => self.manifest['epub-stylesheet'],).convert
64
+
65
+ end
66
+
67
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-ebook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Lincoln A. Mullen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-01 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A gem to generate an EPUB e-book from Jekyll posts and pages
15
+ email: lincoln@lincolnmullen.com
16
+ executables:
17
+ - jekyll-ebook
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/jekyll-ebook.rb
22
+ - lib/jekyll-ebook/article.rb
23
+ - lib/jekyll-ebook/ebook.rb
24
+ - bin/jekyll-ebook
25
+ homepage: https://github.com/lmullen/jekyll-ebook
26
+ licenses:
27
+ - MIT
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 1.8.24
47
+ signing_key:
48
+ specification_version: 3
49
+ summary: EPUB generator for Jekyll
50
+ test_files: []
51
+ has_rdoc: