Last active
August 29, 2015 14:15
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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