User:Taavi/Aligner.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. |
This user script seems to have a documentation page at User:Taavi/Aligner. |
// <nowiki>
window.alignerDebug = false;
window.alignerDefaultSearches = [
"{{infobox",
"{{speciesbox",
"{{taxobox",
'{{automatic taxobox',
];
window.alignerExtraSearches = [];
mw.loader.using(['mediawiki.util'], function () {
if (mw.config.get('wgAction') !== 'edit' && mw.config.get('wgAction') !== 'submit') {
return;
}
mw.util.addPortletLink('p-cactions', 'javascript:void(0);', 'Align template params', 'us-majavah-align');
$('#us-majavah-align').click(function () {
const splitParam = string => {
const split = string.split('=');
if (split.length <= 2) {
return split;
}
let first = split.shift();
return [first, split.join('=')];
};
const splitIntoParams = string => {
if (string.startsWith('{{') && string.endsWith('}}')) {
if (!string.includes('|')) {
return [string];
}
const results = splitIntoParams(string.slice(2, -2));
return ['{{' + results[0]].concat(splitIntoParams(string.slice(2, -2)).slice(1), ['}}']);
}
const params = [];
let temp = '';
let open = 0;
for (let i = 0; i < string.length; i++) {
const char = string[i];
temp += char;
if (char === '{' || char === '[') {
open += 1;
} else if (char === '}' || char === ']') {
open -= 1;
} else if (char === '|' && open === 0 && temp.trim() !== '|') {
params.push(temp.slice(0, -1).trim());
temp = '|';
}
}
params.push(temp);
return params;
};
const debug = string => {
if (window.alignerDebug) {
mw.notify(string);
}
};
const useWikEd = window.wikEd && window.wikEd.useWikEd;
if (useWikEd) {
window.wikEd.UpdateTextarea();
}
const editBox = $('#wpTextbox1');
if (!editBox) {
mw.notify('Edit box not found');
return;
}
const text = editBox.val();
if (!text || text.length === 0) {
mw.notify('Edit box value not found');
return;
}
const searches = window.alignerDefaultSearches.concat(window.alignerExtraSearches || []);
let count = 0;
const processInfobox = template => {
if (template === '') {
mw.notify('Infobox not found');
return;
}
if (open !== 0) {
console.error({ open });
mw.notify('Template was not properly closed');
return;
}
let maxLength = 0;
const origTemplate = String(template);
const lines = template.split("\n");
const newLines = [];
for (let lineNumber in lines) {
const paramsInLine = splitIntoParams(lines[lineNumber].trim());
console.log({
line: lines[lineNumber].trim(),
paramsInLine,
});
for (let paramNumber in paramsInLine) {
let line = paramsInLine[paramNumber].trim();
if (!line.startsWith('|') || line.split('=').length !== 2) {
newLines.push(line);
continue;
}
let [firstPart, lastPart] = splitParam(line);
firstPart = firstPart.slice(1).trim();
if (firstPart.length > maxLength) {
maxLength = firstPart.length;
}
newLines.push('| ' + firstPart + '=' + lastPart);
}
}
let output = '';
maxLength += 2; // to include '| '
for (let lineNumber in newLines) {
let line = newLines[lineNumber];
const parts = splitParam(line);
if (parts.length < 2) {
output += line += "\n";
continue;
}
let firstPart = parts[0].trim();
console.log({ firstPart, maxLength });
while (firstPart.length < maxLength) {
firstPart += ' ';
}
output += firstPart + ' = ' + parts[1].trim() + "\n";
}
if (output.endsWith("\n")) {
output = output.slice(0, -1);
}
editBox.val(editBox.val().replace(origTemplate, output));
if (useWikEd) {
wikEd.UpdateFrame();
}
};
let template = '';
let open = 0;
for (let i = 0; i < text.length; i++) {
let foo = false;
for (let searchIndex in searches) {
const search = searches[searchIndex];
const searchLength = search.length;
if (text.length - i > searchLength) {
if ((text.slice(i, i + searchLength).toLowerCase() === search) || (text.slice(i, i + searchLength).toLowerCase() === search.replace(" ", "_"))) {
open += 1;
template += text[i];
foo = true;
}
}
}
if (open >= 1 && !foo) {
template += text[i];
if (text[i] == '{') {
open += 1;
} else if (text[i] == '}') {
open -= 1;
if (open === 0) {
count += 1;
processInfobox(template);
template = '';
}
}
}
}
mw.notify('Successfully aligned ' + String(count) + ' templates. Remember to preview before saving and report any issues at [[User talk:Majavah]]');
});
});
// </nowiki>