@antora/pdf-extension 1.0.0-alpha.9 → 1.0.0-beta.1

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
@@ -1,12 +1,12 @@
1
1
  # Antora PDF Extension
2
2
 
3
- The Antora PDF Extension is the official extension for producing PDFs of an Antora site.
4
- Specifically, it generates and publishes PDFs of aggregate pages in the site.
3
+ The Antora PDF extension is an Antora extension and exporter for Assembler.
4
+ It configures and invokes Assembler and it provides the converter function and metadata that Assembler uses to export content to the PDF format.
5
+ This extension is the official extension for exporting content in an Antora site to PDF.
5
6
 
6
- This extension is based on Antora Assembler.
7
- It calls Assembler to construct aggregate AsciiDoc documents from pages based on the navigation tree per component version.
8
- It then iterates over those aggregate documents and converts them to PDF using Asciidoctor PDF or the specified command.
9
- Finally, it pushes those PDFs into the site catalog so they are published as attachments alongside the other files in the site.
7
+ This extension first delegates to Assembler, passing it the converter metadata, to construct assembly files from pages per component version using the navigation as a model.
8
+ Assembler then iterates over those assembly files and, using the converter provided by this extension, converts them to PDF using Asciidoctor PDF (or the specified command).
9
+ Finally, Assembler adds those PDFs to the content catalog as exports, which Antora then publishes alongside the other files in the site.
10
10
 
11
11
  ## Usage
12
12
 
@@ -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 convertDocumentToPdf = require('./convert-document-to-pdf')
3
+ const converter = require('./converter')
4
+ const runCommand = require('@antora/run-command-helper')
4
5
 
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 })
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,7 +1,7 @@
1
1
  {
2
2
  "name": "@antora/pdf-extension",
3
- "version": "1.0.0-alpha.9",
4
- "description": "An Antora extension that assembles content pages into PDF files by version and publishes them with the site.",
3
+ "version": "1.0.0-beta.1",
4
+ "description": "An Antora extension that uses Assembler to merge pages into assembly files, export them to the PDF format, and publishe them with the site.",
5
5
  "license": "MPL-2.0",
6
6
  "author": "OpenDevise Inc. (https://opendevise.com)",
7
7
  "contributors": [
@@ -9,24 +9,31 @@
9
9
  "Sarah White <sarah@opendevise.com>"
10
10
  ],
11
11
  "homepage": "https://antora.org",
12
- "repository": "gitlab:antora/antora-assembler",
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
  },
16
20
  "scripts": {
17
- "test": "_mocha test",
21
+ "test": "node --test test/*-test.js",
18
22
  "prepublishOnly": "npx -y downdoc --prepublish",
19
23
  "postpublish": "npx -y downdoc --postpublish"
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
- "#convert-document-to-pdf": "./lib/convert-document-to-pdf.js"
32
+ "#package": "./package.json"
27
33
  },
28
34
  "dependencies": {
29
- "@antora/assembler": "1.0.0-alpha.9"
35
+ "@antora/assembler": "1.0.0-beta.1",
36
+ "@antora/run-command-helper": "~1.0"
30
37
  },
31
38
  "engines": {
32
39
  "node": ">=16.0.0"
@@ -42,8 +49,5 @@
42
49
  "pdf",
43
50
  "generator",
44
51
  "documentation"
45
- ],
46
- "publishConfig": {
47
- "access": "public"
48
- }
52
+ ]
49
53
  }
@@ -1,47 +0,0 @@
1
- 'use strict'
2
-
3
- const fsp = require('node:fs/promises')
4
- const ospath = require('node:path')
5
-
6
- // Q: how can we simplify this to avoid redundant code?
7
- // Q: rename to convertDocumentToPDF?
8
- async function convertDocumentToPdf (doc, buildConfig, runCommand) {
9
- const {
10
- asciidoc: { attributes: baseAttributes } = { attributes: {} },
11
- contents: input,
12
- src: { component, version, basename, extname = doc.extname },
13
- } = doc
14
- const { cwd = process.cwd(), dir = cwd } = buildConfig
15
- const command = buildConfig.command ?? (await scopeDefaultCommand(cwd))
16
- const docfile = `${version}@${component}::pdf$${basename}`
17
- const docname = basename.slice(0, basename.length - extname.length)
18
- const convertAttributes = Object.assign({}, baseAttributes, {
19
- docfile,
20
- docfilesuffix: extname,
21
- 'docname@': docname,
22
- imagesdir: dir,
23
- })
24
- Object.assign(doc, { contents: null, extname: '.pdf', mediaType: 'application/pdf' })
25
- const argv = Object.entries(convertAttributes).reduce(
26
- (accum, [name, val]) =>
27
- accum.push('-a', val ? `${name}=${val}` : val === '' ? name : `!${name}${val === false ? '=@' : ''}`) && accum,
28
- []
29
- )
30
- const output = ospath.join(dir, doc.path)
31
- // Q: should mkdirs be the default behavior?
32
- if (buildConfig.mkdirs) await fsp.mkdir(ospath.dirname(output), { recursive: true, force: true })
33
- argv.push('-o', output)
34
- // Q: should runCommand accept outputFlag and automatically append to argv?
35
- return runCommand(command, argv, { cwd, input, output }).then((contents) =>
36
- Object.assign(doc, { contents, out: { path: doc.path } })
37
- )
38
- }
39
-
40
- function scopeDefaultCommand (cwd, baseCommand = 'asciidoctor-pdf') {
41
- return fsp.stat(ospath.join(cwd, 'Gemfile.lock')).then(
42
- () => `bundle exec ${baseCommand}`,
43
- () => baseCommand
44
- )
45
- }
46
-
47
- module.exports = convertDocumentToPdf