Jump to content

User:Plastikspork/monobook.js/script.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// ---------------------------------------  --------------------------------------- //
// ----------------------------------- Credits ------------------------------------ //
//
// This script was created by modifying the following scripts:
//      [[User:Lightmouse/monobook.js/script.js]]
//      [[Wikipedia:WikiProject User scripts/Scripts/Formatter]]
//
// This script is intended to be complimentary to these scripts with very little
// to no duplication of function.

// ---------------------------------- Disclaimer ---------------------------------- //
//
// Use at your own risk and make sure you check the edit changes before you save
//
// Let me know [[User_Talk:Plastikspork]] if you find bugs!

// ----------------------------- Installing the Script ---------------------------- //
//
// (1) Open/Create your USERNAME/monobook.js page, where USERNAME is your username.
//     A quick way to get there is to go to your user page, then append
//     '/monobook.js' to the end of the URL.
//
// (2) Put the following command on your monobook.js page:
//          importScript('User:Plastikspork/monobook.js/script.js');
//
// (3) Save the page and reload it by following the instructions at the top of your
//     monobook.js page.  For example, Ctrl+Shift+R in Firefox.

// ------------------------------- Using the Script ------------------------------- //
//
// (1) Open an article which you would like to edit, and you should see a bunch of
//     'Spork' buttons in your 'toolbox' on the left side of your browser.
//
// (2) Click on one of the Spork buttons and the script will run, performing edits
//     by pattern matches.  When it is finished, it will show you the changes and
//     add some comments to the edit summary.  It is up to you to accept these
//     changes as is by clicking 'Save page', modify the changes by further editing
//     the edit form, or reject by leaving the page.
//
//  Note: The script will run very slowly on very large pages, but it does always
//        eventually complete in my experience.

// ----------------------------- Current Functionality ---------------------------- //
//
// [Sprk: WS] Removes excessive non-essential whitespace, non-breaking space, and
//        some minor wikification.
//
// [Sprk: Wki/Lnk Rpair] Wikification, fixes of simple repeated references,
//        conversion of external wikilinks to wikilinks, and simplification of
//        piped wikilinks.
//
// [Sprk: [] to Refs] Replaces [URL] with <ref>URL</ref>
//
// [Sprk: Cite Templte] Adds citation templates to some common
//        untemplated references.
//
// [Sprk: Ref Repeat] Fixes obvious repeated references and marks
//        non-obvious ones for human inspection with REPEAT tags.
//        Once the script runs, you can inspect the tagged refs to
//        see if they are actual repeats, or if they are false
//        positives.
//
// [Sprk: Color] Simplifies some hex color codes to named colors.
//
// [Sprk: Table to Wiki] An attempt to turn HTML tables into
//        wikitables.  Requires some hand tuning after it runs.
//
// ----------------------- Turning off and Renaming Buttons ----------------------- //
//
// The buttons in the script can be selectively disabled and renamed, to rename a
// button, say 'Sprk: WS' to 'Spork: Whitespace', paste the following into your
// monobook.js:
//
//  if( typeof( SporkConfig ) == 'undefined' ) SporkConfig = {};
//  SporkConfig.ws = "Spork: Whitespace";
//
// To rename more buttons, just add an additional line for each button. The initial
// creation of the empty structure only needs to be performed once.
//
// To remove a button, give it a null string for its name. For example,
//
//  if( typeof( SporkConfig ) == 'undefined' ) SporkConfig = {};
//  SporkConfig.ws = "";
//
// will turn off the 'Sprk: WS' button.
//
// The variable names for each button can be found in the code directly below.
// ---------------------------------------  --------------------------------------- //

