From af9f9c4450aad31b369afa0ae07475cc464d551e Mon Sep 17 00:00:00 2001 From: Mottie Date: Sat, 6 Sep 2014 22:30:53 -0500 Subject: [PATCH] Parser: Added ignoreArticlesExcept option to ignoreArticles parser --- docs/example-parsers-ignore-articles.html | 101 +++++++++++++++------- js/parsers/parser-ignore-articles.js | 28 ++++-- 2 files changed, 92 insertions(+), 37 deletions(-) diff --git a/docs/example-parsers-ignore-articles.html b/docs/example-parsers-ignore-articles.html index 7de6c801..54d87ace 100644 --- a/docs/example-parsers-ignore-articles.html +++ b/docs/example-parsers-ignore-articles.html @@ -8,6 +8,8 @@ + + @@ -28,7 +30,8 @@ headers: { 2: { sorter: 'ignoreArticles', - ignoreArticles : 'en' + ignoreArticles : 'en', + ignoreArticlesExcept : '' } } }); @@ -45,20 +48,54 @@
-

- NOTE! -

+
+ +

Demo


@@ -379,7 +419,8 @@ $(function() { headers: { 2: { sorter: 'ignoreArticles', - ignoreArticles : 'fr' + ignoreArticles : 'fr', + ignoreArticlesExcept : '' } } }); diff --git a/js/parsers/parser-ignore-articles.js b/js/parsers/parser-ignore-articles.js index d8429807..e84ed0d0 100644 --- a/js/parsers/parser-ignore-articles.js +++ b/js/parsers/parser-ignore-articles.js @@ -7,8 +7,10 @@ ;(function($){ "use strict"; +var ts = $.tablesorter; + // basic list from http://en.wikipedia.org/wiki/Article_%28grammar%29 - $.tablesorter.ignoreArticles = { + ts.ignoreArticles = { "en" : "the, a, an", "de" : "der, die, das, des, dem, den, ein, eine, einer, eines, einem, einen", "nl" : "de, het, de, een", @@ -24,22 +26,34 @@ // and then set the language id 'xx' in the headers option // ignoreArticles : 'xx' - $.tablesorter.addParser({ + ts.addParser({ id: 'ignoreArticles', is: function() { return false; }, format: function(s, table, cell, cellIndex) { - var c = table.config, art, lang; + var art, ignore, lang, + c = table.config, + str = s || ''; if ( !(c.headers && c.headers[cellIndex] && c.headers[cellIndex].ignoreArticlesRegex) ) { - // initialize - save regex in c.headers[cellIndex].ignoreArticles + // initialize - save regex in c.headers[cellIndex].ignoreArticlesRegex if (!c.headers) { c.headers = {}; } if (!c.headers[cellIndex]) { c.headers[cellIndex] = {}; } - lang = $.tablesorter.getData(c.$headers.eq(cellIndex), c.headers[cellIndex], 'ignoreArticles'); - art = ($.tablesorter.ignoreArticles[lang] || "the, a, an" ) + ""; + lang = ts.getData( c.$headers.eq(cellIndex), ts.getColumnData( table, c.headers, cellIndex ), 'ignoreArticles' ); + art = (ts.ignoreArticles[lang] || "the, a, an" ) + ""; c.headers[cellIndex].ignoreArticlesRegex = new RegExp('^(' + $.trim( art.split(/\s*\,\s*/).join('\\s|') + "\\s" ).replace("_\\s","") + ')', 'i'); + // exception regex stored in c.headers[cellIndex].ignoreArticlesRegex2 + ignore = ts.getData( c.$headers.eq(cellIndex), ts.getColumnData( table, c.headers, cellIndex ), 'ignoreArticlesExcept' ); + c.headers[cellIndex].ignoreArticlesRegex2 = ignore !== '' ? new RegExp('^(' + ignore.replace(/\s/g, "\\s") + ')', 'i') : ''; } - return (s || '').replace(c.headers[cellIndex].ignoreArticlesRegex, ''); + art = c.headers[cellIndex].ignoreArticlesRegex; + if (art.test(str)) { + ignore = c.headers[cellIndex].ignoreArticlesRegex2; + if ( !(ignore && ignore.test(str)) ) { + return str.replace(art, ''); + } + } + return str; }, type: 'text' });