Sophivorus has uploaded a new change for review.

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

Change subject: Support for nested templates
......................................................................

Support for nested templates

Bug: T148235
Change-Id: I8b8895247e1c29ab938c6ff333bf6b9080914d9e
---
M proveit.js
1 file changed, 46 insertions(+), 15 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikipedia/gadgets/ProveIt 
refs/changes/26/318126/1

diff --git a/proveit.js b/proveit.js
index 5484e80..5c2fb5c 100755
--- a/proveit.js
+++ b/proveit.js
@@ -340,29 +340,57 @@
                        });
                        reference.template = template;
 
-                       // Extract the parameters and normalize them
-                       var paramsArray = match[2].split( '|' ),
-                               paramString, indexOfEqual, paramName, 
paramValue;
+                       /**
+                        * Now it's time to parse the parameters:
+                        * {{Cite book
+                        * |value1
+                        * |param1 = value2
+                        * |param2 = [[Some|link]]
+                        * |param3 = {{Subtemplate |foo |bar=baz}}
+                        * }}
+                        */
 
-                       paramsArray.shift(); // The first element is always 
empty
+                       // We split by pipe, knowing that we may match pipes 
inside links and subtemplates
+                       var paramArray = match[2].split( '|' ),
+                               paramString, inLink = 0, inSubtemplate = 0, 
indexOfEqual, paramNumber = 0, paramName, paramValue;
 
-                       for ( var i = 0; i < paramsArray.length; i++ ) {
-                               paramString = $.trim( paramsArray[ i ] );
-                               indexOfEqual = paramString.indexOf( '=' );
+                       paramArray.shift(); // Get rid of the stuff before the 
first pipe
 
-                               // If there's no = sign, it means we matched a 
pipe inside a link or template in the previous run of the loop
-                               // for example |param=[[Joe|Doe]] or 
|param={{Some|template}}
-                               // so we append the current paramString to the 
PREVIOUS paramValue
-                               if ( indexOfEqual === -1 ) {
+                       for ( var i = 0; i < paramArray.length; i++ ) {
+                               paramString = $.trim( paramArray[ i ] );
+
+                               // If we're in a link or subtemplate, we append 
the current paramString to the previous paramValue
+                               if ( inLink || inSubtemplate ) {
                                        reference.params[ paramName ] += '|' + 
paramString;
+                                       if ( paramString.indexOf( ']]' ) > -1 ) 
{
+                                               inLink--;
+                                       }
+                                       if ( paramString.indexOf( '}}' ) > -1 ) 
{
+                                               inSubtemplate--;
+                                       }
+                                       continue;
+                               }
+
+                               // If there's no = sign, it's an anonymous 
parameter
+                               indexOfEqual = paramString.indexOf( '=' );
+                               if ( indexOfEqual === -1 ) {
+                                       paramNumber++;
+                                       paramName = paramNumber;
+                                       paramValue = paramString;
                                        continue;
                                }
 
                                paramName = $.trim( paramString.substring( 0, 
indexOfEqual ) );
                                paramValue = $.trim( paramString.substring( 
indexOfEqual + 1 ) );
 
-                               if ( !paramName || !paramValue ) {
-                                       continue; // Malformed param, maybe 
"|foo=" or "|=bar"
+                               // If we find "[[" or "{{" in the paramValue, 
it means there's a link or subtemplate
+                               // so we flag it to ignore all pipes and equal 
signs in future runs of the loop
+                               // until all links and subtemplates are closed
+                               if ( paramValue.indexOf( '[[' ) > -1 ) {
+                                       inLink++;
+                               }
+                               if ( paramValue.indexOf( '{{' ) > -1 ) {
+                                       inSubtemplate++;
                                }
 
                                reference.params[ paramName ] = paramValue;
@@ -749,10 +777,13 @@
                                if ( 'main' in templateMap && templateMap.main 
in this.params ) {
                                        mainValue = this.params[ 
templateMap.main ];
                                } else {
-                                       var templateData = 
this.getTemplateData();
-                                       for ( var paramName in 
templateData.params ) {
+                                       var templateData = 
this.getTemplateData(),
+                                               paramName;
+                                       for ( var i = 0; i < 
templateData.paramOrder.length; i++ ) {
+                                               paramName = 
templateData.paramOrder[ i ];
                                                if ( paramName in this.params ) 
{
                                                        mainValue = 
this.params[ paramName ];
+                                                       break;
                                                }
                                        }
                                }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8b8895247e1c29ab938c6ff333bf6b9080914d9e
Gerrit-PatchSet: 1
Gerrit-Project: wikipedia/gadgets/ProveIt
Gerrit-Branch: master
Gerrit-Owner: Sophivorus <[email protected]>

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

Reply via email to