// Set Default Button Names
if( typeof( SporkConfig ) == 'undefined' ) SporkConfig = {};
if( typeof( SporkConfig.wlr  ) == 'undefined' ) SporkConfig.wlr  = "Sprk: Wki/LinkRepair";
if( typeof( SporkConfig.b2r  ) == 'undefined' ) SporkConfig.b2r  = "Sprk: [] to Refs";
if( typeof( SporkConfig.ct   ) == 'undefined' ) SporkConfig.ct   = "Sprk: CiteTemplate";
if( typeof( SporkConfig.rr   ) == 'undefined' ) SporkConfig.rr   = "Sprk: Ref Repeat";
if( typeof( SporkConfig.ws   ) == 'undefined' ) SporkConfig.ws   = "Sprk: WS";
if( typeof( SporkConfig.c    ) == 'undefined' ) SporkConfig.c    = "Sprk: Color";
if( typeof( SporkConfig.t2w  ) == 'undefined' ) SporkConfig.t2w  = "Sprk: Table to Wiki";
if( typeof( SporkConfig.noa  ) == 'undefined' ) SporkConfig.noa  = ""; // testing 1
if( typeof( SporkConfig.tst  ) == 'undefined' ) SporkConfig.tst  = ""; // testing 2
if( typeof( SporkConfig.fullurl  ) == 'undefined' ) SporkConfig.fullurl  = ""; // testing 3

// ---------------------------------------  --------------------------------------- //
// Import Spork-Tools scripts

importScript('User:Plastikspork/tools.js');
importScript('User:Plastikspork/whitespace.js');
importScript('User:Plastikspork/citetools.js');
importScript('User:Plastikspork/reftools.js');
importScript('Wikipedia:AutoEd/htmltowikitext.js');
importScript('Wikipedia:AutoEd/tablestowikitext.js');
importScript('Wikipedia:AutoEd/extrabreaks.js');
importScript('Wikipedia:AutoEd/headlines.js');
importScript('Wikipedia:AutoEd/unicodify.js');
importScript('Wikipedia:AutoEd/unicodehex.js');
importScript('Wikipedia:AutoEd/wikilinks.js');

// ---------------------------------------  --------------------------------------- //


// Spork: Wiki/LinkRepair
function spork_wiki_link_repair(clk){
  var txt = document.editform.wpTextbox1;
  txt.value = autoEdUnicodify(txt.value);           //See WP:AutoEd/unicodify.js
  txt.value = autoEdHTMLtoWikitext(txt.value);      //See WP:AutoEd/htmltowikitext.js
  txt.value = autoEdHeadlines(txt.value);           //See WP:AutoEd/headlines.js
  txt.value = spork_external_links(txt.value);
  txt.value = spork_extern_wikilinks_to_wikilinks(txt.value); // See User:Plastikspork/tools.js
  txt.value = spork_ref_clean(txt.value);
  txt.value = autoEdUnicodeHex(txt.value);          //See WP:AutoEd/unicodehex.js
  txt.value = spork_special_char(txt.value);
  txt.value = autoEdWikilinks(txt.value);           //See WP:AutoEd/wikilinks.js
  txt.value = spork_convert(txt.value);
  txt.value = spork_lists(txt.value);

  spork_edit_summary("wikification");
  spork_edit_summary("link repair");

  if(clk) document.editform.wpDiff.click();
}

// Spork: [] to ref
function spork_bracket_to_ref(clk){
  var txt = document.editform.wpTextbox1;

  txt.value = spork_bracket_url_to_ref(txt.value);

  spork_edit_summary("wikification");

  if(clk) document.editform.wpDiff.click();
}

// Spork: CiteTemplate
function spork_citation_template(clk){
  var txt = document.editform.wpTextbox1;

  txt.value = txt.value.replace(/([\[]+http[^\[\]<>]*)(<!-- Bot generated title -->)([\]]+)/gi, '$1$3$2');
  txt.value = txt.value.replace(/(<ref[^<>\/]*>[^<>]*)(<!-- Bot generated title -->)([^<>]*<\/ref>)/gi, '$1$3$2');
  txt.value = spork_cite_web(txt.value);
  txt.value = spork_cite_article(txt.value);
  txt.value = txt.value.replace(/(<ref[^<>\/]*>[^<>]*)(<\/ref>)(<!-- Bot generated title -->)/gi, '$1$3$2');
  txt.value = txt.value.replace(/(\{\{cite[^}]*\|[ ]*title[ ]*=[^\|}]*)([^\|}]*?\}\}[^<>]*)(<!-- Bot generated title -->)/gi, '$1$3$2');
  txt.value = txt.value.replace(/([\[]+http[^\[\]<>]*)([\]]+)(<!-- Bot generated title -->)/gi, '$1$3$2');

  spork_edit_summary("cite template");

  if(clk) document.editform.wpDiff.click();
}

// Spork: Ref Repeat
function spork_ref_repeat(clk){
  var txt = document.editform.wpTextbox1;

  txt.value = spork_ref_clean(txt.value);

  // Mark duplicate ref names with NAME REPEAT1 REPEAT2 (change to name by hand and re-run script)
  txt.value=txt.value.replace(/<ref name[ ]*=[ ]*(["'])([^'"<>]*)\1[ ]*>((?:.|[\r\n])*)<ref name[ ]*=[ ]*\1\2\1[ ]*>/gim, '<ref name=$1$2$1><!-- NAME REPEAT1 -->$3<ref name=$1$2$1><!-- NAME REPEAT2 -->');
  // Mark repeated references with REPEAT (change to name by hand and re-run script)
  txt.value=txt.value.replace(/<ref>([^<>]*)<\/ref>((?:.|[\r\n])*)<ref>\1<\/ref>/gim, '<ref><!--REPEAT-->$1</ref>$2<ref>$1</ref>');
  // Mark possible repeated references with REPEAT1 REPEAT2 (change to name by hand and re-run script)
  txt.value=txt.value.replace(/(<ref[^<>]*>)(|[^<>]*[^<>\/])(https?:[^ <>\[\]\|]*)([ \|}\]][^<>]*)<\/ref>((?:.|[\r\n])*)(<ref[^<>]*>)(|[^<>]*[^<>\/])\3([ \|}\]][^<>]*)<\/ref>/gim, '$1<!--?REPEAT1?-->$2$3$4</ref>$5$6<!--?REPEAT2?-->$7$3$8</ref>');

  spork_edit_summary("ref simplification");

  if(clk) document.editform.wpDiff.click();
}

// Spork: WS
function spork_ws_compress(clk){
  var txt = document.editform.wpTextbox1;

  txt.value = spork_whitespace(txt.value);           //See User:Plastikspork/whitespace.js
  txt.value = spork_ws_refs(txt.value);              //See User:Plastikspork/whitespace.js
  txt.value = spork_ws_nowrap(txt.value);            //See User:Plastikspork/whitespace.js
  txt.value = txt.value.replace(/<[\/\\ ]*br[ ]+clear[ ]*=["' ]*all['" ]*\/?>/gi, '\{\{-\}\}');
  txt.value = autoEdExtraBreaks(txt.value);          //See WP:AutoEd/extrabreaks.js
  txt.value = spork_ws_tables_infoboxes(txt.value);  //See User:Plastikspork/whitespace.js
  txt.value = autoEdUnicodeHex(txt.value);           //See WP:AutoEd/unicodehex.js
  txt.value = spork_special_char(txt.value);
  txt.value = spork_whitespace(txt.value);           //See User:Plastikspork/whitespace.js

  spork_edit_summary("wikification");
  spork_edit_summary("WS removal");

  if(clk) document.editform.wpDiff.click();
}

// Spork: Color
function spork_color(clk){
  var txt = document.editform.wpTextbox1;

  txt.value = spork_color_simple(txt.value);

  spork_edit_summary("color simplification");

  if(clk) document.editform.wpDiff.click();
}

// Spork: Table to Wiki
function spork_wiki_table(clk){
  var txt = document.editform.wpTextbox1;

  txt.value = autoEdTablestoWikitext(txt.value);

  spork_edit_summary("wikification");

  if(clk) document.editform.wpDiff.click();
}

