All files / src ebook-convert.ts

3.57% Statements 4/112
0% Branches 0/85
0% Functions 0/10
3.57% Lines 4/112

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 2241x 1x 1x                                                                                                                                                                                                                                                                                                                                     1x                                                                                                                    
import * as mkdirp from "mkdirp";
import * as path from "path";
import { execFile } from "child_process";
// ebook-convert is requied (calibre), which can be got from https://calibre-ebook.com/download
// xpath http://www.w3schools.com/xsl/xpath_syntax.asp
 
function processMetadata(config = {}, args) {
  const title = config["title"] || "No Title";
  args.push("--title", title);
 
  if (config["authors"]) {
    args.push("--authors", config["authors"]);
  }
 
  if (config["cover"]) {
    args.push("--cover", config["cover"]);
  }
 
  if (config["comment"]) {
    args.push("--comments", config["comments"]);
  }
 
  if (config["publisher"]) {
    args.push("--publisher", config["publisher"]);
  }
 
  if (config["book-producer"]) {
    args.push("--book-producer", config["book-producer"]);
  }
 
  if (config["pubdate"]) {
    args.push("--pubdate", config["pubdate"]);
  }
 
  if (config["language"]) {
    args.push("--language", config["language"]);
  }
 
  if (config["isbn"]) {
    args.push("--isbn", config["isbn"]);
  }
 
  if (config["tags"]) {
    args.push("--tags", config["tags"]);
  }
 
  if (config["series"]) {
    args.push("--series", config["series"]);
  }
 
  if (config["rating"]) {
    args.push("--rating", config["rating"]);
  }
}
 
function processAppearance(config = {}, args) {
  if (config["asciiize"]) {
    args.push("--asciiize");
  }
 
  if (config["base-font-size"]) {
    args.push("--base-font-size=" + config["base-font-size"]);
  }
 
  if (config["disable-font-rescaling"]) {
    args.push("--disable-font-rescaling");
  }
 
  if (config["line-height"]) {
    args.push("--line-height=" + config["line-height"]);
  }
 
  let marginTop = 72;
  let marginRight = 72;
  let marginBottom = 72;
  let marginLeft = 72;
  if (config["margin"]) {
    const margin: number[] | number = config["margin"];
    if (margin instanceof Array) {
      if (margin.length === 1) {
        marginTop = margin[0];
        marginBottom = margin[0];
        marginLeft = margin[0];
        marginRight = margin[0];
      } else if (margin.length === 2) {
        marginTop = margin[0];
        marginBottom = margin[0];
        marginLeft = margin[1];
        marginRight = margin[1];
      } else if (margin.length === 4) {
        marginTop = margin[0];
        marginRight = margin[1];
        marginBottom = margin[2];
        marginLeft = margin[3];
      }
    } else if (typeof margin === "number") {
      marginTop = margin;
      marginBottom = margin;
      marginLeft = margin;
      marginRight = margin;
    }
  } else {
    if (config["margin-top"]) {
      marginTop = config["margin-top"];
    }
    if (config["margin-right"]) {
      marginRight = config["margin-right"];
    }
    if (config["margin-bottom"]) {
      marginBottom = config["margin-bottom"];
    }
    if (config["margin-left"]) {
      marginLeft = config["margin-left"];
    }
  }
 
  args.push("--margin-top=" + marginTop);
  args.push("--margin-bottom=" + marginBottom);
  args.push("--margin-left=" + marginLeft);
  args.push("--margin-right=" + marginRight);
}
 
function processEPub(config = {}, args) {
  if (config["no-default-epub-cover"]) {
    args.push("--no-default-epub-cover");
  }
  if (config["no-svg-cover"]) {
    args.push("--no-svg-cover");
  }
  if (config["pretty-print"]) {
    args.push("--pretty-print");
  }
}
 
function processPDF(config = {}, args) {
  if (config["paper-size"]) {
    args.push("--paper-size", config["paper-size"]);
  }
 
  if (config["default-font-size"]) {
    args.push("--pdf-default-font-size=" + config["default-font-size"]);
  }
 
  if (config["header-template"]) {
    args.push("--pdf-header-template", config["header-template"]);
  }
 
  if (config["footer-template"]) {
    args.push("--pdf-footer-template", config["footer-template"]);
  }
 
  if (config["page-numbers"]) {
    args.push("--pdf-page-numbers");
  }
 
  if (config["pretty-print"]) {
    args.push("--pretty-print");
  }
}
 
/**
 * @param src: link to .html file
 * @param dest: output path
 */
 
export function ebookConvert(src, dest, config = {}) {
  return new Promise((resolve, reject) => {
    const args = [
      src,
      dest,
      "--level1-toc",
      "//*[@ebook-toc-level-1]/@heading",
      "--level2-toc",
      "//*[@ebook-toc-level-2]/@heading",
      "--level3-toc",
      "//*[@ebook-toc-level-3]/@heading",
      "--no-chapters-in-toc",
    ];
 
    processMetadata(config, args);
    processAppearance(config, args);
 
    // output formats
    const format = path.extname(dest).slice(1);
    if (format === "epub") {
      processEPub(config["epub"], args);
    } else if (format === "pdf") {
      processPDF(config["pdf"], args);
    }
 
    // arguments
    const ebookArgs = config["args"] || [];
    ebookArgs.forEach((arg) => {
      args.push(arg);
    });
 
    // ebook-convert will cause error if directory doesn't exist,
    // therefore I will create directory first.
    mkdirp(path.dirname(dest))
      .then(() => {
        execFile("ebook-convert", args, (error2) => {
          if (error2) {
            return reject(error2.toString());
          } else {
            return resolve();
          }
        });
      })
      .catch((error) => {
        if (error) {
          return reject(error);
        }
      });
  });
}
 
/*
# Example
ebookConvert('test.html', 'test.epub', {title: 'hehe', authors: 'shd101wyy'})
.then(()=> {...})
.catch((error)=> {...})
###
*/