-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvg2csv.js
87 lines (73 loc) · 2.35 KB
/
svg2csv.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
const fs = require("fs");
const path = require("path");
async function main() {
for (let f of fs.readdirSync("./svgfiles")) {
if (f.match("\.svg")) {
var rows = [];
var currentRow = { text: "", speaker: "", notes: "", timecode: "" };
const text = fs.readFileSync(path.join("svgfiles", f)).toString();
var started = false;
var lastSpeaker
for (let line of text.split("\n")) {
//console.log(line)
var speaker
var timecode
if (line.match(`font-weight="700"`)) {
speaker = "A"
} else {
speaker = "B"
}
line = line.replace(/<.*?>/g, "");
line = line.replace(/\s*/, "");
line = line.replace(/^\d+$/);
line = line.replace(/'/, "'");
line = line.replace(/'/, "'");
line = line.replace(/"/, `""`);
if (!line) {
speaker = lastSpeaker
}
if (line.match(/^\d+\.\d+\s+$/)) {
timecode = line.replace(/\s+/, "");
started = true;
line = "";
rows.push(currentRow);
currentRow = { text: "", speaker: "", notes: "", timecode: "" };
currentRow.timecode = timecode
currentRow.speaker = speaker
//csv += `"\n"${timecode}","${speaker}","`
} else if (!(speaker === lastSpeaker)) {
if (currentRow.text) {
rows.push(currentRow);
currentRow = { text: "", speaker: "", notes: "", timecode: "" };
}
if (started) {
currentRow.speaker = speaker
currentRow.text = line
//csv += `"\n"","${speaker}","${line}`;
} else {
currentRow.notes = line
//csv += `"\n"","","", "${line}`;
}
} else if (line) {
if (started) {
currentRow.text += line
} else {
currentRow.notes += line
}
//csv += line
}
lastSpeaker = speaker;
}
//csv += `"`;
rows.push(currentRow)
}
var csv = `"time","speaker","text","notes"\n`;
for (let row of rows) {
if (row.timecode || row.speaker || row.notes || row.text) {
csv += `"${ row.timecode }","${ row.speaker }","${ row.text }","${ row.notes }"\n`;
}
}
fs.writeFileSync(path.join("csvfiles", f.replace(".svg", ".csv")), csv)
}
}
main();