aboutsummaryrefslogtreecommitdiff
path: root/node_modules/htmlparser2/lib/FeedHandler.js
diff options
context:
space:
mode:
authorruki <waruqi@gmail.com>2018-11-08 00:38:48 +0800
committerruki <waruqi@gmail.com>2018-11-07 21:53:09 +0800
commit26105034da4fcce7ac883c899d781f016559310d (patch)
treec459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/htmlparser2/lib/FeedHandler.js
parent2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff)
downloadxmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz
xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip
switch to vuepress
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 00000000..8b7d32cd
--- /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;