@antora/pdf-extension 1.0.0-alpha.10 → 1.0.0-alpha.12
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 +6 -5
- package/lib/converter.js +29 -0
- package/lib/index.js +11 -15
- package/package.json +13 -10
- package/lib/convert-document-to-pdf.js +0 -55
- package/lib/util/lazy-readable.js +0 -18
package/README.md
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
# Antora PDF Extension
|
|
2
2
|
|
|
3
|
-
The Antora PDF
|
|
4
|
-
|
|
3
|
+
The Antora PDF extension is an exporter extension for Assembler that assembles and converts content to the PDF format using Asciidoctor PDF.
|
|
4
|
+
The Antora PDF extension is the official extension for exporting content in an Antora site to PDF.
|
|
5
5
|
|
|
6
6
|
This extension is based on Antora Assembler.
|
|
7
|
-
It
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
It provides the converter function and metadata that Assembler uses to export content.
|
|
8
|
+
Assembler constructs assembly AsciiDoc documents from pages per component version using the navigation as a model.
|
|
9
|
+
Using the converter provided by this extension, it then iterates over those assembly documents and converts them to PDF using Asciidoctor PDF (or the specified command).
|
|
10
|
+
Finally, it adds those PDFs to the content catalog as exports, which Antora then publishes alongside the other files in the site.
|
|
10
11
|
|
|
11
12
|
## Usage
|
|
12
13
|
|
package/lib/converter.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const fsp = require('node:fs/promises')
|
|
4
|
+
const ospath = require('node:path')
|
|
5
|
+
|
|
6
|
+
const DEFAULT_COMMAND = 'asciidoctor-pdf'
|
|
7
|
+
|
|
8
|
+
async function convert (runCommand, file, convertAttributes, buildConfig) {
|
|
9
|
+
const { cwd = process.cwd(), command = await getDefaultCommand(cwd) } = buildConfig
|
|
10
|
+
const args = convertAttributes.toArgs('-a', command).concat('-o', convertAttributes.outfile, '-')
|
|
11
|
+
await runCommand(command, args, { parse: true, cwd, stdin: file.contents, stdout: 'print', stderr: 'print' })
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function getDefaultCommand (cwd) {
|
|
15
|
+
return fsp.access(ospath.join(cwd, 'Gemfile.lock')).then(
|
|
16
|
+
() => `bundle exec ${DEFAULT_COMMAND}`,
|
|
17
|
+
() => DEFAULT_COMMAND
|
|
18
|
+
)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
module.exports = {
|
|
22
|
+
bind (thisArg, runCommand) {
|
|
23
|
+
return Object.assign({}, this, { convert: convert.bind(thisArg, runCommand) })
|
|
24
|
+
},
|
|
25
|
+
convert,
|
|
26
|
+
backend: 'pdf',
|
|
27
|
+
extname: '.pdf',
|
|
28
|
+
mediaType: 'application/pdf',
|
|
29
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -1,19 +1,15 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const converter = require('./converter')
|
|
4
|
+
const runCommand = require('@antora/run-command-helper')
|
|
4
5
|
|
|
5
|
-
module.exports.register = function ({ config
|
|
6
|
-
this.
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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 })
|
|
18
|
-
})
|
|
6
|
+
module.exports.register = function ({ config }, providers) {
|
|
7
|
+
requireAssembler(this).configure(this, converter.bind(this, runCommand), config, providers)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function requireAssembler (context, packageName = '@antora/assembler') {
|
|
11
|
+
try {
|
|
12
|
+
return context.require(packageName) // when installed with Antora
|
|
13
|
+
} catch {}
|
|
14
|
+
return require(packageName) // when installed separate from Antora, perhaps in project
|
|
19
15
|
}
|
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.12",
|
|
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)",
|
|
@@ -9,7 +9,11 @@
|
|
|
9
9
|
"Sarah White <sarah@opendevise.com>"
|
|
10
10
|
],
|
|
11
11
|
"homepage": "https://antora.org",
|
|
12
|
-
"repository":
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://gitlab.com/antora/antora-assembler.git",
|
|
15
|
+
"directory": "packages/pdf-extension"
|
|
16
|
+
},
|
|
13
17
|
"bugs": {
|
|
14
18
|
"url": "https://gitlab.com/antora/antora-assembler/issues"
|
|
15
19
|
},
|
|
@@ -20,14 +24,16 @@
|
|
|
20
24
|
},
|
|
21
25
|
"main": "lib/index.js",
|
|
22
26
|
"exports": {
|
|
23
|
-
".": "./lib/index.js"
|
|
27
|
+
".": "./lib/index.js",
|
|
28
|
+
"./converter": "./lib/converter.js",
|
|
29
|
+
"./package.json": "./package.json"
|
|
24
30
|
},
|
|
25
31
|
"imports": {
|
|
26
|
-
"#
|
|
27
|
-
"#run-command": "@antora/run-command-helper"
|
|
32
|
+
"#package": "./package.json"
|
|
28
33
|
},
|
|
29
34
|
"dependencies": {
|
|
30
|
-
"@antora/assembler": "1.0.0-alpha.
|
|
35
|
+
"@antora/assembler": "1.0.0-alpha.12",
|
|
36
|
+
"@antora/run-command-helper": "~1.0"
|
|
31
37
|
},
|
|
32
38
|
"engines": {
|
|
33
39
|
"node": ">=16.0.0"
|
|
@@ -43,8 +49,5 @@
|
|
|
43
49
|
"pdf",
|
|
44
50
|
"generator",
|
|
45
51
|
"documentation"
|
|
46
|
-
]
|
|
47
|
-
"publishConfig": {
|
|
48
|
-
"access": "public"
|
|
49
|
-
}
|
|
52
|
+
]
|
|
50
53
|
}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const fs = require('node:fs')
|
|
4
|
-
const { promises: fsp } = fs
|
|
5
|
-
const LazyReadable = require('./util/lazy-readable')
|
|
6
|
-
const ospath = require('node:path')
|
|
7
|
-
|
|
8
|
-
// Q: how can we simplify this to avoid redundant code?
|
|
9
|
-
// Q: rename to convertDocumentToPDF?
|
|
10
|
-
async function convertDocumentToPdf (doc, buildConfig, runCommand) {
|
|
11
|
-
const {
|
|
12
|
-
asciidoc: { attributes: baseAttributes } = { attributes: {} },
|
|
13
|
-
contents: stdin,
|
|
14
|
-
src: { component, version, basename, extname = doc.extname },
|
|
15
|
-
} = doc
|
|
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 ')
|
|
19
|
-
const docfile = `${version}@${component}::pdf$${basename}`
|
|
20
|
-
const docname = basename.slice(0, basename.length - extname.length)
|
|
21
|
-
const convertAttributes = Object.assign({}, baseAttributes, {
|
|
22
|
-
docfile,
|
|
23
|
-
docfilesuffix: extname,
|
|
24
|
-
'docname@': docname,
|
|
25
|
-
imagesdir: dir,
|
|
26
|
-
})
|
|
27
|
-
Object.assign(doc, { contents: null, extname: '.pdf', mediaType: 'application/pdf' })
|
|
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 } })
|
|
45
|
-
)
|
|
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
|
|
52
|
-
)
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
module.exports = convertDocumentToPdf
|
|
@@ -1,18 +0,0 @@
|
|
|
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
|