მომხმარებელი:Deu/wikificator.js

ვიქსიკონი გვერდიდან

შენიშვნა: გამოქვეყნების შემდეგ შესაძლოა დაგჭირდეთ ბრაუზერის ქეშის გაწმენდა ცვლილებების სანახავად.

  • Firefox / Safari: დააჭირეთ Shift და Reload, ან დააჭირეთ Ctrl-F5, ან Ctrl-R (⌘-R Mac-ზე);
  • Google Chrome: დააჭირეთ Ctrl-Shift-R (⌘-Shift-R Mac-ზე);
  • IE / Edge: დააჭირეთ Ctrl და Refresh, ან დააჭირეთ Ctrl-F5;
  • Opera გაწმინდეთ კეში Tools→Preferences-დან.
//<source lang=javascript>
 
var txt;
 
var wmCantWork = 'Wikificator cannot work in your browser';
var wmFullText = 'Wikificator will process ALL text on this page. Continue?';
var wmTalkPage = 'Викификатор не обрабатывает обсуждения\n\nWikificator doesn\'t work on discussion pages';
 
var wmCategoryNS = 'კატეგორია';
var wmTemplateNS = 'თარგი';
var wmUserNS = 'მომხმარებელი';
var wmImageNS = 'სურათი';
var wmMediaNS = 'მედია';
 
