forked from SuperFlyTV/node-inews
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinewsStoryParser.js
130 lines (114 loc) · 2.71 KB
/
inewsStoryParser.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
module.exports = function(nsml, callback) {
var htmlparser = require('htmlparser');
var camelcase = require('camelcase');
var unescape = require('unescape');
var parseHandler = new htmlparser.DefaultHandler(function (error, dom) {
if (error)
callback(error, null);
else
callback(null, parseNsml(dom));
});
var parser = new htmlparser.Parser(parseHandler);
parser.parseComplete(nsml);
function parseNsml(nodes, story) {
if(typeof story === 'undefined') {
story = {
fields: {},
meta: {},
cues: []
};
}
nodes.forEach(function(node) {
parseNsmlNode(node, story);
});
return story;
}
function stringifyNodes(nodes) {
var nodeStr = "";
if(Array.isArray(nodes)) {
nodes.forEach(function (node) {
nodeStr += stringifyNode(node);
});
}
return nodeStr;
}
function stringifyNode(node) {
if(node.type === 'text')
return node.data;
else if(node.type === 'tag') {
var nodeStr = "<" + node.name;
var attrStr = stringifyAttributes(node.attribs);
if(attrStr.length > 0)
nodeStr += " " + attrStr;
nodeStr += ">";
nodeStr += stringifyNodes(node.children);
nodeStr += "</" + node.name + ">";
}
return nodeStr;
}
function stringifyAttributes(attributes) {
var attrStr = "";
for (var key in attributes) {
if(attrStr.length > 0)
attrStr += " ";
attrStr += key + "=\"" + attributes[key].replace(/\"/g,'\\"') + "\"";
}
return attrStr;
}
function nodesToArray(nodes, tag) {
var lines = [];
nodes.forEach(function(node, index) {
if(node.type === 'tag') {
if (!node.children) {
return;
}
if(node.name === tag) {
lines.push(unescape(stringifyNodes(node.children)));
}
lines = lines.concat(nodesToArray(node.children, tag));
}
});
// Filter out leading lines in production cues
lines = lines.filter(function(line, index) {
return line > 0 || line != ']] S3.0 G 0 [[';
});
return lines;
}
function parseNsmlNode(node, story) {
if(node.type === 'tag') {
switch(node.name) {
case 'ae':
try {
var id = node.attribs['id'];
story.cues[id] = nodesToArray(node.children, 'ap');
}
catch(error) {}
break;
case 'body':
story.body = unescape(stringifyNodes(node.children));
break;
case 'meta':
story.meta = node.attribs;
break;
case 'storyid':
try {
story.id = node.children[0]['data'];
}
catch(error) {}
break;
case 'f':
try {
var key = node.attribs['id'];
var val = node.children[0]['data'];
story.fields[camelcase(key)] = unescape(val);
}
catch(error) {}
break;
default:
if(Array.isArray(node.children))
parseNsml(node.children, story);
break;
}
}
}
};