MediaWiki:Gadget-Kriipsud.js

Allikas: Vikipeedia

Märkus: Võimalik, et pärast avaldamist tuleb muudatuste nägemiseks brauseri puhver tühjendada.

  • Firefox / Safari: Hoia all Shift-klahvi ja klõpsa Laadi uuesti või vajuta kas Ctrl-F5 või Ctrl-R (Macis ⌘-R).
  • Google Chrome: Vajuta Ctrl-Shift-R (Macis ⌘-Shift-R).
  • Internet Explorer / Edge: Hoia all Ctrl-klahvi ja klõpsa Värskenda või vajuta Ctrl-F5.
  • Opera: Vajuta Ctrl-F5.
//  skripti //en.wikipedia.org/wiki/User:GregU/dashes.js lokaliseering
//  Fix hyphens, dashes, and minus signs per [[MOS:DASH]].
//
//  See talk page for instructions.
//
//  The user can disable these conversions by putting "nodashes" somewhere
//  in the text — either temporarily or permanently.  You can similarly add
//  "scores" if the score-detection heuristic doesn't trigger automatically.
//
//  This tool can be used standalone until it is added to AutoEd and wikEd.
//  This module should follow unicodify.js if it is used.
//  Testing page is at [[User:GregU/dashes.js/tests]].
//  Please report false positives on the talk page.
 
