UNPKG

crunchy

Version:

Crunchy is a fork of Crunchyroll.js, capable of downloading anime episodes from the popular CrunchyRoll streaming service.

71 lines 1.93 kB
'use strict'; exports.__esModule = true; var xml2js = require("xml2js"); /** * Converts an input buffer to a SubRip subtitle. */ function default_1(config, input, done) { var options = { explicitArray: false, explicitRoot: false }; xml2js.parseString(input.toString(), options, function (err, xml) { try { if (err) { return done(err); } done(null, xml.events.event.map(function (event, index) { var attributes = event.$; return (index + 1) + '\n' + time(attributes.start) + ' --> ' + time(attributes.end) + '\n' + text(attributes.text) + '\n'; }).join('\n')); } catch (err) { done(err); } }); } exports["default"] = default_1; /** * Prefixes a value. */ function prefix(value, length) { while (value.length < length) { value = '0' + value; } return value; } /** * Suffixes a value. */ function suffix(value, length) { while (value.length < length) { value = value + '0'; } return value; } /** * Formats a text value. */ function text(value) { return value .replace(/{\\i1}/g, '<i>').replace(/{\\i0}/g, '</i>') .replace(/{\\b1}/g, '<b>').replace(/{\\b0}/g, '</b>') .replace(/{\\u1}/g, '<u>').replace(/{\\u0}/g, '</u>') .replace(/{[^}]+}/g, '') .replace(/(\s+)?\\n(\s+)?/ig, '\n') .trim(); } /** * Formats a time stamp. */ function time(value) { var all = value.match(/^([0-9]+):([0-9]+):([0-9]+)\.([0-9]+)$/); if (!all) { throw new Error('Invalid time.'); } var hours = prefix(all[1], 2); var minutes = prefix(all[2], 2); var seconds = prefix(all[3], 2); var milliseconds = suffix(all[4], 3); return hours + ':' + minutes + ':' + seconds + ',' + milliseconds; } //# sourceMappingURL=srt.js.map