/* AutoEd further fixes */
/*
- put footnotes after punctuation rather than just before
- use reflist if more than 2 references
- use reflist|2 if more than 6 references
- trim duplicate wlinks, leaving just first instance
- remove default image right aligned option
*/
function FaeFixes(mytext){
var cmt='',mycount=0;
//
// Ensure footnotes are after punctuation
//
var fPattern=/(<ref[^>]*>([^>]*|[^>]*(-->){1}[^>]*)ref>)(\.|,|;)/g;
if(mytext.match(fPattern)!=null){
mytext=mytext.replace(fPattern,'$4$1'); // Fix wrong ref punctuation
cmt+='Fix inconsistent ref placement, see [[WP:FN|FN]]. ';
}
//
// Ref tags, tidy footnote list
//
// Calc total no. of refs (must be closing tags otherwise over count multiple tags to same footnote)
var refcount=mytext.match(/<\/ref>/g)!=null?mytext.match(/<\/ref>/g).length:0;
// Convert older type tag
if(mytext.match(/<references *\/>/)!=null && refcount>2){
if(refcount>5){
mytext=mytext.replace(/<references *\/>/,'{{reflist|2}}');
}else{
mytext=mytext.replace(/<references *\/>/,'{{reflist}}');
}
cmt+='Standardize reflist. ';
}
// Upgrade to 2+col if 6 or more footnotes
if(refcount>5 && mytext.match(/\{\{reflist\}\}/gi)!=null){
mytext=mytext.replace(/\{\{reflist\}\}/gi,'{{reflist|colwidth=30em}}');
cmt+='Better reflist layout ('+refcount+' footnotes). ';
}
//
// Remove odd newlines
//
mycount=0;
while(mytext.match(/[\n\r]{3}/)!=null){
mytext=mytext.replace(/[\n\r]{3,}/,"\n\n");
mycount++;
}
if(mycount>0 && cmt.length<100){
cmt+=(mycount==1?'Rm unnecessary newline. ':'Rm '+mycount+' areas of unnecessary newlines. ');
}
//
// Remove duplicate w-links (leaving 1st instance)
//
//
// Things to park (duplicates to keep)
mytext=mytext.replace(/\{\{(Further)\|\[\[/gi,"xxpark$1xx");
// Do the dup remove
mycount=0;
while(mycount<7 && mytext.match(/\[\[([\s_\w]+)(\|[^\]]*)?\]\][\w\W\n\r]*\[\[\1(\|[^\]]*)?\]\]/gim)!=null){
var mymatch=mytext.match(/\[\[([\s_\w]+)(\|[^\]]*)?\]\]([\w\W\n\r]*)\[\[(\1)(\|[^\]]*)?\]\]/im)[0]
mytext=mytext.replace(/\[\[([\s_\w]+)(\|[^\]]*)?\]\]([\w\W\n\r]*)\[\[(\1)(\|[^\]]*)?\]\]/im,"[[$1$2]]$3!!xmx!!$4!!xnx!!$5!!xox!!");
var mymatch=mytext.match(/!!xmx!!.*?!!xox!!/)[0];
var myaltname=mymatch.split('!!')[4].replace('|',''); // Use the alternative link text rather than the w-link itself
if(myaltname==''){mytext=mytext.replace(/!!xmx!!/,'');mytext=mytext.replace(/!!xnx!!!!xox!!/,'')}else{mytext=mytext.replace(/!!xmx!!.*?!!xnx!!\|/,'');mytext=mytext.replace(/!!xox!!/,'')}
mycount++;
}
if(mycount>6){ // en-mass for large pages otherwise may cause excessive wait
mytext=mytext.replace(/\[\[([\s_\w]+)(\|[^\]]*)?\]\]([\w\W\n\r]*)\[\[(\1)(\|[^\]]*)?\]\]/gim,"[[$1$2]]$3!!xmx!!$4!!xnx!!$5!!xox!!");
var mymatch=mytext.match(/!!xmx!!.*?!!xox!!/)[0];
var myaltname='';
while(mymatch!=null){
myaltname=mymatch.split('!!')[4].replace('|',''); // Use the alternative link text rather than the w-link itself
if(myaltname==''){mytext=mytext.replace(/!!xmx!!/,'');mytext=mytext.replace(/!!xnx!!!!xox!!/,'')}else{mytext=mytext.replace(/!!xmx!!.*?!!xnx!!\|/,'');mytext=mytext.replace(/!!xox!!/,'')}
if(mytext.match(/!!xmx!!.*?!!xox!!/)!=null){var mymatch=mytext.match(/!!xmx!!.*?!!xox!!/)[0]}else{mymatch=null};
}
}
// Unparking
mytext=mytext.replace(/xxpark(\w+)xx/g,"{{$1|[[");
//
// Tidy See also duplicates (now unlinked)
// (Would be better to delete the duplicates of these rather than the reverse)
//
while(mytext.match(/== ?See also ?==[^=]+\* ?[^\n\[]*\n/gim)!=null){
mytext=mytext.replace(/(== ?See also ?==[^=]+)\* ?[^\n\[]*\n/gim,"$1");
}
if(mycount>0){
if(mycount==1){
cmt+='Rm a dup w-link, see [[wp:OVERLINK|OVERLINK]]. ';
}else{
cmt+='Rm ';
if(mycount<7){
cmt+=mycount.toString();
}else{
cmt+='many';
}
cmt+=' duplicate w-links, see [[wp:OVERLINK|OVERLINK]]. ';
}
}
//
// Tidy unneeded quotes on ref names
//
var rnPattern=/(<ref name) ?= ?"([\w_\.\-]+)" ?/g;
if(mytext.match(rnPattern)!=null){
mytext=mytext.replace(rnPattern,"$1=$2");
if(cmt.length<100){cmt+='Rm quotes on ref names without spaces. '};
}
rnPattern=/(<ref name) ?= ?([\w_\.\-\s"]*)([\w_\.\-"]+) ?(\/?>)/g;
if(mytext.match(rnPattern)!=null){
mytext=mytext.replace(rnPattern,"$1=$2$3$4"); // Tidy up surplus spacing in refs
}
//
// Tidy surplus template spaces
//
mytext=mytext.replace(/\{\{ +/g,"{{");
mytext=mytext.replace(/(\{\{[^\{^\}]+) +\}\}/g,"$1}}");
mytext=mytext.replace(/(<.ref>)[\s]+(<ref)/g,"$1$2"); // Rm odd spacing between footnotes
mytext=mytext.replace(/(\.|,|;) (<ref)/g,"$1$2"); // Rm odd spacing before footnotes
//
// Remove image right option from thumbnail images (default value)
//
mytext=mytext.replace(/(\[\[image[^\]]*)(thumb[^\]]*)\|right/gi,'$1$2');
mytext=mytext.replace(/(\[\[image[^\]]*)\|right([^\]]*thumb)/gi,'$1$2');
//
// Final output
//
return mytext;
}