Wikipedysta:Persino/Gadget-StronicowyParser.js/SzablonSubst.js

Z Wikibooks, biblioteki wolnych podręczników.

Uwaga: aby zobaczyć zmiany po opublikowaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.

  • Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5, lub Ctrl+R (⌘-R na komputerze Mac)
  • Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
  • Internet Explorer / Edge: Przytrzymaj Ctrl, jednocześnie klikając Odśwież, lub naciśnij klawisze Ctrl+F5
  • Opera: Naciśnij klawisze Ctrl+F5.
mw.loader.using('ext.gadget.gConfig', function(){
let globalne_zmienne=this;
StronicowyParser.SzablonSubst=function(){
	let szablonsubst_rozwijanie_szablonow=undefined;
	let szablonsubst_dymki_nad_szablonami=undefined;
	let szablonsubst_numerowanie=undefined;
	let szablonsubst_nie_uwzgledniaj_edycji_sekcji=undefined;
	let szablonsubst_spis_tresci=undefined;
	let szablonsubst_latex=undefined;
	let szablonsubst_mathml=undefined;
	if((globalne_zmienne.gConfig!==undefined)&&(globalne_zmienne.gConfig.register!==undefined)&&(globalne_zmienne.gConfig.get!==undefined)){
  		mw.messages.set({
			'SzablonSubst-name': 'Rozwijanie szablonów',
			'SzablonSubst-description-page': 'Wikibooks:StronicowyParser/SzablonSubst',
			'SzablonSubst-rozwijanie_szablonow': 'Włącz rozwijanie szablonów w JavaScript.',
			'SzablonSubst-dymki_nad_szablonami': 'Włącz obsługę dymków nad linkami, tworząc je inteligentnymi.',
			'SzablonSubst-szablonsubst_numerowanie': 'Włącz numerowanie wewnątrz szablonu.',
			'SzablonSubst-nie_uwzgledniaj_edycji_sekcji': 'Nie uwzględniaj edycji sekcji.',
			'SzablonSubst-spis_tresci': 'Generuj Spis treści artykułu, w któym uruchomiono gadżet SzablonSubst.',
			'SzablonSubst-latex': 'Zamiast wzorów, wstaw tekst latexowy, w przeciwnym przypadku jest w formie LaTeX.',
			'SzablonSubst-mathml': 'Zamiast wzorów latexowych, wstaw je w formie znaczników MathML.',
  		});
  		gConfig.register('SzablonSubst', {name: mw.msg('SzablonSubst-name'), descriptionPage: mw.msg('SzablonSubst-description-page')}, [
		{
			name: 'rozwijanie_szablonow',
			desc: mw.msg('SzablonSubst-rozwijanie_szablonow'),
			type: 'boolean',
			deflt: true,
		    legacy: [window, 'wb_szablonsubst_rozwijanie_szablonow']
		},
		{
			name: 'dymki_nad_szablonami',
			desc: mw.msg('SzablonSubst-dymki_nad_szablonami'),
			type: 'boolean',
			deflt: true,
		    legacy: [window, 'wb_szablonsubst_dymki_nad_szablonami']
		},
		{
			name: 'szablonsubst_numerowanie',
			desc: mw.msg('SzablonSubst-szablonsubst_numerowanie'),
			type: 'boolean',
			deflt: true,
		    legacy: [window, 'wb_szablonsubst_szablonsubst_numerowanie']
		},
		{
			name: 'nie_uwzgledniaj_edycji_sekcji',
			desc: mw.msg('SzablonSubst-nie_uwzgledniaj_edycji_sekcji'),
			type: 'boolean',
			deflt: false,
		    legacy: [window, 'wb_szablonsubst_nie_uwzgledniaj_edycji_sekcji']
		},
		{
			name: 'spis_tresci',
			desc: mw.msg('SzablonSubst-spis_tresci'),
			type: 'boolean',
			deflt: true,
		    legacy: [window, 'wb_szablonsubst_latex']
		},
		{
			name: 'latex',
			desc: mw.msg('SzablonSubst-latex'),
			type: 'boolean',
			deflt: false,
		    legacy: [window, 'wb_szablonsubst_latex']
		},
		{
			name: 'mathml',
			desc: mw.msg('SzablonSubst-mathml'),
			type: 'boolean',
			deflt: true,
			legacy: [window, 'wb_szablonsubst_mathml']
		},
		]);
		
		szablonsubst_rozwijanie_szablonow = (gConfig.get('SzablonSubst', 'rozwijanie_szablonow')==true);
		szablonsubst_dymki_nad_szablonami = (gConfig.get('SzablonSubst', 'dymki_nad_szablonami')==true);
		szablonsubst_numerowanie = (gConfig.get('SzablonSubst', 'szablonsubst_numerowanie')==true);
		szablonsubst_nie_uwzgledniaj_edycji_sekcji = (gConfig.get('SzablonSubst', 'nie_uwzgledniaj_edycji_sekcji')==true);
		szablonsubst_spis_tresci = (gConfig.get('SzablonSubst', 'spis_tresci')==true);
		szablonsubst_latex = (gConfig.get('SzablonSubst', 'latex')==true);
		szablonsubst_mathml = (((gConfig.get('SzablonSubst', 'mathml'))&&(!szablonsubst_latex))==true);
	}
	szablonsubst_rozwijanie_szablonow=((szablonsubst_rozwijanie_szablonow!=undefined)?szablonsubst_rozwijanie_szablonow:true);
	szablonsubst_dymki_nad_szablonami=((szablonsubst_dymki_nad_szablonami!=undefined)?szablonsubst_dymki_nad_szablonami:true);
	szablonsubst_numerowanie=((szablonsubst_numerowanie!=undefined)?szablonsubst_numerowanie:true);
	szablonsubst_nie_uwzgledniaj_edycji_sekcji=((szablonsubst_nie_uwzgledniaj_edycji_sekcji!=undefined)?szablonsubst_nie_uwzgledniaj_edycji_sekcji:false);
	szablonsubst_spis_tresci=((szablonsubst_spis_tresci!==undefined)?szablonsubst_spis_tresci:true);
	szablonsubst_latex=((szablonsubst_latex!==undefined)?szablonsubst_latex:false);
	szablonsubst_mathml=((szablonsubst_mathml!==undefined)?szablonsubst_mathml:true);
	
	let tab_num=new Array();
	
	this.LadujSzablon=async function(element,nazwa,titles,czy_numerowac,tab_promise){
		if(!szablonsubst_rozwijanie_szablonow){if(tab_promise){tab_promise.zwolnij();}; return;}
		if(czy_numerowac==undefined){czy_numerowac=true;}
		await StronicowyParser.muteks.zablokuj();
		//await StronicowyParser.script.LoadScript('skins.vector.es6');
		StronicowyParser.NaprawNaglowkiStrony(czy_numerowac);
		let szablon_js;
		if(!element){
			szablon_js=$('#mw-content-text .mw-parser-output .szablon_subst_js');
		}else{
			szablon_js=$(element).find('.szablon_subst_js');
		}
		if((szablon_js!==null)&&(szablon_js.length>0)){
			let catlinks_fun=new StronicowyParser.LinkiKategorii();
			await catlinks_fun.PobierzKategorieStrony();
			let tab_szablon_js=new Array();
			for(let i=0;i<szablon_js.length;++i){tab_szablon_js[i]=szablon_js[i];}
			for(let i=0;i<tab_szablon_js.length;++i){
				let element=tab_szablon_js[i];
				let potomek=((element)?($('.szablon_subst_js').find(element)):element);
				if((potomek===null)||(potomek.length==0)){
					let strona_szablon_js=undefined;
					let nazwa_strony=$(element).parent().first()[0].querySelector('.nazwa_strony');
					if(nazwa_strony){
						strona_szablon_js=StronicowyParser.style.UsunStyle(nazwa_strony.innerHTML).replace(/<[^<>]+>/g,"").replace(/^[_\s]*/g,"").replace(/[_\s]*$/g,"").replace(/[_\s]+/g,"_");
						if(/^[_\s]*$/g.test(strona_szablon_js)){
							strona_szablon_js=undefined;
						}
					}
					if(!strona_szablon_js){
						strona_szablon_js=((nazwa)?nazwa:(StronicowyParser.pageinfo.fullname));
					}
					let zawartosc;
					let wzor_element=$(element).children("*");
					if((wzor_element!==null)&&(wzor_element.length>0)){
						zawartosc=element.innerHTML;
						for(let i=0;i<wzor_element.length;++i){
							let math=wzor_element[i].outerHTML;
							let math_temp=math;
						
							math=math.replace(/[\{\}\=\|]/g,function(s){
								let tab={
									"{":"{{(}}",
									"}":"{{)}}",
									"=":"{{=}}",
									"|":"{{!}}"
								};
								return tab[s]||s;
							}).replace(/>/g,"&amp;gt;").replace(/</g,"&amp;lt;").replace(/\//g,"&amp;#47;");
						
							math_temp=StronicowyParser.wzor.BezZnakowSpecjalnych(math_temp);
							let re=new RegExp(math_temp,"g");
						
							zawartosc=zawartosc.replace(re,math);
						}
					}else{
					   zawartosc=element.innerHTML;
					}
					let grupa=zawartosc.match(/\|\s*grupa\s*=\s*[^\|\{\}<>]+/g);
					if(grupa!==null){
						let grupa_temp=grupa.toString();
						let grupa_temp2=grupa_temp;
						grupa_temp=StronicowyParser.wzor.BezZnakowSpecjalnych(grupa_temp);
						let re=new RegExp(grupa_temp,"g");
						
						grupa=grupa.toString().replace(/\|\s*grupa\s*=\s*/g,"").replace(/^\s*/g,"").replace(/\s*$/g,"");
						tab_num[grupa]=((tab_num[grupa])?tab_num[grupa]:0)+1;
						
						zawartosc=zawartosc.replace(re,grupa_temp2+"|numer szablonu="+tab_num[grupa]);
					}
					
					if(tab_promise){tab_promise.wygeneruj(1);};
					StronicowyParser.api.ZadaniaApi({
						action: 'parse',
						format: 'json',
						prop: 'text|categories',
						title: strona_szablon_js,
						text: "<div class=\"hn hn_"+i+"\">"+((szablonsubst_nie_uwzgledniaj_edycji_sekcji)?"__NOEDITSECTION__":"")+zawartosc+"</div>",
						disabletoc: true,
						disablelimitreport: true,
						contentmodel: 'wikitext',
						smaxage: 600,
						maxage: 600,
					}).then(async function(data){
						    await StronicowyParser.muteks.zablokuj();
	    					let wikikod=data.parse.text['*'];
	    					let title=data.parse.title;
	    					wikikod=wikikod.replace(/&amp;gt;/g,">").replace(/&amp;lt;/g,"<").replace(/&amp;#47;/g,"/");
	    					
	    					let tab=StronicowyParser.potrzebne.UzyskajLiczbe(wikikod)
	    		        	let i=tab[0];
		    	        	let hn=tab[1];
	    					if(hn==null){StronicowyParser.muteks.odblokuj();if(tab_promise){tab_promise.zwolnij();};return;}
	    					
	    					catlinks_fun.AnalizaKategorii(data).then((result) => {}).catch((error) => {});
	    			
	    					let parent_szablon_js=szablon_js[i].parentNode;
	    					let element_div;
	    					if($(parent_szablon_js).hasClass('rodzic_szablon_subst_js')){
	    						element_div=parent_szablon_js;
	    					}else{
	    						element_div=szablon_js[i];
	    					}
	    					element_div.innerHTML=hn.innerHTML;
	    					element_div.setAttribute('nazwa_strony',title);
	    					element_div.classList.add('szablon_subst_js_zaanalizowany');

	    				    if(!nazwa){
	    				    	if((StronicowyParser.numerowanie)&&(StronicowyParser.numerowanie.NumerujRozdzialyArtykulu)&&(szablonsubst_numerowanie)){
	    				    		if(tab_promise){tab_promise.wygeneruj(1);};
									StronicowyParser.numerowanie.NumerujRozdzialyArtykulu(tab_promise).then((result) => {}).catch((error) => {});
	    				    	}
	    				    }else{
	    				    	if((self_gadget_strona_zbiorcza.numerowanie)&&(self_gadget_strona_zbiorcza.numerowanie.NumerujRozdzialyPoSciagnieciuArtykulu)&&(szablonsubst_numerowanie)){
	    				    		if(tab_promise){tab_promise.wygeneruj(1);};
									StronicowyParser.numerowanie.NumerujRozdzialyPoSciagnieciuArtykulu(element,tab_promise);//Funkcja jako gadżet do numerowania rozdziałów.
								}
								if((titles)&&(titles.length>0)){
									StronicowyParser.ZamienLinkiNaLinkiDoStronyAktualnej(element_div,nazwa,titles);
								}
	    				    }
	    				    
							if((StronicowyParser.gadgetdymkisubst.UstawTagiIKlasyKomunikatuInfo)&&(szablonsubst_dymki_nad_szablonami)){
								if(tab_promise){tab_promise.wygeneruj(1);};
								StronicowyParser.gadgetdymkisubst.UstawTagiIKlasyKomunikatuInfo(element_div,tab_promise).then((result) => {}).catch((error) => {});
	    					}
	    		    		if((StronicowyParser.gadgetdymkisubst.UstawZdarzeniaKomunikatuInfo)&&(szablonsubst_dymki_nad_szablonami)){
	    		    			if(tab_promise){tab_promise.wygeneruj(1);};
								StronicowyParser.gadgetdymkisubst.UstawZdarzeniaKomunikatuInfo(element_div,tab_promise).then((result) => {}).catch((error) => {});
	    					}
	    					if(StronicowyParser.TrybCiemnyIJasny){
								StronicowyParser.TrybCiemnyIJasny(element_div);
							}
							
							StronicowyParser.ZmieniajNazwyLinkowNaLinkiPodrecznika(element_div);
							
							let tab_element_div=$(element_div).find("h1, h2, h3, h4, h5, h6");
							if((tab_element_div!==null)&&(tab_element_div.length>0)){
								
								if(szablonsubst_spis_tresci){
									let strona_start=$('#mw-content-text .mw-parser-output .strona_start').find(element_div).parents('.strona_start');
									if((strona_start!==null)&&(strona_start.length>0)){
										StronicowyParser.DodawajRozdzialyDoTOCSpisTresci(strona_start[0],czy_numerowac,szablonsubst_latex,szablonsubst_mathml);
									}else{
										StronicowyParser.DodawajRozdzialyDoTOCSpisTresci(undefined,czy_numerowac,szablonsubst_latex,szablonsubst_mathml);
									}
								}
								$(".spis_treści").has('ul li a').css('visibility',"unset");
							}
	    					if(tab_promise){tab_promise.wygeneruj(1);};
	    					StronicowyParser.szablony.LadujSzablon(element_div,nazwa,titles,czy_numerowac,tab_promise).then((result) => {}).catch((error) => {});
	    					StronicowyParser.muteks.odblokuj();
	    					if(tab_promise){tab_promise.zwolnij();};
					}).catch(async (error) => {if(tab_promise){tab_promise.zwolnij();};});
				}
			}
		}
		StronicowyParser.muteks.odblokuj();
		if(tab_promise){tab_promise.zwolnij();};
		return tab_promise;
    }
}
StronicowyParser.szablony=new StronicowyParser.SzablonSubst();
});