User:Darkest Hour/format.js

Hello, you have come here looking for the meaning of the word User:Darkest Hour/format.js. In DICTIOUS you will not only get to know all the dictionary meanings for the word User:Darkest Hour/format.js, but we will also tell you about its etymology, its characteristics and you will know how to say User:Darkest Hour/format.js in singular and plural. Everything you need to know about the word User:Darkest Hour/format.js you have here. The definition of the word User:Darkest Hour/format.js will help you to be more precise and correct when speaking or writing your texts. Knowing the definition ofUser:Darkest Hour/format.js, as well as those of other words, enriches your vocabulary and provides you with more and better linguistic resources.
function format() {
    var txt = 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);
    document.editform.wpSummary.value = "Formatting";
    document.editform.submit();
}

function whitespace(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, "\n$1");

    str = str.replace(/^ ? ? \n/gm, "\n");
    str = str.replace(/\n\n\*/g, "\n*");
    str = str.replace(/+/g, " ");
    str = str.replace(/(\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(/()(—|—|—|—|—|–|–|–|–|–)()/g, "$1 $2 $3");

    return trim(str);
}

function entities(str){
    //str = str.replace(//g, "");
    str = str.replace(/–|–|–/g, "–");
    str = str.replace(/—|—|—/g, "—");
   // str = str.replace(/(cm| m|km|mi)<sup>2</sup>/g, "$1²");
    str = str.replace(/&sup2;/g, "²");
    str = str.replace(/&deg;/g, "°");

    return trim(str);
}

//Fix ==See also== and similar section common errors.
function fixheadings(str)
{
  if( !str.match(/= ?See also ?=/) )
    str = str.replace(/(== ?)(see also:?|related topics:?|related articles:?|internal links:?|also see:?)( ?==)/gi, "$1See also$3");

  str = str.replace(/(== ?)(external links:?|outside links|web ?links:?|exterior links:?)( ?==)/gi, "$1External links$3");
  str = str.replace(/(== ?)(external link:?|web ?link:?|exterior link:?)( ?==)/gi, "$1External link$3");
  str = str.replace(/(== ?)(reference:?)(s? ?==)/gi, "$1Reference$3");
  str = str.replace(/(== ?)(source:?)(s? ?==)/gi, "$1Source$3");
  str = str.replace(/(== ?)(further readings?:?)( ?==)/gi, "$1Further reading$3");

  return str;
}

function catFixer(str){
    str = str.replace(/\ategory ?: ?/g, "[[Category:");

    return trim(str);
}

//fixes many common syntax problems
function fixsyntax(str)
{
  //replace html with wiki syntax
  if( !str.match(/'<\/?>|<\/?>'/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 />");                     
  return trim(str);
}

//formats links in standard fashion
function linkfixer(str, checkImages)
{ 
  str = str.replace(/\]\ [");
  var m = str.match(/\]*?\]\]?/g);
  if (m)
  {
    for (var i = 0; i < m.length; i++)
    {
      var x = m.toString();
      var y = 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, "]"); 
  str = str.replace(/\]*?)( |_)#(]*?)\]\]/g, "]");

  return trim(str);
}

//fixes images
function imagefixer(str)
{

  //remove external images
  str = str.replace(/\]*?)\]\]?/gi, "");

  //fix links within internal images
  var m = str.match(/\]*?(\]*?\]*?]*?)*?\]+/gi);
  if (m)
  {
    for (var i = 0; i < m.length; i++)
    {
      var x = m.toString();
      var y = x;

      y = y.replace(/^\\]$/, "");
      y = y.replace(/(\]*?)$/, "$1]");
      y = linkfixer(y, true);
      y = "]";

      str = str.replace(x, y);
    }
  }

  return trim(str);
}

//simplifies some links e.g. ] to ] and ] to ]s
function linksimplifyer(str){
  var m = str.match(/\*?)\|(*?)\]\]/g);
  if (m)
  {
    for (var i = 0; i < m.length; i++)
    {
      var n_arr = m.toString().match(/\*?)\|(*?)\]\]/);
      var n = n_arr;
      var a = n_arr;
      var b = n_arr;

      if (b.indexOf(a) == 0 || b.indexOf(TurnFirstToLower(a)) == 0)
      {
        var k = n.replace(/\\|]*?)\|(\1)(*?)\]\]/i, "]$3");
        str = str.replace(n, k);
      }
    }
  }

  str = str.replace(/\\|]+)\|(\|]+)\]\](*)()/g, "]$4");

  return str;
}

//trim start and end, trim spaces from the end of lines
function trim(str) {
   str = str.replace(/ $/gm, "");
   return str.replace(/^\s*|\s*$/g, "");
}

//turns first character to lowercase
function TurnFirstToLower(input) {
  if (input != "")
  {
    var input = trim(input);
    var temp = input.substr(0, 1);
    return temp.toLowerCase() + input.substr(1, input.length);
  }
  else
    return "";
}

//entities that should never be unicoded
function noUnicodify(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;)/, "&amp;$1");
  str = str.replace(/&(#0?12;)/, "&amp;$1");

  return str;
}

$(function () {
  if(document.forms.editform) {
    mw.util.addPortletLink('p-cactions', 'javascript:format()', 'format', 'ca-format', 'Format article', '', document.getElementById('ca-history'));
  }
});