-
Notifications
You must be signed in to change notification settings - Fork 40
/
print.js
209 lines (186 loc) · 5.27 KB
/
print.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const fs = require('fs')
const path = require('path')
const colorsTmpl = require('colors-tmpl')
const through2 = require('through2')
const split = require('split')
const inherits = require('util').inherits
const CombinedStream = require('combined-stream')
const StrToStream = require('string-to-stream')
function getText (i18n, contents) {
contents = colorsTmpl(contents)
.replace(/\{+([^{}]+)\}+/gi, function (match, k) {
var numPart = /^([^#]*)#([^#]+)$/.exec(k)
if (numPart) {
k = numPart[1]
return i18n.has(k) ? getText(i18n, i18n.__n(k, parseFloat(numPart[2]))) : match
} else {
return i18n.has(k) ? getText(i18n, i18n.__(k)) : match
}
})
.replace(/\$([A-Z_]+)/g, function (match, k) {
return i18n.has(k) ? getText(i18n, i18n.__(k)) : ('$' + k)
})
if (i18n.has('appDir')) {
// proper path resolution
contents = contents.replace(/\{rootdir:([^}]+)\}/gi, function (match, subpath) {
return 'file://' + path.join(i18n.__('appDir'), subpath)
})
}
return contents
}
function localisedFileName (lang, file) {
// Since the files that will be printed are subject to user manipulation
// a null can happen here, checking for it just in case.
if (file === undefined || file === null) {
return null
}
file = file.replace(/\{?\{lang\}?\}/g, lang)
try {
if (fs.accessSync ? (fs.accessSync(file, fs.R_OK) || true) : fs.existsSync(file)) {
var stat = fs.statSync(file)
if (stat && stat.isFile()) {
return file
}
}
} catch (e) {}
return null
}
function localisedFirstFile (files, lang) {
if (files === null) {
return null
}
var file = null
if (!Array.isArray(files)) {
file = localisedFileName(lang, files)
} else {
for (var i = 0; i < files.length && !file; i++) {
file = localisedFileName(lang, files[i])
}
}
return file
}
var PrintStream = function (i18n, lang) {
if (!(this instanceof PrintStream)) {
return new PrintStream(i18n, lang)
}
CombinedStream.call(this, {})
this.i18n = i18n
this.lang = lang
this.typeOpen = 'md'
}
inherits(PrintStream, CombinedStream)
PrintStream.prototype._append = CombinedStream.prototype.append
PrintStream.prototype._end = CombinedStream.prototype.end
PrintStream.prototype.end = function () {
if (this.typeOpen) {
this.typeOpen = null
this.emit('data', '```')
}
this._end()
}
PrintStream.prototype.append = function (content, contentType) {
var stream = null
if (typeof content === 'function') {
content = content(this.i18n, this.lang)
}
if (content === null || content === undefined) {
return false
}
if (Array.isArray(content)) {
return content.reduce(function (found, child) {
if (this.append(child)) {
return true
}
return found
}.bind(this), false)
}
if (content.hasOwnProperty('first')) {
return content.first.reduce(function (found, child) {
return found || this.append(child)
}.bind(this), false)
} else if (content.hasOwnProperty('files')) {
var files = content.files
.map(localisedFileName.bind(null, this.lang))
.filter(function (file) { return file !== null })
if (files.length > 0) {
stream = new PrintStream(this.i18n, this.lang)
files.forEach(function (file) {
stream.append('---', 'md')
if (files.length > 1) {
stream.append('`_' + file + '_` :')
}
stream.append({file: file})
})
stream.append('---', 'md')
this._append(stream)
return true
} else {
return false
}
}
if (content.hasOwnProperty('file')) {
var file = localisedFirstFile(content.file, this.lang)
if (file) {
stream = fs.createReadStream(file, {encoding: 'utf8'})
contentType = content.type || contentType || path.extname(file).replace(/^\./, '').toLowerCase()
}
} else if (content.pipe) {
stream = content
} else if (content.hasOwnProperty('text')) {
contentType = content.type
stream = new StrToStream(content.text + (content.skipNewline ? '' : '\n'))
} else {
stream = new StrToStream(content + '\n')
}
if (!stream) {
return false
}
if (!contentType) {
contentType = 'md'
}
var i18n = this.i18n
var buffer = []
this._append(
stream
.pipe(split())
.pipe(through2({objectMode: true}, function (contents, encoding, done) {
buffer.push(getText(i18n, contents.toString()))
done()
}, function (done) {
this.push({
text: buffer.join('\n'),
type: contentType
})
done()
}))
)
return true
}
PrintStream.prototype.write = function (data) {
if (typeof data === 'string') {
this.emit('data', data)
return
}
if (this.typeOpen !== data.type) {
if (data.type !== 'md') {
data.text = '```' + data.type + '\n' + data.text
}
if (this.typeOpen !== 'md') {
data.text = '\n```\n' + data.text
}
this.typeOpen = data.type
}
this.emit('data', data.text)
}
PrintStream.prototype._end = CombinedStream.prototype.end
PrintStream.prototype.end = function () {
if (this.typeOpen !== 'md') {
this.emit('data', '```')
}
this._end()
}
PrintStream.prototype.appendChain = function (content, type) {
this.append(content, type)
return this
}
module.exports = PrintStream