Skip to content

Commit

Permalink
Allow re-use of existing interface through a file object for stdin an…
Browse files Browse the repository at this point in the history
…d fix tests
  • Loading branch information
danyill committed Jan 26, 2020
1 parent cafcc16 commit c9dc3d3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 35 deletions.
36 changes: 9 additions & 27 deletions 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')
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 15 additions & 6 deletions lib/converter.js
Expand Up @@ -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) {
Expand All @@ -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']
Expand Down
4 changes: 2 additions & 2 deletions test/pdf_test.js
Expand Up @@ -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))
}

Expand Down Expand Up @@ -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')
})
})

0 comments on commit c9dc3d3

Please sign in to comment.