'use strict'; export = main; import xml2js = require('xml2js'); import typings = require('../../typings'); /** * Converts an input buffer to a SubStation Alpha subtitle. */ function main(input: string|Buffer, done: (err: Error, subtitle?: string) => void) { xml2js.parseString(input.toString(), { explicitArray: false, explicitRoot: false }, (err: Error, xml: typings.ISubtitle) => { if (err) return done(err); try { done(null, script(xml) + '\n' + style(xml.styles) + '\n' + event(xml.events)); } catch (err) { done(err); } }); } /** * Converts the event block. */ function event(block: typings.ISubtitleEvent): string { var format = 'Layer,Start,End,Style,Name,MarginL,MarginR,MarginV,Effect,Text'; return '[Events]\n' + 'Format: ' + format + '\n' + block.event.map(style => ('Dialogue: 0,' + style.$.start + ',' + style.$.end + ',' + style.$.style + ',' + style.$.name + ',' + style.$.margin_l + ',' + style.$.margin_r + ',' + style.$.margin_v + ',' + style.$.effect + ',' + style.$.text)).join('\n'); } /** * Converts the script block. */ function script(block: typings.ISubtitle): string { return '[Script Info]\n' + 'Title: ' + block.$.title + '\n' + 'ScriptType: v4.00+\n' + 'WrapStyle: ' + block.$.wrap_style + '\n' + 'PlayResX: ' + block.$.play_res_x + '\n' + 'PlayResY: ' + block.$.play_res_y + '\n' + 'Subtitle ID: ' + block.$.id + '\n' + 'Language: ' + block.$.lang_string + '\n' + 'Created: ' + block.$.created; } /** * Converts the style block. */ function style(block: typings.ISubtitleStyle): string { var format = 'Name,Fontname,Fontsize,PrimaryColour,SecondaryColour,' + 'OutlineColour,BackColour,Bold,Italic,Underline,StrikeOut,ScaleX,' + 'ScaleY,Spacing,Angle,BorderStyle,Outline,Shadow,Alignment,' + 'MarginL,MarginR,MarginV,Encoding'; return '[V4+ Styles]\n' + 'Format: ' + format + '\n' + block.style.map(style => 'Style: ' + style.$.name + ',' + style.$.font_name + ',' + style.$.font_size + ',' + style.$.primary_colour + ',' + style.$.secondary_colour + ',' + style.$.outline_colour + ',' + style.$.back_colour + ',' + style.$.bold + ',' + style.$.italic + ',' + style.$.underline + ',' + style.$.strikeout + ',' + style.$.scale_x + ',' + style.$.scale_y + ',' + style.$.spacing + ',' + style.$.angle + ',' + style.$.border_style + ',' + style.$.outline + ',' + style.$.shadow + ',' + style.$.alignment + ',' + style.$.margin_l + ',' + style.$.margin_r + ',' + style.$.margin_v + ',' + style.$.encoding).join('\n'); }