Utente:Alex brollo/PersonalButtons.js

Nota: dopo aver pubblicato, potrebbe essere necessario pulire la cache del proprio browser per vedere i cambiamenti.

  • Firefox / Safari: tieni premuto il tasto delle maiuscole Shift e fai clic su Ricarica, oppure premi Ctrl-F5 o Ctrl-R (⌘-R su Mac)
  • Google Chrome: premi Ctrl-Shift-R (⌘-Shift-R su un Mac)
  • Internet Explorer / Edge: tieni premuto il tasto Ctrl e fai clic su Aggiorna, oppure premi Ctrl-F5
  • Opera: premi Ctrl-F5.
// da attivare usando grandi schermi ad alta risoluzione
//$(".mw-editfont-monospace").css("font-size","1.1pc"); $(".ace_editor").css("font-size","1.1pc");
// pone un diacritico breve sul carattere precedente il focus
shortcut.add("Ctrl+Shift+s", function () {
    var short="ă".normalize("NFD")[1];
    var t=selection();
    var c=t[0].substr(-1);
    c=c.normalize("NFD")[0]+short;
    c=c.normalize("NFC");
    t[0]=t[0].substr(0, t[0].length-1)+c;
    scriviSel(t);
});

// pone un diacritico lungo sul carattere precedente il focus
shortcut.add("Ctrl+Shift+l", function () {
    var long="ā".normalize("NFD")[1];
    var t=selection();
    var c=t[0].substr(-1);
    c=c.normalize("NFD")[0]+long;
    c=c.normalize("NFC");
    t[0]=t[0].substr(0, t[0].length-1)+c;
    scriviSel(t);
});

