Skip to content

Commit

Permalink
Merge 39e5c13 into 1ceb80b
Browse files Browse the repository at this point in the history
  • Loading branch information
rjm226 committed Oct 11, 2016
2 parents 1ceb80b + 39e5c13 commit 1676ab4
Show file tree
Hide file tree
Showing 9 changed files with 711 additions and 0 deletions.
409 changes: 409 additions & 0 deletions examples/ebook/Readme.md

Large diffs are not rendered by default.

192 changes: 192 additions & 0 deletions examples/ebook/custom-modules/metalsmith-ebook/index.js
@@ -0,0 +1,192 @@
var pdf = require('html-pdf');
var Epub = require("epub-gen");
var kindlegen = require('kindlegen');
var fs = require('fs');
var mkdirp = require('mkdirp');

var my_plugin = function (options) {
return function (files, metalsmith, done) {

var destPath = metalsmith.destination()+'/';
var metadata = metalsmith.metadata();

// THROW ERROR IF TITLE ATTRIBUTE IS NOT SET
if (!metadata.title) {
throw("YOU MUST SET TITLE ATTRIBUTE OF METADATA");
}else{
var bookTitle = metadata.title;
}

Object.keys(files).forEach(function(file){

var contents = files[file].contents.toString();

var path = files[file].path + '/';

var title = files[file].title;

var p0 = new Promise(

function(resolve, reject){

if (files[file].path) {

var filePath = destPath + path + title + '.pdf';

createPDF(contents, options.pdf, filePath);

}else{

var filePath = destPath + bookTitle + '.pdf';

createPDF(contents, options.pdf, filePath);

}

function createPDF(contents, options, filePath){

pdf.create(contents, options).toFile(filePath, function(err, res) {

if (err) {
reject();
} else {
resolve(contents, options);
}

});

}

}
);

p0.then(function(){

console.log("PDF CREATED");

var p1 = new Promise(

function(resolve, reject){

var multiChapterCheck = contents.includes('<meta name="layout" content="full-book">');

if (multiChapterCheck) {
var res = contents.split('<div class="chapter-divider"></div>');

var chapterArray = [];

res.forEach(function(entry){

chapterArray.push({data: entry});

});
}

// Generate EPUB

if (multiChapterCheck) {

chapterArray.pop();

options.epub = {
title: options.title, // *Required, title of the book.
author: options.author, // *Required, name of the author.
content: chapterArray
};

}else{

options.epub = {
title: options.title, // *Required, title of the book.
author: options.author, // *Required, name of the author.
content: [
{
data: contents
}
]
};

}

if (files[file].path) {
var epubpath = destPath+path+title+'.epub';
var mobipath = destPath+path+title+'.mobi';
var dir = destPath+path;
}else{
var epubpath = destPath + bookTitle + '.epub';
var mobipath = destPath + bookTitle + '.mobi';
var dir = destPath
}

if (!fs.existsSync(dir)){
mkdirp(dir, function (err) {
if (err){
reject(err);
}
else {
var paths = {
epubpath: epubpath,
mobipath: mobipath
}
resolve(paths);
}
});
}else{
var paths = {
epubpath: epubpath,
mobipath: mobipath
}
resolve(paths);
}


}

);

p1.then(function(paths) {

new Epub(options.epub, paths.epubpath).promise.then(function(){

console.log('EPUB CREATED');

kindlegen(fs.readFileSync(paths.epubpath), (error, mobi) => {
if (error) {
console.log(error);
}else{
fs.writeFile(paths.mobipath, mobi, function(err){
if (err) {
console.log(err);
}else{
console.log("MOBI CREATED");
};
});
};
});

}, function(err){

console.error("Failed to generate Ebook because of ", err)

});

}).catch(function(err){

console.log(err);

});

}).catch( function() {

console.log('ERROR GENERATING PDF');

});

});

done();
};
};

// Expose the plugin
module.exports = my_plugin;
10 changes: 10 additions & 0 deletions examples/ebook/custom-modules/metalsmith-ebook/package.json
@@ -0,0 +1,10 @@
{
"name": "metalsmith-ebook",
"version": "1.0.0",
"dependencies": {
"html-pdf": "^2.1.0",
"epub-gen": "^0.0.16",
"kindlegen": "1.0.1",
"mkdirp": "^0.5.1"
}
}
43 changes: 43 additions & 0 deletions examples/ebook/index.js
@@ -0,0 +1,43 @@
var Metalsmith = require('metalsmith');
var markdown = require('metalsmith-markdown');
var layouts = require('metalsmith-layouts');
var permalinks = require('metalsmith-permalinks');
var collections = require('metalsmith-collections');
var ebook = require('./custom-modules/metalsmith-ebook');

