@sub37/webvtt-adapter 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -0
- package/lib/Adapter.d.ts +7 -0
- package/lib/Adapter.js +279 -0
- package/lib/InvalidFormatError.d.ts +8 -0
- package/lib/InvalidFormatError.js +28 -0
- package/lib/MissingContentError.d.ts +3 -0
- package/lib/MissingContentError.js +8 -0
- package/lib/Parser/RenderingModifiers.d.ts +18 -0
- package/lib/Parser/RenderingModifiers.js +300 -0
- package/lib/Parser/Tags/Node.d.ts +10 -0
- package/lib/Parser/Tags/Node.js +10 -0
- package/lib/Parser/Tags/NodeQueue.d.ts +12 -0
- package/lib/Parser/Tags/NodeQueue.js +41 -0
- package/lib/Parser/Tags/index.d.ts +3 -0
- package/lib/Parser/Tags/index.js +4 -0
- package/lib/Parser/Tags/tokenEntities.d.ts +4 -0
- package/lib/Parser/Tags/tokenEntities.js +12 -0
- package/lib/Parser/Tags/utils.d.ts +15 -0
- package/lib/Parser/Tags/utils.js +63 -0
- package/lib/Parser/Timestamps.utils.d.ts +1 -0
- package/lib/Parser/Timestamps.utils.js +17 -0
- package/lib/Parser/index.d.ts +3 -0
- package/lib/Parser/index.js +4 -0
- package/lib/Parser/parseCue.d.ts +19 -0
- package/lib/Parser/parseCue.js +95 -0
- package/lib/Parser/parseRegion.d.ts +5 -0
- package/lib/Parser/parseRegion.js +81 -0
- package/lib/Parser/parseStyle.d.ts +22 -0
- package/lib/Parser/parseStyle.js +159 -0
- package/lib/Token.d.ts +24 -0
- package/lib/Token.js +40 -0
- package/lib/Tokenizer.d.ts +41 -0
- package/lib/Tokenizer.js +280 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +2 -0
- package/package.json +28 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @see https://www.w3.org/TR/2019/CR-webvtt1-20190404/#webvtt-data-state
|
|
3
|
+
*
|
|
4
|
+
* An "annotation" is:
|
|
5
|
+
* (1) the content between the tag name and the ">";
|
|
6
|
+
* (2) the content between ">" and "<";
|
|
7
|
+
*
|
|
8
|
+
* As (1), some disallow them (c, i, u, b, ruby, rt). All but "ruby" (which take its from "rt") require one as (2)
|
|
9
|
+
* As (1), some requires them (v, lang).
|
|
10
|
+
*
|
|
11
|
+
* Therefore the annotation state is after the "<" and the tag name
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
*
|
|
15
|
+
* ```vtt
|
|
16
|
+
* <v Fred></v> (Fred)
|
|
17
|
+
* <lang en-US></lang> (en-US)
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
import { Token } from "./Token.js";
|
|
21
|
+
export declare class Tokenizer {
|
|
22
|
+
private rawContent;
|
|
23
|
+
/** Character index in the content */
|
|
24
|
+
private cursor;
|
|
25
|
+
/** Next token index in the content */
|
|
26
|
+
private startPoint;
|
|
27
|
+
constructor(rawContent: string);
|
|
28
|
+
static isWhitespace(character: string): boolean;
|
|
29
|
+
static isNewLine(character: string): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Attempts to convert a set of character (a supposed HTML Entity)
|
|
32
|
+
* to the correct character, by accumulating its char code;
|
|
33
|
+
*
|
|
34
|
+
* @param source
|
|
35
|
+
* @param currentCursor
|
|
36
|
+
* @param additionalAllowedCharacters
|
|
37
|
+
* @returns
|
|
38
|
+
*/
|
|
39
|
+
static parseHTMLEntity(source: string, currentCursor: number, additionalAllowedCharacters?: string[]): [content: string, cursor: number];
|
|
40
|
+
nextToken(): Token | null;
|
|
41
|
+
}
|
package/lib/Tokenizer.js
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @see https://www.w3.org/TR/2019/CR-webvtt1-20190404/#webvtt-data-state
|
|
3
|
+
*
|
|
4
|
+
* An "annotation" is:
|
|
5
|
+
* (1) the content between the tag name and the ">";
|
|
6
|
+
* (2) the content between ">" and "<";
|
|
7
|
+
*
|
|
8
|
+
* As (1), some disallow them (c, i, u, b, ruby, rt). All but "ruby" (which take its from "rt") require one as (2)
|
|
9
|
+
* As (1), some requires them (v, lang).
|
|
10
|
+
*
|
|
11
|
+
* Therefore the annotation state is after the "<" and the tag name
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
*
|
|
15
|
+
* ```vtt
|
|
16
|
+
* <v Fred></v> (Fred)
|
|
17
|
+
* <lang en-US></lang> (en-US)
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
import { Token } from "./Token.js";
|
|
21
|
+
var TokenizerState;
|
|
22
|
+
(function (TokenizerState) {
|
|
23
|
+
TokenizerState[TokenizerState["DATA"] = 0] = "DATA";
|
|
24
|
+
TokenizerState[TokenizerState["HTML_CHARACTER_REFERENCE"] = 1] = "HTML_CHARACTER_REFERENCE";
|
|
25
|
+
TokenizerState[TokenizerState["TAG"] = 2] = "TAG";
|
|
26
|
+
TokenizerState[TokenizerState["START_TAG"] = 3] = "START_TAG";
|
|
27
|
+
TokenizerState[TokenizerState["START_TAG_CLASS"] = 4] = "START_TAG_CLASS";
|
|
28
|
+
TokenizerState[TokenizerState["START_TAG_ANNOTATION"] = 5] = "START_TAG_ANNOTATION"; /** Content in tag */
|
|
29
|
+
TokenizerState[TokenizerState["HTML_CHARACTER_REFERENCE_ANNOTATION"] = 6] = "HTML_CHARACTER_REFERENCE_ANNOTATION"; /** HTML Entity in annotation */
|
|
30
|
+
TokenizerState[TokenizerState["END_TAG"] = 7] = "END_TAG";
|
|
31
|
+
TokenizerState[TokenizerState["TIMESTAMP_TAG"] = 8] = "TIMESTAMP_TAG";
|
|
32
|
+
})(TokenizerState || (TokenizerState = {}));
|
|
33
|
+
const SHARED_DOM_PARSER = new DOMParser();
|
|
34
|
+
export class Tokenizer {
|
|
35
|
+
constructor(rawContent) {
|
|
36
|
+
this.rawContent = rawContent;
|
|
37
|
+
/** Character index in the content */
|
|
38
|
+
this.cursor = 0;
|
|
39
|
+
/** Next token index in the content */
|
|
40
|
+
this.startPoint = 0;
|
|
41
|
+
}
|
|
42
|
+
static isWhitespace(character) {
|
|
43
|
+
return character == " " || character == "\x09" || character == "\x0C";
|
|
44
|
+
}
|
|
45
|
+
static isNewLine(character) {
|
|
46
|
+
return character == "\x0A";
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Attempts to convert a set of character (a supposed HTML Entity)
|
|
50
|
+
* to the correct character, by accumulating its char code;
|
|
51
|
+
*
|
|
52
|
+
* @param source
|
|
53
|
+
* @param currentCursor
|
|
54
|
+
* @param additionalAllowedCharacters
|
|
55
|
+
* @returns
|
|
56
|
+
*/
|
|
57
|
+
static parseHTMLEntity(source, currentCursor, additionalAllowedCharacters = []) {
|
|
58
|
+
if (!(source === null || source === void 0 ? void 0 : source.length)) {
|
|
59
|
+
return ["", currentCursor];
|
|
60
|
+
}
|
|
61
|
+
let cursor = currentCursor;
|
|
62
|
+
let result = "";
|
|
63
|
+
/**
|
|
64
|
+
* This partial implementation, compared to Chromium's implementation
|
|
65
|
+
* is due to my lack of understanding and laziness of everything
|
|
66
|
+
* that concerns Unicode characters conversion. I mean, wth is this?
|
|
67
|
+
* It is okay for me until it is just matter of following parsing
|
|
68
|
+
* and states, but when it comes to decimals and unicodes... I'm out.
|
|
69
|
+
* It is better to use a DOMParser, even if it might be a little bit
|
|
70
|
+
* slower than native implementation (or... maybe not?).
|
|
71
|
+
*
|
|
72
|
+
* Maybe one day I'll try to understand this whole world.
|
|
73
|
+
* Right now, I'm going to integrate only the basic logic.
|
|
74
|
+
*
|
|
75
|
+
* @see https://github.com/chromium/chromium/blob/c4d3c31083a2e1481253ff2d24298a1dfe19c754/third_party/blink/renderer/core/html/parser/html_entity_parser.cc#L107
|
|
76
|
+
*/
|
|
77
|
+
while (cursor < source.length) {
|
|
78
|
+
const char = source[cursor];
|
|
79
|
+
const maybeHTMLEntity = "&" + result + char;
|
|
80
|
+
if (Tokenizer.isWhitespace(char) ||
|
|
81
|
+
Tokenizer.isNewLine(char) ||
|
|
82
|
+
char === "<" ||
|
|
83
|
+
char === "&" ||
|
|
84
|
+
additionalAllowedCharacters.includes(char)) {
|
|
85
|
+
/**
|
|
86
|
+
* Not a valid HTMLEntity. Returning what we
|
|
87
|
+
* discovered, so it can be appended to the result
|
|
88
|
+
*/
|
|
89
|
+
return [maybeHTMLEntity, cursor];
|
|
90
|
+
}
|
|
91
|
+
if (char === ";") {
|
|
92
|
+
return [
|
|
93
|
+
SHARED_DOM_PARSER.parseFromString(maybeHTMLEntity, "text/html").documentElement
|
|
94
|
+
.textContent || maybeHTMLEntity,
|
|
95
|
+
cursor,
|
|
96
|
+
];
|
|
97
|
+
}
|
|
98
|
+
result += char;
|
|
99
|
+
cursor++;
|
|
100
|
+
}
|
|
101
|
+
return ["&" + result, cursor];
|
|
102
|
+
}
|
|
103
|
+
// ************************ //
|
|
104
|
+
// *** INSTANCE METHODS *** //
|
|
105
|
+
// ************************ //
|
|
106
|
+
nextToken() {
|
|
107
|
+
if (this.cursor >= this.rawContent.length) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
/** Our token starts at this index of the raw content */
|
|
111
|
+
this.startPoint = this.cursor;
|
|
112
|
+
let state = TokenizerState.DATA;
|
|
113
|
+
let result = "";
|
|
114
|
+
/**
|
|
115
|
+
* Buffer is an additional container for data
|
|
116
|
+
* that should be associated to result but should
|
|
117
|
+
* not belong to the same content
|
|
118
|
+
*/
|
|
119
|
+
let buffer = "";
|
|
120
|
+
const classes = [];
|
|
121
|
+
while (this.cursor <= this.rawContent.length) {
|
|
122
|
+
const char = this.rawContent[this.cursor];
|
|
123
|
+
switch (state) {
|
|
124
|
+
case TokenizerState.DATA: {
|
|
125
|
+
if (char === "&") {
|
|
126
|
+
state = TokenizerState.HTML_CHARACTER_REFERENCE;
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
if (char === "<") {
|
|
130
|
+
if (!result.length) {
|
|
131
|
+
state = TokenizerState.TAG;
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
return Token.String(result, { start: this.startPoint, end: this.cursor });
|
|
135
|
+
}
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
if (this.cursor === this.rawContent.length) {
|
|
139
|
+
return Token.String(result, { start: this.startPoint, end: this.cursor });
|
|
140
|
+
}
|
|
141
|
+
result += char;
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
case TokenizerState.HTML_CHARACTER_REFERENCE: {
|
|
145
|
+
const [content, nextCursor] = Tokenizer.parseHTMLEntity(this.rawContent, this.cursor);
|
|
146
|
+
result += content;
|
|
147
|
+
this.cursor = nextCursor;
|
|
148
|
+
state = TokenizerState.DATA;
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
case TokenizerState.TAG: {
|
|
152
|
+
if (Tokenizer.isWhitespace(char) || Tokenizer.isNewLine(char)) {
|
|
153
|
+
state = TokenizerState.START_TAG_ANNOTATION;
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
if (char === ".") {
|
|
157
|
+
state = TokenizerState.START_TAG_CLASS;
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
if (char === "/") {
|
|
161
|
+
state = TokenizerState.END_TAG;
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
if (!Number.isNaN(parseInt(char))) {
|
|
165
|
+
state = TokenizerState.TIMESTAMP_TAG;
|
|
166
|
+
result += char;
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
if (this.cursor === this.rawContent.length) {
|
|
170
|
+
this.cursor++;
|
|
171
|
+
return Token.StartTag(result, { start: this.startPoint, end: this.cursor }, classes);
|
|
172
|
+
}
|
|
173
|
+
state = TokenizerState.START_TAG;
|
|
174
|
+
result += char;
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
case TokenizerState.START_TAG: {
|
|
178
|
+
if (Tokenizer.isWhitespace(char)) {
|
|
179
|
+
state = TokenizerState.START_TAG_ANNOTATION;
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
if (Tokenizer.isNewLine(char)) {
|
|
183
|
+
buffer += char;
|
|
184
|
+
state = TokenizerState.START_TAG_ANNOTATION;
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
if (char === ".") {
|
|
188
|
+
state = TokenizerState.START_TAG_CLASS;
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
if (char === ">" || this.cursor === this.rawContent.length) {
|
|
192
|
+
this.cursor++;
|
|
193
|
+
return Token.StartTag(result, { start: this.startPoint, end: this.cursor }, classes);
|
|
194
|
+
}
|
|
195
|
+
result += char;
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
case TokenizerState.START_TAG_CLASS: {
|
|
199
|
+
if (Tokenizer.isWhitespace(char)) {
|
|
200
|
+
classes.push(buffer);
|
|
201
|
+
buffer = "";
|
|
202
|
+
state = TokenizerState.START_TAG_ANNOTATION;
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
if (Tokenizer.isNewLine(char)) {
|
|
206
|
+
buffer += char;
|
|
207
|
+
state = TokenizerState.START_TAG_ANNOTATION;
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
if (char === ".") {
|
|
211
|
+
classes.push(buffer);
|
|
212
|
+
buffer = "";
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
if (char === ">" || this.cursor === this.rawContent.length) {
|
|
216
|
+
this.cursor++;
|
|
217
|
+
classes.push(buffer);
|
|
218
|
+
return Token.StartTag(result, { start: this.startPoint, end: this.cursor }, classes);
|
|
219
|
+
}
|
|
220
|
+
buffer += char;
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
case TokenizerState.START_TAG_ANNOTATION: {
|
|
224
|
+
if (char === "&") {
|
|
225
|
+
state = TokenizerState.HTML_CHARACTER_REFERENCE_ANNOTATION;
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
if (char === ">" || this.cursor === this.rawContent.length) {
|
|
229
|
+
this.cursor++;
|
|
230
|
+
return Token.StartTag(result, { start: this.startPoint, end: this.cursor }, classes,
|
|
231
|
+
/** \x20 is classic space (U+0020 SPACE character) */
|
|
232
|
+
buffer.trim().replace(/\s+/g, "\x20").split("\x20"));
|
|
233
|
+
}
|
|
234
|
+
buffer += char;
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
case TokenizerState.HTML_CHARACTER_REFERENCE_ANNOTATION: {
|
|
238
|
+
const [content, nextCursor] = Tokenizer.parseHTMLEntity(this.rawContent, this.cursor, [
|
|
239
|
+
">",
|
|
240
|
+
]);
|
|
241
|
+
buffer += content;
|
|
242
|
+
this.cursor = nextCursor;
|
|
243
|
+
state = TokenizerState.START_TAG_ANNOTATION;
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
case TokenizerState.END_TAG: {
|
|
247
|
+
/**
|
|
248
|
+
* Reminder: this will be accessed only if we have found a
|
|
249
|
+
* "/" first in a Tag state
|
|
250
|
+
*/
|
|
251
|
+
if (char === ">" || this.cursor === this.rawContent.length) {
|
|
252
|
+
this.cursor++;
|
|
253
|
+
return Token.EndTag(result, { start: this.startPoint, end: this.cursor });
|
|
254
|
+
}
|
|
255
|
+
result += char;
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
case TokenizerState.TIMESTAMP_TAG: {
|
|
259
|
+
if (char === ">" || this.cursor === this.rawContent.length) {
|
|
260
|
+
this.cursor++;
|
|
261
|
+
return Token.TimestampTag(result, { start: this.startPoint, end: this.cursor });
|
|
262
|
+
}
|
|
263
|
+
if (Tokenizer.isWhitespace(char)) {
|
|
264
|
+
/**
|
|
265
|
+
* Timestamp is incomplete and not a timestamp tag.
|
|
266
|
+
* TIMESTAMP_TAG can be accessed only from TAG and, before,
|
|
267
|
+
* from DATA.
|
|
268
|
+
*/
|
|
269
|
+
state = TokenizerState.DATA;
|
|
270
|
+
}
|
|
271
|
+
result += char;
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
this.cursor++;
|
|
276
|
+
}
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
//# sourceMappingURL=Tokenizer.js.map
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as WebVTTAdapter } from "./Adapter.js";
|
package/lib/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sub37/webvtt-adapter",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "A subtitles adapter for WebVTT subtitles",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"peerDependencies": {
|
|
8
|
+
"@sub37/server": "^1.0.0"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "rm -rf lib && npx tsc -p tsconfig.build.json",
|
|
12
|
+
"test": "npm run build && npm --prefix ../.. run test",
|
|
13
|
+
"prepublishOnly": "npm run build"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/alexandercerutti/sub37.git"
|
|
18
|
+
},
|
|
19
|
+
"author": "Alexander P. Cerutti <cerutti.alexander@gmail.com>",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/alexandercerutti/sub37/issues"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/alexandercerutti/sub37#readme",
|
|
25
|
+
"files": [
|
|
26
|
+
"lib/**/*.+(js|d.ts)!(*.map)"
|
|
27
|
+
]
|
|
28
|
+
}
|