-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
107 lines (90 loc) · 3.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'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/core')()
const docbookConverter = require('@asciidoctor/docbook-converter')
docbookConverter.register()
/**
* Test is the given object is a class
* @param {object} obj The object to test
*/
function isClass (obj) {
return typeof obj === 'function' && obj.name !== 'Object'
}
module.exports = function (theOptions = {}) {
var options = theOptions || {}
var asciidoctorOptions = { ...theOptions }
// default config
var extension = options.extension || '.html'
if (options.backend === 'docbook5') {
extension = options.extension || '.xml'
}
// AsciiDoctor options
asciidoctorOptions.safe = options.safe || 'unsafe' // unsafe, safe, server or secure
asciidoctorOptions.doctype = options.doctype || 'article' // book,inline
asciidoctorOptions.attributes = options.attributes || ['showtitle']
// Fixed typo for issue #29
asciidoctorOptions.backend = options.backend || 'html5' // defaults to html5
asciidoctorOptions.doctype = options.doctype || 'article' // defaults to article
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.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])
} 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
var stream = through(function (file, encoding, callback) {
// do whatever necessary to process the file
if (file.isNull()) {
callback(null, file)
return
}
if (file.isStream()) {
callback(new PluginError('gulp-asciidoctor',
'Streaming not supported'))
return
}
if (file.isBuffer()) {
}
// Set a base_dir if no one is given to resolve relative filenames
// when using include::[]
// see https://github.com/asciidoctor/gulp-asciidoctor/issues/5
asciidoctorOptions.base_dir = theOptions.base_dir || file.dirname
// just pipe data next, or just do nothing to process file later in flushFunction
// never forget callback to indicate that the file has been processed.
var data = asciidoctor.convert(file.contents.toString(),
asciidoctorOptions)
file.contents = Buffer.from(data)
file.path = replaceExt(file.path, extension)
callback(null, file)
}, function (callback) {
// just pipe data next, just callback to indicate that the stream's over
// this.push(something);
callback()
})
// returning the file stream
return stream
}