Skip to content

Commit

Permalink
resolves #57 add js build that publishes an npm package (PR #58)
Browse files Browse the repository at this point in the history
  • Loading branch information
mojavelinux authored Nov 24, 2024
1 parent 7a5c7bd commit 1279e47
Show file tree
Hide file tree
Showing 16 changed files with 283 additions and 29 deletions.
19 changes: 14 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ name: CI
on:
push:
branches: ['**']
paths-ignore: ['*.adoc', 'docs/**']
paths-ignore: ['*.adoc', 'docs/**', 'js/*.adoc']
pull_request:
branches: [main]
paths-ignore: ['*.adoc', 'docs/**']
paths-ignore: ['*.adoc', 'docs/**', 'js/*.adoc']
schedule:
- cron: '30 2 * * MON'
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
activate:
if: |
if: >-
github.event_name == 'push' ||
(github.event_name == 'schedule' && github.repository_owner == 'asciidoctor') ||
(github.event_name == 'push') ||
(github.event_name == 'pull_request' && !startsWith(github.head_ref, 'docs/'))
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -64,7 +64,7 @@ jobs:
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: ${{ github.event_name != 'schedule' }}
- name: Install dependencies (scheduled build only)
- name: Install Ruby dependencies (scheduled build only)
if: github.event_name == 'schedule'
run: |
bundle config --local path vendor/bundle
Expand All @@ -74,3 +74,12 @@ jobs:
run: bundle exec rake lint
- name: Run tests
run: bundle exec ruby -w $(bundle exec ruby -e 'print File.join Gem.bindir, %q(rake)') spec
- name: Install Node.js
if: matrix.primary
uses: actions/setup-node@v3
with:
node-version: '22'
- name: Run smoke test for npm package
if: matrix.primary
working-directory: js
run: npm run ci
25 changes: 12 additions & 13 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@ on:
description: Enter version to release (e.g., 1.0.1).
required: false
jobs:
activate:
runs-on: ubuntu-latest
if: github.repository_owner == 'asciidoctor' && github.event_name == 'workflow_dispatch'
steps:
- run: echo ok go
perform:
needs: activate
if: github.repository_owner == 'asciidoctor' && github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
environment: releases
steps:
Expand All @@ -24,19 +19,23 @@ jobs:
with:
ruby-version: '3.3'
bundler-cache: false
- name: Configure Bundler
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: '22'
- name: Install Ruby dependencies
run: |
bundle config --local path vendor/bundle
bundle config --local without coverage docs
- name: Install dependencies
run: bundle --jobs 3 --retry 3
- name: Run tests
run: bundle exec rake spec
bundle --jobs 3 --retry 3
- name: Run linter
run: bundle exec rake lint
- name: Setup release environment
- name: Run tests
run: bundle exec rake spec
- name: Set up release environment
run: |
echo RELEASE_VERSION=${{ github.event.inputs.release-version }} >> $GITHUB_ENV
echo RELEASE_NPM_TOKEN=${{ secrets[format('NPM_TOKEN_{0}', github.actor)] }} >> $GITHUB_ENV
echo RELEASE_RUBYGEMS_API_KEY=${{ secrets[format('RUBYGEMS_API_KEY_{0}', github.actor)] }} >> $GITHUB_ENV
- name: Build, tag, and publish gem
- name: Build, tag, and publish packages
run: ./release.sh
3 changes: 3 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ endif::[]
Additionally, the tool 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 tool returns the unmodified source.

TIP: This extension is also published as an npm package named `@asciidoctor/reducer` for use with Asciidoctor.js, and hence, with Antora.
See the xref:js/README.adoc[README] to find instructions on how to use this package.

== Prerequisites

{project-name} is a Ruby application that you install using Ruby packaging.
Expand Down
3 changes: 3 additions & 0 deletions js/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.nvmrc
/dist/
/node_modules/
5 changes: 5 additions & 0 deletions js/.npmrc
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
63 changes: 63 additions & 0 deletions js/README.adoc
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.
5 changes: 5 additions & 0 deletions js/lib/extensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict'

require('../dist')

module.exports = Opal.Asciidoctor.Reducer.Extensions
5 changes: 5 additions & 0 deletions js/lib/index.js
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)
27 changes: 27 additions & 0 deletions js/npm/transpile.js
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)
49 changes: 49 additions & 0 deletions js/package.json
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"
]
}
1 change: 1 addition & 0 deletions js/test/fixtures/smoke-include.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Text in include.
7 changes: 7 additions & 0 deletions js/test/fixtures/smoke.adoc
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.
42 changes: 42 additions & 0 deletions js/test/reducer-smoke-test.js
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)
})
})
10 changes: 6 additions & 4 deletions lib/asciidoctor/reducer/extensions.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# frozen_string_literal: true

require 'asciidoctor' unless defined? Asciidoctor.load
require_relative 'header_attribute_tracker'
require_relative 'preprocessor'
require_relative 'tree_processor'
unless RUBY_ENGINE == 'opal'
require 'asciidoctor' unless defined? Asciidoctor.load
require_relative 'header_attribute_tracker'
require_relative 'preprocessor'
require_relative 'tree_processor'
end

module Asciidoctor::Reducer
module Extensions
Expand Down
6 changes: 4 additions & 2 deletions lib/asciidoctor/reducer/preprocessor.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

require_relative 'include_directive_tracker'
require_relative 'conditional_directive_tracker'
unless RUBY_ENGINE == 'opal'
require_relative 'include_directive_tracker'
require_relative 'conditional_directive_tracker'
end

module Asciidoctor::Reducer
class Preprocessor < ::Asciidoctor::Extensions::Preprocessor
Expand Down
Loading

0 comments on commit 1279e47

Please sign in to comment.