-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
7a5c7bd
commit 1279e47
Showing
16 changed files
with
283 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/.nvmrc | ||
/dist/ | ||
/node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
access=public | ||
audit=false | ||
fund=false | ||
omit=optional | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
= Asciidoctor Reducer | ||
|
||
An Asciidoctor.js extension that reduces an AsciiDoc document containing include directives to a single AsciiDoc document by expanding the includes reachable from the parent document. | ||
Additionally, the extension evaluates preprocessor conditionals (unless the option to preserve them is enabled), only keeping those lines from conditions which are true. | ||
If the document does not contain any preprocessor directives, the extension provides access to the unmodified source. | ||
|
||
(The CLI and API are not currently available in the JavaScript version). | ||
|
||
== Install | ||
|
||
This package depends on the `asciidoctor` package (>= 2.2.0, < 3.0.0), but doesn't declare it as a dependency. | ||
Therefore, you must install that package when installing this one. | ||
|
||
$ npm i asciidoctor @asciidoctor/reducer | ||
|
||
If you're using the extension with Antora, there's no need to install the `asciidoctor` package as Antora provides it. | ||
|
||
== Usage | ||
|
||
=== Extension | ||
|
||
You can use this extension in combination with the load API provided by Asciidoctor. | ||
If you want to register the extension globally, require the library as follows: | ||
|
||
[,js] | ||
---- | ||
const Asciidoctor = require('asciidoctor')() | ||
require('@asciidoctor/reducer').register() | ||
---- | ||
|
||
When you use the Asciidoctor load API, the document will automatically be reduced. | ||
You can access the reduced source by calling either the `getSource()` or `getSourceLines()` on the loaded document. | ||
|
||
[,js] | ||
---- | ||
const doc = Asciidoctor.loadFile('main.adoc', { safe: 'safe' }) | ||
console.log(doc.getSource()) | ||
---- | ||
|
||
You can pass a registry instance to the `register` method to register the extension with a scoped registry (scoped to the load API call). | ||
|
||
[,js] | ||
---- | ||
const Asciidoctor = require('asciidoctor')() | ||
const registry = Asciidoctor.Extensions.create() | ||
require('@asciidoctor/reducer').register(registry) | ||
const doc = Asciidoctor.loadFile('main.adoc', { extension_registry: registry, safe: 'safe' }) | ||
---- | ||
|
||
You can also require `@asciidoctor/reducer/extensions` to access the `Extensions` class. | ||
|
||
== Copyright and License | ||
|
||
Copyright (C) 2021-present Dan Allen and the individual contributors to this project. | ||
Use of this software is granted under the terms of the MIT License. | ||
|
||
== Trademarks | ||
|
||
AsciiDoc(R) and AsciiDoc Language(TM) are trademarks of the Eclipse Foundation, Inc. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict' | ||
|
||
require('../dist') | ||
|
||
module.exports = Opal.Asciidoctor.Reducer.Extensions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict' | ||
|
||
const Extensions = require('./extensions') | ||
|
||
module.exports.register = (registry) => Extensions.$register(registry) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict' | ||
|
||
const { env: ENV } = require('node:process') | ||
const fs = require('node:fs') | ||
const ospath = require('node:path') | ||
|
||
let opalCompilerPath = 'opal-compiler' | ||
try { | ||
require.resolve(opalCompilerPath) | ||
} catch { | ||
const npxInstallDir = ENV.PATH.split(':')[0] | ||
if (npxInstallDir?.endsWith('/node_modules/.bin') && npxInstallDir.startsWith(ENV.npm_config_cache + '/')) { | ||
opalCompilerPath = require.resolve('opal-compiler', { paths: [ospath.dirname(npxInstallDir)] }) | ||
} | ||
} | ||
|
||
const transpiled = require(opalCompilerPath).Builder | ||
.create() | ||
.build('../lib/asciidoctor/reducer/conditional_directive_tracker.rb') | ||
.build('../lib/asciidoctor/reducer/include_directive_tracker.rb') | ||
.build('../lib/asciidoctor/reducer/header_attribute_tracker.rb') | ||
.build('../lib/asciidoctor/reducer/preprocessor.rb') | ||
.build('../lib/asciidoctor/reducer/tree_processor.rb') | ||
.build('../lib/asciidoctor/reducer/extensions.rb') | ||
.toString() | ||
fs.mkdirSync('dist', { recursive: true }) | ||
fs.writeFileSync('dist/index.js', transpiled) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"name": "@asciidoctor/reducer", | ||
"version": "1.1.0", | ||
"description": "An Asciidoctor.js extension to reduce an AsciiDoc document containing includes and conditionals to a single AsciiDoc document.", | ||
"license": "MIT", | ||
"author": "Dan Allen", | ||
"contributors": [ | ||
"Dan Allen <[email protected]>" | ||
], | ||
"repository": "github:asciidoctor/asciidoctor-reducer", | ||
"bugs": { | ||
"url": "https://github.com/asciidoctor/asciidoctor-reducer/issues" | ||
}, | ||
"scripts": { | ||
"build": "npx -y --package [email protected] node npm/transpile.js", | ||
"preci": "npm i", | ||
"ci": "npm run build", | ||
"postci": "npm test", | ||
"clean": "npx rimraf dist node_modules", | ||
"postpublish": "npx -y downdoc --postpublish", | ||
"prepublishOnly": "npx -y downdoc --prepublish", | ||
"test": "node --test test/*-test.js" | ||
}, | ||
"main": "lib/index.js", | ||
"exports": { | ||
".": "./lib/index.js", | ||
"./extensions": "./lib/extensions.js", | ||
"./dist/*": "./dist/*", | ||
"./package.json": "./package.json" | ||
}, | ||
"devDependencies": { | ||
"@asciidoctor/core": "~2" | ||
}, | ||
"files": [ | ||
"bin", | ||
"dist", | ||
"lib" | ||
], | ||
"engines": { | ||
"node": ">=16.0.0" | ||
}, | ||
"keywords": [ | ||
"asciidoc", | ||
"asciidoctor", | ||
"extension", | ||
"include", | ||
"preprocessor" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Text in include. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
= Smoke Test | ||
|
||
Text in main document. | ||
|
||
include::smoke-include.adoc[] | ||
|
||
Text in main document. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict' | ||
|
||
const assert = require('node:assert/strict') | ||
const { describe, before, after, it } = require('node:test') | ||
const Asciidoctor = require('@asciidoctor/core')() | ||
const ospath = require('node:path') | ||
|
||
const FIXTURES_DIR = ospath.join(__dirname, 'fixtures') | ||
|
||
describe('reducer smoke test', () => { | ||
it('should reduce document with includes', async () => { | ||
const registry = Asciidoctor.Extensions.create() | ||
require('@asciidoctor/reducer').register(registry) | ||
const input = ospath.join(FIXTURES_DIR, 'smoke.adoc') | ||
const expected = [ | ||
'= Smoke Test', | ||
'', | ||
'Text in main document.', | ||
'', | ||
'Text in include.', | ||
'', | ||
'Text in main document.', | ||
] | ||
const actual = Asciidoctor.loadFile(input, { extension_registry: registry, safe: 'safe' }).getSourceLines() | ||
assert.deepEqual(actual, expected) | ||
}) | ||
|
||
it('should provide access to header attributes defined in source', async () => { | ||
const registry = Asciidoctor.Extensions.create() | ||
require('@asciidoctor/reducer').register(registry) | ||
const input = [ | ||
'= Smoke Test', | ||
':icons: font', | ||
':toc:', | ||
'', | ||
'body text', | ||
] | ||
const expected = { icons: 'font', toc: '' } | ||
const actual = Asciidoctor.load(input, { extension_registry: registry, safe: 'safe' }).source_header_attributes | ||
assert.deepEqual(actual.$$smap, expected) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.