summaryrefslogtreecommitdiffstats
path: root/lib/output/ebook/getConvertOptions.js
blob: bc80493a874898222a0da2bef00778e109e8b338 (plain)
1
2
3
4
5
6
7
8
9
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var extend = require('extend');

var Promise = require('../../utils/promise');
var getPDFTemplate = require('./getPDFTemplate');
var getCoverPath = require('./getCoverPath');

/**
    Generate options for ebook-convert

    @param {Output}
    @return {Promise<Object>}
*/
function getConvertOptions(output) {
    var options = output.getOptions();
    var format = options.get('format');

    var book = output.getBook();
    var config = book.getConfig();

    return Promise()
    .then(function() {
        var coverPath = getCoverPath(output);
        var options = {
            '--cover':                      coverPath,
            '--title':                      config.getValue('title'),
            '--comments':                   config.getValue('description'),
            '--isbn':                       config.getValue('isbn'),
            '--authors':                    config.getValue('author'),
            '--language':                   book.getLanguage() || config.getValue('language'),
            '--book-producer':              'GitBook',
            '--publisher':                  'GitBook',
            '--chapter':                    'descendant-or-self::*[contains(concat(\' \', normalize-space(@class), \' \'), \' book-chapter \')]',
            '--level1-toc':                 'descendant-or-self::*[contains(concat(\' \', normalize-space(@class), \' \'), \' book-chapter-1 \')]',
            '--level2-toc':                 'descendant-or-self::*[contains(concat(\' \', normalize-space(@class), \' \'), \' book-chapter-2 \')]',
            '--level3-toc':                 'descendant-or-self::*[contains(concat(\' \', normalize-space(@class), \' \'), \' book-chapter-3 \')]',
            '--max-levels':                 '1',
            '--no-chapters-in-toc':         true,
            '--breadth-first':              true,
            '--dont-split-on-page-breaks':  format === 'epub'? true : undefined
        };

        if (format !== 'pdf') {
            return options;
        }

        return Promise.all([
            getPDFTemplate(output, 'header'),
            getPDFTemplate(output, 'footer')
        ])
        .spread(function(headerTpl, footerTpl) {
            var pdfOptions = config.getValue('pdf').toJS();

            return options = extend(options, {
                '--chapter-mark':           String(pdfOptions.chapterMark),
                '--page-breaks-before':     String(pdfOptions.pageBreaksBefore),
                '--margin-left':            String(pdfOptions.margin.left),
                '--margin-right':           String(pdfOptions.margin.right),
                '--margin-top':             String(pdfOptions.margin.top),
                '--margin-bottom':          String(pdfOptions.margin.bottom),
                '--pdf-default-font-size':  String(pdfOptions.fontSize),
                '--pdf-mono-font-size':     String(pdfOptions.fontSize),
                '--paper-size':             String(pdfOptions.paperSize),
                '--pdf-page-numbers':       Boolean(pdfOptions.pageNumbers),
                '--pdf-sans-family':        String(pdfOptions.fontFamily),
                '--pdf-header-template':    headerTpl,
                '--pdf-footer-template':    footerTpl
            });
        });
    });
}


module.exports = getConvertOptions;