forked from jsdoc2md/dmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
139 lines (120 loc) · 4.19 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* @module dmd
*/
const path = require('path')
const Cache = require('cache-point')
const DmdOptions = require('./lib/dmd-options')
const dmdVersion = require('./package').version
/**
* Transforms doclet data into markdown documentation.
* @param {object[]}
* @param [options] {module:dmd-options} - The render options
* @return {string}
* @alias module:dmd
*/
function dmd (templateData, options) {
options = new DmdOptions(options)
if (skipCache(options)) {
return generate(templateData, options)
} else {
const cached = dmd.cache.readSync([templateData, options, dmdVersion])
if (cached) {
return cached
} else {
return generate(templateData, options)
}
}
}
dmd.async = function (templateData, options) {
options = new DmdOptions(options)
if (skipCache(options)) {
return Promise.resolve(generate(templateData, options))
} else {
return dmd.cache.read([templateData, options, dmdVersion])
.catch(function () {
return generate(templateData, options)
})
}
}
dmd.cache = new Cache({ dir: path.join(require('os').tmpdir(), 'dmd') })
function generate (templateData, options) {
const fs = require('fs')
const path = require('path')
const arrayify = require('array-back')
const handlebars = require('handlebars')
const walkBack = require('walk-back')
const DmdOptions = require('./lib/dmd-options')
const FileSet = require('file-set')
function registerPartials (paths) {
const fileSet = new FileSet(paths)
fileSet.files.forEach(function (file) {
handlebars.registerPartial(
path.basename(file, '.hbs'),
fs.readFileSync(file, 'utf8') || ''
)
})
}
function registerHelpers (helpers) {
const fileSet = new FileSet(helpers)
fileSet.files.forEach(function (file) {
handlebars.registerHelper(require(path.resolve(process.cwd(), file)))
})
}
/* Register handlebars helper modules */
;['./helpers/helpers', './helpers/ddata', './helpers/selectors'].forEach(function (modulePath) {
handlebars.registerHelper(require(modulePath))
})
const inputData = templateData.map(function (row) {
return Object.assign({}, row)
})
const inputOptions = Object.assign({}, options)
templateData = arrayify(templateData)
options = Object.assign(new DmdOptions(), options)
options.plugin = arrayify(options.plugin)
options._depth = 0
options._indexDepth = 0
/* state module, for sharing with the helpers */
const state = require('./lib/state')
state.templateData = templateData
state.options = options
/* register all dmd partials. */
registerPartials(path.resolve(__dirname, './partials/**/*.hbs'))
/* if plugins were specified, register the helpers/partials from them too */
if (options.plugin) {
for (let i = 0; i < options.plugin.length; i++) {
const plugin = options.plugin[i]
let modulePath = ''
/* user supplied an existing path */
if (fs.existsSync(path.resolve(plugin))) {
modulePath = path.resolve(plugin)
/* else user supplied a module name, search npm installed modules */
} else {
modulePath = walkBack(process.cwd(), path.join('node_modules', plugin))
}
if (modulePath) {
/* load the plugin options */
const pluginOptions = require(modulePath)(options)
options.partial = options.partial.concat(pluginOptions.partial)
options.helper = options.helper.concat(pluginOptions.helper)
} else {
throw new Error('Cannot find plugin: ' + plugin)
}
}
}
/* if additional partials/helpers paths were specified, register them too */
if (options.partial.length) registerPartials(options.partial)
if (options.helper.length) registerHelpers(options.helper)
const compiled = handlebars.compile(options.template, {
preventIndent: true,
strict: false
})
templateData.options = options
const output = compiled(templateData)
dmd.cache.writeSync([inputData, inputOptions, dmdVersion], output)
return output
}
/* always skip the cache when custom plugins, partials or helpers are used */
function skipCache (options) {
return options.noCache || options.plugin.length || options.partial.length || options.helper.length
}
module.exports = dmd