ass.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict';
  2. export = main;
  3. import xml2js = require('xml2js');
  4. import typings = require('../../typings');
  5. /**
  6. * Converts an input buffer to a SubStation Alpha subtitle.
  7. */
  8. function main(input: string|Buffer, done: (err: Error, subtitle?: string) => void) {
  9. xml2js.parseString(input.toString(), {
  10. explicitArray: false,
  11. explicitRoot: false
  12. }, (err: Error, xml: typings.ISubtitle) => {
  13. if (err) return done(err);
  14. try {
  15. done(null, script(xml) + '\n' +
  16. style(xml.styles) + '\n' +
  17. event(xml.events));
  18. } catch (err) {
  19. done(err);
  20. }
  21. });
  22. }
  23. /**
  24. * Converts the event block.
  25. */
  26. function event(block: typings.ISubtitleEvent): string {
  27. var format = 'Layer,Start,End,Style,Name,MarginL,MarginR,MarginV,Effect,Text';
  28. return '[Events]\n' +
  29. 'Format: ' + format + '\n' +
  30. block.event.map(style => ('Dialogue: 0,' +
  31. style.$.start + ',' +
  32. style.$.end + ',' +
  33. style.$.style + ',' +
  34. style.$.name + ',' +
  35. style.$.margin_l + ',' +
  36. style.$.margin_r + ',' +
  37. style.$.margin_v + ',' +
  38. style.$.effect + ',' +
  39. style.$.text)).join('\n');
  40. }
  41. /**
  42. * Converts the script block.
  43. */
  44. function script(block: typings.ISubtitle): string {
  45. return '[Script Info]\n' +
  46. 'Title: ' + block.$.title + '\n' +
  47. 'ScriptType: v4.00+\n' +
  48. 'WrapStyle: ' + block.$.wrap_style + '\n' +
  49. 'PlayResX: ' + block.$.play_res_x + '\n' +
  50. 'PlayResY: ' + block.$.play_res_y + '\n' +
  51. 'Subtitle ID: ' + block.$.id + '\n' +
  52. 'Language: ' + block.$.lang_string + '\n' +
  53. 'Created: ' + block.$.created;
  54. }
  55. /**
  56. * Converts the style block.
  57. */
  58. function style(block: typings.ISubtitleStyle): string {
  59. var format = 'Name,Fontname,Fontsize,PrimaryColour,SecondaryColour,' +
  60. 'OutlineColour,BackColour,Bold,Italic,Underline,StrikeOut,ScaleX,' +
  61. 'ScaleY,Spacing,Angle,BorderStyle,Outline,Shadow,Alignment,' +
  62. 'MarginL,MarginR,MarginV,Encoding';
  63. return '[V4+ Styles]\n' +
  64. 'Format: ' + format + '\n' +
  65. block.style.map(style => 'Style: ' +
  66. style.$.name + ',' +
  67. style.$.font_name + ',' +
  68. style.$.font_size + ',' +
  69. style.$.primary_colour + ',' +
  70. style.$.secondary_colour + ',' +
  71. style.$.outline_colour + ',' +
  72. style.$.back_colour + ',' +
  73. style.$.bold + ',' +
  74. style.$.italic + ',' +
  75. style.$.underline + ',' +
  76. style.$.strikeout + ',' +
  77. style.$.scale_x + ',' +
  78. style.$.scale_y + ',' +
  79. style.$.spacing + ',' +
  80. style.$.angle + ',' +
  81. style.$.border_style + ',' +
  82. style.$.outline + ',' +
  83. style.$.shadow + ',' +
  84. style.$.alignment + ',' +
  85. style.$.margin_l + ',' +
  86. style.$.margin_r + ',' +
  87. style.$.margin_v + ',' +
  88. style.$.encoding).join('\n');
  89. }