-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
66 lines (50 loc) · 1.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict';
var execAll = require('regexp.execall')
, fzip = require('fzip')
, flatmap = require('flatmap');
var parseDescriptor = require('./src/descriptor');
/**
* Parse section lines.
*
* "Format" descriptor (if there is one) specifies format for subsequent
* lines in the same section.
*
* @arg {string[]} lines
* @arg {Object} [options]
* @return {Object}
*/
var parseSection = function (lines, options) {
options = options || {};
// Format descriptor for subsequent section lines.
var format = null;
return flatmap(lines, function (line) {
var descriptor = parseDescriptor(line, format);
if (!descriptor) {
// Empty line.
return null;
}
if (descriptor.type == 'comment' && !options.comments) {
return null;
}
if (!format && descriptor.key == 'Format') {
format = descriptor.value;
}
return [descriptor];
});
};
var parseAss = function (text, options) {
text = text.toString();
var sections = execAll(/^\s*\[(.*)\]\s*$/mg, text);
return fzip(sections, sections.slice(1), function (section, nextSection) {
var sectionName = section[1];
var begin = section.index + section[0].length + 1;
var end = nextSection ? nextSection.index : text.length;
var lines = text.slice(begin, end).split('\n');
return {
section: sectionName,
body: parseSection(lines, options)
};
});
};
module.exports = parseAss;
module.exports.default = parseAss;