function autoEdDashes (str)
{
  if (str.search(/nodashes/i) >= 0)
    return str;
 
 
  // Ensure the replacement isn't a link such as [[FOO - BAR]] before
  // replacing it, so that we don't break the link. But we do want to
  // replace dashes used in the right-side label part of links.  Also,
  // don't break templates, URLs, DOIs, {{#expr:}}, <math> equations,
  // source code, or <ref name="13-70">.
  //
  function ifNotLink (str)
  {
    var pos    = arguments[ arguments.length - 2 ];
    var string = arguments[ arguments.length - 1 ];
 
    var pat = /\[\[[^|\]]*$|\{\{[^|}]*$|[:\/%][^\s|>]+$|<[^>]*$|#\w*expr:.*$/i;
    if (string.substring(pos-260,pos+1).search(pat) >= 0)
        return str;             // it's a link, so don't change it
 
    var pat2 = /\{\{(main|see|detail|about|for\b|other|redir|conv|coor|sort)[^}]*$/i;
    if (string.substring(pos-260,pos+1).search(pat2) >= 0)
        return str;             // likely templates with page-name or neg params
 
    var m = string.slice(pos).search(/<\/?(math|pre|code|tt|source|syntaxhighlight)\b/i);
    if (m >= 0 && string.charAt(pos+m+1) == '/')
        return str;             // don't break a <math> equation, or source code
 
    if (string.slice(pos).search(/^[^|{}[\]<>\n]*\.[a-z]{3,4}\s*[|}]|^.*hyphen/i) >= 0)
        return str;             // it's a file name parameter, or <!--sic hyphen-->
 
    if (str.search(/[ |(>][-–]\b/) >= 0)
        return str.replace(/[-–]/, "−");       // minus sign
    else
        return str.replace(/--+\b/g, "—") . replace(/[-–−]+/g, "–");     // dash
  }
 
  str = str.replace(/\s--?\s/g, ifNotLink);                 // en dash looks better
  str = str.replace(/[a-z\d]---?[a-z\d]/ig, ifNotLink);     // em dash
  str = str.replace(/[^\w−-](18|19|20)\d\d]*}*[-−][^\w−-]/g, ifNotLink);     // (1973-)
  str = str.replace(/\d(%\?|''')[-−]\d/g, ifNotLink);    // 40%-50%
  str = str.replace(/\d[-−](\$|'+)\d/g, ifNotLink);         // $40-$50, 7-'''4''', '49-'53
  str = str.replace(/[½⅓⅔¼¾⅛⅜⅝⅞]%?[-−][\d½⅓⅔¼¾⅛⅜⅝⅞]/g, ifNotLink);           // 3½-6
 
  str = str.replace(/([a-z,'"”\]>] +|\(|^\| *|\|\| *)[-–]\d/mig, ifNotLink);   // minus -35
  str = str.replace(/<((sup|sub|td)>\s*)[-–](\d)/ig, "<$1−$3");         // 10<sup>-3</sup>
  str = str.replace(/,*(?=.? ) *[-–—−] *(\d*:\d\d[\s*<])/g, " – $1");   // album track listings
 
  // July-August 2007
  // str = str.replace(/\b((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*[-−]?\b){2,}/g, ifNotLink);
  // [[266]]-[[283]]
  str = str.replace(/(\d(?: BC)?\]\])[-−]((ca?\.|AD ?)?\[\[\d+[^\d-])/g, "$1–$2");
  // (1984 – 1992)
  str = str.replace(/([(|=] *\[*\d+\]*) +[–—−] +(\[*\d+\]*\s*[)|}])/g, "$1–$2");
  // iv-xii
  str = str.replace(/[ ;(=](?=\w+-)(m*(cm|cd|d?c*)(xc|xl|l?x*)(ix|iv|v?i*)-?\b){2}[^\w-]/g, ifNotLink);
 
  // str = str.replace(/\b(\d+)[–−](year|month|day|hour|minute|mile|yard|foot|inch|bit|door|speed|gun|page|seat|way|point|ton|man)\b/g, "$1-$2");     // hyphen
 
  // Number ranges and scores should use en dashes, per [[MOS:DASH]].
  // This has been well-tested and false positives (e.g., ID nos.) are rare.
  //
  function range (str, from,to, pos,string)
  {
    var dash   = true;
    var except = /\b(fig(ure)?|table|example|exhibit|circular|section|part|number|no|nr|id|model|pub|std|report|rpt|law|[P.]L|p|page|date|IS\wN\b[ a-z]*|SCOTUS)[^\w(,;]*$/i;
    var rpat   = /^([^A-Za-z]|nbsp)*(AD|BC|B?CE|BP|[kMG]a|km|mi|kg|lb|\w?Hz|vote|decision|record|odds|scor\w*|win|loss|tie|draw|lead|victory|defeat|upset|run|deficit|start|finish|season|game)\b/;
    var lpat   = /\b(pages|pp|rp|nos|\d+\)?'*[:,]|(w[io]n|lost?|tie|dr.w|lea?d|f.ll|vot|rul|decid|pass|fail|defeat|scor|gam|match|trail|finish|end)e?[ds]?|\w\w+ing|ahead|behind|up|down|from|to|is|are|was|were|of|out|by|an?|at|it|went|go|gone|beaten|between)([^a-z]|nbsp)*$/i;
    var inorder   = (to-0 > from.slice(-to.length));     // pp 362-5
    var precision = Math.max( from.search(/0*$/), to.search(/0*$/) );
 
    if (string.substring(pos-20,pos+1).search(except) >= 0) {
        return str;      // based on preceding word, looks like a ref number
    }
    if (from == 9 && to == 11) {
        dash = false;    // 9-11 is a common special case
    }
    if (from-0 >= to) {
        dash = false;    // values don't look like a range
    }
    if (to-from > 120 && from * (precision > 2 ? 5 : 50) < to && from > 1) {
        dash = false;    // values don't look like a range
    }
    if (str.charAt(0) == '(' && string.charAt(pos + str.length) == ')') {
        dash = true;     // scores often seen as (8-4)
    }
    if (from.search(/^0./) >= 0 || to.search(/^0./) >= 0) {
        dash = false;    // 3-07 and 0123-4567 look like ref numbers
    }
    if (string.substr(pos-1,15).search(/^\d([:,.])\d+.\d+\1\d/) >= 0) {
        dash = true;     // 10:30-11:30, 35,000-40,000, 2.5-4.0
    }
    if (string.substr(pos,30).search(rpat) >= 0) {
        dash = true;     // 12-5 BC, 5-5000 km, 6-4 win, 73-50 vote
    }
    if (string.substring(pos-80,pos).search(lpat) >= 0) {
        dash = true;     // pp. 8, 25, 270-74, 313-7; won 6-4, 6-2
    }
    if (from > 1000 && from < 2100 && to.length == 2 && inorder) {
        dash = true;     // 1994-95 year range
    }
    return dash ? ifNotLink(str,pos,string) : str;
  }
 
  str = str.replace(/[^\w\/+−–-](\d{1,4})[-−](\d{1,4})(?!'*[\w\/+−–-])/g, range);
 
  return str;
}

function autoEdExecute() {
	 // Check if there is wpTextbox1 on the page
	if(document.getElementById('wpTextbox1')) {
		var txt = document.editform.wpTextbox1;
		// simplify core regexes
        var str = txt.value.
                  replace(/—/g, '—').
                  replace(/–/g, '–').     // simplify core regexes
                  replace(/−/g, '−');
		// Run the main function
		var newstr = autoEdDashes( str );
		// Check if something was changed in the article text
		if (newstr != str) {
			txt.value = newstr;
			document.forms.editform.wpSummary.value += 'parandasin [[Vikipeedia:Tööriistad/Kriipsuskript|skripti]] abil kriipsud';
		}
		// Set it to minor edit and click View Differences button
		document.forms.editform.wpMinoredit.checked = true;
		document.forms.editform.wpDiff.click();
	}
}

// Make sure the document is ready and our dependencies are loaded
$.when(
 $.ready,
 mw.loader.using(['mediawiki.util'])
).done(function () {
 // Check if page URL already has AutoEd parameter (the link has been clicked)
 if( mw.util.getParamValue('AutoEd') ) {
	 autoEdExecute();
 }
 // Add portlet link to the script
 if( document.getElementById('ca-edit') ) {
  var url = mw.util.getUrl(mw.config.get('wgPageName'), { action: 'edit', AutoEd: 'true' });
  var link = $(mw.util.addPortletLink(
   'p-cactions',
   url,
   '–',
   'ca-AutoEd',
   'Paranda side- ja mõttekriipsud ning miinusmärgid',
   '',
   document.getElementById('ca-move')
  ));
  // If the portlet link is clicked on the edit page, just run the replacements function, don't load new page
  if( typeof document.forms.editform !== 'undefined' ) {
    link.on('click', function (e) {
    e.preventDefault();
    autoEdExecute();
   });
  }
 }
});