-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
156 lines (134 loc) · 4.54 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict'
var through = require('through2')
var PluginError = require('plugin-error')
var replaceExtension = require('replace-ext')
var mustache = require('mustache')
var fs = require('fs')
var path = require('path')
var escapeRegex = require('escape-string-regexp')
module.exports = function(view, options, partials) {
options = options || {}
partials = partials || {}
var viewError = null
if (options.tags) {
mustache.tags = options.tags
}
// if view is string, interpret as path to json filename
if (typeof view === 'string') {
try {
view = JSON.parse(fs.readFileSync(view, 'utf8'))
} catch (e) {
viewError = e
}
}
return through.obj(function(file, enc, cb) {
if (file.isNull()) {
this.push(file)
return cb()
}
if (file.isStream()) {
this.emit(
'error',
new PluginError('gulp-mustache', 'Streaming not supported')
)
}
if (viewError) {
this.emit('error', new PluginError('gulp-mustache', viewError.toString()))
}
var template = file.contents.toString()
try {
loadPartials.call(this, template, file.path)
} catch (e) {
this.emit('error', new PluginError('gulp-mustache', e.message))
}
try {
file.contents = Buffer.from(
mustache.render(template, file.data || view, partials)
)
} catch (e) {
this.emit('error', new PluginError('gulp-mustache', e.message))
}
if (typeof options.extension === 'string') {
file.path = replaceExtension(file.path, options.extension)
}
this.push(file)
cb()
})
// find and load partials not already in partials list from disk, recursively
function loadPartials(template, templatePath) {
var templateDir = path.dirname(templatePath)
var partialRegexp = new RegExp(
escapeRegex(mustache.tags[0]) +
'>\\s*(\\S+)\\s*' +
escapeRegex(mustache.tags[1]),
'g'
)
var partialMatch
while ((partialMatch = partialRegexp.exec(template))) {
var partialName = partialMatch[1]
if (!partials[partialName]) {
try {
var partialPath = null
var partial = null
// ignore `partial` with file extension.
// e.g.
// 1, `{{> ./path/to/partial.html }}`
// 2, `{{> ./path/to/partial. }}`
if (path.extname(partialName) !== '') {
partialPath = path.resolve(templateDir, partialName)
partial = fs.readFileSync(partialPath, 'utf8')
} else {
// ignore `partial` file is exists without file extension.
// e.g.
// 1, `{{> ./path/to/partial }}` is exists.
// 2, `{{> ./path/to/.partial }}` is exists.
partialPath = path.resolve(templateDir, partialName)
if (fs.existsSync(partialPath)) {
partial = fs.readFileSync(partialPath, 'utf8')
} else {
// or check if `partial + options.extension` is exists.
// e.g.
// if `options.extension` equals ".html":
// the `{{> ./path/to/partial }}` will load
// `./path/to/partial.html`.
if (typeof options.extension === 'string') {
partialPath = path.resolve(
templateDir,
partialName + options.extension
)
if (fs.existsSync(partialPath)) {
partial = fs.readFileSync(partialPath, 'utf8')
}
}
// when `options.extension` is not a string or
// `partialName + options.extension` does not exists.
// try use `.mustache` extension to load `partial` file.
if (partial === null) {
partialPath = path.resolve(
templateDir,
partialName + '.mustache'
)
partial = fs.readFileSync(partialPath, 'utf8')
}
}
}
partials[partialName] = partial
loadPartials.call(this, partial, partialPath)
} catch (ex) {
this.emit(
'error',
new PluginError(
'gulp-mustache',
// use `ex.message` property instead of `partialPath`,
// because `this.emit()` seems not a sync method.
// also the `ex.message` property provide more details
// about error information.
'Unable to load partial file: ' + ex.message
)
)
}
}
}
}
}
module.exports.mustache = mustache