Skip to content

Commit

Permalink
Merge pull request #17 from asciidoctor/asciidoctor_2_2_0-branch
Browse files Browse the repository at this point in the history
Fixed issue #15 Upgrade to Asciidoctor 2.2.0
  • Loading branch information
henriette-einstein committed May 13, 2020
2 parents 2ead38a + 243fde3 commit c5015d7
Show file tree
Hide file tree
Showing 12 changed files with 1,220 additions and 1,913 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
yarn.lock
.DS_Store

3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"editor.tabSize": 2,
"standard.autoFixOnSave": true
"standard.autoFixOnSave": true,
"liveServer.settings.port": 5501
}
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# gulp-asciidoctor

[![Build](https://github.com/asciidoctor/gulp-asciidoctor/workflows/Build/badge.svg)](https://github.com/asciidoctor/gulp-asciidoctor/actions?query=workflow%3ABuild)
[![npm version](https://img.shields.io/npm/v/gulp-asciidoctor.svg)](https://www.npmjs.org/package/gulp-asciidoctor)

A [gulp](https://gulpjs.com) plugin to support Asciidoctor processing in gulp tasks.

## Installation

### Requirements

You need [Node](https://nodejs.org) installed on your machine to install and run the gulp-asciidoctor plugin.

You need [gulp](https://gulpjs.com) installed globally or in your project directory.

### Install gulp-asciidoctor via npm

```
$ npm install gulp-asciidoctor --save
```

### Install gulp-asciidoctor via yarn

```
$ yarn add gulp-asciidoctor
```

## Documentation

More information can be found in the [documentation](./doc/documentation.adoc) on Github.
150 changes: 150 additions & 0 deletions doc/documentation.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
= Asciidoctor plugin for Gulp

A https://gulpjs.com[gulp] plugin to support https://asciidoctor-docs.netlify.app/asciidoctor.js/[Asciidoctor.js] processing in gulp tasks.

== Installation

=== Requirements

* You need https://nodejs.org[node] installed on your machine to install and run the gulp-asciidoctor plugin.
* You need https://gulpjs.com[gulp] installed globally or in your project directory.

=== Installation with npm
----
npm install gulp-asciidoctor --save
----

=== Installation with yarn
----
yarn add gulp-asciidoctor
----

== Usage
This plugin can be used to integrate Asciidoctor processing in your gulp tasks. Here is a typical usage scenario.

.Gulpfile
[source,javascript]
----
const gulp = require('gulp')
const asciidoctor = require('gulp-asciidoctor')
function processAdocFiles (cb) {
gulp.src('/path/to/my/files/**/*.adoc')
.pipe(asciidoctor({})
.pipe(gulp.dest('targetdir'))
cb()
}
function copyImages(cb) {
gulp.src('/path/to/my/files/**/*.jpg')
.pipe(gulp.dest('targetdir'))
cb()
}
exports.process = gulp.parallel(processAdocFiles, copyImages)
----

You can configure the plugin itself by adding options:

.Set plugin options
[source,javascript]
----
...
.pipe(asciidoctor({
extension: '.htm'
})
...
----

And you can also configure the underlying Asciidoctor processor via the respective Asciidoctor options:

.Set Asciidoctor options
[source,javascript]
----
...
.pipe(asciidoctor({
safe: 'secure', // unsafe, safe, server or secure
doctype: 'article', // book, inline
standalone: true, // true or false
attributes: ['showtitle']
})
...
----

The plugin also supports the https://asciidoctor-docs.netlify.app/asciidoctor.js/extend/converter/template-converter[Asciidoctor Template Converter]

.With Asciidoctor templates
[source,javascript]
----
var path = require('path')
...
.pipe(asciidoctor({
template_dirs: path.join(__dirname, 'templates')
})
...
----

== Options
The behaviour of the plugin can be controlled via options. There are options that tweak the behaviour of the Gulp-plugin itself and options that controll the underlying Asciidoctor processor.

== Plugin specific Options
The following options can be used to configure the Gulp-Plugin

extension:: Sets the output extension of the plugin. Defaults to '.html'
cnv:: Sets a custom converter that should be used for processing. Defaults to 'undefined'.

== AsciiDoctor Options
All options of the underlying Asciidoctor can be set. Please refer to the
https://asciidoctor-docs.netlify.app/asciidoctor.js/processor/convert-options[Converter options] page to see, which options are available.

However, the following Asciidoctor options:

* mkdirs
* to_dir
* to_file

will be removed, since they do not make sense in a gulp processing chain.

If not set, the following options will be set to the default values described here

backed:: Sets the converter backend to use. Defaults to 'html5'
base_dir:: The base directory for resolving relative resources. Defaults to the directory of the currently processed file
doctype:: The doctype. May be 'article', 'book' or 'inline'. Defaults to 'article'
safe:: The safemode to use. May be 'unsafe', 'safe', 'server' or 'secure'. Defaults to 'unsafe'
standalone:: Should headers and footers be included. Defaults to 'true'

== Important

=== Base Directory
Do not forget to set the Asciidoctor option `base_dir` if you want to include
files from locations that are non-relative to the currently proecssed file.

=== Obsolete option 'header_footer'
The 'old' option 'header_footer' will be skipped in favor of the new option 'standalone'.

* If 'header_footer' is set and 'standalone' is not set, the processor will receive 'standalone' = value of 'header_footer' option and the option 'header_footer' will be stripped.
* If both 'header_footer' and 'standalone' are set, the option 'header_footer' will be stripped.

== Changelog
- V2.2.0: upgrade to asciidoctor 2.2.0
- V2.1.1: upgrade to asciidoctor 2.1.1, mocha 7.1.0 and replaced gulp-util with replace-ext and plugin-error
- v1.5.5-4: upgrade asciidoctor.js to version 1.5.5-4 and use version consistent with asciidoctor.js.
- v1.0.8: upgrade asciidoctor.js to version 1.5.5-1 and other deps to latest.
- v1.0.7: clean dependences(remove gulp and gulp-debug).
- v1.0.6: bug fixed
- v1.0.5: fix asciidoctor.js default init bug (Jan/11/2015)
- v1.0.4: bug fixed (Jan/11/2015)
- v1.0.3: bug fixed (Jan/8/2015)
* Initialize asciidoctor.js only once (Thanks https://github.com/amr[Amr Mostafa])
- v1.0.2: update to asciidoctor 1.5.2

== TODO
- more test case

41 changes: 25 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict'

// PLUGIN_NAME: gulp-asciidoctor
var through = require('through-gulp')
var PluginError = require('plugin-error')
var replaceExt = require('replace-ext')
var asciidoctor = require('asciidoctor')()
var asciidoctor = require('@asciidoctor/core')()

/**
* Test is the given object is a class
Expand All @@ -14,39 +16,46 @@ function isClass (obj) {

module.exports = function (theOptions = {}) {
var options = theOptions || {}
var asciidoctorOptions = {}
var asciidoctorOptions = { ...theOptions }

// default config
var extension = options.extension || '.html'
// var extensionRegistry = asciidoctor.Extensions

// AsciiDoctor options
asciidoctorOptions.safe = options.safe || 'unsafe' // unsafe, safe, server or secure
asciidoctorOptions.doctype = options.doctype || 'article' // book,inline
asciidoctorOptions.attributes = options.attributes || ['showtitle']
asciidoctorOptions.backend = options.backed || 'html5' // defaults to html5
asciidoctorOptions.doctype = options.doctype || 'article' // defaults to article
asciidoctorOptions.header_footer = (options.header_footer === undefined
? true : options.header_footer)

// If user overrides extension registry, use his registry
// if (options.extension_registry !== undefined) {
// extensionRegistry = options.extension_registry
// }
if (asciidoctorOptions.standalone === undefined) {
if (asciidoctorOptions.header_footer !== undefined) {
asciidoctorOptions.standalone = asciidoctorOptions.header_footer
} else {
asciidoctorOptions.standalone = true // standalone defaults to true
}
}
delete asciidoctorOptions.header_footer

// Extension is only used by gulp
delete asciidoctorOptions.extension
delete asciidoctorOptions.to_file
delete asciidoctorOptions.to_dir
delete asciidoctorOptions.mkdirs

// Load converter if option is set
if (options.converter !== undefined) {
var cnv = options.converter
if (isClass(options.converter)) {
const CnvClass = options.converter
cnv = new CnvClass()
if (options.cnv !== undefined) {
var cnv = options.cnv
if (isClass(options.cnv)) {
const CnvClazz = options.cnv
cnv = new CnvClazz()
}

if (typeof cnv.convert === 'function') {
asciidoctor.ConverterFactory.register(cnv, asciidoctorOptions.backend)
asciidoctor.ConverterFactory.register(cnv, [asciidoctorOptions.backend])
} else {
throw new PluginError('gulp-asciidoctor', 'Provided custom converter must implement a convert() method')
}
delete asciidoctorOptions.cnv
}

// creating a stream through which each file will pass
Expand Down
Loading

0 comments on commit c5015d7

Please sign in to comment.