Skip to content

Instantly share code, notes, and snippets.

@scripting
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scripting/4bf21443fe47bb709989 to your computer and use it in GitHub Desktop.
Save scripting/4bf21443fe47bb709989 to your computer and use it in GitHub Desktop.
A routine that builds on Dan MacTough's opmlparser package for node.js. I think this should be part of what's released in opmlparser. It's a higher level interface, and much easier for the app that's using opmlparser to deal with. It actually generates a JavaScript tree structure from the OPML.
function readOpml (urloutline, callback) {
var outlineArray = new Array ();
var req = request (urloutline);
var opmlparser = new opmlParser ();
req.on ("response", function (res) {
var stream = this;
if (res.statusCode == 200) {
stream.pipe (opmlparser);
}
});
req.on ("error", function (res) {
});
opmlparser.on ("error", function (error) {
console.log ("readOpml: opml parser error == " + error.message);
});
opmlparser.on ("readable", function () {
var outline;
while (outline = this.read ()) {
var ix = Number (outline ["#id"]);
outlineArray [ix] = outline;
}
});
opmlparser.on ("end", function () {
for (var i = 0; i < outlineArray.length; i++) {
var obj = outlineArray [i];
if (obj) {
var idparent = obj ["#parentid"];
if (idparent) {
var parent = outlineArray [idparent];
if (parent.subs == undefined) {
parent.subs = new Array ();
}
parent.subs [parent.subs.length] = obj;
}
delete obj ["#id"];
delete obj ["#parentid"];
}
}
if (callback != undefined) {
callback (outlineArray [1]);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment