User:Monster Domosed/common.js: Difference between revisions

Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
/*  Patch Formatter */
/*  Patch Formatter */
( function () {
( function () {
'use strict';
'use strict';
// Copy-safe "" and "" — never write them as literals in this editor.
var L = '{' + '{';
var R = '}' + '}';


// ---- CONFIG ---------------------------------------------------------
// ---- CONFIG ---------------------------------------------------------
var GAME_MODE_SECTIONS = [ 'Street Brawl' ];
var GAME_MODE_SECTIONS = [ 'Street Brawl' ];


// (RegExp, replacement) — generic terms turned into wiki links.
var WIKI_LINKS = [
var WIKI_LINKS = [
[ /(?<!\w)Urn(?!\w)/g, '[[Urn]]' ],
[ /(?<!\w)Urn(?!\w)/g, '[[Urn]]' ],
Line 17: Line 20:
function getWikiData( templateName ) {
function getWikiData( templateName ) {
var api = new mw.Api();
var api = new mw.Api();
var text = L + '#invoke:MonsterDomosed/patchformatter|' + templateName + R;
return api.get( {
return api.get( {
action: 'expandtemplates',
action: 'expandtemplates',
text: '#invoke:MonsterDomosed/patchformatter|' + templateName + '',
text: text,
prop: 'wikitext',
prop: 'wikitext',
format: 'json',
format: 'json',
formatversion: 2
formatversion: 2
} ).then( function ( data ) {
} ).then( function ( data ) {
var wikitext = data.expandtemplates.wikitext || '';
var wikitext = ( data.expandtemplates && data.expandtemplates.wikitext ) || '';
return wikitext.split( ',' )
return wikitext.split( ',' )
.map( function ( item ) { return item.trim().replace( /^'+|'+$/g, '' ); } )
.map( function ( item ) { return item.trim().replace( /^'+|'+$/g, '' ); } )
Line 41: Line 45:
current = stripped.slice( 1 ).trim();
current = stripped.slice( 1 ).trim();
} else if ( current !== null ) {
} else if ( current !== null ) {
current += ' ' + stripped; // continuation of previous bullet
current += ' ' + stripped;
}
}
} );
} );
Line 78: Line 82:


function wrap( terms, template ) {
function wrap( terms, template ) {
// Longest names first so multi-word names win.
terms.slice().sort( function ( a, b ) { return b.length - a.length; } )
terms.slice().sort( function ( a, b ) { return b.length - a.length; } )
.forEach( function ( term ) {
.forEach( function ( term ) {
Line 88: Line 91:
}
}


wrap( abilities, 'AbilityIcon|%s' );
wrap( abilities, L + 'AbilityIcon|%s' + R );
wrap( items, 'ItemIcon|%s' );
wrap( items, L + 'ItemIcon|%s' + R );
wrap( heroes, 'HeroIcon|%s' );
wrap( heroes, L + 'HeroIcon|%s' + R );


WIKI_LINKS.forEach( function ( pair ) {
WIKI_LINKS.forEach( function ( pair ) {
Line 130: Line 133:
var out = [];
var out = [];


// General changes
general.forEach( function ( entry ) { out.push( '* ' + entry ); } );
general.forEach( function ( entry ) { out.push( '* ' + entry ); } );


// Game mode sections
modeOrder.forEach( function ( mode ) {
modeOrder.forEach( function ( mode ) {
out.push( '', '== [[' + mode + ']] ==' );
out.push( '', '== [[' + mode + ']] ==' );
Line 139: Line 140:
} );
} );


// Items
if ( itemOrder.length ) {
if ( itemOrder.length ) {
out.push( '', '== Items ==' );
out.push( '', '== Items ==' );
itemOrder.forEach( function ( item ) {
itemOrder.forEach( function ( item ) {
out.push( '=== ItemIcon|' + item + ' ===' );
out.push( '=== ' + L + 'ItemIcon|' + item + R + ' ===' );
itemSections[ item ].forEach( function ( body ) {
itemSections[ item ].forEach( function ( body ) {
out.push( CHANGE_PREFIX + body );
out.push( CHANGE_PREFIX + body );
Line 151: Line 151:
}
}


// Heroes
if ( heroOrder.length ) {
if ( heroOrder.length ) {
out.push( '', '== Heroes ==', '' );
out.push( '', '== Heroes ==', '' );
heroOrder.forEach( function ( hero ) {
heroOrder.forEach( function ( hero ) {
out.push( '=== HeroIcon|' + hero + ' ===' );
out.push( '=== ' + L + 'HeroIcon|' + hero + R + ' ===' );
heroSections[ hero ].forEach( function ( body ) {
heroSections[ hero ].forEach( function ( body ) {
out.push( CHANGE_PREFIX + body );
out.push( CHANGE_PREFIX + body );
Line 169: Line 168:
function init() {
function init() {
var app = document.getElementById( 'patchformatter' );
var app = document.getElementById( 'patchformatter' );
if ( !app ) { return; }
if ( !app ) {
 
return;
}
app.innerHTML =
app.innerHTML =
'<p><strong>Raw patch notes</strong></p>' +
'<p><strong>Raw patch notes</strong></p>' +
Line 184: Line 180:
'<p><strong>Formatted wikitext</strong></p>' +
'<p><strong>Formatted wikitext</strong></p>' +
'<textarea id="pf-output" rows="20" style="width:100%;font-family:monospace;" readonly></textarea>';
'<textarea id="pf-output" rows="20" style="width:100%;font-family:monospace;" readonly></textarea>';
 
var input = document.getElementById( 'pf-input' );
var input = document.getElementById( 'pf-input' );
var output = document.getElementById( 'pf-output' );
var output = document.getElementById( 'pf-output' );
Line 190: Line 186:
var copyBtn = document.getElementById( 'pf-copy' );
var copyBtn = document.getElementById( 'pf-copy' );
var status = document.getElementById( 'pf-status' );
var status = document.getElementById( 'pf-status' );
 
var listsPromise = null;
var listsPromise = null;
function loadLists() {
function loadLists() {
if ( !listsPromise ) {
if ( !listsPromise ) {
Line 203: Line 198:
return listsPromise;
return listsPromise;
}
}
 
runBtn.addEventListener( 'click', function () {
runBtn.addEventListener( 'click', function () {
status.textContent = 'Loading wiki data...';
status.textContent = 'Loading wiki data...';
runBtn.disabled = true;
runBtn.disabled = true;
loadLists().then( function ( lists ) {
loadLists().then( function ( lists ) {
var heroes = lists[ 0 ];
var heroes = lists[ 0 ], items = lists[ 1 ], abilities = lists[ 2 ];
var items = lists[ 1 ];
var abilities = lists[ 2 ];
var entries = parseEntries( input.value );
var entries = parseEntries( input.value );
output.value = buildOutput( entries, heroes, abilities, items, GAME_MODE_SECTIONS );
output.value = buildOutput(
status.textContent = 'Processed ' + entries.length + ' entries (' +
entries,
heroes.length + ' heroes, ' + abilities.length + ' abilities, ' +
heroes,
abilities,
items,
GAME_MODE_SECTIONS
);
status.textContent =
'Processed ' + entries.length +
' entries (' +
heroes.length + ' heroes, ' +
abilities.length + ' abilities, ' +
items.length + ' items).';
items.length + ' items).';
} ).catch( function ( err ) {
} ).catch( function ( err ) {
Line 235: Line 215:
} );
} );
} );
} );
 
copyBtn.addEventListener( 'click', function () {
copyBtn.addEventListener( 'click', function () {
if ( navigator.clipboard ) {
if ( navigator.clipboard ) {