-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsbv.js
More file actions
119 lines (77 loc) · 2.33 KB
/
sbv.js
File metadata and controls
119 lines (77 loc) · 2.33 KB
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
#!/usr/bin/env node
const fs = require('fs')
function read(file) {
const contents = fs.readFileSync(file).toString()
const data = []
contents.replace(
/^(\d:.*)\n((?:(?!\d+:\d+:\d+).*\n*)+)$/gm,
(string, time, text) =>
data.push({
time: {
start: time.split(',')[0],
end: time.split(',')[1]
},
text: text.replace(/\n/g, ' ').trim()
})
)
return data
}
const functions = {
json: function(input, output='filename.json') {
if (arguments.length) {
const data = JSON.stringify(read(input))
return arguments.length === 1
? console.log(data)
: fs.writeFileSync(output, data)
} else {
return console.log('please enter a filename to read, and optionally an output filename to write')
}
},
text: function(input, output='filename.txt') {
if (arguments.length) {
const data = read(input).map(caption => `${caption.text}`).join(' ')
return arguments.length === 1
? console.log(data)
: fs.writeFileSync(output, data)
} else {
return console.log('please enter a filename to read, and optionally an output filename to write')
}
},
html: function(input, output='filename.html') {
if (arguments.length) {
const data = '<!DOCTYPE html>\n'
+ '<meta charset=utf-8>\n'
+ read(input).map(caption =>
'<p'
+ ' title="' + caption.time.start + ' – ' + caption.time.end + '"'
+ ' data-start="' + caption.time.start + '"'
+ ' data-end="' + caption.time.end + '"'
+ '>'
+ caption.text
+ '</p>\n'
)
.join('')
return arguments.length === 1
? console.log(data)
: fs.writeFileSync(output, data)
} else {
return console.log('please enter a filename to read, and optionally an output filename to write')
}
},
}
if (process.argv[2]) {
const [command, ...args] = [
...process.argv[2].split(' '), ...process.argv.slice(3)
]
if (functions[command]) {
functions[command](...args)
} else {
console.log('command not recognized')
}
} else {
console.log(`\navailable output formats include:\n\n${
Object.entries(functions)
.map(entry => `- ${entry[0]}\n`)
.join('')
}`)
}