1
- #!/usr/bin/env nodejs
1
+ #!/usr/bin/env node
2
2
3
3
const cheerio = require ( 'cheerio' ) ;
4
- const Streampub = require ( 'streampub ' ) ;
4
+ const nodepub = require ( 'nodepub ' ) ;
5
5
const jetpack = require ( 'fs-jetpack' ) ;
6
6
const { execSync } = require ( 'child_process' ) ;
7
+ const archiver = require ( 'archiver' ) ;
7
8
8
- var version = process . argv . length > 2 ? process . argv [ 2 ] : 'default' ;
9
+ let version = process . argv . length > 2 ? process . argv [ 2 ] : 'default' ;
9
10
10
11
const config = JSON . parse ( jetpack . read ( 'meta/' + version + '.json' ) ) ;
11
12
config . metadata = Object . assign ( {
@@ -20,27 +21,20 @@ config.metadata = Object.assign({
20
21
includeTOC : false
21
22
} , config . metadata ) ;
22
23
23
- var scrapeError = false ;
24
+ let scrapeError = false ;
24
25
25
- var epub = new Streampub ( config . metadata ) ;
26
- var epubPath = `output/${ version } .epub`
27
- epub . pipe ( jetpack . createWriteStream ( epubPath ) ) ;
28
-
29
- let cover = cheerio . load ( jetpack . read ( 'templates/cover.xhtml' ) ) ;
30
- cover ( 'h1#title' ) . text ( config . metadata . title ) ;
31
- cover ( 'figure#cover' ) . append ( `<img alt="Cover" src="${ config . img } " />` ) ;
32
- cover ( 'span#author' ) . text ( config . metadata . author ) ;
33
- epub . write ( Streampub . newChapter ( config . metadata . title , cover . html ( ) , 0 ) ) ;
34
-
35
- let toc = cheerio . load ( jetpack . read ( 'templates/nav.xhtml' ) ) ;
36
-
37
- var chapterNumber = 2 ;
26
+ let epub = nodepub . document ( config . metadata , config . img , writeTOC ) ;
38
27
39
28
function addChapterToBook ( html , url , cache_path ) {
40
29
let $ = cheerio . load ( html ) ;
41
30
let title = $ ( config . titleSelector ) . first ( ) . text ( ) ;
42
- let content = $ ( config . contentSelector ) . html ( ) ;
31
+ console . log ( 'Adding "' + title + '" to the book.' )
43
32
if ( config . withoutSelector ) $ ( config . withoutSelector ) . remove ( ) ;
33
+ $ ( 'br' ) . replaceWith ( '\n' ) ;
34
+ $ ( 'img' ) . insertAfter ( '</img>' ) ;
35
+ $ ( 'hr' ) . insertAfter ( '</hr>' ) ;
36
+ $ ( '[async]' ) . removeAttr ( 'async' )
37
+ let content = $ ( config . contentSelector ) ;
44
38
let path = url ;
45
39
if ( typeof url === 'object' ) {
46
40
path = url . url ;
@@ -53,12 +47,25 @@ function addChapterToBook(html, url, cache_path){
53
47
scrapeError = true ;
54
48
}
55
49
let safe_title = title . toLowerCase ( ) . replace ( / / g, '-' ) ;
56
- let newDoc = cheerio . load ( jetpack . read ( 'templates/chapter.xhtml' ) ) ;
57
- newDoc ( '#chapter' )
58
- . append ( '<h1>' + title + '</h1>' )
59
- . append ( content ) ;
60
- epub . write ( Streampub . newChapter ( title , newDoc ( 'body' ) . html ( ) , chapterNumber ) ) ;
61
- toc ( '#list' ) . append ( `<li><a href="chapter-${ chapterNumber ++ } .xhtml">${ title } </a></li>` ) ;
50
+ let newDoc = `
51
+ <h1 style="margin: 1rem auto;">${ title } </h1>
52
+ <div style="">
53
+ ${ content . html ( ) }
54
+ </div>
55
+ ` ;
56
+ epub . addSection ( title , newDoc ) ;
57
+ }
58
+
59
+ function writeTOC ( links ) {
60
+ let toc = cheerio . load ( jetpack . read ( 'templates/nav.xhtml' ) ) ;
61
+ let list = toc ( '#list' ) ;
62
+ links . forEach ( link => {
63
+ if ( link . itemType !== "contents" ) {
64
+ list . append ( `\n <li><a href="${ link . link } ">${ link . title } </a></li>` ) ;
65
+ }
66
+ } ) ;
67
+ list . append ( '\n' ) ;
68
+ return toc ( 'body' ) . html ( ) ;
62
69
}
63
70
64
71
config . urls . forEach ( url => {
@@ -76,15 +83,10 @@ config.urls.forEach(url => {
76
83
addChapterToBook ( jetpack . read ( cache_path ) , url , cache_path ) ;
77
84
} ) ;
78
85
79
- epub . write ( Streampub . newChapter ( "Table of Contents" , toc . html ( ) , 1 ) ) ;
80
-
81
- epub . write ( Streampub . newFile ( 'style/blitz.css' , jetpack . createReadStream ( 'style/blitz.css' ) ) ) ;
82
- epub . write ( Streampub . newFile ( 'style/blitz-kindle.css' , jetpack . createReadStream ( 'style/blitz-kindle.css' ) ) ) ;
83
- epub . write ( Streampub . newFile ( config . img , jetpack . createReadStream ( config . img ) ) ) ;
84
-
85
- if ( scrapeError ) {
86
- console . error ( 'Scrape errors occurred: No epub produced.' ) ;
87
- } else {
88
- epub . end ( ) ;
89
- console . log ( `Book successfully written to ${ epubPath } ` ) ;
90
- }
86
+ const archive = archiver ( 'zip' , { store : false } ) ;
87
+ const output = jetpack . createWriteStream ( `${ __dirname } /output/${ version } .epub` ) ;
88
+ archive . pipe ( output ) ;
89
+ epub . writeFilesForEPUB ( './temp' , err => { if ( err ) { console . log ( err ) } } ) ;
90
+ archive . directory ( './temp/' , false ) ;
91
+ output . on ( 'close' , ( ) => console . log ( archive . pointer ( ) + ' total bytes' ) ) ;
92
+ archive . finalize ( ) ;
0 commit comments