-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
40 lines (33 loc) · 940 Bytes
/
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
var split = require('split2')
var pumpify = require('pumpify')
var through = require('through2')
var utf8 = require('to-utf-8')
module.exports = function () {
var buf = []
var convert = function () {
return buf.join('\r\n')
.replace(/\{\\([ibu])\}/g, '</$1>')
.replace(/\{\\([ibu])1\}/g, '<$1>')
.replace(/\{([ibu])\}/g, '<$1>')
.replace(/\{\/([ibu])\}/g, '</$1>')
.replace(/(\d\d:\d\d:\d\d),(\d\d\d)/g, '$1.$2')
.replace(/\r\n\{\\an8\}/g, ' line:5%\r\n') +
'\r\n\r\n'
}
var write = function (line, enc, cb) {
if (line.trim()) {
buf.push(line.trim())
return cb()
}
line = convert()
buf = []
cb(null, line)
}
var flush = function (cb) {
if (buf.length) this.push(convert())
cb()
}
var parse = through.obj(write, flush)
parse.push('WEBVTT FILE\r\n\r\n')
return pumpify(utf8({newline: false, detectSize: 4095}), split(), parse)
}