User:Monster Domosed/common.js: Difference between revisionsGive feedback
No edit summary |
No edit summary |
||
| Line 112: | Line 112: | ||
} | } | ||
function wrap( terms, template ) { | function wrap( terms, template, usedSet ) { | ||
terms.slice() | terms.slice() | ||
| Line 120: | Line 120: | ||
.forEach( function ( term ) { | .forEach( function ( term ) { | ||
var | var idx = 0; | ||
var found = false; | |||
while ( ( idx = text.indexOf( term, idx ) ) !== -1 ) { | |||
var before = text[ idx - 1 ]; | |||
var after = text[ idx + term.length ]; | |||
var validBefore = !before || /\s|:|\.|\(|\[/ .test( before ); | |||
var validAfter = !after || /\s|\.|,|\)|\]/.test( after ); | |||
} ); | if ( validBefore && validAfter ) { | ||
found = true; | |||
var replacement = protect( | |||
template.replace( '%s', term ) | |||
); | |||
text = | |||
text.slice( 0, idx ) + | |||
replacement + | |||
text.slice( idx + term.length ); | |||
idx += replacement.length; | |||
} else { | |||
idx += 1; | |||
} | |||
} | |||
if ( found ) { | |||
usedSet.add( term ); | |||
} | |||
} ); | } ); | ||
} | } | ||
wrap( abilities, L + 'AbilityIcon|%s' + R ); | wrap( abilities, L + 'AbilityIcon|%s' + R, usedAbilities ); | ||
wrap( items, L + 'ItemIcon|%s' + R ); | wrap( items, L + 'ItemIcon|%s' + R, usedItems ); | ||
wrap( heroes, L + 'HeroIcon|%s' + R ); | wrap( heroes, L + 'HeroIcon|%s' + R, usedHeroes ); | ||
WIKI_LINKS.forEach( function ( pair ) { | WIKI_LINKS.forEach( function ( pair ) { | ||
| Line 170: | Line 189: | ||
var c = classify( entry, heroes, items, gameModes ); | var c = classify( entry, heroes, items, gameModes ); | ||
var body = formatBody( | var body = formatBody( | ||
| Line 183: | Line 200: | ||
); | ); | ||
if ( | if ( c[ 0 ] === 'general' ) { | ||
general.push( body ); | general.push( body ); | ||
} else if ( c[ 0 ] === 'mode' ) { | |||
if ( !modes[ c[ 1 ] ] ) { | |||
modes[ c[ 1 ] ] = []; | |||
if ( !modes[ | modeOrder.push( c[ 1 ] ); | ||
modes[ | |||
modeOrder.push( | |||
} | } | ||
modes[ | modes[ c[ 1 ] ].push( body ); | ||
} else if ( | } else if ( c[ 0 ] === 'item' ) { | ||
if ( !itemSections[ | if ( !itemSections[ c[ 1 ] ] ) { | ||
itemSections[ | itemSections[ c[ 1 ] ] = []; | ||
itemOrder.push( | itemOrder.push( c[ 1 ] ); | ||
} | } | ||
itemSections[ | itemSections[ c[ 1 ] ].push( body ); | ||
} else if ( | } else if ( c[ 0 ] === 'hero' ) { | ||
if ( !heroSections[ | if ( !heroSections[ c[ 1 ] ] ) { | ||
heroSections[ | heroSections[ c[ 1 ] ] = []; | ||
heroOrder.push( | heroOrder.push( c[ 1 ] ); | ||
} | } | ||
heroSections[ | heroSections[ c[ 1 ] ].push( body ); | ||
} | } | ||
} ); | } ); | ||