UNPKG

packdir-cli

Version:
157 lines 5.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Packdocument = exports.Metadata = void 0; const tslib_1 = require("tslib"); //import * as fs from "fs" const archiver_1 = tslib_1.__importDefault(require("archiver")); const promises_1 = require("fs/promises"); const fs_1 = require("fs"); const pd_marked_1 = require("../pd-marked"); /** * metadata */ class Metadata { constructor(data) { if (data) { this.title = data.title; } else { this.title = '<null>'; } } } exports.Metadata = Metadata; /** * Equals the package element in Epub 3.2. */ class Packdocument { constructor(data, content) { if (typeof data == 'string') { this.metadata = this.constructWithMarkdownFile(data); } else if (data instanceof Metadata) { this.metadata = new Metadata(data); } else { // undefined this.metadata = this.constructWithMarkdownFile(); } this.version = "3.0"; } static async generateFromPath() { //let html = '' let htmls = []; let docTitle = ''; let coverUrl = ''; try { // Get config const rawconfig = await (0, promises_1.readFile)(`./packdir.json`, { encoding: 'utf-8' }); const config = JSON.parse(rawconfig); docTitle = config.documentName; let count = 1; for (const file of config.content) { const content = await (0, promises_1.readFile)(`./${file}`, { encoding: 'utf-8' }); const [articleTitle, mdstring] = this.getMarkdownArticleTitle(content, file, count++); htmls.push({ title: articleTitle, content: (0, pd_marked_1.pdMarked)(mdstring) }); } coverUrl = (config.cover && config.cover !== '') ? config.cover : ''; // path to the book //const pathname = basename(process.cwd()) //const pathbook = `.packdir/epubs/${pathname}` //stat(pathbook).catch(async (err) => { // if (err.code === 'ENOENT') { // Directory is not existing // await mkdir(pathbook, { recursive: true }) // } //}) //const files = await readdir('./') //for (const file of files) { // if ('.md' === file.substring(file.length - 3).toLowerCase()) { // const content = await readFile(`./${file}`, { encoding: 'utf-8' }) // html = marked(content) // } //} } catch (err) { console.log('Error to read dir. ', err); } const date = new Date(); date.setFullYear(2000); const options = { title: docTitle, //author: "Lewis Carroll", //publisher: "Macmillan & Co.", date: date.toString(), version: 3, verbose: true, }; if (coverUrl) { options.cover = coverUrl; } return [options, htmls]; } /** * Get the title of markdown document. * @param markdown File content * @param filename Filename as default title if no title in markdown document. * @returns Chapter title and content. */ static getMarkdownArticleTitle(markdown, filename, count) { let title = `${count}. ${filename}`; const lines = markdown.split('\n'); let removedTitle = false; for (let i = 0; i < 10 && i < lines.length; i++) { if ('# ' === lines[i].substring(0, 2) && lines[i].length > 2) { title = `${count}. ${lines[i].substring(2)}`; lines.splice(i, 1); removedTitle = true; break; } if ('===' === lines[i].substring(0, 3) && i > 0 && lines[i - 1].length > 1) { title = `${count}. ${lines[i - 1]}`; lines.splice(i - 1, 2); removedTitle = true; break; } } if (removedTitle) { markdown = lines.join('\n'); } return [title, markdown]; } constructWithMarkdownFile(pathname) { console.log('pathname: ', pathname); let metadata = new Metadata(); if (pathname) { //metadata = new Metadata() } else { // Load current directory } return metadata; } saveAsEpub() { let archive = (0, archiver_1.default)("zip", { zlib: { level: 9 } }); let output = (0, fs_1.createWriteStream)('/mnt/d/Henry/packdir/oo.zip'); archive.pipe(output); // if (self.options.verbose) { // console.log("Zipping temp dir to", self.options.output); // } // mimetype archive.append("application/epub+zip", { store: true, name: "mimetype" }); archive.file('/mnt/d/Henry/packdir/md-contents/epub/content.opf', { name: 'content.opf' }); archive.file('/mnt/d/Henry/packdir/md-contents/epub/page_style.css', { name: 'page_style.css' }); archive.file('/mnt/d/Henry/packdir/md-contents/epub/stylesheet.css', { name: 'stylesheet.css' }); archive.file('/mnt/d/Henry/packdir/md-contents/epub/toc.ncx', { name: 'toc.ncx' }); archive.directory('/mnt/d/Henry/packdir/md-contents/epub/META-INF', 'META-INF'); archive.directory('/mnt/d/Henry/packdir/md-contents/epub/OEBPS', 'OEBPS'); console.log('生成zip new'); archive.finalize(); } } exports.Packdocument = Packdocument; //# sourceMappingURL=index.js.map