Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #3 from FB55/master

Some enhancements
  • Loading branch information...
commit e1ea93d4a8fa4d539a0c877e290dd7d7c5bd5bb9 2 parents 3a3f47d + 994e1fa
Andris Reinman andris9 authored
Showing with 89 additions and 83 deletions.
  1. +89 −83 lib/nodepie.js
172 lib/nodepie.js
View
@@ -66,26 +66,38 @@ NodePie.NS = {
* Hash map to convert HTML entities into unicode symbols
**/
NodePie.HTMLEntities = {
- nbsp: " ", iexcl: "¡", cent: "¢", pound: "£", curren: "¤",
- yen: "¥", brvbar: "¦", sect: "§", uml: "¨", copy: "©",
- ordf: "ª", laquo: "«", not: "¬", reg: "®", macr: "¯",
- deg: "°", plusmn: "±", sup2: "²", sup3: "³", acute: "´",
- micro: "µ", para: "", middot: "·", cedil: "¸", sup1: "¹",
- ordm: "º", raquo: "»", frac14: "¼", frac12: "½", frac34: "¾",
- iquest: "¿", times: "×", divide: "÷", Agrave: "À",
- Aacute: "Á", Acirc: "Â", Atilde: "Ã", Auml: "Ä",
- Aring: "Å", AElig: "Æ", Ccedil: "Ç", Egrave: "È",
- Eacute: "É", Ecirc: "Ê", Euml: "Ë", Igrave: "Ì", Iacute: "Í",
- Icirc: "Î", Iuml: "Ï", ETH: "Ð", Ntilde: "Ñ", Ograve: "Ò",
- Oacute: "Ó", Ocirc: "Ô", Otilde: "Õ", Ouml: "Ö", Oslash: "Ø",
- Ugrave: "Ù", Uacute: "Ú", Ucirc: "Û", Uuml: "Ü", Yacute: "Ý",
- THORN: "Þ", szlig: "ß", agrave: "à", aacute: "á", acirc: "â",
- atilde: "ã", auml: "ä", aring: "å", aelig: "æ", ccedil: "ç",
- egrave: "è", eacute: "é", ecirc: "ê", euml: "ë", igrave: "ì",
- iacute: "í", icirc: "î", iuml: "ï", eth: "ð", ntilde: "ñ",
- ograve: "ò", oacute: "ó", ocirc: "ô", otilde: "õ",
- ouml: "ö", oslash: "ø", ugrave: "ù", uacute: "ú",
- ucirc: "û", uuml: "ü", yacute: "ý", thorn: "þ", yuml: "ÿ"
+ apos:0x0027,quot:0x0022,amp:0x0026,lt:0x003C,gt:0x003E,nbsp:0x00A0,iexcl:0x00A1,cent:0x00A2,pound:0x00A3,
+ curren:0x00A4,yen:0x00A5,brvbar:0x00A6,sect:0x00A7,uml:0x00A8,copy:0x00A9,ordf:0x00AA,laquo:0x00AB,
+ not:0x00AC,shy:0x00AD,reg:0x00AE,macr:0x00AF,deg:0x00B0,plusmn:0x00B1,sup2:0x00B2,sup3:0x00B3,
+ acute:0x00B4,micro:0x00B5,para:0x00B6,middot:0x00B7,cedil:0x00B8,sup1:0x00B9,ordm:0x00BA,raquo:0x00BB,
+ frac14:0x00BC,frac12:0x00BD,frac34:0x00BE,iquest:0x00BF,Agrave:0x00C0,Aacute:0x00C1,Acirc:0x00C2,Atilde:0x00C3,
+ Auml:0x00C4,Aring:0x00C5,AElig:0x00C6,Ccedil:0x00C7,Egrave:0x00C8,Eacute:0x00C9,Ecirc:0x00CA,Euml:0x00CB,
+ Igrave:0x00CC,Iacute:0x00CD,Icirc:0x00CE,Iuml:0x00CF,ETH:0x00D0,Ntilde:0x00D1,Ograve:0x00D2,Oacute:0x00D3,
+ Ocirc:0x00D4,Otilde:0x00D5,Ouml:0x00D6,times:0x00D7,Oslash:0x00D8,Ugrave:0x00D9,Uacute:0x00DA,Ucirc:0x00DB,
+ Uuml:0x00DC,Yacute:0x00DD,THORN:0x00DE,szlig:0x00DF,agrave:0x00E0,aacute:0x00E1,acirc:0x00E2,atilde:0x00E3,
+ auml:0x00E4,aring:0x00E5,aelig:0x00E6,ccedil:0x00E7,egrave:0x00E8,eacute:0x00E9,ecirc:0x00EA,euml:0x00EB,
+ igrave:0x00EC,iacute:0x00ED,icirc:0x00EE,iuml:0x00EF,eth:0x00F0,ntilde:0x00F1,ograve:0x00F2,oacute:0x00F3,
+ ocirc:0x00F4,otilde:0x00F5,ouml:0x00F6,divide:0x00F7,oslash:0x00F8,ugrave:0x00F9,uacute:0x00FA,ucirc:0x00FB,
+ uuml:0x00FC,yacute:0x00FD,thorn:0x00FE,yuml:0x00FF,OElig:0x0152,oelig:0x0153,Scaron:0x0160,scaron:0x0161,
+ Yuml:0x0178,fnof:0x0192,circ:0x02C6,tilde:0x02DC,Alpha:0x0391,Beta:0x0392,Gamma:0x0393,Delta:0x0394,
+ Epsilon:0x0395,Zeta:0x0396,Eta:0x0397,Theta:0x0398,Iota:0x0399,Kappa:0x039A,Lambda:0x039B,Mu:0x039C,
+ Nu:0x039D,Xi:0x039E,Omicron:0x039F,Pi:0x03A0,Rho:0x03A1,Sigma:0x03A3,Tau:0x03A4,Upsilon:0x03A5,
+ Phi:0x03A6,Chi:0x03A7,Psi:0x03A8,Omega:0x03A9,alpha:0x03B1,beta:0x03B2,gamma:0x03B3,delta:0x03B4,
+ epsilon:0x03B5,zeta:0x03B6,eta:0x03B7,theta:0x03B8,iota:0x03B9,kappa:0x03BA,lambda:0x03BB,mu:0x03BC,
+ nu:0x03BD,xi:0x03BE,omicron:0x03BF,pi:0x03C0,rho:0x03C1,sigmaf:0x03C2,sigma:0x03C3,tau:0x03C4,
+ upsilon:0x03C5,phi:0x03C6,chi:0x03C7,psi:0x03C8,omega:0x03C9,thetasym:0x03D1,upsih:0x03D2,piv:0x03D6,
+ ensp:0x2002,emsp:0x2003,thinsp:0x2009,zwnj:0x200C,zwj:0x200D,lrm:0x200E,rlm:0x200F,ndash:0x2013,
+ mdash:0x2014,lsquo:0x2018,rsquo:0x2019,sbquo:0x201A,ldquo:0x201C,rdquo:0x201D,bdquo:0x201E,dagger:0x2020,
+ Dagger:0x2021,bull:0x2022,hellip:0x2026,permil:0x2030,prime:0x2032,Prime:0x2033,lsaquo:0x2039,rsaquo:0x203A,
+ oline:0x203E,frasl:0x2044,euro:0x20AC,image:0x2111,weierp:0x2118,real:0x211C,trade:0x2122,alefsym:0x2135,
+ larr:0x2190,uarr:0x2191,rarr:0x2192,darr:0x2193,harr:0x2194,crarr:0x21B5,lArr:0x21D0,uArr:0x21D1,
+ rArr:0x21D2,dArr:0x21D3,hArr:0x21D4,forall:0x2200,part:0x2202,exist:0x2203,empty:0x2205,nabla:0x2207,
+ isin:0x2208,notin:0x2209,ni:0x220B,prod:0x220F,sum:0x2211,minus:0x2212,lowast:0x2217,radic:0x221A,
+ prop:0x221D,infin:0x221E,ang:0x2220,and:0x2227,or:0x2228,cap:0x2229,cup:0x222A,int:0x222B,
+ there4:0x2234,sim:0x223C,cong:0x2245,asymp:0x2248,ne:0x2260,equiv:0x2261,le:0x2264,ge:0x2265,
+ sub:0x2282,sup:0x2283,nsub:0x2284,sube:0x2286,supe:0x2287,oplus:0x2295,otimes:0x2297,perp:0x22A5,
+ sdot:0x22C5,lceil:0x2308,rceil:0x2309,lfloor:0x230A,rfloor:0x230B,lang:0x2329,rang:0x232A,loz:0x25CA,
+ spades:0x2660,clubs:0x2663,hearts:0x2665,diams:0x2666
};
/**
@@ -95,15 +107,9 @@ NodePie.HTMLEntities = {
* Decodes any HTML entities in a string into their unicode form
**/
NodePie._decodeHTMLEntities = function(text){
- return text.replace(/&#(\d{2,4});/g, function(o, nr){
- if(nr!="173"){ // keep ­
- return String.fromCharCode(nr);
- }else{
- return o;
- }
- }).replace(/&([a-zA-Z]+([0-9]+)?);/g, function(o, code){
- return NodePie.HTMLEntities[code] || o;
- });
+ return text.replace(/&(.+?);/g, function(str, ent){
+ return String.fromCharCode( ent[0]!='#' ? NodePie.HTMLEntities[ent] : ent[1]=='x' ? parseInt(ent.substr(2),16): parseInt(ent.substr(1)) )}
+ );
};
/**
@@ -125,11 +131,11 @@ NodePie._walkForNS = function(node, that, depth){
var keys = Object.keys(node), key;
for(var i=0, len = keys.length; i<len; i++){
key = keys[i];
- if(typeof node[key] == "string"){
- if(key.trim().substr(0,6).toLowerCase() == "xmlns:"){
+ if(typeof node[key] === "string"){
+ if(key.trim().substr(0,6).toLowerCase() === "xmlns:"){
that.namespaces[node[key]] = key.trim().substr(6);
}
- }else if(node[key] && typeof node[key] == "object"){
+ }else if(node[key] && typeof node[key] === "object"){
NodePie._walkForNS(node[key], that, depth+1);
}
}
@@ -188,39 +194,39 @@ NodePie.prototype._checkEncoding = function(xml){
* entry arrays etc.
**/
NodePie.prototype._checkType = function(){
- var root_keys = Object.keys(this.feed), key;
+ var root_keys = Object.keys(this.feed), key, type, elem;
for(var i=0, len = root_keys.length; i<len; i++){
key = root_keys[i];
+ elem = this.feed[key];
- if(typeof this.feed[key] != "object" || Array.isArray(this.feed[key])){
+ if(typeof elem !== "object" || Array.isArray(elem))
continue;
- }
- if(key.trim().toLowerCase() == "rdf" || key.trim().substr(-4).toLowerCase() == ":rdf"){
- this.feedType = "rdf";
- this.rootElement = this.feed[root_keys[i]];
- this.channelElement = this.rootElement.channel || {};
- this.itemsElement = this.rootElement.item || [];
- break;
- }
+ type = key.trim().toLowerCase();
- if(key.trim().toLowerCase() == "feed"){
+ if(type === "feed"){
this.feedType = "atom";
- this.rootElement = this.feed[root_keys[i]];
- this.channelElement = this.rootElement || {};
- this.itemsElement = this.rootElement.entry || [];
+ this.rootElement = elem;
+ this.channelElement = elem || {};
+ this.itemsElement = elem.entry || [];
break;
}
-
- if(key.trim().toLowerCase() == "rss"){
+ else if(type === "rss"){
this.feedType = "rss";
- this.rootElement = this.feed[root_keys[i]];
- this.channelElement = this.rootElement.channel || {};
+ this.rootElement = elem;
+ this.channelElement = elem.channel || {};
this.itemsElement = this.channelElement.item || [];
break;
}
+ else if(type === "rdf" || type.substr(-4) === ":rdf"){
+ this.feedType = "rdf";
+ this.rootElement = elem;
+ this.channelElement = elem.channel || {};
+ this.itemsElement = elem.item || [];
+ break;
+ }
}
if(!this.rootElement){
@@ -250,13 +256,13 @@ NodePie.prototype._parseContents = function(str){
return false;
}
- if(typeof str == "string"){
+ if(typeof str === "string"){
str = str.trim();
return str && this._formatStr(str) || false;
}
- if(typeof str == "object"){
- if(typeof str.$t == "string"){
+ if(typeof str === "object"){
+ if(typeof str.$t === "string"){
str.$t = str.$t.trim();
return str.$t && this._formatStr(str.$t) || false;
}
@@ -400,16 +406,16 @@ NodePie.prototype.getLink = function(rel, type){
return false;
}
- if(typeof link == "string"){
- if(rel == "alternate" && type == "text/html"){
+ if(typeof link === "string"){
+ if(rel === "alternate" && type === "text/html"){
return link.trim();
}else{
return false;
}
}
- if(typeof link == "object" && !Array.isArray(link)){
- if(rel == link.rel && (!link.type || type==link.type)){
+ if(typeof link === "object" && !Array.isArray(link)){
+ if(rel === link.rel && (!link.type || type==link.type)){
return link.href;
}else{
return false;
@@ -418,7 +424,7 @@ NodePie.prototype.getLink = function(rel, type){
if(Array.isArray(link)){
for(var i=0, len = link.length; i<len; i++){
- if(rel == link[i].rel && (!link[i].type || type==link[i].type)){
+ if(rel === link[i].rel && (!link[i].type || type==link[i].type)){
return link[i].href;
}
}
@@ -443,7 +449,7 @@ NodePie.prototype.getDate = function(){
var dcns = this.namespaces[NodePie.NS.DC], date;
date = this.channelElement.lastBuildDate || this.channelElement.updated ||
- (dcns && this.channelElement[dcns+":date"]) || false;
+ (dcns && this.channelElement[dcns+":date"]);
if(!date){
return false;
@@ -481,12 +487,11 @@ NodePie.prototype.getItemQuantity = function(max){
this._item_count = 0;
- if(!this.itemsElement){
- this._item_count = 0;
- }else if(Array.isArray(this.itemsElement)){
- this._item_count = this.itemsElement.length;
- }else if(typeof this.itemsElement == "object"){
- this._item_count = 1;
+ if(this.itemsElement){
+ if(Array.isArray(this.itemsElement))
+ this._item_count = this.itemsElement.length;
+ else if(typeof this.itemsElement === "object")
+ this._item_count = 1;
}
return max && max<this._item_count ? max : this._item_count;
@@ -501,17 +506,18 @@ NodePie.prototype.getItemQuantity = function(max){
**/
NodePie.prototype.getItems = function(start, length){
start = start || 0;
- length = length || this.getItemQuantity();
+ var quantity = this.getItemQuantity();
+ length = length || quantity;
- if(start >= this.getItemQuantity()){
- start = this.getItemQuantity()-1;
+ if(start >= quantity){
+ start = quantity-1;
if(start<0){
start = 0;
}
}
- if(length > this.getItemQuantity() - start){
- length = this.getItemQuantity() - start;
+ if(length > quantity - start){
+ length = quantity - start;
}
var items = [];
@@ -547,7 +553,7 @@ NodePie.prototype.getItem = function(i){
}
}
- if(typeof this.itemsElement == "object"){
+ if(typeof this.itemsElement === "object"){
if(i === 0){
this._items[i] = new NodePie.Item(this.itemsElement, this);
return this._items[i];
@@ -635,16 +641,16 @@ NodePie.Item.prototype.getLink = function(rel, type){
return false;
}
- if(typeof link == "string"){
- if(rel == "alternate" && type == "text/html"){
+ if(typeof link === "string"){
+ if(rel === "alternate" && type === "text/html"){
return link.trim();
}else{
return false;
}
}
- if(typeof link == "object" && !Array.isArray(link)){
- if(rel == link.rel && (!link.type || type==link.type)){
+ if(typeof link === "object" && !Array.isArray(link)){
+ if(rel === link.rel && (!link.type || type==link.type)){
return link.href;
}else{
return false;
@@ -653,7 +659,7 @@ NodePie.Item.prototype.getLink = function(rel, type){
if(Array.isArray(link)){
for(var i=0, len = link.length; i<len; i++){
- if(rel == link[i].rel && (!link[i].type || type==link[i].type)){
+ if(rel === link[i].rel && (!link[i].type || type==link[i].type)){
return link[i].href;
}
}
@@ -713,11 +719,11 @@ NodePie.Item.prototype.getAuthors = function(){
author = this.element[dcns+":creator"];
}
- if(typeof author == "string"){
+ if(typeof author === "string"){
return [this._parseAuthor(author)];
}
- if(typeof author == "object"){
+ if(typeof author === "object"){
if(Array.isArray(author)){
for(var i=0, len = author.length; i<len; i++){
if(author[i] && author[i].name){
@@ -725,7 +731,7 @@ NodePie.Item.prototype.getAuthors = function(){
}
}
return authors.length ? authors : false;
- }else if(typeof author.name == "string"){
+ }else if(typeof author.name === "string"){
return [this._parseAuthor(author.name)];
}
}
@@ -887,12 +893,12 @@ NodePie.Item.prototype.getCategories = function(){
return false;
}
- if(typeof category == "string"){
+ if(typeof category === "string"){
category = category.trim();
return category && [this._formatStr(category)] || false;
}
- if(typeof category == "object" && !Array.isArray(category)){
+ if(typeof category === "object" && !Array.isArray(category)){
category = category.term || category.$t || false;
if(!category){
return false;
@@ -903,13 +909,13 @@ NodePie.Item.prototype.getCategories = function(){
if(Array.isArray(category)){
for(var i=0, len = category.length; i<len; i++){
- if(typeof category[i] == "string"){
+ if(typeof category[i] === "string"){
if(category[i].trim()){
categories.push(category[i].trim());
}
continue;
}
- if(typeof category[i] == "object"){
+ if(typeof category[i] === "object"){
if((category[i].term || category[i].$t || "").trim()){
categories.push(this._formatStr((category[i].term || category[i].$t || "").trim()));
}
Please sign in to comment.
Something went wrong with that request. Please try again.