// Spork: No Align
function spork_no_align(clk){
  var txt = document.editform.wpTextbox1;

  txt.value=txt.value.replace(/([\r\n]\||\|\||[\r\n]!|!!|\|-)([ ]*[a-z][^\|!]* |[ ]*)align[ ]*=["' ]*(?:center|left|right)['" ]*/gi, '$1$2 ');
  txt.value=txt.value.replace(/([\r\n]\||\|\||[\r\n]!|!!|\|-)([ ]*[a-z][^\|!]* |[ ]*)(style[ ]*=["' ]*)([ ]*[a-z][^'"\|!]*;|[ ]*)text-align[ ]*:[ ]*(?:center|left|right);?[ ]*/gi, '$1$2$3$4');
  txt.value=txt.value.replace(/([^a-z])style[ ]*=[ ]*["'][ ]*['"]/gi, '$1 ');
  txt.value=txt.value.replace(/([\r\n]\||\|\||[\r\n]!|!!|\|-)[ ]+\|([^\|])/g, '$1 $2');
  txt.value=txt.value.replace(/<[\/]*center[ ]*>/gi, '');
  txt.value=txt.value.replace(/([\r\n]\||\|\||[\r\n]!|!!|\|-)[ ]+([\r\n])/g, '$1$2');
  txt.value=txt.value.replace(/<span [^<>]*>/g, '');
  txt.value=txt.value.replace(/<\/span>/g, '');

  spork_edit_summary("table simplification");
  if(clk) document.editform.wpDiff.click();
}

// Spork: Testing
function spork_testing(clk){
    // Spork: WS
    spork_ws_compress(0);
    // Spork: Wiki/Link/Repair
    spork_wiki_link_repair(0);
    // Spork: [] to Refs
    spork_bracket_to_ref(0);
    // Spork: CiteTemplate
    spork_citation_template(0);
    // Spork: Ref Repeat
    spork_ref_repeat(0);
    // Spork: Color
    spork_color(0);
    // Spork: WS
    spork_ws_compress(0);

    // WP:DATE
    var txt = document.editform.wpTextbox1;
//    txt.value = txt.value.replace(/(\(|years_?active[\s]*=[\s]*)([12][0-9]{3})[\s]*(?:[–—\-–]| to[\s]+present|[–—\-–][\s]*present)([\s]*\)|[\s]*\||[\s]*\}\}|[\s]*$)/gi, '$1since $2$3');

    //txt.value=txt.value.replace(/\[http:\/\/www\.hulu\.com\/watch[^ \[\]]*\]/g, '');
    //txt.value=txt.value.replace(/\[http:\/\/www\.hulu\.com\/watch[^ \[\]]* ([^\[\]]*)\]/g, '$1');

    document.editform.wpSummary.value = "Script assisted wikification";
    if(clk) document.editform.wpDiff.click();
}

// Spork: Fullurl Template
function spork_fullurl(clk){
    // WP:DATE
    var txt = document.editform.wpTextbox1;
    var loopcount = 0;

    txt.value = txt.value.replace(/\{\{SERVER\}\}\/wiki\/([^\?]*)\?([^\[\]{}\|\r\n ]*)([}\]\| \r\n])/gi, '\{\{fullurl:$1|$2\}\}$3');
    txt.value = txt.value.replace(/http:\/\/en\.wikipedia\.org\/wiki\/([^\?]*)\?([^\[\]{}\|\r\n ]*)([}\]\| \r\n])/gi, '\{\{fullurl:$1|$2\}\}$3');
    txt.value = txt.value.replace(/http:\/\/en\.wikipedia\.org\/w\/index\.php\?title=([^&]*)&([^\[\]{}\|\r\n ]*)([}\]\| \r\n])/gi, '\{\{fullurl:$1|$2\}\}$3');
    txt.value = txt.value.replace(/https:\/\/secure\.wikipedia\.org\/wikipedia\/en\/wiki\/([^\?]*)\?([^\[\]{}\|\r\n ]*)([}\]\| \r\n])/gi, '\{\{fullurl:$1|$2\}\}$3');
    loopcount = 0;
    while( txt.value.search( /\{\{fullurl:Special:Search\|[^}]*&search=[^&}]*%20/gi ) >= 0 && loopcount <= 10 ) {
      txt.value = txt.value.replace(/(\{\{fullurl:Special:Search\|[^}]*&search=[^&}]*)%20/gi, '$1+');
      loopcount++;
    }
    loopcount = 0;
    while( txt.value.search( /\{\{fullurl:Special:Search\|[^}]*&search=[^&}]*%27/gi ) >= 0 && loopcount <= 10 ) {
      txt.value = txt.value.replace(/(\{\{fullurl:Special:Search\|[^}]*&search=[^&}]*)%27/gi, "$1'"); // "'"
      loopcount++;
    }

    document.editform.wpSummary.value = "Script assisted wikification";
    if(clk) document.editform.wpDiff.click();
}


