Phuedx has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/322626

Change subject: gateway: Remove parentheticals from extract
......................................................................

gateway: Remove parentheticals from extract

Extracted from ext.popups.renderer.article#removeParensFromText.

Change-Id: I200d431234c4b235a8621e1b261934b1282dd8e6
---
M resources/ext.popups/gateway.js
M tests/qunit/ext.popups/gateway.test.js
2 files changed, 76 insertions(+), 12 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Popups 
refs/changes/26/322626/1

diff --git a/resources/ext.popups/gateway.js b/resources/ext.popups/gateway.js
index 05d8bad..074e027 100644
--- a/resources/ext.popups/gateway.js
+++ b/resources/ext.popups/gateway.js
@@ -98,26 +98,81 @@
        }
 
        /**
-        * @private
-        *
         * Processes the extract returned by the TextExtracts MediaWiki API 
query
         * module.
         *
-        * @param {String} extract
-        * @return {String|undefined} If the extract is set, then, if it's 
there, the
-        *  trailing ellipsis is removed and the new extract returned; 
otherwise,
-        *  `undefined` is returned.
+        * @param {String|undefined} extract
+        * @return {String|undefined}
         */
        function processExtract( extract ) {
+               var result;
+
                if ( extract === undefined || extract === '' ) {
                        return undefined;
                }
 
-               if ( extract.length ) {
-                       extract = extract.replace( /\.\.\.$/, '' );
+               result = extract;
+               result = removeParentheticals( result );
+               result = removeEllipsis( result );
+
+               return result.length > 0 ? result : undefined;
+       }
+
+       /**
+        * Removes the trailing ellipsis from the extract, if it's there.
+        *
+        * This function was extracted from
+        * `mw.popups.renderer.article#removeEllipsis`.
+        *
+        * @param {String} extract
+        * @return {String}
+        */
+       function removeEllipsis( extract ) {
+               return extract.replace( /\.\.\.$/, '' );
+       }
+
+       /**
+        * Removes parentheticals from the extract.
+        *
+        * If the parenthesis are unbalanced or out of order, then the extract 
is
+        * returned without further processing.
+        *
+        * This function was extracted from
+        * `mw.popups.renderer.article#removeParensFromText`.
+        *
+        * @param {String} extract
+        * @return {String}
+        */
+       function removeParentheticals( extract ) {
+               var
+                       ch,
+                       result = '',
+                       level = 0,
+                       i = 0;
+
+               for ( i; i < extract.length; i++ ) {
+                       ch = extract.charAt( i );
+
+                       if ( ch === ')' && level === 0  ) {
+                               return extract;
+                       }
+                       if ( ch === '(' ) {
+                               level++;
+                               continue;
+                       } else if ( ch === ')' ) {
+                               level--;
+                               continue;
+                       }
+                       if ( level === 0 ) {
+                               // Remove leading spaces before brackets
+                               if ( ch === ' ' && extract.charAt( i + 1 ) === 
'(' ) {
+                                       continue;
+                               }
+                               result += ch;
+                       }
                }
 
-               return extract.length > 0 ? extract : undefined;
+               return ( level === 0 ) ? result : extract;
        }
 
 }( mediaWiki, jQuery ) );
diff --git a/tests/qunit/ext.popups/gateway.test.js 
b/tests/qunit/ext.popups/gateway.test.js
index 90552e7..de3cc0a 100644
--- a/tests/qunit/ext.popups/gateway.test.js
+++ b/tests/qunit/ext.popups/gateway.test.js
@@ -126,7 +126,7 @@
                                        width: 200
                                },
                                lastModified: new Date( '2016-11-10T00:14:14Z' 
),
-                               extract: 'Richard Paul "Rick" Astley 
(/\u02c8r\u026ak \u02c8\u00e6stli/; born 6 February 1966) is an English singer, 
songwriter, musician, and radio personality. His 1987 song, "Never Gonna Give 
You Up" was a No. 1 hit single in 25 countries. By the time of his retirement 
in 1993, Astley had sold approximately 40 million records worldwide.\nAstley 
made a comeback in 2007, becoming an Internet phenomenon when his video "Never 
Gonna Give You Up" became integral to the meme known as "rickrolling". Astley 
was voted "Best Act Ever" by Internet users at the',
+                               extract: 'Richard Paul "Rick" Astley is an 
English singer, songwriter, musician, and radio personality. His 1987 song, 
"Never Gonna Give You Up" was a No. 1 hit single in 25 countries. By the time 
of his retirement in 1993, Astley had sold approximately 40 million records 
worldwide.\nAstley made a comeback in 2007, becoming an Internet phenomenon 
when his video "Never Gonna Give You Up" became integral to the meme known as 
"rickrolling". Astley was voted "Best Act Ever" by Internet users at the',
                                isRecent: false
                        } );
 
@@ -134,12 +134,21 @@
                } );
        } );
 
-       QUnit.test( 'it handle empty-ish extracts', function ( assert ) {
+       QUnit.test( 'it handles extracts', function ( assert ) {
                var cases = [
+                         // removeEllipsis
                                [ '', undefined ],
                                [ 'Extract...', 'Extract' ],
                                [ 'Extract.', 'Extract.' ],
-                               [ '...', undefined ]
+                               [ '...', undefined ],
+
+                               // removeParentheticals
+                               [ 'Foo', 'Foo' ],
+                               [ 'Foo (', 'Foo (' ],
+                               [ 'Foo (Bar)', 'Foo' ],
+                               [ 'Foo (Bar))', 'Foo (Bar))' ],
+                               [ 'Foo )(Bar)', 'Foo )(Bar)' ],
+                               [ '(Bar)', undefined ],
                        ],
                        done = assert.async( cases.length );
 

-- 
To view, visit https://gerrit.wikimedia.org/r/322626
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I200d431234c4b235a8621e1b261934b1282dd8e6
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Popups
Gerrit-Branch: mpga
Gerrit-Owner: Phuedx <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to