-
Notifications
You must be signed in to change notification settings - Fork 4
/
serialize.js
149 lines (129 loc) · 3.75 KB
/
serialize.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
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/
"use strict";
if (typeof module !== "undefined") {
var helpers = require("./helpers.js");
var printTimestamp = helpers.printTimestamp;
}
var WebVTTSerializer = function() {
var that = this,
serializeTree = function (tree) {
var result = "",
treeLength = tree.length;
for (var i = 0; i < treeLength; i++) {
var node = tree[i],
nodeType = node.type,
nodeClasses = node.classes;
if (nodeType === "text") {
result += node.value;
} else if (nodeType === "object") {
result += "<" + node.name;
if (nodeClasses) {
var nodeClassesLength = nodeClasses.length;
for(var y = 0; y < nodeClassesLength; y++) {
result += "." + nodeClasses[y];
}
}
if (node.value) {
result += " " + node.value;
}
result += ">";
if (node.children) {
result += serializeTree(node.children);
}
result += "</" + node.name + ">";
} else {
result += "<" + node.value + ">";
}
}
return result;
},
serializeCueSettings = function(cue) {
var result = "";
if (cue === "") {
return result;
}
// writing direction
if (cue.direction !== "horizontal") {
result += "vertical:" + cue.direction + "% ";
}
// line position
if (cue.linePosition !== "auto") {
result += "line:" + cue.linePosition;
if (cue.snapToLines === false) {
result += "%";
}
if (cue.lineAlign !== "") {
result += "," + cue.lineAlign;
}
result += " ";
}
// text position
if (cue.textPosition) {
result += "position:" + cue.textPosition + "%";
if (cue.positionAlign && cue.positionAlign !== "") {
result += "," + cue.positionAlign;
}
result += " ";
}
// size
if (cue.size !== 100) {
result += "size:" + cue.size + "% ";
}
// alignment
if (cue.alignment !== "middle") {
result += "align:" + cue.alignment + " ";
}
// region
if (cue.region !== "") {
result += "region:" + cue.region;
}
return result;
},
serializeCue = function (cue) {
var result = "";
if (cue.id) {
result += cue.id + "\n";
}
result += printTimestamp(cue.startTime) + " --> " + printTimestamp(cue.endTime);
result += " " + serializeCueSettings(cue) + "\n";
result += serializeTree(cue.tree.children) + "\n";
return result;
},
serializeRegion = function(attributes) {
var result = "";
if (attributes.id) {
result += " id=" + attributes.id;
}
result += " width=" + attributes.width + "%";
result += " lines=" + attributes.lines;
result += " regionanchor=" + attributes.regionanchorX + "%," + attributes.regionanchorY + "%";
result += " viewportanchor=" + attributes.viewportanchorX + "%," + attributes.viewportanchorY + "%";
if (attributes.scroll) {
result += " scroll=" + attributes.scroll;
}
return result;
};
that.serialize = function(cues, metadatas) {
var result = "WEBVTT\n",
cueLength = cues.length,
metadataLength = metadatas.length,
i=0;
for(i=0; i<metadataLength; i++) {
if (metadatas[i].name === "Region") {
result += metadatas[i].name + ":" + serializeRegion(metadatas[i].regionAttributes) + "\n";
} else {
result += metadatas[i].name + ":" + metadatas[i].value + "\n";
}
}
for(i=0; i<cueLength; i++) {
result += "\n";
result += serializeCue(cues[i]);
}
return result;
};
return that;
};
if (typeof module !== "undefined") {
module.exports.WebVTTSerializer = WebVTTSerializer;
}