// --------------------------------- WS Functions --------------------------------- //

// Called by Spork: WS
function spork_special_char(str) {
  str=str.replace(/([a-z][ ]*) -- ([ ]*[a-z])/gi, '$1 – $2');
  str=str.replace(/&mdash;/gi, '—');
  str=str.replace(/&ndash;/gi, '–');
  // str=str.replace(/[‘’]/g, "'"); //'
  // str=str.replace(/[“”]/g, '"'); //"

  return str;
}

// Called by Spork: Wiki/LinkRepair
function spork_external_links(str) {
  // Common broken links
  str=str.replace(/(\[https?:[^\[\]]+\])([a-z])/gi, '$1 $2');
  str=str.replace(/(\[https?:\/\/[^\]\[\| ]+)[\s]*(?:\||\[)[\s]*([^\]\[\|]*\])/gi, '$1 $2');
  // Convert links to secure wikipedia to standard wikipedia
  str=str.replace(/\https:\/\/secure.wikimedia.org\/wikipedia\/([a-z]+)\//g, 'http://$1.wikipedia.org/');
  // Linking to upload page rather than image page
  str=str.replace(/http:\/\/upload\.wikimedia\.org\/wikipedia\/commons\/[a-z0-9]\/[a-z0-9][a-z0-9]\//gi, 'http://commons.wikimedia.org/wiki/Image:');
  // Punctuation inside external links
  str=str.replace(/(\[https?:[^\[\]\| ]*)[\| ][ ]*([']+|["]+)([^"'\|\[\]]*)\2([:\.,\? ]*\])/g, '$2$1 $3$4$2');
  str=str.replace(/(\[https?:[^\[\]\| ]*)[\| ][ ]*([‘’]|[“])([^“’‘\|\[\]]*)([‘’]|[”])([:\.,\? ]*\])/g, '$2$1 $3$5$4');
  // Extra brackets around references
  str=str.replace(/\[(\[)(http|ftp|https)(:[^\[\]]+\])\]/gi, '$1$2$3');
  // Extra brackets inside references
  str=str.replace(/(<ref[^<>\/]*>)\[([^\[\]<> ]*)\](<\/ref>)/gi, '$1$2$3');

  // Convert "external wikipedia" image references to (PIC) links
  str=str.replace(/(\[)http:\/\/(commons)\.wikimedia\.org\/wiki\/(Image:[^ \[\]\|<>]*)(\])/gi, '($1$1$2:$3|pic$4$4)');
  str=str.replace(/(\[)http:\/\/en\.wikipedia\.org\/wiki\/Image(:[^ \[\]\|<>]*)(\])/gi, '($1$1:File$2|pic$3$3)');

  return str;
}

// Called by Spork: Wiki/LinkRepair
function spork_convert(str) {
  // Template Fun
  str=str.replace(/ ([1-9])[ ]*'[ ]*([1-9]|1[01])[ ]*"[ ]*\([12]\.[0-9][0-9]?[ ]*m\)/g, ' \{\{height|ft=$1|in=$2\}\}'); // " '
  str=str.replace(/ ([0-9][0-9,\.]*)(&nbsp;| )+(\[\[)square mile(\]\])s[ ]+\([0-9][0-9,\.]*(&nbsp;| )+(\[\[)km²(\]\])\)/g, ' \{\{convert|$1|sqmi|km2|lk=on\}\}');
  str=str.replace(/ ([0-9][0-9,\.]*)(&nbsp;| )+(\[\[)square mile(\]\])s[ ]+\([0-9][0-9,\.]*(&nbsp;| )+km²\)/g, ' \{\{convert|$1|sqmi|km2|lk=in\}\}');
  str=str.replace(/ ([0-9][0-9,\.]*)(&nbsp;| )+square miles[ ]+\([0-9][0-9,\.]*(&nbsp;| )+(\[\[)km²(\]\])\)/g, ' \{\{convert|$1|sqmi|km2|lk=out\}\}');
  str=str.replace(/ ([0-9][0-9,\.]*)(&nbsp;| )+square miles[ ]+\([0-9][0-9,\.]*(&nbsp;| )+km²\)/g, ' \{\{convert|$1|sqmi|km2\}\}');
  str=str.replace(/ ([0-9][0-9,\.]*) people per square mile \([0-9][0-9,\.]*\/km²\)/g, ' \{\{convert|$1|PD/sqmi|/km2\}\}');
  str=str.replace(/ ([0-9][0-9,\.]+) people per square mile/g, ' \{\{convert|$1|PD/sqmi|/km2\}\}');
  str=str.replace(/ ([0-9][0-9,\.]*)\/sq&nbsp;mi \([0-9][0-9,\.]*\/km²\)/g, ' \{\{convert|$1|/sqmi|/km2\}\}');
  str=str.replace(/(\{\{convert\|[0-9]+),/g, '$1');

  return str;
}

// Called by Spork: Wiki/LinkRepair
function spork_lists(str) {

  // Wikify bullet lists
  str=str.replace(/([\r\n])•(.*[\r\n])[\r\n]/gim, '$1* $2');
  // Convert all-bold lines to use semicolon markup
  str=str.replace(/([\r\n])'''([^':]*)[']*[ ]*<[\\\/\. ]*BR[^<>]*>([\r\n])/gi, '$1;$2$3');
  str=str.replace(/([\r\n])'''([^':]*)[']*[ ]*([\r\n])/gi, '$1;$2$3');

  return str;
}

// ------------------------------- Color  Functions ------------------------------- //

// Called by Spork: Color
function spork_color_simple(str){

 str=str.replace(/([\r\n]\||[\r\n]!|\|\||!!)([^\|!<>\[\]]*)style[ ]*=[" ]*color[ ]*:[ ]*([a-z0-9\#]*)[; ]*[ "]*([^\|!<>\[\]]*\|)([^\|])/gi, '$1$2 $4<font color="$3">$5');
 str=str.replace(/([\r\n]\||[\r\n]!|\|\||!!)([^\|!<>\[\]]*style[ ]*=[ ]*"[^"<>\|!]*;)[ ]*color[ ]*:[ ]*([a-z0-9\#]*)[ ;]*([^\|!<>\[\]]*\|)([^\|])/gi, '$1$2$4<font color="$3">$5');
 str=str.replace(/<span style=[ "]*(background|background-color):([^ ";]*)[; ]*[" ]*>((&nbsp;){1,4}|\{\{nbsp\|[1-4]\}\})<\/span>/gi, '\{\{colorbox|$2\}\}'); //"
 str=str.replace(/<span style="(background|background-color):([^ ";]*)[; ]*">([^<>]*)<\/span>/gi, '\{\{colorbox|$2|$3\}\}'); //"
 str=str.replace(/bgcolor="(transparent|none)"/gi, 'style="background: $1"');
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?000000\2/gi,  '$1$2black$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?0000FF\2/gi,   '$1$2blue$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?A52A2A\2/gi,  '$1$2brown$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?FF7F50\2/gi,  '$1$2coral$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?00FFFF\2/gi,   '$1$2cyan$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?FF00FF\2/gi,'$1$2fuchsia$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?FFD700\2/gi,   '$1$2gold$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?808080\2/gi,   '$1$2gray$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?008000\2/gi,  '$1$2green$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?00FF00\2/gi,   '$1$2lime$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?FF00FF\2/gi,'$1$2magenta$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?800000\2/gi, '$1$2maroon$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?000080\2/gi,   '$1$2navy$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?808000\2/gi,  '$1$2olive$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?FFA500\2/gi, '$1$2orange$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?FFC0CB\2/gi,   '$1$2pink$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?800080\2/gi, '$1$2purple$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?FF0000\2/gi,    '$1$2red$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?C0C0C0\2/gi, '$1$2silver$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?D2B48C\2/gi,    '$1$2tan$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?008080\2/gi,   '$1$2teal$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?EE82EE\2/gi, '$1$2violet$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?FFFFFF\2/gi,  '$1$2white$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?FFFF00\2/gi, '$1$2yellow$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?FFF5EE\2/gi,'$1$2seashell$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?F08080\2/gi,'$1$2lightcoral$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\||color[ ]*=|color[ ]*:)[ ]*("?)#?D2691E\2/gi,'$1$2chocolate$2'); // "
 str=str.replace(/(color[ ]?box[ ]*\|)[ ]*"([^\|}{]*)"/gi, '$1$2');

 // Font tag outside of footnote
 str=str.replace(/(<font[^<>]*>[ ]*)(\[\[#fn[^\|\]]*\|)/gi, '$2$1');

 return str;
}

// ------------------------------ testing  Functions ------------------------------ //

function spork_newlines(str) {
  var loopcount = 0;
  // Extra newlines after section headings
  str = str.replace(/([\r\n]=.*=[\r\n])[\t ]*[\r\n][\t ]*([\r\n])/gim, '$1$2');

  // newlines in citation templates (multiple times)
  str = str.replace(/(\{\{(?:cite|citation))[\r\n]/gim, '$1 ');
  loopcount = 0;
  while( str.search( /\{\{(?:cite|citation) [^}]*[\r\n]/gi ) >= 0 && loopcount <= 10 ) {
    str=str.replace(/(\{\{(?:cite|citation) [^}]*)[\r\n]/gim, '$1 ');
    loopcount++;
  }

  // Extra newlines after before bullets
  str=str.replace(/([\r\n]\*.*[\r\n])[\t ]*[\r\n](\*)/gim, '$1$2');
  // Extra newlines before semicolon marked bold lines
  str=str.replace(/([\r\n])([#;:\*].*[\r\n])[ ]*[\r\n]([#;:\*])/gim, '$1$2$3');

  return str;
}

// ----------------------------- interface  Functions ----------------------------- //

function spork_edit_summary(summary){
// Add a tag to the summary box
  var txt = document.editform.wpSummary;
  var fullsummary = "Script assisted " + summary;
  if (txt.value.indexOf(summary) == -1) {
    if (txt.value.match( /Script assisted [A-Za-z]/ )) {
        txt.value = txt.value.replace( /Script assisted /, fullsummary + "/");
    } else if (txt.value.match(/[^\*\/\s][^\/\s]?\s*$/)) {
	txt.value += " | " + fullsummary;
    } else {
        txt.value += fullsummary;
    }
  }
}

$(function () {
  if(document.forms.editform && mw.config.get('wgUserName') == 'Plastikspork') {
    if( SporkConfig.ws != "" )
      mw.util.addPortletLink('p-tb', 'javascript:spork_ws_compress(1)', SporkConfig.ws);
    if( SporkConfig.wlr != "" )
      mw.util.addPortletLink('p-tb', 'javascript:spork_wiki_link_repair(1)', SporkConfig.wlr);
    if( SporkConfig.b2r != "" )
      mw.util.addPortletLink('p-tb', 'javascript:spork_bracket_to_ref(1)', SporkConfig.b2r);
    if( SporkConfig.ct != "" )
      mw.util.addPortletLink('p-tb', 'javascript:spork_citation_template(1)', SporkConfig.ct);
    if( SporkConfig.rr != "" )
      mw.util.addPortletLink('p-tb', 'javascript:spork_ref_repeat(1)', SporkConfig.rr);
    if( SporkConfig.c != "" )
      mw.util.addPortletLink('p-tb', 'javascript:spork_color(1)', SporkConfig.c);
    if( SporkConfig.t2w != "" )
      mw.util.addPortletLink('p-tb', 'javascript:spork_wiki_table(1)', SporkConfig.t2w);
    if( SporkConfig.noa != "" )
      mw.util.addPortletLink('p-tb', 'javascript:spork_no_align(1)', SporkConfig.noa);
    if( SporkConfig.tst != "" )
      mw.util.addPortletLink('p-tb', 'javascript:spork_testing(1)', SporkConfig.tst);
    if( SporkConfig.fullurl != "" )
      mw.util.addPortletLink('p-tb', 'javascript:spork_fullurl(1)', SporkConfig.fullurl);
  }
});