Metalsmith(__dirname)
.metadata({
title: "Ebook Example",
description: "Generate ebooks easily with Metalsmith!",
generator: "Metalsmith",
url: "http://www.metalsmith.io/"
})
.source('./src')
.destination('./build')
.clean(true)
.use(collections({
chapters: {
pattern: 'chapters/*.md'
}
}))
.use(markdown())
.use(permalinks())
.use(layouts({
engine: 'handlebars'
}))
.use(ebook({
title: "Metalsmith Ebook Example",
author: "Team Metalsmith",
pdf: {
"format": "Letter", // allowed units: A3, A4, A5, Legal, Letter, Tabloid
"orientation": "portrait", // portrait or landscape
}
}))
.build(function(err, files) {
if (err) {
throw err;
}
});



20 changes: 20 additions & 0 deletions examples/ebook/layouts/layout.html
@@ -0,0 +1,20 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>{{ title }}</title>
<meta name="description" content="{{ description }}">
<meta name="layout" content="full-book">
</head>
<body>

{{#each collections.chapters }}

{{{this.contents}}}

<div class="chapter-divider"></div>

{{/each}}

</body>
</html>
13 changes: 13 additions & 0 deletions examples/ebook/package.json
@@ -0,0 +1,13 @@
{
"name": "ebook-example",
"version": "1.0.0",
"dependencies": {
"handlebars": "^4.0.5",
"metalsmith": "^2.1.0",
"metalsmith-layouts": "^1.4.1",
"metalsmith-markdown": "^0.2.1",
"metalsmith-permalinks": "^0.5.0",
"metalsmith-collections": "^0.7.0",
"mkdirp": "^0.5.1"
}
}
10 changes: 10 additions & 0 deletions examples/ebook/src/chapters/chapter001.md
@@ -0,0 +1,10 @@
---
chapter: 1
title: The First Example Chapter
---

## Chapter 1: The First Example Chapter

The more she shat robb nymeria jaehaerys euron night's king. Mya stone pate lyanna white walkers. Qhorin halfhand ashara yoren gendry the hound, robb mya stone varys aemon. Wights thoros areo hotah shaggydog, olenna night's king tormund balon edmure asha ghost summer bran. Gendry areo margaery craster aegon daeron.

Stannis brandon stark areo hotah, greywind varamyr meera reed dunk theon euron baelor night's king sandor. Catelyn myrcella gerold hightower, rhaegar tommen podrick grey worm mance rayder donal noye robb dunk old nan areo hotah craster. Arthur dayne meera reed dagmer cleftjaw areo hotah white walkers the hound. Olenna Wun Weg Wun Dar Wun aemon edmure rickon khal drogo.
11 changes: 11 additions & 0 deletions examples/ebook/src/chapters/chapter002.md
@@ -0,0 +1,11 @@
---
chapter: 2
title: The Second Example Chapter
---

## Chapter 2: The Second Example Chapter

The more she shat robb nymeria jaehaerys euron night's king. Mya stone pate lyanna white walkers. Qhorin halfhand ashara yoren gendry the hound, robb mya stone varys aemon. Wights thoros areo hotah shaggydog, olenna night's king tormund balon edmure asha ghost summer bran. Gendry areo margaery craster aegon daeron.

Stannis brandon stark areo hotah, greywind varamyr meera reed dunk theon euron baelor night's king sandor. Catelyn myrcella gerold hightower, rhaegar tommen podrick grey worm mance rayder donal noye robb dunk old nan areo hotah craster. Arthur dayne meera reed dagmer cleftjaw areo hotah white walkers the hound. Olenna Wun Weg Wun Dar Wun aemon edmure rickon khal drogo.

3 changes: 3 additions & 0 deletions examples/ebook/src/index.md
@@ -0,0 +1,3 @@
---
layout: layout.html
---

0 comments on commit 1676ab4

Please sign in to comment.