User:Closeapple/common.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. |
The accompanying .css page for this skin is at User:Closeapple/common.css. |
// Probably should merge/update [[User:Closeapple/RMFset.js]] into this someday; it has other complex TemplateScript regexes.
/**
* TemplateScript adds configurable templates and scripts to the sidebar, and adds an example regex editor.
* @see http://meta.wikimedia.org/wiki/TemplateScript
* @update-token [[File:Pathoschild/templatescript.js]]
*/
// <nowiki>
$.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', { dataType:'script', cache:true }).then(function() {
var nowMajorEdit = false; // Items should set this flag true whenever they make a major edit.
pathoschild.TemplateScript.add([
// add your own templates or scripts here
{ name: 'welcome', template: '{{subst:welcome}} ~~~~', position: 'after', editSummary: 'welcome!', forNamespaces: 'user talk' },
{
category: 'universal fixups',
name: 'obvious fixes',
tooltip: 'uncontroversial safe edits, like removing trailing spaces',
// isMinorEdit: true, // icky: makes TS force it even if no edit was made
accessKey: ' ',
script: function(editor) {
old = editor.get();
editor
.replace(/\n?[ \t]*(\[\[)\s*(Category:[^\{\}\]]+\]\])/gim, "\n$1$2"); // all categories should have 1 newline and no horizontal spaces before; does not eat multiple newlines (because category blocks are supposed to be separated by a blank line)
if (editor.get() != old) {
editor.appendEditSummary('spacing fixups');
}
editor
.replace(/[ \t]+$/gm, ''); // trim trailing spaces
if (editor.get() != old) {
if (!nowMajorEdit) {
editor.options({ minor: true });
}
}
}
},
{
category: 'universal fixups',
name: 'melt wikilinks',
tooltip: '[[link|links]]→[[link]]s; remove insignificant link spaces',
// isMinorEdit: true, // icky: makes TS force it even if no edit was made
script: function(editor){
old = editor.get();
editor
.replace(/\[\[\s*([^\s\{\}\|\]][^\{\|\]]+[^\s\{\}\|\]])\s*\|\s*\1(\w*)\]\]/g, "[[$1]]$2"); // [[link|links]] -> [[link]]s; doesn't match capitalization difference on first character, which would also be a candidate for conversion
if (editor.get() != old) {
editor.appendEditSummary('wikilink fixups');
if (!nowMajorEdit) {
editor.options({ minor: true });
}
}
}
},
{
category: 'universal fixups',
name: 'ref punct',
tooltip: 'Move punctuation after refs. (Don\'t use if refs are followed by file extensions starting with .)',
// isMinorEdit: true, // icky: makes TS force it even if no edit was made
script: function(editor){
old = editor.get();
editor
.replace(/([.?,:;]+)(["\)]*)\s*((?:<ref\b[^<]+<\/ref> *'*)+) *\1/gi, '$1$2$3') // deduplicate same sentence punctuation before and after refs
.replace(/[,;]\s*((?:<ref\b[^<]+<\/ref> *'*)+) *\./gi, '.$1') // , or ; before but . after refs, nearly always ends up being . before refs
.replace(/([\w"\)\]])\s*((?:<ref\b[^<]+<\/ref> *'*)+) *([.?,:;]+)/gi, '$1$3$2'); // sentence punctuation after refs goes before refs
if (editor.get() != old) {
editor.appendEditSummary('ref punctuation');
if (!nowMajorEdit) {
editor.options({ minor: true });
}
}
}
},
{
category: 'potential fixups',
name: 'ref spacing',
tooltip: 'Eliminates weird spacing around refs, both visible and invisible. (Avoids after = or 10k after {{reflist}}.)',
// isMinorEdit: true, // icky: makes TS force it even if no edit was made
script: function(editor){
old = editor.get();
// The \{\{\s*reflist\b.{2,10000} bit in regex is to make it not match within 10,000 characters after the {{reflist}} tag, so that it doesn't try to reorganize the refs inside that tag. Maybe it should look for the refs= parametes in the reflist also.
editor
.replace(/(?<!=|\||\{\{\s*reflist\b.{2,10000})\s*<ref\b([^/>]*)\s*>\s*/gi, '<ref$1>') // start full ref, not after = or {{reflist}}
.replace(/(?<!\{\{\s*reflist\b.{2,10000})\s*<\s*\/\s*ref\s*>/gi, '</ref>') // end full ref, not after = or {{reflist}}
.replace(/(?<!=|\|)\s*<ref\b(\s?)\s*([^>]*?\s?)\s*\/\s*>/gi, '<ref$1$2/>'); // backreference; primitive attempt at retaining optional 1 space before /; not after =
if (editor.get() != old) {
editor.appendEditSummary('ref spacing');
if (!nowMajorEdit) {
editor.options({ minor: true });
}
}
}
}
// end of custom template/script additions for TemplateScript
]);
});
// </nowiki>