User:Yair rand/autoformat.js

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

// throwing together a bunch of potentially useful autoformatting regexes
// sort language sections, add ----s, move interwikis to the bottom, tbalance, use {t} templates
var w=document.getElementById("wpTextbox1");
var s = w.value.replace(/==(Translingual|English)==/g,"==     $1==").match(/==[\s\S]+?(?=(\n+----|\n+==[^=]|$))/g).sort()
if(s[0].indexOf("==     English==")==0 && s[1] && s[1].indexOf("==     Translingual==")==0){s[0] = [s[1],s[1]=s[0]][0]}
w.value=(w.value.substr(0,w.value.indexOf("=="))+s.join("\n\n----\n\n")).replace(/==     (Translingual|English)==/g,"==$1==")

w.value=w.value.replace("{{count page|[[Wiktionary:Page count]]}}","");
if(w.value.indexOf("[")==-1){w.value+="\n{{count page|[[Wiktionary:Page count]]}}"}

s=RegExp("\\n\\[\\[[a-z]{2,3}(ple|-rup|-min-nan)?:"+util.escapeRe(wgPageName)+"\\]\\]","g")
w.value = w.value.match(s)?w.value.replace(s,'') + w.value.match(s).join(''):w.value;
w.value = w.value.replace(/\n{3,}/g,'\n\n');

w.value = w.value.replace(/\{\{trans-mid\}\}\n/g,'').replace(/\{\{trans-top[\s\S]+?(?=\n\{\{trans-bottom\}\})/g,function(z){
z=z.replace(RegExp("(([\\s\\S]*?\\n\\*){"+Math.floor(z.split("\n\*").length/2)+"}[\\s\\S]*?(?=\\n\\*(?![\\:\\*])|$))"),'$1\n{{trans-mid}}');
z=z.replace(/(\* \[?\[?([^\:\]]+)\]?\]?: )\[\[([^\]\|]+)\]\]/g, '$1{\{t|{\{subst:langrev/$2}}|$3}}');
z=z.replace(/(\{\{t[\-\+ø]?\|[^\|]+\|[^\{\}]+)\}\} \{\{(m|f|n|c)\}\}/g,'$1|$2}}');
return z
});