diff --git a/lib/cli.js b/lib/cli.js index e3369903..e749d5b0 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -1,6 +1,5 @@ 'use strict' -const fs = require('fs') const pkg = require('../package.json') const { Options, Invoker, processor } = require('@asciidoctor/cli') const chokidar = require('chokidar') @@ -10,29 +9,19 @@ class ProcessorEmitter extends EventEmitter {} const processorEmitter = new ProcessorEmitter() const converter = require('./converter.js') +// we look forward to the CLI having Invoker.readFromStdin() +// https://github.com/Mogztter/asciidoctor-pdf.js/pull/158#issuecomment-577788688 const stdin = require('./stdin') async function convertFiles (files, argv, options, verbose, preview) { for (const file of files) { if (verbose) { - console.log(`converting file ${file}`) + console.log(`converting file ${file.contents ? '-' : file.path}`) } await converter.convert(processor, file, options, argv.timings, argv.watch, preview) } } -function getTemporaryAdocFile (workingDir) { - let tempFile - // random file name for stdin data - const name = 'asciidoctor-pdf-' + Math.random().toString(36).substring(2, 15) - if (path.isAbsolute(workingDir)) { - tempFile = path.join(workingDir, `${name}.adoc`) - } else { - tempFile = path.normalize(path.join(process.cwd(), workingDir, `${name}.adoc`)) - } - return tempFile -} - class PdfOptions { constructor () { this.options = new Options() @@ -102,23 +91,16 @@ class PdfInvoker extends Invoker { Invoker.prepareProcessor(args, processor) const options = this.options.options if (this.options.stdin) { - const dir = this.options.base_dir || this.options.doc_dir || process.cwd() - const adocFile = getTemporaryAdocFile(dir) - const adocFilePath = path.parse(adocFile) - const htmlFilePath = path.join(adocFilePath.dir, adocFilePath.name + '.html') + const fictiveInputFile = `${this.options.base_dir || this.options.doc_dir || process.cwd()}/asciidoctor-pdf-stdin.adoc` stdin.read((data) => { - fs.writeFile(adocFile, data, { flag: 'wx' }, function (err) { - if (err) throw err - convertFiles([adocFile], args, options, verbose, preview).then(() => { - // this should be handled async but skills inadequate :-( - fs.unlinkSync(adocFile) - fs.unlinkSync(htmlFilePath) - }) + const fileObj = {path: fictiveInputFile, + contents: data} + convertFiles([fileObj], args, options, verbose, preview) }) - }) return { exit: false } } else if (files && files.length > 0) { - await convertFiles(files, args, options, verbose, preview) + const fileObjs = files.map(file => new Object({ path: file })) + await convertFiles(fileObjs, args, options, verbose, preview) if (watch) { const watchFiles = files.map((file) => { const dirname = path.dirname(file) diff --git a/lib/converter.js b/lib/converter.js index e74b832c..55504819 100644 --- a/lib/converter.js +++ b/lib/converter.js @@ -30,15 +30,15 @@ function registerTemplateConverter (processor, templates) { } async function convert (processor, inputFile, options, timings, watch, preview) { - const tempFile = getTemporaryHtmlFile(inputFile, options) + const tempFile = getTemporaryHtmlFile(inputFile.path, options) let workingDir if (options.to_dir) { await mkdirs(options.to_dir) workingDir = options.to_dir } else { - workingDir = path.dirname(inputFile) + workingDir = path.dirname(inputFile.path) } - const inputFilenameWithoutExt = path.basename(inputFile, path.extname(inputFile)) + const inputFilenameWithoutExt = path.basename(inputFile.path, path.extname(inputFile.path)) let outputFile = path.join(workingDir, inputFilenameWithoutExt + '.pdf') let outputToStdout = false if (options.to_file) { @@ -55,14 +55,23 @@ async function convert (processor, inputFile, options, timings, watch, preview) } const instanceOptions = Object.assign({}, options, { to_file: tempFile }) let doc + if (timings) { const timings = processor.Timings.$new() instanceOptions.timings = timings - doc = processor.convertFile(inputFile, instanceOptions) - timings.$print_report(Opal.gvars.stderr, inputFile) + } + + if ( inputFile.contents ) { + // data from stdin + doc = processor.convert(inputFile.contents, instanceOptions) } else { - doc = processor.convertFile(inputFile, instanceOptions) + doc = processor.convertFile(inputFile.path, instanceOptions) } + + if (timings) { + timings.$print_report(Opal.gvars.stderr, inputFile.contents ? '-' : inputFile.path) + } + const puppeteerConfig = { headless: !preview, args: ['--no-sandbox', '--allow-file-access-from-files'] diff --git a/test/pdf_test.js b/test/pdf_test.js index ddb8e756..3e5a7436 100644 --- a/test/pdf_test.js +++ b/test/pdf_test.js @@ -61,7 +61,7 @@ describe('PDF converter', function () { const convert = async (inputFile, outputFile, options) => { const opts = options || {} opts.to_file = outputFile - await converter.convert(asciidoctor, inputFile, opts, false) + await converter.convert(asciidoctor, { path: inputFile }, opts, false) return PDFDocument.load(fs.readFileSync(outputFile)) } @@ -113,7 +113,7 @@ describe('PDF converter', function () { const outputFile = `${__dirname}/output/title-page-background-color.pdf` opts.to_file = outputFile opts.attributes = { stylesheet: `${__dirname}/../css/asciidoctor.css;${__dirname}/../css/document.css;${__dirname}/../css/features/book.css;${__dirname}/fixtures/black-title-page.css` } - await converter.convert(asciidoctor, `${__dirname}/fixtures/title-page.adoc`, opts, false) + await converter.convert(asciidoctor, { path: `${__dirname}/fixtures/title-page.adoc` }, opts, false) expect(outputFile).to.be.visuallyIdentical('title-page-background-color.pdf') }) })