aboutsummaryrefslogtreecommitdiff
path: root/node_modules/htmlparser2/lib/FeedHandler.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2017-05-03 15:35:00 +0200
committerFlorian Dold <florian.dold@gmail.com>2017-05-03 15:35:00 +0200
commitde98e0b232509d5f40c135d540a70e415272ff85 (patch)
treea79222a5b58484ab3b80d18efcaaa7ccc4769b33 /node_modules/htmlparser2/lib/FeedHandler.js
parente0c9d480a73fa629c1e4a47d3e721f1d2d345406 (diff)
node_modules
Diffstat (limited to 'node_modules/htmlparser2/lib/FeedHandler.js')
-rw-r--r--node_modules/htmlparser2/lib/FeedHandler.js95
1 files changed, 95 insertions, 0 deletions
diff --git a/node_modules/htmlparser2/lib/FeedHandler.js b/node_modules/htmlparser2/lib/FeedHandler.js
new file mode 100644
index 000000000..8b7d32cd0
--- /dev/null
+++ b/node_modules/htmlparser2/lib/FeedHandler.js
@@ -0,0 +1,95 @@
+var index = require("./index.js"),
+ DomHandler = index.DomHandler,
+ DomUtils = index.DomUtils;
+
+//TODO: make this a streamable handler
+function FeedHandler(callback, options){
+ this.init(callback, options);
+}
+
+require("util").inherits(FeedHandler, DomHandler);
+
+FeedHandler.prototype.init = DomHandler;
+
+function getElements(what, where){
+ return DomUtils.getElementsByTagName(what, where, true);
+}
+function getOneElement(what, where){
+ return DomUtils.getElementsByTagName(what, where, true, 1)[0];
+}
+function fetch(what, where, recurse){
+ return DomUtils.getText(
+ DomUtils.getElementsByTagName(what, where, recurse, 1)
+ ).trim();
+}
+
+function addConditionally(obj, prop, what, where, recurse){
+ var tmp = fetch(what, where, recurse);
+ if(tmp) obj[prop] = tmp;
+}
+
+var isValidFeed = function(value) {
+ return value === "rss" || value === "feed" || value === "rdf:RDF";
+};
+
+FeedHandler.prototype.onend = function() {
+ var feed = {},
+ feedRoot = getOneElement(isValidFeed, this.dom),
+ tmp, childs;
+
+ if (feedRoot) {
+ if(feedRoot.name === "feed"){
+ childs = feedRoot.children;
+
+ feed.type = "atom";
+ addConditionally(feed, "id", "id", childs);
+ addConditionally(feed, "title", "title", childs);
+ if((tmp = getOneElement("link", childs)) && (tmp = tmp.attribs) && (tmp = tmp.href)) feed.link = tmp;
+ addConditionally(feed, "description", "subtitle", childs);
+ if(tmp = fetch("updated", childs)) feed.updated = new Date(tmp);
+ addConditionally(feed, "author", "email", childs, true);
+
+ feed.items = getElements("entry", childs).map(function(item){
+ var entry = {}, tmp;
+
+ item = item.children;
+
+ addConditionally(entry, "id", "id", item);
+ addConditionally(entry, "title", "title", item);
+ if((tmp = getOneElement("link", item)) && (tmp = tmp.attribs) && (tmp = tmp.href)) entry.link = tmp;
+ addConditionally(entry, "description", "summary", item);
+ if(tmp = fetch("updated", item)) entry.pubDate = new Date(tmp);
+ return entry;
+ });
+ } else{
+ childs = getOneElement("channel", feedRoot.children).children;
+
+ feed.type = feedRoot.name.substr(0, 3);
+ feed.id = "";
+ addConditionally(feed, "title", "title", childs);
+ addConditionally(feed, "link", "link", childs);
+ addConditionally(feed, "description", "description", childs);
+ if(tmp = fetch("lastBuildDate", childs)) feed.updated = new Date(tmp);
+ addConditionally(feed, "author", "managingEditor", childs, true);
+
+ feed.items = getElements("item", feedRoot.children).map(function(item){
+ var entry = {}, tmp;
+
+ item = item.children;
+
+ addConditionally(entry, "id", "guid", item);
+ addConditionally(entry, "title", "title", item);
+ addConditionally(entry, "link", "link", item);
+ addConditionally(entry, "description", "description", item);
+ if(tmp = fetch("pubDate", item)) entry.pubDate = new Date(tmp);
+ return entry;
+ });
+ }
+ }
+ this.dom = feed;
+ DomHandler.prototype._handleCallback.call(
+ this, feedRoot ? null : Error("couldn't find root of feed")
+ );
+};
+
+module.exports = FeedHandler;