// normalizza (ossia elimina i diacritici) nel testo selezionato
shortcut.add("Ctrl+Shift+0",function() {
    var t=selection();
    var s=t[1].split("");
    for (i=0; i<s.length; i+=1) {
        s[i]=s[i].normalize("NFD")[0];
    }
    t[1]=s.join("");
    scriviSel(t);
});
// translittera da latino a greco (convenzione www.grecoantico.com)
shortcut.add("Ctrl+Shift+9", function () {
	var t=selection();
    var voc_lg={"a":"α","à":"ά", "b":"β","g":"γ","d":"δ","e":"ε","z":"ζ","h":"η","q":"θ","i":"ι","k":"κ","l":"λ","m":"μ",
    "n":"ν","x":"ξ","o":"ο","p":"π","r":"ρ","s":"σ","t":"τ","y":"υ","f":"φ","c":"χ","j":"ψ","w":"ω",
    "A":"Α","B":"Β","G":"Γ","D":"Δ","E":"Ε","Z":"Ζ","H":"Η","Q":"Θ","I":"Ι","K":"Κ","L":"Λ","M":"Μ","N":"Ν",
    "X":"Ξ","O":"Ο","P":"Π","R":"Ρ","S":"Σ","T":"Τ","Y":"Υ","F":"Φ","C":"Χ","J":"Ψ","W":"Ω"};
    var greco="";
    var testo=t[1];
    testo=testo.split("");
    for (i=0;i<testo.length;i+=1)
    { if (testo [i] in voc_lg) greco+=voc_lg [testo [i]]; else greco+=testo [i];
    }
    greco+=" ";
    greco=greco.replace("σ ","ς ").trim();
    greco=greco.replace("υ'","ύ")
    	.replace("ω'","ώ")
    	.replace("η'","ή")
    	.replace("α'","ά")
    	.replace("ε'","έ")
    	.replace("ι'","ί")
    	.replace("ο'","ό");
    t[1]=greco;
    scriviSel(t);
});
// indenta versi alternativamente 
shortcut.add("Ctrl+Alt+m",function (){
    var testo=leggiBox().split("\n");
    var ini=testo.indexOf("<poem>");
    var fin=testo.indexOf("</poem>");
    if (ini>-1) {
        for (i=ini+1;i<fin; i+=2) {
            testo[i+1]="     "+testo[i+1];
        }
    testo=testo.join("\n");
    scriviBox(testo);

    }
});
shortcut.add("Ctrl+Alt+F",function () {
    var lb=leggiBox();
    var l=produciLista(lb,"<ref","</ref>",1);
    var lx="";
    for (i=0;i<l.length;i+=1) {
        lx=l[i].replace(/ (\d+)/g," ''$1''");
        lx=lx.replace(/{{Sc\|([ivxldc]+)}}/g, "''{" + "{"+"Scx|$1}}''");
        lx=lx.replace(/{{Sc\|(.+?)}}/g,"$1");
        lx=lx.replace(/{{Scx/g,"{{Sc");
        lb=lb.replace(l[i],lx);
        
    }
    scriviBox(lb);
});

/*shortcut.add("Alt+Up", function() {
	var im=$(".prp-page-image")[0];
	im.scroll({top:im.scrollTop-120,left:0,behavior:"smooth"});
}); */

shortcut.add("Ctrl+Alt+r", function (){$("a[rel='wsOcr1']").click();
});

importScript("Utente:Alex brollo/transclAlt.js");
var addReorder=function() {
	var testo=leggiBox();
	if(testo.indexOf("<OBJECT")===0) {
		scriviBox("reorder\n"+testo);
		cleanup();
	}
};
// sistema una pagina ns0 principale "raccolta" correggendo autoNs0
function aggiusta(){
    var testo=leggiBox();
    var newTesto="";
    newIndice="";
    newTesto=find_stringa(testo,"{"+"{Qualità","}}",1)+"\n";
    var indice=find_stringa(testo,"URL della versione cartacea a fronte = ","\n",0);
    var som=Sommario(indice);
    var dati=parseTemplate("Intestazione",testo);
    var raccolta=som[0].nome;
    dati[0].Titolo=mw.config.get("wgTitle");
    newTesto+=rewriteTemplate(dati, false);
    newTesto+="{{raccolta|"+raccolta+"}}\n\n";
    var listaIndice=produciLista(testo,"{{testo|","}}",0);
    var voceIndice="";
    for (i=0;i<listaIndice.length;i+=1) {
        if (listaIndice[i].indexOf(mw.config.get("wgTitle")+"/")===0) {
            voceIndice=listaIndice[i].replace(mw.config.get("wgTitle"),"");
            newIndice+="*".repeat(count(listaIndice[i],"/"))+" {{testo|"+voceIndice+"}}\n";
        }
    } 
    if (newIndice.length>0){
        newIndice="\n== Indice ==\n\n"+newIndice;
    }
    for (i=0;i<som.length;i+=1) {
        if (som[i].nome===mw.config.get("wgTitle")) {
            newTesto+='<pages ' + 'index="'+indice.replace("Indice:","")+'" from='+som[i].from
            + ' to='+(som[i+1].from-1)+' />\n';
            break;
        }
    }
    scriviBox(newTesto+newIndice);
    return;
}

function toCol() {
    var testo=leggiBox();
    var r0=[];
    var r1=[];
    var linee=[];
    var linea=[];
    linee=testo.split("\n");
    for (i=0;i<linee.length;i+=1) {
        linea=linee[i].split("\t");
        r0.push(linea[0]);
        if (linea[1]!==undefined) r1.push($.trim(linea[1]));
    }
    if (r1.length>0) {
        testo="{{colonna|em=-1}}\n"+$.trim(r0.join("\n"))+"\n{{AltraColonna|em=-1}}\n"+$.trim(r1.join("\n"));
        scriviBox(testo);
    }
}

// formatta sonetti Laterza (elimina righe vuote, indenta primo verso delle strofe)
function son(){

	var ts=selection();
	var tsm=$.trim(ts[1]).replace(/\n+/g, "\n").replace(/\n[ :]*/g,"\n").split("\n");
	if (ts[1].length>0){
		for (i=0;i<tsm.length;i+=1) {
			if ([0,4,8,11].indexOf(i)!==-1) tsm[i]=":"+tsm[i];
		}
	    tsm=tsm.join("\n");
		scriviBox(ts[0]+tsm+ts[2]);
	}
	var t=leggiBox();
	t=t.replace(/([^\|])<poem>/g,"$1{{Blocco centrato|<poem>");
	t=t.replace(/<\/poem>([^}])/g, "</poem>}}$1");
	scriviBox(t);
}

function poem_down(){
	var t=leggiBox();
    t=t.replace(/(.*<poem.*\n)(.+\n)/,"$2$1");
    t=t.replace(/(.*{{poem t\|.*\n)(.+\n)/,"$2$1");
	scriviBox(t);
	return false;
}

function poem_t(){
	var t=selection();
	t[1]=t[1].replace(/<poem>/g,"{{poem t|").replace(/<\/poem>/g,"}}");
	scriviBox(t.join(""));
}
/*function poem_t(){
	var t=selection();
	t[1]=t[1].replace(/{{sc\|/gi,"{{Sc|");
	t[1]=t[1].replace(/\n\n/g,"\n");
	while (t[1].indexOf("{{Sc")!=-1) {
		t[1]=t[1].replace(find_stringa(t[1],"{{Sc|","}}. ",1,"{{"),find_stringa(t[1],"{{Sc|","}}. ",0,"{{")+".+");
		
	}
	t[1]="{{poem t|"+t[1]+"}}";
	scriviBox(t.join(""));
}*/
function forcedNewRi() {
	scriviBox("",0);
	newRi();
}

shortcut.add("Ctrl+Alt+r",forcedNewRi); 

function poemAll() {
	scriviBox("<poem>\n"+$.trim(leggiBox()+"\n</poem>"));
	return;
}
function full_page(){
	$("#mw-panel").toggle();
	$("#mw-sidebar-button").click();
	if ($("#content").css("margin-left")=="0px") {
		$("#content").css("margin-left", "176px");
		$("#prp-page-image-openseadragon-vertical").css('width','480px');}
	else {
		$("#content").css("margin-left", "0");
		$("#prp-page-image-openseadragon-vertical").css('width','100%');}
		
}
// inserimento Pg in Indice generale Tiraboschi (disabilitato) e Giucciardini (abilitato)
function pgPlus() {
    var testo=leggiBox();
    var ts=testo.split(/([ .;,-])/);
    var prevPg="";
    var tomo="";
    var npag="";

    function isnum(val) {
    return /^\d+,*$/.test(val);
    }
    function t2v(t,p) {
        var v="";
        /*if (t==="I") v="t1";
        if (t==="II") v="t2";
        if (t==="III") v="t3";
        if (t==="IV") v="t4";
        if (t==="V" && p*1 <517) v="t5";
        if (t==="V" && p*1 >= 517) v="t6";
        if (t==="VI" && p*1 < 653) v="t7";
        if (t==="VI" && p*1 >=653 && p*1 <1225 ) v="t8";
        if (t==="VI" && p*1 >=1225) v="t9";
        if (t==="VII" && p*1 <609) v="t10";
        if (t==="VII" && p*1 >=609 && p*1 < 1161) v="t11";
        if (t==="VII" && p*1 >= 1161 && p*1 < 1971) v="t12";
        if (t==="VII" && p*1 >= 1971) v="t13";
        if (t==="VIII" && p*1 < 543) v="t14";
        if (t==="VIII" && p*1 >=543) v="t15";*/
        
        if (t==="I") v="gui1";
        if (t==="II") v="gui2";
        if (t==="III") v="gui3";
        if (t==="IV") v="gui4";
        if (t==="V") v="gui5";

        return v;
    }
    for (i=0;i<ts.length;i+=1) {
        if (/[IV]+/.test(ts[i]) && ts[i+1]===",") tomo=ts[i];
        if (isnum(ts[i])) {
            npag=/\d+/.exec(ts[i])[0];
            ts[i]="{{Pg|"+npag+"||"+t2v(tomo,npag)+"}}";
            prevPg=ts[i];
            }
        if (/{{Pg\|\d+\|\|t\d+}}/.test(ts[i])) prevPg=ts[i];
        if (ts[i]==="''ivi''") {
        	ts[i]=prevPg.replace("||","|''ivi''|");
        	}
    }
    scriviBox(ts.join(""));
}





function alt1() {$(".quality1 input").click(); $("#wpSave").click(); return false;}
function alt2() {$(".quality2 input").click(); $("#wpSave").click(); return false;}
function alt3() {$(".quality3 input").click(); $("#wpSave").click(); return false;}
function alt4() {$(".quality4 input").click(); $("#wpSave").click(); return false;}
function ctplus() {
	var t=sel()[1];
	t[1]=t[1].split("\n");
	t[1]=t[1].filter(function(e){return $.trim(e).length>0});
	for (i=0;i<t[1].length;i+=1) {
		t[1][i]="{{Ct|c= |"+t[1][i]+"}}";
	}
	t[1]=t[1].join("\n");
	scriviBox(t.join(""));
	return false;
}
/*function altp() {
	var t=sel()[1];
	t[0]+="<poem>\n";
	t[2]=$.trim(t[2])+"\n</poem>";
	scriviBox(t.join(""));
	cleanup();
	return false;
}
function altp() {
	cleanup();
	cleanup();
	var t=leggiBox();
	if (t.indexOf("<poem>")===-1) {
		t="<poem>\n"+$.trim(t);
		t=t+"\n</poem>";
	}
	scriviBox(t);
	$("#wpSave").click();
	return false;
}*/
function altp() {
	var s=sel();
	var t=s[1];
	t[1]=t[1].replace(/\n+/g," — ");
	scriviBox(t.join(""));
	return false;
}
shortcut.add("Alt+1",alt1);
shortcut.add("Alt+1",alt2);
shortcut.add("Ctrl+Alt+p",altp);
shortcut.add("Ctrl+Alt+4",alt4);
shortcut.add("Ctrl+Alt+n",note);
/*function nextImage() {
	var image=$(".prp-page-image img"); 
	var np=/djvu\/page(\d+)/.exec(image.attr("src"))[1]; 
	image.attr("src",image.attr("src").replace("djvu/page"+np,"djvu/page"+(np*1+1))); 
	return }
shortcut.add("Ctrl+Alt+2",nextImage);
function prevImage() {
	var image=$(".prp-page-image img"); 
	var np=/djvu\/page(\d+)/.exec(image.attr("src"))[1]; 
	image.attr("src",image.attr("src").replace("djvu/page"+np,"djvu/page"+(np*1-1))); 
	return }
shortcut.add("Ctrl+Alt+1",prevImage);*/	
var appendr=function () {
	var t=selection();
	t[1]=t[1].replace(/ *\n/g,"\t");
	scriviBox(t.join(""));
};
	
function rml() {
	// eliminazione acapo che esclude sia i testi <poem> che le righe che iniziano con  ";:#*" (liste html)
	var editbox = document.getElementsByName('wpTextbox1')[0];
	// fase 1: codifica
	if (editbox.value.indexOf("{{rml") != -1) {
		lista = produciLista(editbox.value, "{" + "{rml", "}}", 1);
		for (i = 0; i <= lista.length - 1; i = i + 1) {
			marcatore = "###" + i + "###";
			editbox.value = editbox.value.replace(lista[i], marcatore);
		}
	}
	
	// fase 2: elaborazione
	editbox.value = editbox.value.replace(/([ivxl,0-9]{2,} [ivxl0-9,; ]+)/gi, ' {'+'{'+'rml|$1}}');
	// fase 3: decodifica
	if (editbox.value.indexOf("###0###") != -1) {
		for (i = 0; i <= lista.length - 1; i = i + 1) {
			marcatore = "###" + i + "###";
			editbox.value = editbox.value.replace(marcatore, lista[i]);
		}
	}
	
}	
// sectionBegin='<section begin="Della natura degli uomini" />';
// sectionEnd='<section end="Della natura degli uomini" />'
sectionBegin='{{ZbPensiero|';
sectionEnd='|noasc}}';
shortcut.add("Ctrl+Alt+o", function() {incapsula('<ref ' + 'group=lower-alpha>',"</ref>");});
//newButton("rml","rml(1)","es","esegue applicazione rml");


//newButton("Xml","xmlGo(1)","es","esegue cleanup tipo normale");
//newButton("Reorder","xmlGo(2)","es","esegue cleanup tipo reorder");

//newButton("Z", "incapsula(sectionBegin,sectionEnd)", "es","section per Zibaldone");
newButton("fp","full_page()","es","elimina mw_panel");
newButton("FR","mostraTrovaSostituisci()","es","apri Trova e sostituisci");
//newButton("autoNs0","mw.ns0Auto()","es","esegue autoNs0 forzato");
newButton("gap", "incapsula('{{gap|1em}}','')", "es","Applica il template gap");

newButton("«»", "incapsula('«','»')", "es","Applica caporali (Ctrl+Alt+c)"); shortcut.add("Ctrl+Alt+c", function(){$(".baseButton[title*='caporali']").click();} );
//newButton("‘’", "incapsula('‘','’')", "es","Applica virgolette singole (Ctrl+Alt+v)"); shortcut.add("Ctrl+Alt+v", function(){$(".baseButton[title*='virgolette']").click();} );
newButton("“”", "incapsula('“','”')", "es","Applica virgolette singole (Ctrl+Alt+v)"); shortcut.add("Ctrl+Alt+v", function(){$(".baseButton[title*='virgolette']").click();} );
//newButton("Ct", "incapsula('{{Ct|f=100%|v=1|L=0px|','}}')", "es","Applica il template Ct");
newButton("Ct+", "ctplus()", "es","Applica il template Ct sulla selezione");
//newButton("Bl", "incapsula('{{Smaller|','}}')", "es","Applica il template Smaller");
//newButton("Bl", "incapsula('{{block|}}\\n','\\n</div>')", "es","Applica il template Block");
//newButton("C", "incapsula('{{Centrato|','}}')", "es","Applica il template Centrato");
newButton("Ac", "mw.itsg.selAut()", "es","Applica il template AutoreCitato scegliendo in una lista");
//newButton("Ec", "incapsula('{{Ec|','||ec}}')", "es","Applica il template Ec");
//newButton("Ign", "incapsula('{{AutoreIgnoto|','}}')", "es","Applica il template AutoreIgnoto");
newButton("Sc", "incapsula('{{Sc|','}}')", "es","Applica il template SmallCaps (maiuscoletto)");
newButton("Ind", "incapsula('{{Indentatura|testo=','}}')", "es","Applica il template Indentatura");
newButton("rule", "incapsula('{{Rule|8em|t=1|v=1}}','')", "es","Applica il template Rule");
newButton("Aaaa", "capit()", "es","Applica Maiuscole alle iniziali delle parole nella selezione");
newButton("A↔a", "up_low()", "es","Applica maiuscolo/minuscolo alla selezione, alternandole");
newButton("→", "indentSelection()", "es","Indenta le righe della selezione (utile per i versi nelle poesie)");
newButton("←", "deIndentSelection()", "es","Riduce l'indentazione delle righe");
newButton("Rv", "eliminaRigheVuote()", "es","Elimina le righe vuote (talora presenti ed erronee nelle scansioni di testi poetici)");
newButton("poem","incapsula('<poem>\\n','\\n</poem>')","es","Applica il tag poem alla selezione");
//newButton("poem+","poemAll()","es","Applica il tag poem all'intero body");
newButton("poem_t","incapsula('{{poem t|\\n','\\n}}')","es","Applica il tag poem_t alla selezione");
//newButton("poem_d","poem_down()", "es", "sposta tag poem in basso");
newButton("-poem t","incapsula('}}\\n','\\n{{poem t|')","es","Chiude e riapre poem t");
//newButton("Ids","incapsula('{{Ids|','|r}}')","es","Applica Ids r");
//newButton("i","incapsula('<i>','</i>')","es","tag i per i casi problematici");
//newButton("note", "note", "es","Gestisce le annotazioni (vedi istruzioni in Utente:Alex brollo/Aiuto tool note)");
newButton("n!", "toggleNote()", "es","Sposta le annotazioni");
newButton("pt", "autoPt()", "es","autoPt");
newButton("autoRi", "forcedNewRi", "es","Inserisce un template RigaIntestazione ricavandolo da quello eventualmente presente due pagine prima");
newButton("sections", "newSections()", "es","splitta in sezioni");
newButton("tableM", "tableMaker()", "es","Trasforma in tabella wiki  il codice copiaincollato da Excel (separatori di cella: tabulazioni; separatori di righe; a capo)");
//newButton("toCol", "toCol()", "es","testo in colonne");
//newButton("appc", "append()", "es","copiaincolla colonne");
//newButton("appr", "appendr()", "es","riunisce righe");
//newButton("autoNs0", "ns0Auto()", "es","chiama autoNs0");
//newButton("Pg+","pgPlus()","es","Pagine Tiraboschi");
//newButton("son", "son()", "es","formatta sonetto selezionato");
//newButton("iisn", "esporta()", "es","esporta in pagina autonoma");
//newButton("agg", "aggiusta()", "es","aggiusta autoNs0");
//shortcut.add("Alt+Shift+3",alt3);
shortcut.add("Alt+3",alt3);
shortcut.add("Ctrl+Alt+s", function(){$(".baseButton[title*='(maiuscoletto)']").click();} );
shortcut.add("Ctrl+Alt+g", function(){incapsula("{{Greek|","|?}}");} );
// shortcut.add("Ctrl+Shift+z", function(){$(".baseButton[title*='(Zibaldone)']").click();} );

shortcut.add("Alt+Right",indentSelection);
shortcut.add("Alt+Left",deIndentSelection);
shortcut.add("Ctrl+Alt+1", function() {$("[rel='section']").click();});
$("#mw-editbutton-codemirror").remove();
function iw() {
	$(".quality0 input").click(); 
	scriviBox("{"+"{Iwpage|la}}");
	$("#wpSave").click(); 
	return false;
	
}// </nowiki>
// shortcut.add("Alt+i",iw)
function guic() {
	var testo=selection();
	var testoSelect=testo[1];
	// qui aggiungere  regex
	testoSelect=testoSelect.replace(/^p[.-][\t ]+(\d+)[\t ]+/gim,"|-\n|p. {{Pg|$1}}||" );
	testoSelect=testoSelect.replace(/\n+[—-] T/,"\n\n— T");
	
	
	// fine regex
	testoSelect=testoSelect.replace("|-","{| class=table1");
	testoSelect+="\n|}";
	scriviBox(testo[0]+testoSelect+testo[2]);
	return;
	
	
}
//newButton("x", "guic()", "es","formatta tabella");
importScript("Utente:Alex brollo/Allinea.js");
newButton("ItLa", "allinea(allineamento)", "es","allineamento it-la");
/*
img_enlarge=function(){
	var img=$(".prp-page-image img")[0];
	$(".prp-page-image img").css({"width":Math.trunc(img.width*1.12)+"px",
	"height":Math.trunc(img.height*1.12)+"px",
	"top":Math.trunc(img.top-img.height*.03)+"px",
	"top":Math.trunc(img.left-img.width*.03)+"px"
	});
	return false;
}
newButton("img+","img_enlarge","es","Ingrandisce immagine");

img_reduce=function(){
	var img=$(".prp-page-image img")[0];
	$(".prp-page-image img").css({"width":Math.trunc(img.width/1.12)+"px",
	"height":Math.trunc(img.height/1.12)+"px",
	"top":Math.trunc(img.top+img.height*.03)+"px",
	"top":Math.trunc(img.left+img.width*.03)+"px"
	});
	return false;
}
newButton("img-","img_reduce","es","Riduce immagine");
// <nowiki>

shortcut.add("Ctrl+Alt+i", function() {
$(".openseadragon-container").remove();
$(".prp-page-image img").css("display","initial");
$(".prp-page-image").attr("id","prp-page-image");
$(".prp-page-image").css("overflow","auto");
$(".prp-page-image img").draggable();
$("#prp-page-zoomIn").click(img_enlarge);
$("#prp-page-zoomOut").click(img_reduce);
});
*/
shortcut.add("Ctrl+Alt+x", function () {$("#findReplace_next").click();});
shortcut.add("Ctrl+Alt+k",function (){
    var t=sel();
    var spl=t[1][0].split("<ref>");
    var lst=spl.pop();
    var name="pag"+/\d+$/m.exec(mw.config.get("wgPageName"))[0];
    t[1][0]=spl.join("<ref>")+"<ref name="+name+">"+lst+"</ref>";
    t[1][1]="<ref follow="+name+">"+t[1][2];
    t[1][2]="";
    scriviSel(t[1]);
    return false;

	
});
// $(".mw-editfont-monospace").css("font-size","1.1pc"); $(".ace_editor").css("font-size","1.1pc");