@antora/pdf-extension 1.0.0-alpha.1 → 1.0.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -21,7 +21,7 @@ antora:
21
21
  # ...
22
22
  ```
23
23
 
24
- You can configure the behavior of the extension using the optional *antora-assembler.yml* file.
24
+ You can configure the behavior of the extension using the optional _antora-assembler.yml_ file.
25
25
 
26
26
  ## Copyright and License
27
27
 
@@ -1,42 +1,54 @@
1
1
  'use strict'
2
2
 
3
- const { runCommand } = (() => {
4
- try {
5
- return require('@antora/assembler')
6
- } catch {
7
- return require('../../assembler')
8
- }
9
- })()
10
- const ospath = require('path')
3
+ const fs = require('node:fs')
4
+ const { promises: fsp } = fs
5
+ const LazyReadable = require('./util/lazy-readable')
6
+ const ospath = require('node:path')
11
7
 
12
8
  // Q: how can we simplify this to avoid redundant code?
13
9
  // Q: rename to convertDocumentToPDF?
14
- function convertDocumentToPdf (doc, buildConfig) {
10
+ async function convertDocumentToPdf (doc, buildConfig, runCommand) {
15
11
  const {
16
12
  asciidoc: { attributes: baseAttributes } = { attributes: {} },
17
- contents: input,
18
- src: { component, version, basename, extname: docfilesuffix },
13
+ contents: stdin,
14
+ src: { component, version, basename, extname = doc.extname },
19
15
  } = doc
20
- const { command = 'asciidoctor-pdf', cwd, dir } = buildConfig
16
+ const { cwd = process.cwd(), dir = cwd } = buildConfig
17
+ const command = buildConfig.command ?? (await scopeDefaultCommand(cwd))
18
+ const padCharRef = process.platform === 'win32' && command.startsWith('bundle exec ')
21
19
  const docfile = `${version}@${component}::pdf$${basename}`
22
- const docname = basename.substr(0, basename.length - docfilesuffix.length)
20
+ const docname = basename.slice(0, basename.length - extname.length)
23
21
  const convertAttributes = Object.assign({}, baseAttributes, {
24
22
  docfile,
25
- docfilesuffix,
23
+ docfilesuffix: extname,
26
24
  'docname@': docname,
27
25
  imagesdir: dir,
28
26
  })
29
27
  Object.assign(doc, { contents: null, extname: '.pdf', mediaType: 'application/pdf' })
30
- const argv = Object.entries(convertAttributes).reduce(
31
- (accum, [name, val]) =>
32
- accum.push('-a', val ? `${name}=${val}` : val === '' ? name : `!${name}${val === false ? '=@' : ''}`) && accum,
33
- []
28
+ const extraArgs = Object.entries(convertAttributes).reduce((accum, [name, val]) => {
29
+ if (val) {
30
+ val = `${name}=${padCharRef && val.charAt() === '&' ? ' ' : ''}${val}`
31
+ } else if (val === '') {
32
+ val = name
33
+ } else {
34
+ val = `!${name}${val === false ? '=@' : ''}`
35
+ }
36
+ accum.push('-a', val)
37
+ return accum
38
+ }, [])
39
+ const outputPath = ospath.join(dir, doc.path)
40
+ // Q: should mkdirs be the default behavior?
41
+ if (buildConfig.mkdirs) await fsp.mkdir(ospath.dirname(outputPath), { recursive: true, force: true })
42
+ extraArgs.push('-o', outputPath, '-')
43
+ return runCommand(command, extraArgs, { parse: true, cwd, stdin, stdout: 'print', stderr: 'print' }).then(() =>
44
+ Object.assign(doc, { contents: new LazyReadable(() => fs.createReadStream(outputPath)), out: { path: doc.path } })
34
45
  )
35
- const output = ospath.join(dir, doc.path)
36
- argv.push('-o', output)
37
- // Q: should runCommand accept outputFlag and automatically append to argv?
38
- return runCommand(command, argv, { cwd, input, output }).then((contents) =>
39
- Object.assign(doc, { contents, out: { path: doc.path } })
46
+ }
47
+
48
+ function scopeDefaultCommand (cwd, baseCommand = 'asciidoctor-pdf') {
49
+ return fsp.access(ospath.join(cwd, 'Gemfile.lock')).then(
50
+ () => `bundle exec ${baseCommand}`,
51
+ () => baseCommand
40
52
  )
41
53
  }
42
54
 
package/lib/index.js CHANGED
@@ -1,26 +1,19 @@
1
1
  'use strict'
2
2
 
3
3
  const convertDocumentToPdf = require('./convert-document-to-pdf')
4
- const { assembleContent } = (() => {
5
- try {
6
- return require('@antora/assembler')
7
- } catch {
8
- return require('../../assembler')
9
- }
10
- })()
11
4
 
12
- module.exports.register = function () {
13
- //this.on('beforeProcess', ({ asciidocConfig }) => {
14
- // asciidocConfig.keepSource = true
15
- //})
16
- this.on('contentClassified', ({ contentCatalog }) => {
17
- contentCatalog.getPages((page) => {
18
- if (!page.out) return
19
- page.src.contents = page.contents
20
- page.src = new Proxy(page.src, { deleteProperty: (o, p) => (p === 'contents' ? true : delete o[p]) })
21
- })
5
+ module.exports.register = function ({ config: { configFile: configSource } }) {
6
+ this.once('beforeProcess', ({ siteAsciiDocConfig }) => {
7
+ siteAsciiDocConfig.keepSource = true
8
+ })
9
+ this.once('navigationBuilt', ({ playbook, contentCatalog, siteCatalog }) => {
10
+ const { assembleContent } = (() => {
11
+ try {
12
+ return this.require('@antora/assembler') // when installed with Antora
13
+ } catch {
14
+ return require('@antora/assembler') // when installed separate from Antora, perhaps in project
15
+ }
16
+ })()
17
+ return assembleContent.call(this, playbook, contentCatalog, convertDocumentToPdf, { configSource, siteCatalog })
22
18
  })
23
- this.on('beforePublish', ({ playbook, contentCatalog, siteCatalog }) =>
24
- assembleContent.call(this, playbook, contentCatalog, convertDocumentToPdf, { siteCatalog })
25
- )
26
19
  }
@@ -0,0 +1,18 @@
1
+ 'use strict'
2
+
3
+ const { PassThrough } = require('node:stream')
4
+
5
+ // adapted from https://github.com/jpommerening/node-lazystream/blob/master/lib/lazystream.js | license: MIT
6
+ class LazyReadable extends PassThrough {
7
+ constructor (fn, options) {
8
+ super(options)
9
+ this._read = function () {
10
+ delete this._read // restores original method
11
+ fn.call(this, options).on('error', this.emit.bind(this, 'error')).pipe(this)
12
+ return this._read.apply(this, arguments)
13
+ }
14
+ this.emit('readable')
15
+ }
16
+ }
17
+
18
+ module.exports = LazyReadable
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antora/pdf-extension",
3
- "version": "1.0.0-alpha.1",
3
+ "version": "1.0.0-alpha.10",
4
4
  "description": "An Antora extension that assembles content pages into PDF files by version and publishes them with the site.",
5
5
  "license": "MPL-2.0",
6
6
  "author": "OpenDevise Inc. (https://opendevise.com)",
@@ -14,15 +14,20 @@
14
14
  "url": "https://gitlab.com/antora/antora-assembler/issues"
15
15
  },
16
16
  "scripts": {
17
- "prepublishOnly": "node $npm_config_local_prefix/npm/prepublishOnly.js",
18
- "postpublish": "node $npm_config_local_prefix/npm/postpublish.js"
17
+ "test": "node --test test/*-test.js",
18
+ "prepublishOnly": "npx -y downdoc --prepublish",
19
+ "postpublish": "npx -y downdoc --postpublish"
19
20
  },
20
21
  "main": "lib/index.js",
21
22
  "exports": {
22
23
  ".": "./lib/index.js"
23
24
  },
25
+ "imports": {
26
+ "#convert-document-to-pdf": "./lib/convert-document-to-pdf.js",
27
+ "#run-command": "@antora/run-command-helper"
28
+ },
24
29
  "dependencies": {
25
- "@antora/assembler": "1.0.0-alpha.1"
30
+ "@antora/assembler": "1.0.0-alpha.10"
26
31
  },
27
32
  "engines": {
28
33
  "node": ">=16.0.0"