Revision as of 17:34, 24 April 2024 view sourceOhconfucius (talk | contribs)Autopatrolled, Extended confirmed users, Pending changes reviewers328,947 edits bug fixTag: Reverted← Previous edit |
Revision as of 17:37, 24 April 2024 view source Ohconfucius (talk | contribs)Autopatrolled, Extended confirmed users, Pending changes reviewers328,947 edits Revert to revision 1197251545 dated 2024-01-19 19:14:37 by Ohconfucius using popupsTag: Manual revertNext edit → |
Line 6: |
Line 6: |
|
// |
|
// |
|
// Please see the edit history for ] for attribution |
|
// Please see the edit history for ] for attribution |
|
// |
|
// |
|
// -------------------------------------------------------------------------- |
|
// -------------------------------------------------------------------------- |
|
// Fix hyphens, dashes, and minus signs per ]. |
|
// Fix hyphens, dashes, and minus signs per ]. |
Line 21: |
Line 21: |
|
// Please report false positives on the talk page. |
|
// Please report false positives on the talk page. |
|
|
|
|
|
function autoEdDashes(str) { |
|
function autoEdDashes (str) |
|
|
{ |
|
if (str.search(/nodashes/i) >= 0) |
|
if (str.search(/nodashes/i) >= 0) |
|
return str; |
|
return str; |
|
|
|
|
|
var scpat = /\bscores?\b|\[\[Category:.*\b(sport|athlet|players|teams|games|league|champion|tournament|competit|cup\b|\w+ball\b|hockey|lacrosse|cricket|rugby|tennis|golf|polo|boxing|boxers|martial.art|chess)/i; |
|
var scpat = /\bscores?\b|\[\[Category:.*\b(sport|athlet|players|teams|games|league|champion|tournament|competit|cup\b|\w+ball\b|hockey|lacrosse|cricket|rugby|tennis|golf|polo|boxing|boxers|martial.art|chess)/i; |
|
var scoresAreLikely = (str.search(scpat) >= 0); |
|
var scoresAreLikely = (str.search(scpat) >= 0); |
|
|
|
|
|
// Ensure the replacement isn't a link such as ] before |
|
// Ensure the replacement isn't a link such as ] before |
|
// replacing it, so that we don't break the link. But we do want to |
|
// 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, |
|
// replace dashes used in the right-side label part of links. Also, |
|
// don't break templates, URLs, DOIs, {{#expr:}}, <math> equations, |
|
// don't break templates, URLs, DOIs, {{#expr:}}, <math> equations, |
|
// source code, or <ref name="13-70">. |
|
// source code, or <ref name="13-70">. |
|
// |
|
// |
|
function ifNotLink(str) { |
|
function ifNotLink (str) |
|
|
{ |
|
var pos = arguments; |
|
var pos = arguments; |
|
var string = arguments; |
|
var string = arguments; |
|
|
|
|
|
|
var pat = /\]*$|\{\{*$|+$|<*$|#\w*expr:.*$/i; |
|
// Add this condition to check if the position is within an HTML comment tag |
|
|
if (string.substring(pos - 4, pos + 1) === "<!--" && string.substring(pos, pos + 3) !== "-->") { |
|
if (string.substring(pos-260,pos+1).search(pat) >= 0) |
|
return str; // Skip processing if within an HTML comment |
|
return str; // it's a link, so don't change it |
|
} |
|
|
|
|
|
var pat = /\]*$|\{\{*$|<\/?*$|+$|<*$|#\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|excerpt|about|for\b|other|redir|conv|coor|sort|anchor|Israel populations|ISBN|DNB(?: ite|)|ite DNB)*$/i; |
|
var pat2 = /\{\{(main|see|detail|excerpt|about|for\b|other|redir|conv|coor|sort|anchor|Israel populations|ISBN|DNB(?: ite|)|ite DNB)*$/i; |
|
if (string.substring(pos - 260, pos + 1).search(pat2) >= 0) |
|
if (string.substring(pos-260,pos+1).search(pat2) >= 0) |
|
return str; // likely templates with page-name or neg params |
|
return str; // likely templates with page-name or neg params |
|
|
|
|
|
|
|
var pat3 = /\|\s*(CAS_number)\s*=\s*/i; |
|
var pat3 = /\|\s*(CAS_number)\s*=\s*/i; |
|
if (string.substring(pos - 260, pos + 1).search(pat3) >= 0) |
|
if (string.substring(pos-260,pos+1).search(pat3) >= 0) |
|
return str; // drugbox CAS_number |
|
return str; // drugbox CAS_number |
|
|
|
|
|
|
|
var pat4 = /\|\s*(doi|elevation|filename|isbn)\s*=\s*/i; |
|
var pat4 = /\|\s*(doi|elevation|filename|isbn)\s*=\s*/i; |
|
if (string.substring(pos - 260, pos + 1).search(pat4) >= 0) |
|
if (string.substring(pos-260,pos+1).search(pat4) >= 0) |
|
return str; // doi or isbn (or elevation parameter in Template:Infobox German location which can contain range marked using hyphen) |
|
return str; // doi or isbn (or elevation parameter in Template:Infobox German location which can contain range marked using hyphen) |
|
|
|
|
|
var m = string.slice(pos).search(/<\/?(math|pre|code|tt|source|syntaxhighlight|gallery)\b/i); |
|
var m = string.slice(pos).search(/<\/?(math|pre|code|tt|source|syntaxhighlight|gallery)\b/i); |
|
if (m >= 0 && string.charAt(pos + m + 1) == '/') |
|
if (m >= 0 && string.charAt(pos+m+1) == '/') |
|
return str; // don't break a <math> equation, or source code |
|
return str; // don't break a <math> equation, or source code |
|
|
|
|
|
m = string.slice(pos).search(/\{\{*raph:Chart\b/i); |
|
m = string.slice(pos).search(/\{\{*raph:Chart\b/i); |
|
if (m >= 0) |
|
if (m >= 0) |
|
return str; // don't break a Graph:Chart |
|
return str; // don't break a Graph:Chart |
|
|
|
|
|
if (string.slice(pos).search(/^<>\n]*\.({3,4}\s*|jpg|png|svg)|^.*hyphen/i) >= 0) |
|
if (string.slice(pos).search(/^<>\n]*\.({3,4}\s*|jpg|png|svg)|^.*hyphen/i) >= 0) |
|
return str; // it's a file name parameter, or <!--sic hyphen--> |
|
return str; // it's a file name parameter, or <!--sic hyphen--> |
|
|
|
|
|
if (str.search(/\b/) >= 0) |
|
if (str.search(/\b/) >= 0) |
|
return str.replace(//, "−"); // minus sign |
|
return str.replace(//, "−"); // minus sign |
|
else |
|
else |
|
return str.replace(/--+\b/g, "—").replace(/+/g, "–"); // dash |
|
return str.replace(/--+\b/g, "—") . replace(/+/g, "–"); // dash |
|
} |
|
} |
|
|
|
|
|
str = str.replace(/\s--?\s/g, ifNotLink); // en dash looks better |
|
str = str.replace(/\s--?\s/g, ifNotLink); // en dash looks better |
|
str = str.replace(/---?/ig, ifNotLink); // em dash |
|
str = str.replace(/---?/ig, ifNotLink); // em dash |
|
str = str.replace(/\d\d\d]*}*(present|current)\b/ig, ifNotLink); // 1973-present |
|
str = str.replace(/\d\d\d]*}*(present|current)\b/ig, ifNotLink); // 1973-present |
|
str = str.replace(/(18|19|20)\d\d]*}*/g, ifNotLink); // (1973-) |
|
str = str.replace(/(18|19|20)\d\d]*}*/g, ifNotLink); // (1973-) |
|
str = str.replace(/\d(s|%|\?|''')\d/g, ifNotLink); // 1950s-1960s, 40%-50% |
|
str = str.replace(/\d(s|%|\?|''')\d/g, ifNotLink); // 1950s-1960s, 40%-50% |
|
str = str.replace(/\d(\$|'+)\d/g, ifNotLink); // $40-$50, 7-'''4''', '49-'53 |
|
str = str.replace(/\d(\$|'+)\d/g, ifNotLink); // $40-$50, 7-'''4''', '49-'53 |
|
str = str.replace(/%?/g, ifNotLink); // 3½-6 |
|
str = str.replace(/%?/g, ifNotLink); // 3½-6 |
|
str = str.replace(/\d(st|nd|rd|th)?\d+(st|nd|rd|th)\b/g, ifNotLink); // 2nd-3rd |
|
str = str.replace(/\d(st|nd|rd|th)?\d+(st|nd|rd|th)\b/g, ifNotLink); // 2nd-3rd |
|
|
|
|
|
str = str.replace(/(>] +|\(|^\| *|\|\| *)\d/mig, ifNotLink); // minus -35 |
|
str = str.replace(/(>] +|\(|^\| *|\|\| *)\d/mig, ifNotLink); // minus -35 |
|
str = str.replace(/<((sup|sub|td)>\s*)(\d)/ig, "<$1−$3"); // 10<sup>-3</sup> |
|
str = str.replace(/<((sup|sub|td)>\s*)(\d)/ig, "<$1−$3"); // 10<sup>-3</sup> |
|
str = str.replace(/,*(?=.? ) * *(\d*:\d\d)/g, " – $1"); // album track listings |
|
str = str.replace(/,*(?=.? ) * *(\d*:\d\d)/g, " – $1"); // album track listings |
|
|
|
|
|
// November 15, 2005-March 7, 2006; ]-] |
|
// November 15, 2005-March 7, 2006; ]-] |
|
str = str.replace(/(\d\]*)(\* +\d)/g, "$1 – $2"); |
|
str = str.replace(/(\d\]*)(\* +\d)/g, "$1 – $2"); |
|
// July-August 2007 |
|
// July-August 2007 |
|
str = str.replace(/\b((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)*?\b){2,}/g, ifNotLink); |
|
str = str.replace(/\b((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)*?\b){2,}/g, ifNotLink); |
|
// ]-] |
|
// ]-] |
|
str = str.replace(/(\d(?: BC)?\]\])((ca?\.|AD ?)?\)/g, "$1–$2"); |
|
str = str.replace(/(\d(?: BC)?\]\])((ca?\.|AD ?)?\)/g, "$1–$2"); |
|
// (1984 – 1992) |
|
// (1984 – 1992) |
|
|
str = str.replace(/( *\*) + +(\*\s*)/g, "$1–$2"); |
|
str = |
|
|
|
// iv-xii |
|
|
str = str.replace(/(?=\w+-)(m*(cm|cd|d?c*)(xc|xl|l?x*)(ix|iv|v?i*)-?\b){2}/g, ifNotLink); |
|
|
|
|
|
|
if (scoresAreLikely) // W-L-D or 73–70–67–70=280, but not castling |
|
str.replace(/( *\*) + +(\*\s*)/g, "$1–$2"); |
|
|
|
str = str.replace(/(?!0-0-0)(\d\d?\d?){2,}\d\d?/g, ifNotLink); |
|
// iv-xii |
|
|
str = str.replace(/(?=\w+-)(m*(cm|cd|d?c*)(xc|xl|l?x*)(ix|iv|v?i*)-?\b){2}/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 |
|
if (scoresAreLikely) // W-L-D or 73–70–67–70=280, but not castling |
|
|
str = str.replace(/(?!0-0-0)(\d\d?\d?){2,}\d\d?/g, ifNotLink); |
|
|
|
|
|
|
|
// Number ranges and scores should use en dashes, per ]. |
|
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 |
|
|
|
// 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|L|p|page|date|IS\wN\b*|SCOTUS)*$/i; |
|
|
var rpat = /^(|nbsp)*(AD|BC|B?CE|BP|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+\)?'*|(wn|lost?|tie|dr.w|lea?d|f.ll|vot|rul|decid|pass|fail|defeat|scor|gam|match|trail|finish|end)e??|\w\w+ing|ahead|behind|up|down|from|to|is|are|was|were|of|out|by|an?|at|it|went|go|gone|beaten|between)(|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) { |
|
// Number ranges and scores should use en dashes, per ]. |
|
|
|
return str; // based on preceding word, looks like a ref number |
|
// 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|L|p|page|date|IS\wN\b*|SCOTUS)*$/i; |
|
|
var rpat = /^(|nbsp)*(AD|BC|B?CE|BP|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+\)?'*|(wn|lost?|tie|dr.w|lea?d|f.ll|vot|rul|decid|pass|fail|defeat|scor|gam|match|trail|finish|end)e??|\w\w+ing|ahead|behind|up|down|from|to|is|are|was|were|of|out|by|an?|at|it|went|go|gone|beaten|between)(|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 (scoresAreLikely && from <= 900 && to <= 900) { |
|
|
dash = true; // likely a score or wins-losses |
|
|
} |
|
|
if (from < 2 - to && string.search(/Category:.*\bChess\b/i) >= 0) { |
|
|
dash = false; // chess notations 0-0, 0-1, 1-0 |
|
|
} |
|
|
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; |
|
|
} |
|
} |
|
|
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 (scoresAreLikely && from <= 900 && to <= 900) { |
|
|
dash = true; // likely a score or wins-losses |
|
|
} |
|
|
if (from < 2-to && string.search(/Category:.*\bChess\b/i) >= 0) { |
|
|
dash = false; // chess notations 0-0, 0-1, 1-0 |
|
|
} |
|
|
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(/(\d{1,4})(\d{1,4})(?!'*)/g, range); |
|
str = str.replace(/(\d{1,4})(\d{1,4})(?!'*)/g, range); |
|
|
|
|
|
return str; |
|
return str; |
|
} |
|
} |
|
|
|
|
|
// Hook to allow using this tool "standalone" |
|
|
|
|
|
|
|
// Hook to allow using this tool "standalone" |
|
$.when($.ready).then(function () { |
|
|
if (importScript("Misplaced Pages:AutoEd/core.js")) { // if not otherwise using AutoEd |
|
|
console.log('imported'); |
|
|
|
|
|
|
window.autoEdFunctions = function () { |
|
$.when( $.ready ).then(function() { |
|
|
if (importScript("Misplaced Pages:AutoEd/core.js")) { // if not otherwise using AutoEd |
|
var txt = document.editform.wpTextbox1; |
|
|
|
console.log('imported'); |
|
var str = txt.value. |
|
|
|
|
|
replace(/—/g, '—'). |
|
|
|
window.autoEdFunctions = function() { |
|
replace(/–/g, '–'). // simplify core regexes |
|
|
replace(/−/g, '−'); |
|
var txt = document.editform.wpTextbox1; |
|
var newstr = autoEdDashes(str); |
|
var str = txt.value. |
|
if (newstr !== str) { // don't alter encoding style if no fixes |
|
replace(/—/g, '—'). |
|
txt.value = newstr; |
|
replace(/–/g, '–'). // simplify core regexes |
|
|
replace(/−/g, '−'); |
|
autoEdTag = "fixed ] using ]"; |
|
|
} |
|
var newstr = autoEdDashes( str ); |
|
|
if (newstr !== str) { // don't alter encoding style if no fixes |
|
}; |
|
|
autoEdLinkName = "–"; |
|
txt.value = newstr; |
|
autoEdLinkHover = "Fix dashes, hyphens, and minus signs"; |
|
autoEdTag = "fixed ] using ]"; |
|
autoEdTag = ""; |
|
} |
|
} |
|
}; |
|
|
autoEdLinkName = "–"; |
|
|
autoEdLinkHover = "Fix dashes, hyphens, and minus signs"; |
|
|
autoEdTag = ""; |
|
|
} |
|
}); |
|
}); |