User:Quarl/___location canonicalize.js: Difference between revisions

Content deleted Content added
«+"as per Wikipedia:WikiProject Location Format", "requires" → "depends", "addlilink" → "wikitabs"»
«279 words changed» use autoedit.js
Line 1:
// [[User:Quarl/location_canonicalize.js]] - canonicalizes ___location WikiLinks
// as per [[Wikipedia:WikiProject Location Format]]
 
// Example: [[Seattle, Washington]] becomes [[Seattle, Washington|Seattle]], [[Washington]], [[USA]].
 
// depends: wikipage.js, util.js, wikitabs.js, wikiedit.js, autoedit.js
 
// quarl 2006-01-22 initial version
// quarl 2006-02-08 refactored to autoedit.js
 
//<pre><nowiki>
 
location_canonicalize = new Objectautoedit();'location_canonicalize',
'LocZ', 'ca-locz', 'Canonicalize ___location wikilinks',
 
'Location canonicalization');
location_canonicalize.run = function() {
location_canonicalize.initData();
wikiPage.getEditorAsync(location_canonicalize.edit);
}
 
location_canonicalize.edit = function(editor) {
// make changes
 
var result = '';
var input = editor.wpTextbox1;
var changes = [];
 
var inputs = [];
 
// special case for hat link, if there is one
if (input.match(/^: *''.*/)) {
var infobox = RegExp.lastMatch;
var right = RegExp.rightContext;
 
inputs.push(infobox);
input = right;
}
 
// special case the first Infobox, if there is one
if (input.match(/^(?:{{Infobox(?:.|\n)*?\n}}|{\|(?:.|\n)*?\n\|})/i)) {
// var left = RegExp.leftContext;
var infobox = RegExp.lastMatch;
var right = RegExp.rightContext;
 
// treat the infobox separately, so that USA links get added to main
// article.
inputs.push(infobox);
input = right;
}
 
inputs.push(input);
 
var result = location_canonicalize.canonicalizeStrings(inputs, changes);
 
if (changes.length) {
editor.wpTextbox1 = result;
editor.wpSummary = 'Location canonicalization: ' + changes.join('; ');
editor.wpMinoredit = true;
editor.submit('wpDiff');
} else {
alert("No changes to make!");
}
}
 
location_canonicalize.canonicalizeStrings = function(inputs, changes) {
for (var i in inputs) {
inputs[i] = location_canonicalize.canonicalizeString(inputs[i], changes);
}
return inputs.join('');
}
 
location_canonicalize.canonicalizeString = function(input, changes) {
var result = '';
 
while (input.match(/\[\[ *(?:([^|\]]+?) *\| *)?([^\]]+?) *\]\]/)) {
var left = RegExp.leftContext;
var wfull = RegExp.lastMatch;
var wlink = RegExp.$1;
var wtext = RegExp.$2;
var right = RegExp.rightContext;
 
result += left;
var r = location_canonicalize.wikilink((wlink||wtext), wtext, result, right);
if (r) {
var new_wfull = r.wfull;
result = r.left;
right = r.right;
changes.push(wfull + ' → ' + new_wfull);
} else {
var new_wfull = wfull;
}
result += new_wfull;
input = right;
}
result += input;
return result;
}
 
location_canonicalize.initData = function() {
if (this.init) return;
this.init = true;
 
var CountryData = function(states, link_country, regexp_country) {
this.states = states;
Line 151 ⟶ 69:
}
 
location_canonicalizethis.wikilinksplitText = function(wlink, wtext, left, rights) {
var inputs = [];
 
// special case for hat link, if there is one
if (input.match(/^: *''.*/)) {
var infobox = RegExp.lastMatch;
var right = RegExp.rightContext;
 
inputs.push(infobox);
input = right;
}
 
// special case the first Infobox, if there is one
if (input.match(/^(?:{{Infobox(?:.|\n)*?\n}}|{\|(?:.|\n)*?\n\|})/i)) {
// var left = RegExp.leftContext;
var infobox = RegExp.lastMatch;
var right = RegExp.rightContext;
 
// treat the infobox separately, so that USA links get added to main
// article.
inputs.push(infobox);
input = right;
}
 
inputs.push(input);
return inputs;
}
 
this.buildRegExp = function() {
return /\[\[ *(?:([^|\]]+?) *\| *)?([^\]]+?) *\]\]/;
}
 
this.replaceRegExp = function(d, m) {
var wlink = m[1] || m[2];
var wtext = m[2];
 
// non-main namespace - usually a category
if (wtext.match(/:/)) return null;
 
if (wlink != wtext) return;
Line 179 ⟶ 132:
if (!wfull) continue;
 
if (d.left.match(c.regexp_country)) {
// Already mentioned country. Delete redundant subsequent
// country links
if (d.right.match(c.regexp_country_sq)) {
d.right = RegExp.rightContext;
// only count as a change if we actually delete it!
++changes;
Line 189 ⟶ 142:
} else {
// Haven't mentioned country earlier
if (d.right.match(c.regexp_country_sq)) {
// it's thereright alreadyafter the current link; good.
} else {
// not there; add it.
Line 199 ⟶ 152:
 
if (changes) {
returnd.text {= wfull: wfull, left: left, right: right};
} else {
return null;
}
return;
}
 
return null;
}
 
location_canonicalize.load = function() {
if (wikiPage.nsSpecialP) return;
if (!document.menuAutoEdit) {
document.menuAutoEdit = wikitabs.addTabMenu('AutoEdit', 'mn-autoedit');
}
wikitabs.addLiLink(document.menuAutoEdit,
'javascript:location_canonicalize.run()',
'locZ', 'ca-locZ', 'Canonicalize ___location wikilinks');
}