From d5c84a8a5a6d30835af1ac3318bc8db9abb47f6d Mon Sep 17 00:00:00 2001 From: Laurens Laman Date: Fri, 7 Aug 2015 15:32:12 +0200 Subject: [PATCH 1/7] Update jquery.wikipedia.2.0.js fixed issue when wikipedia returns error --- js/jquery.wikipedia.2.0.js | 334 +++++++++++++++++++------------------ 1 file changed, 170 insertions(+), 164 deletions(-) diff --git a/js/jquery.wikipedia.2.0.js b/js/jquery.wikipedia.2.0.js index 4f159e8..7a8fe4e 100644 --- a/js/jquery.wikipedia.2.0.js +++ b/js/jquery.wikipedia.2.0.js @@ -7,184 +7,190 @@ * Version 1.0 : Marcel Grolms - www.suabo.de 2013 * Version 2.0 : Laurens Laman - https://github.com/LauLaman * - */ + */ (function($){ - //-- declare var in global scope - window.Wikipedia = {}; - var Wiki = { - settings: { - showTitle: false, - showInfo: true, - maxThumbnails: 0, - cutFirstInfoTableRows: 0, - maxInfoTableRows: 0, - thumbMaxWidth : '180px', - thumbMaxHeight: '180px', - locale: 'nl', - removeIPA: true, - descriptionLinks: true, - redirect: true, - elements: { - loader : '
Loading...
', - title : ['
', '
'], - description : ['
', '
'], - logo : [''], - gallery: { - parent: [''], - child: ['
  • ', '
  • '], + //-- declare var in global scope + window.Wikipedia = {}; + var Wiki = { + settings: { + showTitle: false, + showInfo: true, + maxThumbnails: 0, + cutFirstInfoTableRows: 0, + maxInfoTableRows: 0, + thumbMaxWidth : '180px', + thumbMaxHeight: '180px', + locale: 'nl', + removeIPA: true, + descriptionLinks: true, + redirect: true, + elements: { + loader : '
    Loading...
    ', + title : ['
    ', '
    '], + description : ['
    ', '
    '], + logo : [''], + gallery: { + parent: [''], + child: ['
  • ', '
  • '], + } + } + }, + article: { + logo: false, + gallery:[], + info:[] } - } - }, - article: { - logo: false, - gallery:[], - info:[] - } - }; + }; - Wikipedia = { - strRemove: function(theTarget, theString) { - return $("
    ").append( - $(theTarget, theString).remove().end() - ).html(); - }, - Get: function(wikipediaPage, options, callback){ - //check if pagetitle is set - if(wikipediaPage == undefined) { - console.log('Kein Wikipedia Suchtitel!Keine Abfrage gestartet!'); - return; - } - if(options != undefined) { - //we got more options - $.extend(Wiki.settings, options); - } - Wiki.article = { - gallery:[], - info:[] - }; - - $.getJSON('http://'+Wiki.settings.locale+'.wikipedia.org/w/api.php?action=parse&format=json&callback=?', {page:wikipediaPage, prop:'text|images', uselang: Wiki.settings.locale}, function(parsedata) { - var data = $(parsedata.parse.text["*"]).wrap('
    ').parent(); - //-- is this a rediredct page? - if(parsedata.parse.text["*"].substring(0,3) == '

    ' || parsedata.parse.text["*"].substring(0,25) == '

    ') { - //-- is there only 1 redirection? - if ($(data).find('ul').first().find('li').length==1 && Wiki.settings.redirect) { - var wikipediaPage = $(data).find('a').first().attr('href'); - //-- extract page from link - if (parsedata.parse.text["*"].substring(0,25) == '
    ') { - wikipediaPage = wikipediaPage.split('title='); - wikipediaPage = wikipediaPage[wikipediaPage.length-1].split('&'); - wikipediaPage = wikipediaPage[0]; - } else { - wikipediaPage = wikipediaPage.split('/'); - wikipediaPage = wikipediaPage[wikipediaPage.length-1]; + Wikipedia = { + strRemove: function(theTarget, theString) { + return $("
    ").append( + $(theTarget, theString).remove().end() + ).html(); + }, + Get: function(wikipediaPage, options, callback){ + //check if pagetitle is set + if(wikipediaPage == undefined) { + console.log('Kein Wikipedia Suchtitel!Keine Abfrage gestartet!'); + return; } - Wikipedia.Get(wikipediaPage, options, callback); - callback = null; //-- prevent callback from firing twice - } + if(options != undefined) { + //we got more options + $.extend(Wiki.settings, options); + } + Wiki.article = { + gallery:[], + info:[] + }; - Wiki.article.description = Wikipedia.strRemove("sup", $(data).find('p').first()) + "
    "+ Wikipedia.strRemove("sup", $(data).find('ul').first()); - - } else { - Wiki.article.title = $(data).find('.infobox tr:first-child th').text(); - if (typeof $(data).find('.infobox .image img').attr('src') !== "undefined") { - Wiki.article.logo = 'http:'+ $(data).find('.infobox .image img').attr('src'); - } else { - Wiki.article.logo = false; - } - Wiki.article.ipa = $(data).find('.IPA').text(); - Wiki.article.description = $(data).find('p').first(); - //-- remove superscript text - Wiki.article.description = Wikipedia.strRemove("sup", Wiki.article.description); - if (Wiki.settings.removeIPA) { - Wiki.article.description = Wikipedia.strRemove(".IPA", Wiki.article.description); - Wiki.article.description = Wiki.article.description.replace('[]', ''); - } - if (!Wiki.settings.descriptionLinks) { - Wiki.article.description = $(Wiki.article.description).text(); - } - $.each(data.find('.thumb a.image img'), function(index, image){ - Wiki.article.gallery.push('http:'+$(image).attr('src')) - }); + $.getJSON('http://'+Wiki.settings.locale+'.wikipedia.org/w/api.php?action=parse&format=json&callback=?', {page:wikipediaPage, prop:'text|images', uselang: Wiki.settings.locale}, function(parsedata) { + //-- check if response returns error + if (typeof parsedata.error !== "undefined") { + Wiki.article.title = parsedata.error.code + Wiki.article.description = parsedata.error.info; + } else { + var data = $(parsedata.parse.text["*"]).wrap('
    ').parent(); + //-- is this a rediredct page? + if (parsedata.parse.text["*"].substring(0, 3) == '

    ' || parsedata.parse.text["*"].substring(0, 25) == '

    ') { + //-- is there only 1 redirection? + if ($(data).find('ul').first().find('li').length == 1 && Wiki.settings.redirect) { + var wikipediaPage = $(data).find('a').first().attr('href'); + //-- extract page from link + if (parsedata.parse.text["*"].substring(0, 25) == '
    ') { + wikipediaPage = wikipediaPage.split('title='); + wikipediaPage = wikipediaPage[wikipediaPage.length - 1].split('&'); + wikipediaPage = wikipediaPage[0]; + } else { + wikipediaPage = wikipediaPage.split('/'); + wikipediaPage = wikipediaPage[wikipediaPage.length - 1]; + } + Wikipedia.Get(wikipediaPage, options, callback); + callback = null; //-- prevent callback from firing twice + } - $.each($(data).find('table.infobox tr'), function(index, element) { - if($(element).has('td')){ - Wiki.article.info.push({ - name: $(element).find('th').text(), - info: $(element).find('td').text() - }); - } - }); - } + Wiki.article.description = Wikipedia.strRemove("sup", $(data).find('p').first()) + "
    " + Wikipedia.strRemove("sup", $(data).find('ul').first()); + + } else { + Wiki.article.title = $(data).find('.infobox tr:first-child th').text(); + if (typeof $(data).find('.infobox .image img').attr('src') !== "undefined") { + Wiki.article.logo = 'http:' + $(data).find('.infobox .image img').attr('src'); + } else { + Wiki.article.logo = false; + } + Wiki.article.ipa = $(data).find('.IPA').text(); + Wiki.article.description = $(data).find('p').first(); + //-- remove superscript text + Wiki.article.description = Wikipedia.strRemove("sup", Wiki.article.description); + if (Wiki.settings.removeIPA) { + Wiki.article.description = Wikipedia.strRemove(".IPA", Wiki.article.description); + Wiki.article.description = Wiki.article.description.replace('[]', ''); + } + if (!Wiki.settings.descriptionLinks) { + Wiki.article.description = $(Wiki.article.description).text(); + } + $.each(data.find('.thumb a.image img'), function (index, image) { + Wiki.article.gallery.push('http:' + $(image).attr('src')) + }); + + $.each($(data).find('table.infobox tr'), function (index, element) { + if ($(element).has('td')) { + Wiki.article.info.push({ + name: $(element).find('th').text(), + info: $(element).find('td').text() + }); + } + }); + } + } - if (typeof callback == 'function') { // make sure the callback is a function - callback.call(this); // brings the scope to the callback - } else{ - return Wiki.article; + if (typeof callback == 'function') { // make sure the callback is a function + callback.call(this); // brings the scope to the callback + } else { + return Wiki.article; + } + }); } - }); - } - }; - - $.fn.Wikipedia = function(wikipediaPage, options){ - var wikiContainer = this; - wikiContainer.html(''); - wikiContainer.append(Wiki.settings.elements.loader); - Wikipedia.Get(wikipediaPage, options, function(){ - wikiContainer.html(''); - if(Wiki.settings.showTitle) { - wikiContainer.append(Wiki.settings.elements.title[0]+Wiki.article.title+Wiki.settings.elements.title[1]); - } + }; - wikiContainer.append(Wiki.settings.elements.description[0]+Wiki.article.description+Wiki.settings.elements.description[1]); - if (Wiki.settings.descriptionLinks) { - $.each($(wikiContainer).find('.wikipediaDescription a'), function(index, element) { - var href = $(element).attr('href'); - console.log(element); - if(href.indexOf("#") >= 0){ - //-- remove anchor - $(element).parent('li').remove(); - } else { - //-- is the link like index.php?title= - if(href.indexOf("php") >= 0){ - //-- check if page exist - if ($(element).hasClass('new')){ - $(element).replaceWith($(element).text()); - } else { - href = href.split('title='); - href = href[href.length-1].split('&'); - href = href[0]; - } - } else { - href = href.split('/'); - href = href[href.length-1]; - } - if (href) { - href = 'javascript:$(\''+wikiContainer.selector+'\').Wikipedia(\''+href+'\');'; - $(element).attr('href', href); + $.fn.Wikipedia = function(wikipediaPage, options){ + var wikiContainer = this; + wikiContainer.html(''); + wikiContainer.append(Wiki.settings.elements.loader); + Wikipedia.Get(wikipediaPage, options, function(){ + wikiContainer.html(''); + if (Wiki.settings.showTitle) { + wikiContainer.append(Wiki.settings.elements.title[0]+Wiki.article.title+Wiki.settings.elements.title[1]); } - } - }); - } + wikiContainer.append(Wiki.settings.elements.description[0]+Wiki.article.description+Wiki.settings.elements.description[1]); + if (Wiki.settings.descriptionLinks) { + $.each($(wikiContainer).find('.wikipediaDescription a'), function(index, element) { + var href = $(element).attr('href'); + console.log(element); + if(href.indexOf("#") >= 0){ + //-- remove anchor + $(element).parent('li').remove(); + } else { + //-- is the link like index.php?title= + if (href.indexOf("php") >= 0){ + //-- check if page exist + if ($(element).hasClass('new')){ + $(element).replaceWith($(element).text()); + } else { + href = href.split('title='); + href = href[href.length-1].split('&'); + href = href[0]; + } + } else { + href = href.split('/'); + href = href[href.length-1]; + } + if (href) { + href = 'javascript:$(\''+wikiContainer.selector+'\').Wikipedia(\''+href+'\');'; + $(element).attr('href', href); + } + } - if (Wiki.article.logo && Wiki.article.logo) { - wikiContainer.append(Wiki.settings.elements.logo[0]+''+Wiki.settings.elements.logo[1]); - } + }); + } - if (Wiki.settings.maxThumbnails > 0 && Wiki.article.gallery.length > 0) { - var data = ''; - $.each(Wiki.article.gallery, function(index, image){ - data += Wiki.settings.elements.gallery.child[0]+''+ Wiki.settings.elements.gallery.child[1]; - if ((index+1) >= Wiki.settings.maxThumbnails) { - return false; - } + if (Wiki.article.logo && Wiki.article.logo) { + wikiContainer.append(Wiki.settings.elements.logo[0]+''+Wiki.settings.elements.logo[1]); + } + + if (Wiki.settings.maxThumbnails > 0 && Wiki.article.gallery.length > 0) { + var data = ''; + $.each(Wiki.article.gallery, function(index, image){ + data += Wiki.settings.elements.gallery.child[0]+''+ Wiki.settings.elements.gallery.child[1]; + if ((index+1) >= Wiki.settings.maxThumbnails) { + return false; + } + }); + $(data).find('img').css({'width': 'auto', 'height': 'auto', 'max-width': Wiki.settings.thumbMaxWidth, 'max-height': Wiki.settings.thumbMaxHeight}).wrap('
  • ') + wikiContainer.append(Wiki.settings.elements.gallery.parent[0]+data+Wiki.settings.elements.gallery.parent[1]); + } }); - $(data).find('img').css({'width': 'auto', 'height': 'auto', 'max-width': Wiki.settings.thumbMaxWidth, 'max-height': Wiki.settings.thumbMaxHeight}).wrap('
  • ') - wikiContainer.append(Wiki.settings.elements.gallery.parent[0]+data+Wiki.settings.elements.gallery.parent[1]); - } - }); - } + } })(jQuery); From 8221c176c4443bb7c921607ddbf4b3374f8a8554 Mon Sep 17 00:00:00 2001 From: Laurens Laman Date: Fri, 7 Aug 2015 15:59:50 +0200 Subject: [PATCH 2/7] Update jquery.wikipedia.2.0.js fixed issue with locale for example nl_be does not work for wikipedia but nl works --- js/jquery.wikipedia.2.0.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/js/jquery.wikipedia.2.0.js b/js/jquery.wikipedia.2.0.js index 7a8fe4e..0d57af1 100644 --- a/js/jquery.wikipedia.2.0.js +++ b/js/jquery.wikipedia.2.0.js @@ -62,7 +62,7 @@ gallery:[], info:[] }; - + $.getJSON('http://'+Wiki.settings.locale+'.wikipedia.org/w/api.php?action=parse&format=json&callback=?', {page:wikipediaPage, prop:'text|images', uselang: Wiki.settings.locale}, function(parsedata) { //-- check if response returns error if (typeof parsedata.error !== "undefined") { @@ -136,6 +136,9 @@ var wikiContainer = this; wikiContainer.html(''); wikiContainer.append(Wiki.settings.elements.loader); + if (typeof options.locale !== "undefined" && options.locale.indexOf('_') > -1) { + options.locale = options.locale.substr(0, options.locale.length - (options.locale.indexOf('_')+1)); + } Wikipedia.Get(wikipediaPage, options, function(){ wikiContainer.html(''); if (Wiki.settings.showTitle) { From 6d290b594513b2446afbcc92c204687e9226dfb9 Mon Sep 17 00:00:00 2001 From: Laurens Laman Date: Mon, 10 Aug 2015 10:26:07 +0200 Subject: [PATCH 3/7] Update and rename jquery.wikipedia.2.0.js to jquery.wikipedia.2.0.1.js --- js/{jquery.wikipedia.2.0.js => jquery.wikipedia.2.0.1.js} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename js/{jquery.wikipedia.2.0.js => jquery.wikipedia.2.0.1.js} (99%) diff --git a/js/jquery.wikipedia.2.0.js b/js/jquery.wikipedia.2.0.1.js similarity index 99% rename from js/jquery.wikipedia.2.0.js rename to js/jquery.wikipedia.2.0.1.js index 0d57af1..f5f6277 100644 --- a/js/jquery.wikipedia.2.0.js +++ b/js/jquery.wikipedia.2.0.1.js @@ -1,5 +1,5 @@ /* - * jQuery Plugin - Wikipedia V2.0 + * jQuery Plugin - Wikipedia V2.0.1 * * todo: add description here * From 7896fafaa191512ed7cc6e8ccb238863308d0b66 Mon Sep 17 00:00:00 2001 From: Laurens Laman Date: Mon, 10 Aug 2015 10:28:24 +0200 Subject: [PATCH 4/7] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 91343c5..0b631b8 100644 --- a/README.md +++ b/README.md @@ -10,5 +10,5 @@ Demo: http://support.suabo.de/opensource/jquery-plugin-wikipedia ## Documentation [Version 1.0](https://github.com/LauLamanApps/Wikipedia/blob/master/documentation/version%201.0.md) -[Version 2.0](https://github.com/LauLamanApps/Wikipedia/blob/master/documentation/version%202.0.md#version-20) +[Version 2.0.1](https://github.com/LauLamanApps/Wikipedia/blob/master/documentation/version%202.0.1.md#version-20) From 64d673f600a9a9e28cf44f9eb48a6b40509ddef3 Mon Sep 17 00:00:00 2001 From: Laurens Laman Date: Mon, 10 Aug 2015 10:28:58 +0200 Subject: [PATCH 5/7] Update and rename version 2.0.md to version 2.0.1.md --- documentation/{version 2.0.md => version 2.0.1.md} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename documentation/{version 2.0.md => version 2.0.1.md} (98%) diff --git a/documentation/version 2.0.md b/documentation/version 2.0.1.md similarity index 98% rename from documentation/version 2.0.md rename to documentation/version 2.0.1.md index c0e8fee..2bd4097 100644 --- a/documentation/version 2.0.md +++ b/documentation/version 2.0.1.md @@ -1,11 +1,11 @@ -## Version 2.0 +## Version 2.0.1 ## Usage 1- Include the Javascript resources into you pages - + 2- Add a placeholder for the widget to your page From feaa31047e9e170ed47c1ce0ea3547aa2e57e76a Mon Sep 17 00:00:00 2001 From: Laurens Laman Date: Mon, 10 Aug 2015 10:32:41 +0200 Subject: [PATCH 6/7] Update README.md changed absolute links to relative links --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0b631b8..3866e96 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ jQuery plugin for easy download of data from Wikipedia about the Wikipedia API u Demo: http://support.suabo.de/opensource/jquery-plugin-wikipedia ## Documentation -[Version 1.0](https://github.com/LauLamanApps/Wikipedia/blob/master/documentation/version%201.0.md) +[Version 1.0](documentation/version%201.0.md) -[Version 2.0.1](https://github.com/LauLamanApps/Wikipedia/blob/master/documentation/version%202.0.1.md#version-20) +[Version 2.0.1](version%202.0.1.md#version-20) From 20d9d6792c0a478e9d4525ca4e53d140e96f6bb6 Mon Sep 17 00:00:00 2001 From: Laurens Laman Date: Mon, 10 Aug 2015 10:36:26 +0200 Subject: [PATCH 7/7] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3866e96..0761720 100644 --- a/README.md +++ b/README.md @@ -10,5 +10,5 @@ Demo: http://support.suabo.de/opensource/jquery-plugin-wikipedia ## Documentation [Version 1.0](documentation/version%201.0.md) -[Version 2.0.1](version%202.0.1.md#version-20) +[Version 2.0.1](documentation/version%202.0.1.md#version-20)