@@ -97,16 +97,27 @@ __TOC__`;
9797
9898 processDelistForArticle ( wikicode ) {
9999 const gaTemplateNames = [ 'ga icon' , 'ga article' , 'good article' ] ;
100+ const templateFinder = new TemplateFinder ( wikicode ) ;
100101 for ( const templateName of gaTemplateNames ) {
102+ const template = templateFinder . firstTemplate ( templateName ) ;
103+ if ( ! template ) {
104+ continue ;
105+ }
106+ const { previousSibling, nextSibling } = template ;
107+ template . remove ( ) ;
101108 // handle lots of line breaks: \n\n{{templateName}}\n\n -> \n\n
102- let regex = new RegExp ( '\\n\\n\\{\\{' + templateName + '\\}\\}\\n\\n' , 'i' ) ;
103- wikicode = wikicode . replace ( regex , '\n\n' ) ;
104-
105- // handle normal: {{templateName}}\n -> '', {{templateName}} -> ''
106- regex = new RegExp ( '\\{\\{' + templateName + '\\}\\}\\n?' , 'i' ) ;
107- wikicode = wikicode . replace ( regex , '' ) ;
109+ if (
110+ previousSibling && previousSibling . type === 'text' && previousSibling . data . endsWith ( '\n\n' ) &&
111+ nextSibling && nextSibling . type === 'text' && nextSibling . data . startsWith ( '\n\n' )
112+ ) {
113+ nextSibling . deleteData ( 0 , 2 ) ;
114+
115+ // handle normal: {{templateName}}\n -> '', {{templateName}} -> ''
116+ } else if ( nextSibling && nextSibling . type === 'text' && nextSibling . data . startsWith ( '\n' ) ) {
117+ nextSibling . deleteData ( 0 , 1 ) ;
118+ }
108119 }
109- return wikicode ;
120+ return templateFinder . getWikitext ( ) ;
110121 }
111122
112123 processDelistForGAList ( wikicode , articleToRemove ) {
@@ -288,8 +299,14 @@ __TOC__`;
288299 'weapons and buildings' : 'Wikipedia:Good articles/Warfare' ,
289300 weapons : 'Wikipedia:Good articles/Warfare'
290301 } ;
291- let topic = wikicode . match ( / (?: \{ \{ A r t i c l e ? h i s t o r y | \{ \{ G A \s * (? = \| ) ) .* ?\| \s * (?: s u b ) ? t o p i c \s * = \s * ( [ ^ | } \n ] + ) / is ) [ 1 ] ;
292- topic = topic . toLowerCase ( ) . trim ( ) ;
302+ const templateFinder = new TemplateFinder ( wikicode ) ;
303+ const templates = templateFinder . getTemplates ( [ 'Article history' , 'Articlehistory' , 'GA' ] ) ;
304+ const template = templates . find ( ( t ) => t . getArgs ( 'topic' ) . size || t . getArgs ( 'subtopic' ) . size ) ;
305+ let topic = template . getValue ( 'topic' ) ;
306+ if ( topic === undefined ) {
307+ topic = template . getValue ( 'subtopic' ) ;
308+ }
309+ topic = topic . toLowerCase ( ) ;
293310 const gaListTitle = dictionary [ topic ] ;
294311 // throw the error a little later rather than now. that way it doesn't interrupt modifying the article talk page.
295312 return gaListTitle ;
@@ -374,16 +391,9 @@ __TOC__`;
374391 }
375392
376393 removeTemplate ( templateName , wikicode ) {
377- const regex = new RegExp ( `\\{\\{${ this . regExEscape ( templateName ) } [^\\}]*\\}\\}\\n?` , 'i' ) ;
378- return wikicode . replace ( regex , '' ) ;
379- }
380-
381- regexGetFirstMatchString ( regex , haystack ) {
382- const matches = haystack . match ( regex ) ;
383- if ( matches !== null && matches [ 1 ] !== undefined ) {
384- return matches [ 1 ] ;
385- }
386- return null ;
394+ const templateFinder = new TemplateFinder ( wikicode ) ;
395+ templateFinder . deleteTemplate ( templateName ) ;
396+ return templateFinder . getWikitext ( ) ;
387397 }
388398
389399 /**
@@ -392,15 +402,15 @@ __TOC__`;
392402 convertGATemplateToArticleHistoryIfPresent ( talkPageTitle , wikicode ) {
393403 const templateFinder = new TemplateFinder ( wikicode ) ;
394404 const hasArticleHistory = templateFinder . hasTemplate ( 'Article ?history' ) ;
395- const gaTemplateWikicode = this . regexGetFirstMatchString ( / ( \{ \{ G A [ ^ } ] * \} \} ) / i , wikicode ) ;
396- if ( ! hasArticleHistory && gaTemplateWikicode ) {
405+ const gaTemplate = templateFinder . firstTemplate ( 'GA' ) ;
406+ if ( ! hasArticleHistory && gaTemplate ) {
397407 // delete {{ga}} template
398408 templateFinder . deleteTemplate ( 'GA' ) ;
399409 wikicode = templateFinder . getWikitext ( ) . trim ( ) ;
400410
401411 // parse its parameters
402412 // example: |21:00, 12 March 2017 (UTC)|topic=Sports and recreation|page=1|oldid=769997774
403- const parameters = this . getParametersFromTemplateWikicode ( gaTemplateWikicode ) ;
413+ const parameters = this . getParametersFromTemplateWikicode ( gaTemplate ) ;
404414
405415 // if no page specified, assume page is 1. so then the good article review link will be parsed as /GA1
406416 const noPageSpecified = parameters . page === undefined ;
@@ -547,28 +557,18 @@ __TOC__`;
547557 /**
548558 * @return {Object } Parameters, with keys being equivalent to the template parameter names. Unnamed parameters will be 1, 2, 3, etc.
549559 */
550- getParametersFromTemplateWikicode ( wikicodeOfSingleTemplate ) {
551- wikicodeOfSingleTemplate = wikicodeOfSingleTemplate . slice ( 2 , - 2 ) ; // remove {{ and }}
552- // TODO: explode without exploding | inside of inner templates
553- const strings = wikicodeOfSingleTemplate . split ( '|' ) ;
560+ getParametersFromTemplateWikicode ( template ) {
561+ if ( typeof template === 'string' ) {
562+ const templateFinder = new TemplateFinder ( template ) ;
563+ template = templateFinder . firstTemplate ( ) ;
564+ }
565+ if ( template . type !== 'template' ) {
566+ throw new Error ( `InvalidArgumentException: ${ template . type }
567+ ${ template } ` ) ;
568+ }
554569 const parameters = { } ;
555- let unnamedParameterCount = 1 ;
556- let i = 0 ;
557- for ( const string of strings ) {
558- i ++ ;
559- if ( i == 1 ) {
560- continue ; // skip the template name, this is not a parameter
561- }
562- const hasEquals = string . indexOf ( '=' ) ;
563- if ( hasEquals === - 1 ) {
564- parameters [ unnamedParameterCount ] = string ;
565- unnamedParameterCount ++ ;
566- } else {
567- const matches = string . match ( / ^ ( [ ^ = ] * ) = ( .* ) / s ) ; // isolate param name and param value by looking for first equals sign
568- const paramName = matches [ 1 ] . trim ( ) . toLowerCase ( ) ;
569- const paramValue = matches [ 2 ] . trim ( ) ;
570- parameters [ paramName ] = paramValue ;
571- }
570+ for ( const parameter of template . getAllArgs ( ) ) {
571+ parameters [ parameter . name . toLowerCase ( ) ] = parameter . getValue ( ) ;
572572 }
573573 return parameters ;
574574 }
0 commit comments