Jump to content
Wikipedia The Free Encyclopedia

Wikipedia:WikiProject User scripts/Scripts/Formatter

From Wikipedia, the free encyclopedia

/*

*/
// NOTICE: Formatter's functionality is now almost entirely included in [[Wikipedia:AutoEd]]'s "Formatter" preset. You may
// use whichever you want, although AutoEd is more actively maintained and more open to customization.
//Wikipedia:WikiProject User scripts | Scripts
functionformat(){
vartxt=document.editform.wpTextbox1;
txt.value=catFixer(txt.value);
txt.value=entities(txt.value);
txt.value=fixheadings(txt.value);
txt.value=fixsyntax(txt.value);
txt.value=linkfixer(txt.value,false);
//txt.value = imagefixer(txt.value);
txt.value=whitespace(txt.value);
txt.value=linksimplifyer(txt.value);
txt.value=trim(txt.value);
}
functionwhitespace(str){
str=str.replace(/\t/g," ");
str=str.replace(/^ ? ? \n/gm,"\n");
str=str.replace(/(\n\n)\n+/g,"1ドル");
str=str.replace(/== ? ?\n\n==/g,"==\n==");
str=str.replace(/\n\n(\* ?\[?http)/g,"\n1ドル");
str=str.replace(/^ ? ? \n/gm,"\n");
str=str.replace(/\n\n\*/g,"\n*");
str=str.replace(/[ \t][ \t]+/g," ");
str=str.replace(/([=\n]\n)\n+/g,"1ドル");
str=str.replace(/ \n/g,"\n");
//* bullet points
str=str.replace(/^([\*#]+) /gm,"1ドル");
str=str.replace(/^([\*#]+)/gm,"1ドル ");
//==Headings==
str=str.replace(/^(={1,4}) ?(.*?) ?(={1,4})$/gm,"1ドル2ドル3ドル");
//dash — spacing
str=str.replace(/ ?(–|–|–|–|–) ?/g,"1ドル");
str=str.replace(/ ?(—|—|—|—|—) ?/g,"1ドル");
str=str.replace(/([^1-9])(—|—|—|—|—|–|–|–|–|–)([^1-9])/g,"1ドル 2ドル 3ドル");
returntrim(str);
}
functionentities(str){
//str = str.replace(//g, "");
str=str.replace(/–|–|–/g,"–");
str=str.replace(/—|—|—/g,"—");
// str = str.replace(/(cm| m|km|mi)<sup>2</sup>/g, "12ドル");
str=str.replace(/&sup2;/g,"2");
str=str.replace(/&deg;/g,"°");
returntrim(str);
}
//Fix ==See also== and similar section common errors.
functionfixheadings(str)
{
if(!str.match(/= ?See also ?=/))
str=str.replace(/(== ?)(see also:?|also see:?)( ?==)/gi,"1ドルSee also3ドル");
str=str.replace(/(== ?)(external links?:?|outside links?|web ?links?:?|exterior links?:?)( ?==)/gi,"1ドルExternal links3ドル");
str=str.replace(/(== ?)(references?:?)( ?==)/gi,"1ドルReferences3ドル");
str=str.replace(/(== ?)(sources?:?)( ?==)/gi,"1ドルSources3ドル");
str=str.replace(/(== ?)(further readings?:?)( ?==)/gi,"1ドルFurther reading3ドル");
returnstr;
}
functioncatFixer(str){
str=str.replace(/\[\[ ?[Cc]ategory ?: ?/g,"[[Category:");
returntrim(str);
}
//fixes many common syntax problems
functionfixsyntax(str)
{
//replace html with wiki syntax
if(!str.match(/'<\/?[ib]>|<\/?[ib]>'/gi))
{
str=str.replace(/<i>(.*?)<\/i>/gi,"''1ドル''");
str=str.replace(/<b>(.*?)<\/b>/gi,"'''1ドル'''");
}
str=str.replace(/<br\/>/gi,"<br />");
str=str.replace(/<br>/gi,"<br />");

returntrim(str);
}
//formats links in standard fashion
functionlinkfixer(str,checkImages)
{
str=str.replace(/\]\[/g,"] [");
varm=str.match(/\[?\[[^\]]*?\]\]?/g);
if(m)
{
for(vari=0;i<m.length;i++)
{
varx=m[i].toString();
vary=x;
//internal links only
if(!y.match(/^\[?\[http:\/\//i)&&!y.match(/^\[?\[image:/i))
{
if(y.indexOf(":")==-1&&y.substr(0,3)!="[[_"&&y.indexOf("|_")==-1)
{
if(y.indexOf("|")==-1)
y=y.replace(/_/g," ");
else
y=y.replace(y.substr(0,y.indexOf("|")),y.substr(0,y.indexOf("|")).replace(/_/g," "));
}

y=y.replace(/ ?\| ?/,"|").replace("|]]","| ]]");

}
str=str.replace(x,y);
}
}
//repair bad internal links
str=str.replace(/\[\[ ?([^\]]*?) ?\]\]/g,"[[1ドル]]");
str=str.replace(/\[\[([^\]]*?)( |_)#([^\]]*?)\]\]/g,"[[1ドル#3ドル]]");
//repair bad external links
str=str.replace(/\[?\[http:\/\/([^\]]*?)\]\]?/gi,"[http://1ドル]");
str=str.replace(/\[http:\/\/([^\]]*?)\|([^\]]*?)\]/gi,"[http://1ドル 2ドル]");
returntrim(str);
}
//fixes images
functionimagefixer(str)
{
//remove external images
str=str.replace(/\[?\[image:http:\/\/([^\]]*?)\]\]?/gi,"[http://1ドル]");
//fix links within internal images
varm=str.match(/\[?\[image:[^\[\]]*?(\[?\[[^\]]*?\]*?[^\[\]]*?)*?\]+/gi);
if(m)
{
for(vari=0;i<m.length;i++)
{
varx=m[i].toString();
vary=x;
y=y.replace(/^\[\[i/i,"I").replace(/\]\]$/,"");
y=y.replace(/(\[[^\]]*?)$/,"1ドル]");
y=linkfixer(y,true);
y="[["+y+"]]";
str=str.replace(x,y);
}
}
returntrim(str);
}
//simplifies some links e.g. [[Dog|dog]] to [[dog]] and [[Dog|dogs]] to [[dog]]s
functionlinksimplifyer(str){
varm=str.match(/\[\[([^[]*?)\|([^[]*?)\]\]/g);
if(m)
{
for(vari=0;i<m.length;i++)
{
varn_arr=m[i].toString().match(/\[\[([^[]*?)\|([^[]*?)\]\]/);
varn=n_arr[0];
vara=n_arr[1];
varb=n_arr[2];
if(b.indexOf(a)==0||b.indexOf(TurnFirstToLower(a))==0)
{
vark=n.replace(/\[\[([^\]\|]*?)\|(1円)([\w]*?)\]\]/i,"[[2ドル]]3ドル");
str=str.replace(n,k);
}
}
}
str=str.replace(/\[\[([^\]\|]+)\|([^\]\|]+)\]\]([A-Za-z\'][A-Za-z]*)([\.,円\;\:\"\!\?\s\n])/g,"[[1ドル|2ドル3ドル]]4ドル");// ' // Help the syntax highlighter...
returnstr;
}
//trim start and end, trim spaces from the end of lines
functiontrim(str){
str=str.replace(/ $/gm,"");
returnstr.replace(/^\s*|\s*$/g,"");
}
//turns first character to lowercase
functionTurnFirstToLower(input){
if(input!="")
{
varinput=trim(input);
vartemp=input.substr(0,1);
returntemp.toLowerCase()+input.substr(1,input.length);
}
else
return"";
}
//entities that should never be unicoded
functionnoUnicodify(str){
str=str.replace(" &amp; "," & ");
str=str.replace("&amp;","&amp;amp;").replace("&amp;lt;","&amp;amp;lt;").replace("&amp;gt;","&amp;amp;gt;").replace("&amp;quot;","&amp;amp;quot;").replace("&amp;apos;","&amp;amp;apos;");
str=str.replace("&minus;","&amp;minus;").replace("&times;","&amp;times;");
str=str.replace("&nbsp;","&amp;nbsp;").replace("&thinsp;","&amp;thinsp;").replace("&shy;","&amp;shy;");
str=str.replace("&prime;","&amp;prime;");
str=str.replace(/&(#0?9[13];)/,"&amp;1ドル");
str=str.replace(/&(#0?12[345];)/,"&amp;1ドル");
returnstr;
}
addOnloadHook(function(){
if(document.forms.editform){
addPortletLink('p-cactions','javascript:format()','format','ca-format','Format article','',document.getElementById('ca-edit'));
}
});
/*
  • /

AltStyle によって変換されたページ (->オリジナル) /