@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 +1 -1
- package/lib/convert-document-to-pdf.js +35 -23
- package/lib/index.js +13 -20
- package/lib/util/lazy-readable.js +18 -0
- package/package.json +9 -4
package/README.md
CHANGED
|
@@ -1,42 +1,54 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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:
|
|
18
|
-
src: { component, version, basename, extname
|
|
13
|
+
contents: stdin,
|
|
14
|
+
src: { component, version, basename, extname = doc.extname },
|
|
19
15
|
} = doc
|
|
20
|
-
const {
|
|
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.
|
|
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
|
|
31
|
-
(
|
|
32
|
-
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return
|
|
39
|
-
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
this.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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.
|
|
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
|
-
"
|
|
18
|
-
"
|
|
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.
|
|
30
|
+
"@antora/assembler": "1.0.0-alpha.10"
|
|
26
31
|
},
|
|
27
32
|
"engines": {
|
|
28
33
|
"node": ">=16.0.0"
|