User:Mike Dillon/Scripts/convert-brewbox.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Mike Dillon/Scripts/convert-brewbox. |
// <pre><nowiki>
$(function () {
var textBox = document.getElementById('wpTextbox1');
if (!textBox) return;
if (!textBox.value.match(/\{\{brewbox[ _]begin/)) return;
if (textBox.value.match(/addOnloadHook/)) return;
var summary = document.getElementById('wpSummary');
var minor = document.getElementById('wpMinoredit');
var diff = document.getElementById('wpDiff');
if (!summary || !minor || !diff) return;
var li = mw.util.addPortletLink("p-tb", "#", "Replace brewbox", "ca-brewbox", "Replace brewbox");
var a = li.getElementsByTagName("a")[0];
a.onclick = function () {
var lines = textBox.value.split(/\r\n|\n/);
var output = '';
var brewbox = {};
var current_beers;
var in_brewbox = false;
var m;
for (var i in lines) {
var cur = lines[i];
if ((m = cur.match(/\{\{brewbox[ _]begin/)) || in_brewbox) {
if (!in_brewbox) {
brewbox = {
name: '',
image: '',
caption: '',
location: '',
owner: '',
opened: '',
production: '',
active_beers: '',
seasonal_beers: '',
other_beers: ''
};
delete current_beers;
in_brewbox = true;
}
cur = cur.replace(/^\s+/, "");
cur = cur.replace(/\s+$/, "");
cur = cur.replace(/\{\{brewbox /, "{{brewbox_");
if (m = cur.match(/^\{\{brewbox_begin\|name=(.*)\}\}$/)) {
brewbox.name = m[1];
} else if (m = cur.match(/^\{\{brewbox_image\|image=(.*)\|caption=(.*)\}\}$/)) {
brewbox.image = m[1];
brewbox.caption = m[2];
} else if (m = cur.match(/^\{\{brewbox_image\|image=(.*)\}\}$/)) {
brewbox.image = m[1];
} else if (m = cur.match(/^\{\{brewbox_location\|location=(.*)\}\}$/)) {
brewbox.location = m[1];
} else if (m = cur.match(/^\{\{brewbox_owner\|owner=(.*)\}\}$/)) {
brewbox.owner = m[1];
} else if (m = cur.match(/^\{\{brewbox_opened\|year=(.*)\}\}$/)) {
brewbox.opened = m[1];
} else if (m = cur.match(/^\{\{brewbox_production\|amount=(.*)\}\}$/)) {
brewbox.production = m[1];
} else if (m = cur.match(/^\{\{brewbox_beers\}\}$/)) {
current_beers = 'active_beers';
} else if (m = cur.match(/^\{\{brewbox_beers\|type=\s*Seasonal\s*\}\}$/)) {
current_beers = 'seasonal_beers';
} else if (m = cur.match(/^\{\{brewbox_beers\b/)) {
current_beers = 'other_beers';
brewbox[current_beers] = brewbox[current_beers] + "\n" +
" " + cur;
} else if (cur.match(/\{\{brewbox_end/)) {
in_brewbox = false;
if (output) output += "\n";
output +=
"{{Infobox Brewery\n" +
"| name = " + brewbox.name + "\n" +
"| image = " + brewbox.image + "\n" +
"| caption = " + brewbox.caption + "\n" +
"| location = " + brewbox.location + "\n" +
"| owner = " + brewbox.owner + "\n" +
"| opened = " + brewbox.opened + "\n" +
"| production = " + brewbox.production + "\n" +
"| active_beers = " + brewbox.active_beers + "\n" +
"| seasonal_beers = " + brewbox.seasonal_beers + "\n" +
"| other_beers = " + brewbox.other_beers + "\n" +
"}}";
} else if (current_beers) {
brewbox[current_beers] += "\n " + cur;
}
} else {
if (output) output += "\n";
output += cur;
}
}
summary.value = 'Convert to {{Infobox Brewery}}';
if (!minor.checked) minor.click();
textBox.value = output;
diff.click();
return false;
};
});
// </nowiki></pre>