User:Msh210/format.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.

  • 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.

document.getElementById('firstHeading').setAttribute('style','color:#604');
/* begin stuff copied from [[User:Connel MacKenzie/reformat.js]] and amended */
function minorFormat() {
 //bad lede:
 txt=txt.replace(/(\{\{(?:slim-)?wikipedia\}\}\n)\n*(==English==\n)/g, "$2$1");//copypasted from Mglovesfun's JS. Thanks Mg!
 //std whitespace for readability by editors and/or bots:
 txt = txt.replace(/ *(==+) */g, "$1");
 txt = txt.replace(/\n(==+[A-ȳ\ ]+==+)\<\!/g, "\n$1\n<!");
 txt = txt.replace(/===\n+\{\{(.*)\}\}\n+#/gi, "===\n{"+"{$1}}\n\n#");
 txt=txt.replace(/\n([\*\#\:\;]+)\ *([\'\{\[a-zA-Z])/g, "\n$1 $2");//Thanks, Conrad.Irwin.
 txt=txt.replace(/\n{3,}/g, "\n\n");
 //more readability:
 txt=txt.replace(/(\S)(\{\{defdate[|}])/g, "$1 $2");
 //txt = txt.replace(/\{\{etyl\|([a-z]{2,3})\|en\}\}/g, "{"+"{etyl|$1}}");//from [[user:Mglovesfun/vector.js]]; thanks, Mg
 //avoid using templates unnecessarily:
 txt=txt.replace(/\{\{w\|([^=\|\}]{1,9})\}\}/g, "["+"[w:$1|$1]]");
 txt=txt.replace(/\{\{w\|([^=\|\}]{1,9})\|([^=\|\}]{1,9})\}\}/g, "["+"[w:$1|$2]]");
 //inflection line replacements:
 txt = txt.replace(/\(''mass +noun''\)/gi, "{"+"{uncountable}}");
 txt = txt.replace(/([^'])''(m|f|c|p|s)''([^'])/g, "$1{"+"{g|$2}}$3");
 txt = txt.replace(/([^'])''pl''([^'])/g, "$1{"+"{g|p}}$2");
 txt = txt.replace(/''m, +f''/g, "{"+"{g|m|f}}");
 // txt = txt.replace(/\{\{mf\}\}/g, "{"+"{m|f}}");
 txt = txt.replace(/''(m|f) +(p|pl|plural)''/g, "{"+"{g|$1-p}}");
 txt = txt.replace(/\{\{(m|f|n|c)\}\} +\{\{(s|p)\}\}/gi, "{"+"{g|$1-$2}}");
 txt = txt.replace(/\{\{m\}\} +\{\{f\}\}/gi, "{"+"{g|m|f}}");
 txt = txt.replace(/\{\{m\}\} +\{\{f\|p\}\}/gi, "{"+"{g|m|f|p}}");
 // txt = txt.replace(/\{\{m\|f\}\} +\{\{p\}\}/gi, "{"+"{m|f|p}}");
 txt = txt.replace(/\(plural +\[/gi, "(''plural:'' [");
 txt = txt.replace(/\n\'\'\'\{\{\{2\|([^\[\{\}\]\']+)\}\}\}\'\'\'\n/g, "\n'''$1'''\n");
 txt=txt.replace(/\{\{infl\|/g, "{"+"{head|");
 //dates:
 txt = txt.replace(/\* *'''([1-9][0-9][0-9]*)'''\:/g, "* '''$1''',");
 txt = txt.replace(/\* *'''([1-9][0-9][0-9]*)\:'''/g, "* '''$1''',");
 txt=txt.replace(/\{\{ca\.([\|\}])/g, "{"+"{circa$1");//snap redirect
 txt=txt.replace(/\* *\'\'\'(circa|c\.|ca\.) ([0-9]*)(\,|\:|)\'\'\'/g, "* {"+"{circa|$2}}");
 txt=txt.replace(/\{\{(ante|post|circa)\}\} *([0-9]*)/g, "{"+"{$1|$2}}");
 txt=txt.replace(/\{\{(ante|post|circa)\|([A-Ea-e\.\,\ \-0-9]*)\}\}([\,\:])/g, "{"+"{$1|$2}}");//template adds a comma
 //extraneous punctuation:
 txt=txt.replace(/\{\{sense\|([a-zA-Z0-9 \-]*)\}\}\,/g, "{"+"{sense|$1}}");
 txt=txt.replace(/\{\{form of\|([a-zA-Z\ \-\,\'\[\]\(\)\"\/]*) of\|/g, "{"+"{form of|$1|");//template adds "of"
 txt=txt.replace(/\{\{proto\|([a-zA-Z\-]*)\|\*/g, "{"+"{proto|$1|");//template adds *
 //delink usexes:
 txt=txt.replace( /(\n\#+\:.* )\[\[([^|\[\]]+)\]\]( |''|\n)/g,
    function(all,first,term,last) {
     if (term==wgTitle) return ""+first+"'''"+term+"'''"+last;
     return ""+first+term+last;
     }
    );
 txt=txt.replace( /(\n\#+\:.* )\[\[([^|\[\]]+)\|([^|\[\]]+)\]\]( |''|\n)/g,
    function(all,first,term,alt,last) {
     if (term==wgTitle) return ""+first+"'''"+alt+"'''"+last;
     return ""+first+alt+last;
     }
    );
 //wrong list style:
 txt=txt.replace(/(s=====*\n)\#/g, "$1*");
 txt=txt.replace(/(lso|inks)(====?\n)\#/g, "$1$2*");
 //inline project links:
 txt=txt.replace(/\{\{PL\:/g, "{"+"{template:PL:");//necessary so the link will work
 txt=txt.replace(/\n\{\{pedia(lite|)([\}\|])/g, "\n* {"+"{pedia$2");//I saw Rukhabot do this, so stole it.
 txt=txt.replace(/\n\{\{template\:PL\:/g, "\n* {"+"{template:PL:");//as previous
 txt=txt.replace(/\[http\:\/\/en\.wikipedia\.org\/wiki\/([a-zA-Z0-9\_\-\:\'\(\)\/]*)\ ([^\]\{\}\|]*)\]/g, "[[w:$1|$2]]");
 txt=txt.replace(/\[\[w:([^\[|\]]+)\]\]/g, "[[w:$1|$1]]");
//snap redirects:
 txt=txt.replace(/\{\{serial[_ ]comma([}|])/g, "{"+"{,$1");
 txt = txt.replace(/sc=Polytonic([\}\|])/g, "sc=polytonic$1");
 txt=txt.replace(/\{\{checktrans-(mid|bottom)([\}\|])/g, "{"+"{trans-$1$2");
 txt=txt.replace(/\{\{qual([\}\|])/g, "{"+"{qualifier$1");
 txt=txt.replace(/\{\{en\-adj\-more([\}\|])/g, "{"+"{en-adj$1");
 txt=txt.replace(/\{\{en\-proper\-noun([\}\|])/g, "{"+"{en-proper noun$1");
 txt=txt.replace(/\{\{substub([\}\|])/g, "{"+"{sectstub$1");
 txt=txt.replace(/\{\{alt( spell|ernate spelling of)\|/g, "{"+"{alternative spelling of|");
 txt=txt.replace(/\{\{(template\:|)PL\:pedia([\}\|])/g, "{"+"{pedia$2");
 txt=txt.replace(/\{\{Wikipedia-inline([\}\|])/g, "{"+"{pedia$1");
 txt=txt.replace(/\{\{trans\-middle([\}\|])/g, "{"+"{trans-mid$1");
 txt=txt.replace(/\{\{trans\-bot([\}\|])/g, "{"+"{trans-bottom$1");
 txt=txt.replace(/\{\{Wikipedia([\|\}])/g, "{"+"{wikipedia$1");
 txt=txt.replace(/\{\{See([\|\}])/g, "{"+"{also$1");
 txt=txt.replace(/\{\{Flowbreak([\|\}])/g, "{"+"{flowbreak$1");
 txt=txt.replace(/\{\{non gloss definition([\|\}])/g, "{"+"{non-gloss definition$1");
 txt=txt.replace(/\{\{eye dialect([\|\}])/g, "{"+"{eye dialect of$1");
 txt=txt.replace(/\{\{sic(\.)?([\|\}])/g, "{"+"{SIC$2");
 txt=txt.replace(/\{\{pf\}\}/g, "{"+"{pf.}}");
//the next two lines have to be after the "alt spell" snap above
 txt=txt.replace(/\n\# *\{\{obsolete\}\} *\{\{alternative spelling of\|/g, "\n# {"+"{obsolete spelling of|");
 txt=txt.replace(/\n\# *\{\{archaic\}\} *\{\{alternative spelling of\|/g, "\n# {"+"{archaic spelling of|");
 txt=txt.replace(/\{\{see also\|/g, "{"+"{also|");
txt =
  txt.replace
  ( /^==([a-zA-Z ]+)==\n+(?:(?:===|[^=]).*\n+)*/gm,
    function(section, langname)
    { if(langname == 'English')
      return '' +
        section.replace
        ( /\{\{(rf(?:def|p|v-pronunciation)|homophones?|exthomophones|no[ _]entry)((?:\|(?:[^=}]|\{\{[^{}]+\}\})*)*\}\})/g,
          '{'+'{$1|lang=en$2'
        ).replace
        ( /\{\{rfap\}\}/g,
          '{'+'{rfap|lang=en}}'
        ).replace
        ( /\{\{rfe\}\}/g,
          '{'+'{rfe|en}}'
        ).replace
        ( /\{\{usex\|lang=en\|([^|}]+)\}\}/g,
          "''$1''"
        ).replace
        ( /\{\{usex\|([^|}]+)\|lang=en\}\}/g,
          "''$1''"
        );
      //else
      return '' +
        section.replace
        ( /\{\{rfap\}\}/g,
          '{'+'{rfap|lang={'+'{subst:#invoke:languages/templates|getByCanonicalName|'+langname+'}}}}'
        ).replace
        ( /\n\*( *(?:\{\{(?:sense|l)\|(?:\{\{[^{}]+\}\}|[^{}])*\}\},? *)*)\[\[([^#:\]]+)]]((?: *\{\{(?:gloss|qualifier|g)\|[^{}]*\}\}|\[\[[^\[\]]*\]\],? *)*\n)/g,
          '\n*$1{{l|{'+'{subst:#invoke:languages/templates|getByCanonicalName|'+langname+'}}|$2}}$3'
        ).replace
        ( /(\n\* *(?:\{\{(?:sense|l)\|(?:\{\{[^{}]+\}\}|[^{}])*\}\},? *)*)\[\[([^|#\]]+)#([^|\]]+)\|([^|#\]]+)\]\]/g,
          function(zero,one,two,three,four)
            {if(two==four) return ''+one+'{'+'{l|{'+'{subst:#invoke:languages/templates|getByCanonicalName|'+three+'}}|'+two+'}}';
             return ''+one+'{'+'{l|{'+'{subst:#invoke:languages/templates|getByCanonicalName|'+three+'}}|'+two+'|'+four+'}}';
            }
        ).replace
        ( /(\n(?:\* *)?)\[\[([^|#\]]+)#Hebrew\|([^|#\]]+)\]\] \{\{romanization of Hebrew\|([^|}=]+)\}\}/g,
          function(zero,one,two,three,four)
            {if(two==three) return ''+one+'{'+'{l|he|'+two+'|tr='+four+'}}';
             return ''+one+'{'+'{l|he|'+two+'|'+three+'tr='+four+'}}';
            }
        ).replace
        ( /([^=]===\n)\'\'\'([^']+)'''\n/g,
          function(zero,one,two)
            {if (two=wgTitle) return ""+one+"{"+"{head|{"+"{subst:#invoke:languages/templates|getByCanonicalName|"+langname+"}}}}\n";
             return ""+one+"{"+"{head|{"+"{subst:#invoke:languages/templates|getByCanonicalName|"+langname+"}}|head="+two+"}}\n";
            }
        );
    }
  );
//Many thanks to [[user:Ruakh]] for most of that last, and to kicken (in freenode's ##javascript) for an earlier version.
//The next line should be after the language-specific stuff just above.
  for (var i=0; i<9; i++) txt=txt.replace(/===Anagrams===\n+\*(.*)\n\*(.*)/gi, "===Anagrams===\n* $1, $2");
// headers
  txt=txt.replace(/\n===\{\{\{pos\|([a-zA-Z\ ]+)\}\}\}===\n/g, "\n===$1===\n");
  txt=txt.replace(/==\[\[([a-zA-ZÀ-ž\-\ ]+)\]\]==/g, "==$1==");
  txt = txt.replace(/\n\'\'\'(adjective|adverb|noun|verb)\'\'\'\n/gi, "\n===$1===\n");//will fix caps later
  txt = txt.replace(/\n\'\'\'(translation|translations)\'\'\'\n/gi, "\n===Translations===\n");
  txt = txt.replace(/\*\[\[IPA\]\]: \/\/\n\*\[\[SAMPA\]\]: \/\/\n/gi, "{"+"{rfp}}\n");
  txt=txt.replace(/\n==(=*[a-z])/g, function(a,b){return "\n==" + b.toUpperCase()});//thanks, Sorella (in freenode's ##javascript)
  txt = txt.replace(/==(adjective form|adjective phrase|adjectivial phrase)==/gi, "==Adjective==");
  txt = txt.replace(/==(adverbs|adverb phrase|adverbal phrase|adverbial phrase|adverbial participle)==/gi, "==Adverb==");
  txt = txt.replace(/==antonym==/gi, "==Antonyms==");
  txt = txt.replace(/==definite article==/gi, "==Article==");
  txt = txt.replace(/==(derived expression|derived expression|derived expressions)==/gi, "==Derived terms==");
  txt = txt.replace(/==(derived term|derived word|derived words)==/gi, "==Derived terms==");
  txt = txt.replace(/==derivation==/gi, "==Etymology==");
  txt = txt.replace(/==External link==/gi, "==External links==");
  txt = txt.replace(/==(compound noun|noun form|noun phrase|nominal phrase)==/gi, "==Noun==");
  txt = txt.replace(/==proper name==/gi, "==Proper noun==");
  txt = txt.replace(/==(quotation|citation|citations)==/gi, "==Quotations==");
  txt = txt.replace(/==(related words|related term)==/gi, "==Related terms==");
  txt = txt.replace(/==symbols==/gi, "==Symbol==");
  txt = txt.replace(/==synonym==/gi, "==Synonyms==");
  txt = txt.replace(/==(usage|usages|usage *note|usage *notes)==/gi, "==Usage notes==");//Usage Notes
  txt = txt.replace(/==(forms and variants|spellings)==/gi, "==Alternative forms==");
  txt = txt.replace(/==(alternate|alternative|variant|other) (form|forms|spelling|spellings)==/gi, "==Alternative forms==");
  txt = txt.replace(/==spelling (variant|variants|variation|variations)==/gi, "==Alternative forms==");
  txt = txt.replace(/==(reference|refrences)==/gi, "==References==");
  //correct header levels
  txt = txt.replace(/==(Adjective|Adverb|Derived terms|Etymology|External links|Verb)==\n/gi, "===$1===\n");
  txt = txt.replace(/==(Noun|Phrase|Pronunciation|See also|Related terms|References|Symbol)==\n/gi, "===$1===\n");
  txt = txt.replace(/==(Alternative forms|Synonyms|Translations|Usage notes|Descendants)==\n/gi, "===$1===\n");
  txt = txt.replace(/===(Synonyms|Translations)===\n/gi, "====$1====\n");
 //add language names to Descendants sections:
 //many thanks to [[user:Ruakh]] for a script (up this page) which I've adopted to:
txt =
  txt.replace
  ( /^==+Descendants==+\n+(?:[^=].*\n+)*/gm,
    function(section)
    { return '' +
        section.replace
        ( /\n\* *\{\{(l|onym)\|([a-z]{2,3})\|/g,
          '\n* {' + '{subst:$2}}: {' + '{$1|$2|'
        ).replace
        ( /\n\* *(\[\[[^\{\[|\#\]\}]+\#)([A-Z][A-Za-z \-]*)(\|[^\{\[|\#\]\}]+\]\])\n/g,
          '\n* $2: $1$2$3'
        );
    }
  );
 //remove 'to' from English verb infl lines
 txt=txt.replace(/\{\{en-verb\|([ -\;A-\|]*)(head|inf)=to /g, "{"+"{en-verb|$1$2=");//thanks, user:Yair_rand
 txt=txt.replace(/\{\{en-verb\|([ -\;A-\|]*)(head|inf)=\[\[to\]\] /g, "{"+"{en-verb|$1$2=");//thanks, user:Yair_rand
 txt=txt.replace(/\{\{(head|infl)\|en\|verb\|head=to /g, "{"+"{head|en|verb|head=");
 txt=txt.replace(/\{\{(head|infl)\|en\|verb\|head=\[\[to\]\] /g, "{"+"{head|en|verb|head=");
 //delink usexes:
 txt=txt.replace(/\n(\#+\: *\'\'.*)\[\[([^\]|]+\|)([^\]|]+)]]/g, "\n$1{"+"{subst:#ifeq:$2{"+"{PAGENAME}}|'''$3'''|$3}}");
 txt=txt.replace(/\n(\#+\: *\'\'.*)\[\[([^\]|]+)]]/g, "\n$1{"+"{subst:#ifeq:$2|{"+"{PAGENAME}}|'''$2'''|$2}}");
 //should manually fix this before saving, of course:
 txt=txt.replace(/(See also=+\n)\*? *(\{\{pedia[\|\}])/g, "$1* {"+"{attention|en|Wiktionary:Votes/2011-07/External_links}}$2");
 //English three-letter words:
 if (((wgPageName.replace(/[’\!-/\:-\?\[-\`\{-\¡\ a-zA-Z]/g, "").length==0) && (wgPageName.replace(/[’\!-/\:-\?\[-\`\{-\¡\ ]/g, "").length==3)) && ((txt.indexOf('English three-letter words')<0) && (txt.indexOf('English_three-letter_words')<0))) txt=txt.replace(/(==English==\n|==\{\{subst:en\}\}==\n)/, "==English==\n["+"[Category:English three-letter words]]{"+"{rfc-auto}}\n");
}

function superAutoFormat() {
    switch(wgAction){case 'submit': case 'edit': break; default: return};
 
  // is this in the main namespace?  If not, stop.
  if (wgNamespaceNumber) return;

  // If editing a section, skip all of everything.
  pagetitle = $('#firstHeading').text();//Thanks, Ruakh!
  if (pagetitle.search(/\(section\)/) != -1) return;
 
  txt = " " + document.editform.wpTextbox1.value;
  txt = txt.substr (1, txt.length-1);
  oldtxt = txt;
  if (txt.length < 2) txt = " " + txt;
 
  //safety valves
  if (txt.search(/^#redirect/i) != -1) return;
 
  minorFormat();
 
 if (txt == " ") txt = "";
 
 document.editform.wpTextbox1.value = txt;

  if (txt != oldtxt) {
    if (document.editform.wpSummary.value.search(/User:/g) == -1) {
      if (document.editform.wpSummary.value != "") document.editform.wpSummary.value += "; ";
      document.editform.wpSummary.value += "[[User:Msh210/format.js|semiautomated]] fixes";
      }
    setTimeout( "document.editform.wpDiff.click();", 2000 );
  }
}

$( superAutoFormat );
/* end stuff copied from User:Connel MacKenzie/reformat.js */