User:IceWelder/citationstyle.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:IceWelder/citationstyle. |
// <nowiki>
// NOTICE: This script is a fork of Meteor sandwich yum's Tidy citations script, all credit goes to them
/**
* This script is used to clean up whitespace in citation templates
* Please leave bug reports and suggestions on my talk page
*/
/**
* TemplateScript adds configurable templates and scripts to the sidebar, and adds an example regex editor.
* @see https://meta.wikimedia.org/wiki/TemplateScript
* @update-token [[File:Pathoschild/templatescript.js]]
*/
$.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', {
dataType: 'script',
cache: true
}).then(function () {
pathoschild.TemplateScript.add([
{
category: 'Citation Style', name: '{{Tidy}}', isMinorEdit: true, script: function (editor) {
format_citations(editor, 'tidy', false);
}
},
{
category: 'Citation Style', name: '{{Tidy}} (vertically)', isMinorEdit: true, script: function (editor) {
format_citations(editor, 'tidy', true);
}
},
{
category: 'Citation Style', name: '{{Crammed}}', isMinorEdit: true, script: function (editor) {
format_citations(editor, 'crammed', false);
}
},
{
category: 'Citation Style', name: '{{Crammed}} (vertically)', isMinorEdit: true, script: function (editor) {
format_citations(editor, 'crammed', true);
}
},
{
category: 'Citation Style', name: '{{Roomy}}', isMinorEdit: true, script: function (editor) {
format_citations(editor, 'roomy', false);
}
},
{
category: 'Citation Style', name: '{{Roomy}} (vertically)', isMinorEdit: true, script: function (editor) {
format_citations(editor, 'roomy', true);
}
}
]);
});
/** ------------------------------------------------------------------------ **/
function format_citations(editor, variation, vertical) {
var text = editor.get();
var original = text;
// Fill an array with one entry per each recognized citation template
var originalTemplates = text.match(/\s*{{[Cc]it(?:ation|e[a-zA-Z0-9\-_\s]+)\s*\|(?:{{[^}]*}}|[^}])+}}\s*/g) || [];
// Duplicate the array for editing; we need to keep the original strings for the replacement step
var tweakedTemplates = originalTemplates.slice();
var replace_regex = /\s*\|\s*([a-zA-Z0-9\-_\s]+(?: [a-zA-Z0-9\-_\s])*)\s*=\s*/g;
var format_parameter = function (prefix, group, suffix) {
return (vertical ? '\n' : '') + prefix + group.replace(/(?:^\s*|\s*$)/g, '') + suffix;
};
for (var i = 0; i < originalTemplates.length; ++i) {
// Normalize spaces around the pipes and equals signs
switch (variation) {
case 'tidy':
tweakedTemplates[i] = tweakedTemplates[i].replace(replace_regex, function (match, group) {
return format_parameter(' |', group, '=');
});
break;
case 'crammed':
tweakedTemplates[i] = tweakedTemplates[i].replace(replace_regex, function (match, group) {
return format_parameter('|', group, '=');
});
break;
case 'roomy':
tweakedTemplates[i] = tweakedTemplates[i].replace(replace_regex, function (match, group) {
return format_parameter(' | ', group, ' = ');
});
break;
}
// Prepend newlines for vertical align
if (vertical) {
tweakedTemplates[i] = '\n' + tweakedTemplates[i];
}
// Align end of the template
if (!vertical) {
tweakedTemplates[i] = tweakedTemplates[i].replace(/\s*}}(\s*)$/g, '}}$1');
} else {
tweakedTemplates[i] = tweakedTemplates[i].replace(/\s*}}(\s*)$/g, '\n}}$1');
}
// Normalize multiple spaces to one
tweakedTemplates[i] = tweakedTemplates[i].replace(/ +/g, ' ');
// Replace the original templates with the tweaked versions
text = text.replace(originalTemplates[i], tweakedTemplates[i]);
}
// Normalize ref tag capitalization
text = text.replace(/<ref/gi, '<ref');
text = text.replace(/<\/ref/gi, '</ref');
// Normalize newlines between citation templates and ref tags
text = text.replace(/(<ref(?:(?!>|\/>).)*>)\s*{{/g, '$1{{');
text = text.replace(/}}\s*<\/ref>/g, '}}</ref>');
// Only insert the edit summary when something has changed
if (text !== original) {
editor.set(text);
editor.appendEditSummary('harmonize whitespace in citation templates by [[w:User:IceWelder/citationstyle.js|script]]');
editor.clickDiff();
}
}
// </nowiki>