Skip to content

Commit

Permalink
Merge 63d03ed into 3323479
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromsilvapt committed Jul 18, 2018
2 parents 3323479 + 63d03ed commit 3b02716
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 32 deletions.
55 changes: 40 additions & 15 deletions dist/subtitle.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,33 +371,58 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
* @return {Array} subtitles
*/

function parse(srtOrVtt) {
function parse(srtOrVtt, options) {
if (!srtOrVtt) return [];
options = options || {
skipInvalidCaptions: false,
errorHandler: null,
skipContiguousErrors: true
};

var source = srtOrVtt.trim().concat('\n').replace(/\r\n/g, '\n').replace(/\n{3,}/g, '\n\n').replace(/^WEBVTT.*\n{2}/, '').split('\n');

var lastWasSuccessful = true;

return source.reduce(function (captions, row, index) {
var caption = captions[captions.length - 1];

if (!caption.index) {
if (/^\d+$/.test(row)) {
caption.index = parseInt(row, 10);
try {
if (!caption.index) {
if (/^\d+$/.test(row)) {
caption.index = parseInt(row, 10);
return captions;
}
}

if (!caption.hasOwnProperty('start')) {
Object.assign(caption, (0, _parseTimestamps2.default)(row));
return captions;
}
}

if (!caption.hasOwnProperty('start')) {
Object.assign(caption, (0, _parseTimestamps2.default)(row));
return captions;
}
if (row === '') {
delete caption.index;
if (index !== source.length - 1) {
captions.push({});
}
} else {
caption.text = caption.text ? caption.text + '\n' + row : row;
}

lastWasSuccessful = true;
} catch (error) {
if (!options.skipInvalidCaptions) {
throw error;
}

if (row === '') {
delete caption.index;
if (index !== source.length - 1) {
captions.push({});
if (lastWasSuccessful || !options.skipContiguousErrors) {
if (typeof options.errorHandler === 'function') {
options.errorHandler({ index: index, row: row, error: error });
}
}
} else {
caption.text = caption.text ? caption.text + '\n' + row : row;

lastWasSuccessful = false;

captions[captions.length - 1] = {};
}

return captions;
Expand Down
59 changes: 42 additions & 17 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import parseTimestamps from './parseTimestamps'
* @return {Array} subtitles
*/

export default function parse (srtOrVtt) {
export default function parse (srtOrVtt, options) {
if (!srtOrVtt) return []
options = options || {
skipInvalidCaptions: false,
errorHandler: null,
skipContiguousErrors: true
}

const source = srtOrVtt
.trim()
Expand All @@ -21,30 +26,50 @@ export default function parse (srtOrVtt) {
.replace(/^WEBVTT.*\n{2}/, '')
.split('\n')

var lastWasSuccessful = true

return source.reduce((captions, row, index) => {
const caption = captions[captions.length - 1]

if (!caption.index) {
if (/^\d+$/.test(row)) {
caption.index = parseInt(row, 10)
try {
if (!caption.index) {
if (/^\d+$/.test(row)) {
caption.index = parseInt(row, 10)
return captions
}
}

if (!caption.hasOwnProperty('start')) {
Object.assign(caption, parseTimestamps(row))
return captions
}
}

if (!caption.hasOwnProperty('start')) {
Object.assign(caption, parseTimestamps(row))
return captions
}
if (row === '') {
delete caption.index
if (index !== source.length - 1) {
captions.push({})
}
} else {
caption.text = caption.text
? caption.text + '\n' + row
: row
}

lastWasSuccessful = true
} catch (error) {
if (!options.skipInvalidCaptions) {
throw error
}

if (row === '') {
delete caption.index
if (index !== source.length - 1) {
captions.push({})
if (lastWasSuccessful || !options.skipContiguousErrors) {
if (typeof options.errorHandler === 'function') {
options.errorHandler({index: index, row: row, error: error})
}
}
} else {
caption.text = caption.text
? caption.text + '\n' + row
: row

lastWasSuccessful = false

captions[captions.length - 1] = {}
}

return captions
Expand Down
28 changes: 28 additions & 0 deletions test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,31 @@ Hi.`
t.deepEqual(value, expected)
})

test('parse text that is partially malformed', t => {
const srt = `
1
00:00:00,000 --> 00:00:00,100Something something something... dark side
3
2
00:00:00,100 --> 00:00:00,200
Hi.`
const value = parse(srt, {
skipInvalidCaptions: true,
errorHandler: function (error) {
console.log(error)
},
skipContiguousErrors: true
})

const expected = [
{
start: 100,
end: 200,
text: 'Hi.'
}
]

t.deepEqual(value, expected)
})

0 comments on commit 3b02716

Please sign in to comment.