var wmLocaleNS = new Array ( wmCategoryNS, wmTemplateNS, wmUserNS, wmImageNS, wmMediaNS );
var wmEnNS = new Array ( 'category', 'template', 'user', 'image', 'media');
 
 
function Wikify(){
 if (('code'.replace(/d/g, 'r') != 'core') //check regexp support
    || (navigator.appName=='Netscape' && navigator.appVersion.substr (0, 1) < 5))
  { alert(wmCantWork); return }
 
 var txt, hidden = [], hidIdx = 0, wpTextbox1 = document.editform.wpTextbox1
 
 if (document.selection && document.selection.createRange) { //IE/Opera
   var winScroll = document.documentElement.scrollTop //remember window scroll
   wpTextbox1.focus()
   var range = document.selection.createRange()
   txt = range.text
   if (txt == '') processAllText()
   else{
     processText()
     range.text = txt
     //restore selection   
     if (range.moveStart) range.moveStart('character', - txt.length)
     range.select() 
   }
   document.documentElement.scrollTop = winScroll
 
 }else if ((wpTextbox1.selectionStart || wpTextbox1.selectionStart == '0') //Mozilla
    && (navigator.productSub > 20031000)) /*Gecko-browsers older then 10.2003*/  {
    var textScroll = wpTextbox1.scrollTop
    wpTextbox1.focus()
    var startPos = wpTextbox1.selectionStart
    var endPos = wpTextbox1.selectionEnd
    txt = wpTextbox1.value.substring(startPos, endPos)
    if (txt == '') processAllText()
    else{
      processText()
      wpTextbox1.value = wpTextbox1.value.substring(0, startPos) + txt + wpTextbox1.value.substring(endPos)
    }
    //restore selection and textarea scroll
    wpTextbox1.selectionStart = startPos
    wpTextbox1.selectionEnd = endPos
    wpTextbox1.scrollTop = textScroll
 
 }else // other browsers
   if (confirm(wmFullText)) processAllText()
 
 
 
function processAllText(){
 txt = wpTextbox1.value
 txt = txt.replace(/^[\n\r]+/, '') //newlines at start
 processText()
 wpTextbox1.value = txt
}
 
 
 
function processText(){
 
 
if (wgNamespaceNumber % 2 || wgNamespaceNumber==4) { //is talk page
 var sigs = txt.match(/\d\d:\d\d, \d\d? \S{3,8} 20\d\d \(UTC\)/g)
 if (sigs && sigs.length > 1) {
   alert(wmTalkPage); return
 }
} 
 
hideTag('nowiki')
hideTag('pre')
hideExpr('<source [^>]+>[\\s\\S]+?<\\/source>')
hideTag('code')
hideTag('tt')
hideTag('math')
hideTag('gallery')
hideExpr('{\\{[\\s\\S]+?}}') //templates
hideExpr('^ .*$') //lines starting with space
hideExpr('(http|https|ftp|tftp|news|nntp|telnet|irc|gopher)://[^ \n\r\u00A0]* ?') //links
 
txt = txt.replace(/(\[\[:?)category:( *)/ig, '$1კატეგორია:')
txt = txt.replace(/(\[\[:?)image:( *)/ig, '$1სურათი:')
txt = txt.replace(/(\[\[:?)template:( *)/ig, '$1თარგი:')
// Year ranges
txt = txt.replace(/(?!ISBN)(\(|\s)(\[\[[12]?\d{3}\]\])[\u00A0 ]?(-|--|–|—) ?(\[\[[12]?\d{3}\]\])(\W)/g, '$1$2—$4$5')
txt = txt.replace(/(\[\[[12]?\d{3}\]\]) ?(წ\.|წწ\.)/g, '$1\u00A0$2')
// Century ranges
txt = txt.replace(/(\(|\s)(\[\[[IVX]{1,5}\]\])[\u00A0 ]?(-|--|–|—) ?(\[\[[IVX]{1,5}\]\])(\W)/g, '$1$2—$4$5')
txt = txt.replace(/(\[\[[IVX]{1,5}\]\]) ?(ს\.|სს\.)/g, '$1\u00A0$2')
 
hideExpr('\\[\\[[^\\]|]+') //internal links
 
 
txt = txt.replace(/<<(\S.+\S)>>/g, '"$1"') //<<text>> -> "text"
//square and cube
txt = txt.replace(/(<sup>2<\/sup>|&sup2;)/gi, '²');
txt = txt.replace(/(<sup>3<\/sup>|&sup3;)/gi, '³');
//tags → wikicode
txt = txt.replace(/<\/?(b|strong)>/gi, "'''")
txt = txt.replace(/<\/?(i|em)>/gi, "''")
txt = txt.replace(/<hr ?\/?>/gi, '----')
//improve hr and br
txt = txt.replace(/<hr ([^\/>]+?) ?\/?>/gi, '<hr $1 />')
txt = txt.replace(/<br( [^\/>]+?)? ?\/?>/gi, '<br$1 />')
 
 
hideExpr('<[^>]*?>') //hide tags
hideExpr('\\w+ *= *"[^"]*"') //also tables attributes //[ \w%;:]
 
 
txt = txt.replace(/(\S)([ \t]{2,})([\S\r])/g, '$1 $3') //remove double spaces
txt = txt.replace(/[ \t]+(\n|\r)/g, '$1') //spaces at EOL
 
txt = txt.replace(/^(=+)[ \t\f\v]*(.*?)[ \t\f\v]*=+$/gm, '$1 $2 $1') //spaces in section headers
txt = txt.replace(/([^\r\n])(\r?\n==.*==\r?\n)/g, '$1\n$2') //empty line before section header
 
 
//Temporary
txt = txt.replace(/–/g, '-')
txt = txt.replace(/„|“|”|„/g, '"')
txt = ' ' + txt
 
// Minus handler
txt = txt.replace(/(sup>|sub>|\s)-(\d)/g, '$1−$2')
// Replace hyphens and en dashes with normal dashes
txt = txt.replace(/&(#151|[nm]dash);/g, '—')
txt = txt.replace(/(&nbsp;|[\f\n\r\t\v\u00A0\u2028\u2029])(-|--|–) /g, '$1— ')
txt = txt.replace(/(\d)--(\d)/g, '$1—$2')
 
// Entities etc. -> Unicode chars
txt = txt.replace(/&copy;/gi,'©')
txt = txt.replace(/&reg;/gi,'®')
txt = txt.replace(/&sect;/gi,'§')
txt = txt.replace(/&euro;/gi,'€')
txt = txt.replace(/&yen;/gi,'¥')
txt = txt.replace(/&pound;/gi,'£')
txt = txt.replace(/&deg;/g,'°')
txt = txt.replace(/\(tm\)|\(тм\)|&trade;/gi,'™')
txt = txt.replace(/\.\.\.|&hellip;|&#133;/g,'…')
txt = txt.replace(/\+[--]|&plusmn;/g,'±')
txt = txt.replace(/~=/g,'≈')
txt = txt.replace(/\^2(\D)/g,'²$1')
txt = txt.replace(/\^3(\D)/g,'³$1')
txt = txt.replace(/&((la|ra|bd|ld)quo|#132|#147|#148|quot);/g,'"')
txt = txt.replace(/([\wა-ჰ])'([\wა-ჰ])/g,'$1’$2') //'
 
// Year ranges
txt = txt.replace(/(\(|\s)([12]?\d{3})[\u00A0 ]?(-|--|–|—) ?([12]?\d{3})(\W)/g, '$1$2—$4$5')
txt = txt.replace(/([12]?\d{3}) ?(წ\.|წწ\.)/g, '$1\u00A0$2')
// Century ranges
txt = txt.replace(/(\(|\s)([IVX]{1,5})[\u00A0 ]?(-|--|–|—) ?([IVX]{1,5})(\W)/g, '$1$2—$4$5')
txt = txt.replace(/([IVX]{1,5}) ?(ს\.|სს\.)/g, '$1\u00A0$2')
 
// Reductions
txt = txt.replace(/ე\. ?ი\./g, 'ე\.\u00A0ი\.')
txt = txt.replace(/(Т|т)\. ?к\./g, '$1ак как')
txt = txt.replace(/(В|в) т\. ?ч\./g, '$1 том числе')
txt = txt.replace(/და ა\. ?შ\./g, 'და\u00A0ა\.\u00A0შ\.')
txt = txt.replace(/ე\. ?წ\./g, 'ე\.\u00A0წ\.')
txt = txt.replace(/н\. ?э\./g, 'н\.\u00A0э\.')
txt = txt.replace(/(Д|д)(о|\.) н\. ?э\./g, '$1о\u00A0н\.\u00A0э\.')
txt = txt.replace(/(\d) ?(млн|млрд|трлн|(?:მ|ს|დ|კ)?მ|[კმ]?г|с)\.?( ([^\.ა-ჰ])|[,;.])(?!\[.*?\|[ა-ჰ].*?\])/g, '$1\u00A0$2$3')
txt = txt.replace(/(\d) (тыс)([^\.ა-ჰ])/g, '$1\u00A0$2.$3')
txt = txt.replace(/^== იხილეთ (ასევე|აგრეთვე) ==$/gm, '== იხილეთ აგრეთვე ==')
//txt = txt.replace(/(\d) (млн|млрд|трлн)([^ა-ჰ])/g, '$1\u00A0$2$3')
 
// Insert/delete spaces
txt = txt.replace(/(\S) (-|--|–|—) (\S)/g, '$1\u00A0— $3')
txt = txt.replace(/([ა-ჰA-Z]\.) ?([ა-ჰA-Z]\.) ?([A-Z][ა-ჰ])/g, '$1\u00A0$2\u00A0$3')
txt = txt.replace(/([ა-ჰA-Z]\.)([ა-ჰA-Z]\.)/g, '$1 $2')
txt = txt.replace(/^([#*:]+)[ \t\f\v]*([^ \t\f\v*#:;])/gm, '$1 $2') //space after #*:
txt = txt.replace(/([ა-ჰ]\.)([ა-ჰA-Z])/g, '$1 $2') // word. word
txt = txt.replace(/([)"ა-ჰa-z\]])\s*,([\[("ა-ჰa-z])/g, '$1, $2') // word, word
txt = txt.replace(/([)"ა-ჰa-z\]])\s([,;])\s([\[("ა-ჰa-z])/g, '$1$2 $3')
txt = txt.replace(/([^%\/\w]\d+?(?:[.,]\d+?)?) ?([%‰])(?!-[ა-ჰ])/g, '$1\u00A0$2') //5 %
txt = txt.replace(/(\d) ([%‰])(?=-[ა-ჰ])/g, '$1$2') //5%-й
txt = txt.replace(/([№§])(\s*)(\d)/g, '$1\u00A0$3')
txt = txt.replace(/\( +/g, '(').replace(/ +\)/g, ')') // (no spaces)
 
// Degrees
txt = txt.replace(/([ =≈≠≤≥<>("'|]|^)([+±−\-]?\d+?(?:[.,]\d+?)?)(([ °^*]| [°^*])[CС])(?=[ "').,;!?|]|$)/gm, '$1$2\u00A0°C') //'
txt = txt.replace(/([ =≈≠≤≥<>("'|]|^)([+±−\-]?\d+?(?:[.,]\d+?)?)(([ °^*]| [°^*])F)(?=[ "').,;|!?]|$)/gm, '$1$2\u00A0°F') //'
 
//Grammar
txt = txt.replace(/რამოდენიმე/gi, 'რამდენიმე')
txt = txt.replace(/მითუმეტეს/gi, 'მით უმეტეს')
txt = txt.replace(/რათქმაუნდა/gi, 'რა თქმა უნდა')
txt = txt.replace(/რათქმა უნდა/gi, 'რა თქმა უნდა')
txt = txt.replace(/რა თქმაუნდა/gi, 'რა თქმა უნდა')
txt = txt.replace(/ეხლა/gi, 'ახლა')
txt = txt.replace(/ფაქტიურად/gi, 'ფაქტობრივად')
txt = txt.replace(/რაღაცეები/gi, 'რაღაცები')
txt = txt.replace(/იმიტო/gi, 'იმიტომ')
txt = txt.replace(/რატო/gi, 'რატომ')
txt = txt.replace(/Ü/gi, 'თ')
txt = txt.replace(/Ü-links/gi, 'მარცხნივ')
txt = txt.replace(/Ü-Abstand/gi, 'დაშორება')
txt = txt.replace(/Ü-rechts/gi, 'მარჯვნივ')

// "" → „“
txt = txt.replace(/([\x01-("\s|+\/])"([^"]*)([^\s"(|])"/g, '$1„$2$3“') //"
if (/"/.test(txt)){ //quotes inside "
  txt = txt.replace(/([\x01-("\s|])"([^"]*)([^\s"(|])"/g, '$1„$2$3“') //"
  while (/„[^“]*„/.test(txt))
    txt = txt.replace(/„([^“]*)„([^“]*)“/g, '„$1„$2“')
}
 
txt = txt.substr(1) //remove leading space
 
restoreAll()
 
if (window.auto_comment && insertSummary) insertSummary('ვიკიფიკატორი')
 
}
 
 
function hideExpr(expr){
 var ma = txt.match(new RegExp(expr, 'mgi'))
 if (!ma) return
 for (var i=0; i<ma.length; i++) {
   hidden[hidIdx] = ma[i].replace(/\$/g, '$$$$') //make all $ into $$ so they won't confuse replace() on restore
   txt = txt.replace(ma[i], '\x01' + hidIdx + '\x02')
   hidIdx++
 }
}
 
function hideTag(tag){
  hideExpr('<' + tag + '>[\\s\\S]+?<\\/' + tag + '>')
}
 
function restoreAll(){
 for (var i=hidIdx-1; i>=0; i--)
   txt = txt.replace('\x01' + i + '\x02', hidden[i])
}
 
}
//</source>