Benutzer:Perhelion/sectionSummary.js

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Internet Explorer/Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
  • Opera: Strg+F5
/** AUTOCOMPLETE for CORRECT SECTION in Edit-Summary
*** aut. section summary / (automatische Abschnittserkennung für die Zusammenfassung) ***
* @description [[w:de:Benutzer:Perhelion/sectionSummary]]
* @created 2012-01-07
* @author [[User:Perhelion]], 2012–2017
* @revision 15:18, 15. Aug. 2018 (CEST)
* @license Dual-licensed under the terms of the GFDL v1.2 or the GPL v2.
* @ToDo FIXME: Remove tags from headline!? Inlude existing code?
* @required modules: jquery, mediawiki.util, user.options
**/
(function($,mw){'use strict';var libs=mw.libs;if(libs.autoSectiontId instanceof Object)
return;libs.autoSectiontId={version:'0.55t',exec:function(){var $editform=$('#editform'),name='autoSectiontId';if($.inArray(mw.config.get('wgAction'),['edit','submit'])<0||!$editform.length)
return;var sumM={'new':'new section ','rem':'removed section – ','top':'top','cancel':name+' canceled: more than one section was changed'};if(mw.config.get('wgContentLanguage')==='de')
sumM={'new':'Neuer Abschnitt ','rem':'Entfernter Abschnitt – ','top':'Einleitung','cancel':name+' Abbruch: mehr als ein Abschnitt wurde verändert'};var $textbox=$editform.find('#wpTextbox1'),txtOld=$.trim($textbox.val()),lrL=txtOld.lastIndexOf('\n'),lnL,$sum=$editform.find('#wpSummary'),headline=/^=+.+=+.*$/gm,trimHeadline=/^=+(.+[^=])=+\s*$/m;txtOld=txtOld.slice(0,lrL)+txtOld.slice(lrL).replace(/\n[:*# ]*$/,'');function forceSummary(){var newS=(/&section=new/.test(location.search)||$editform.children('input[name|=\'wpSection\']').val()==='new')?1:0;if(newS&&$sum.val())
return;function _not(s,s2){return $.grep(s,function(x){return $.inArray(x,s2)<0;})[0];}
var txt=$.trim($textbox.val()),remS,sectNamesOld=txtOld.match(headline)||[],sectNames=txt.match(headline)||[],sections_len=sectNames.length+1,sections_lenOld=sectNamesOld.length+1,section='',comp=0,sumTxt='',$wpAS=$editform.find('input[name|=\'wpAutoSummary\']'),sumOld=/^( *\/\*.+\*\/ *)?(.*) *$/.exec($sum.val())[2];function _uniSectNames(arr){return $.map(arr,function(x){return x.replace(/^(==+) *([^\n]+[^ ]) *\1 *$/m,'$1 $2 $1');});}
function doComp(b){var uSectNamesOld=_uniSectNames(sectNamesOld);var uSectNames=_uniSectNames(sectNames);if(!comp)
comp=new Array(sections_len);for(i=0;i<sections_len;i++){comp[i]=comp[i]||0;if($.inArray(uSectNames[i],uSectNamesOld)<0){b=true;comp[i]=b;}}
if(!b)
comp=0;return comp;}
function uniqueSect(c){var s=[[0,8]],hl,h,i;for(i=sections_len-1;i>0;i--){hl=sectLvl[i];h=s[0][1];if(c[i])
if(hl<h){s=[[i,hl]];}else{if(hl>h)
sectLvl[i]=h;s.unshift([i,h]);}
else if(hl<h&&s.length>1)
s=[[i,hl]];}
return(s.length<2&&s[0][1]!==8&&!c[0])?sectNames[s[0][0]]:s;}
if(sections_len+sections_lenOld===2)
return;var sectLvl=[0],i;for(i=1;i<sections_len;i++)
sectLvl[i]=sectNames[i-1].match(/^=+/m)[0].length;if(sections_lenOld<sections_len){if(sections_lenOld+1===sections_len){sumTxt=sumM['new'];section=_not(_uniSectNames(sectNames),_uniSectNames(sectNamesOld));}else
{section=uniqueSect(doComp());}}
else if(sections_lenOld>sections_len){if(sections_lenOld===sections_len+1){sumTxt=sumM.rem;section=_not(sectNamesOld,sectNames);remS=1;}else{section=[0,0];}}
else if(sections_lenOld===sections_len){if($('#wpMinoredit').is(':checked'))
return;var linesOld=txtOld.match(/^.*$/gm),lines=txt.match(/^.*$/gm),line=0,lineOld=0,lines_l=lines.length,lines_lOld=linesOld.length,lines_max=Math.max(lines_l,lines_lOld),sIdOld=1,sId=1,s,sectionsOld=[[0,0]],sections=[[0,0]];sectNamesOld.unshift('');sectNames.unshift('');i=0;comp=doComp(1);for(var l=0;l<lines_max;l++){if(sectNames[sId]&&sectNames[sId]===lines[l]){line=l-1-line;sections[sId]=[l];sections[sId-1][1]=line;sId++;line=l;}
if(sectNamesOld[sIdOld]&&sectNamesOld[sIdOld]===linesOld[l]){lineOld=l-1-lineOld;sectionsOld[sIdOld]=[l];sectionsOld[sIdOld-1][1]=lineOld;sIdOld++;lineOld=l;}}
sections[sections_len-1][1]=lines_l-sections[sections_len-1][0]-1;sectionsOld[sections_lenOld-1][1]=lines_lOld-sectionsOld[sections_lenOld-1][0]-1;for(i=0;i<sections_len;i++){if(sections[i][1]!==sectionsOld[i][1]){comp[i]=Math.abs(sections[i][1]-sectionsOld[i][1]);continue;}
for(s=1;s<sectionsOld[i][1]+1;s++){lineOld=sectionsOld[i][0]+s;line=sections[i][0]+s;if(linesOld[lineOld].length!==lines[line].length){comp[i]++;break;}}}
section=uniqueSect(comp);if($.isArray(section)&&section.length>1&&comp[sections_len-1]===1){lrL=txtOld.slice(txtOld.lastIndexOf('\n'));lnL=txt.slice(txt.lastIndexOf('\n'));if(lnL!==lrL){comp[sections_len-1]=0;section=uniqueSect(comp);}}}
if(!section)
return;if($.isArray(section))
if(section.length===1&&comp[0]&&section[0][1]===8){section='='+sumM.top+'=';}else{if($wpAS.val().indexOf(name)!==-1)
$sum.val(sumOld);return console.log(sumM.cancel);}
var sect2=trimHeadline.exec(section);if(!sect2||!sect2[1])return;sect2=$.trim(sect2[1]);var trimLink1=/\[\[[\s_]*?:?[\s_]*?[^[]*\]\]/.exec(sect2);var trimLink2=/[^[\]*?\[\[[\s_]*?:?[\s_]*?([^[|]*|[^]]*\|([^|]]*))\]\]/g.exec(sect2);if(trimLink1&&trimLink2)
sect2=sect2.replace(trimLink1[0],trimLink2[1]);sect2=sect2.replace(/\[https?:\/\/[^\s[\]|]*? +([^\n[\]|]*)?\]/gi,'$1');if(!sect2)
return;if(newS){txt=txt.replace(section,'');$sum.val(sect2);return $textbox.val($.trim(txt));}
sect2=(remS)?sumTxt+sect2:sumTxt+'/* '+sect2+' */';var wpAS=$wpAS.val().indexOf(name);if(wpAS<0){$wpAS[0].value+=name+sect2;}
else{sumOld=sumOld.replace(sect2,'');if(!mw.user.options.get('uselivepreview')&&sect2!==$wpAS.val().slice(wpAS+name.length))
sect2='';}
$sum.val(sect2+' '+$.trim(sumOld));}
$('#wpSave,#wpPreview,#wpDiff').click(forceSummary);}};$(function(){if(mw.config.get('wgPageContentModel')==='wikitext')
mw.loader.using('user.options',libs.autoSectiontId.exec);});}(jQuery,mediaWiki));