Wikipedysta:ToSter/wpsk user.js

/*
 * Rozszerzenie WP:SK, skryptu autorstwa Nuxa (pl:User:Nux)
 * http://pl.wikipedia.org/wiki/WP:SK
 * Autor: ToSter (pl:User:ToSter)
 * Licencja:  GNU General Public License v2
 * http://opensource.org/licenses/gpl-license.php
 */
// <nowiki>
mw.hook('userjs.wp_sk.ready').add(function (wp_sk)
{
		//console.log( "Attaching to WP:SK" );
		wp_sk.myver = '3.18';
		wp_sk.extension += "+[[WP:SK/ToS|ToS]]";
		wp_sk.escapeTemplate = function( str ) {
			str = str.replace( /\|(\s*[^\|=\n]*) - ([^\|=\n]*)=/gi, "|$1 <%%%SDASH%%%> $2=" );
			str = str.replace( /\|(\s*altlink\s*=[^\|=\n]*) - ([^\|=\}\n]*)/gi, "|$1 <%%%SDASH%%%> $2" );
			str = str.replace( /([=\|]\s*[^\|\[\n]*)\.(jpe?g|png|gif|tiff?|svg|webm|ogg)/gi, function( a, fname, ext ) {
				fname = fname.replace( /[ _]-[ _]/g, ' <%%%SDASH%%%> ' );
				fname = fname.replace( /[ _]–[ _]/g, ' <%%%NDASH%%%> ' );
				fname = fname.replace( /[ _]—[ _]/g, ' <%%%MDASH%%%> ' );
				fname = fname.replace( /\./g, '<%%%DOT%%%>' );
				fname = fname.replace( /(\d)r/g, "$1<%%%R%%%>" );
				return fname + '.' + ext;
			} );
			return str;
		};

		wp_sk.cleanLinksAndTemplates = function( str ) {
			// półpauzy
			// na początek escape w linkach i szablonach
			for ( i = 0; i <= 3; i++ ) {
				str = str.replace( /\[\[([^\|\]]+?) - ([^\|]+?)([\|\]])/g, '[[$1 <%%%SDASH%%%> $2$3' );
				str = str.replace( /\[\[([^\|\]]+?) – ([^\|]+?)([\|\]])/g, '[[$1 <%%%NDASH%%%> $2$3' );
				str = str.replace( /\[\[([^\|\]]+?)×([^\|]+?)([\|\]])/g, '[[$1<%%%CROSS%%%>$2$3' );
				str = str.replace( /\[\[([^\|\]]+?)\.([^\|]+?)([\|\]])/g, '[[$1<%%%DOT%%%>$2$3' );
				str = str.replace( /\[\[([^\|\]]+?\d)r([^\|]+?)([\|\]])/g, '[[$1<%%%R%%%>$2$3' );
				str = str.replace( /\{\{((s\|)?[^\|\}]+?) - ([^\|]+?)([\|\}])/g, '{{$1 <%%%SDASH%%%> $3$4' );
				str = str.replace( /\{\{((s\|)?[^\|\}]+?) – ([^\|]+?)([\|\}])/g, '{{$1 <%%%NDASH%%%> $3$4' );
				str = str.replace( /\{\{((s\|)?[^\|\}]+?)\.([^\|]+?)([\|\}])/g, '{{$1<%%%DOT%%%>$3$4' );
				str = str.replace( /\{\{((s\|)?[^\|\}]+?)×([^\|]+?)([\|\}])/g, '{{$1<%%%CROSS%%%>$3$4' );
				str = str.replace( /\{\{((s\|)?[^\|\}]+?\d)r([^\|]+?)([\|\}])/g, '{{$1<%%%R%%%>$3$4' );
				str = str.replace( /\{\{(main|mainsec|zobacz też|osobny artykuł|główny artykuł|kategoria główna|zobacz kategorię)\|([^\}]+?) - ([^\}]+?)/gi, '{{$1|$2 <%%%SDASH%%%> $3' );
				str = str.replace( /\{\{[Dd]uża grafika\|([^\|\}]+?) - ([^\|]+?)([\|\}])/g, '{{Duża grafika|$1 <%%%SDASH%%%> $2$3' );
				str = str.replace( /\{\{[Dd]uża grafika\|([^\|\}]+?) – ([^\|]+?)([\|\}])/g, '{{Duża grafika|$1 <%%%NDASH%%%> $2$3' );
				str = str.replace( /\{\{[Dd]uża grafika\|([^\|\}]+?)\.([^\|]+?)([\|\}])/g, '{{Duża grafika|$1<%%%DOT%%%>$2$3' );
				str = str.replace( /\{\{[Dd]uża grafika\|([^\|\}]+?\d)r([^\|]+?)([\|\}])/g, '{{Duża grafika|$1<%%%R%%%>$2$3' );
				str = str.replace( /\{\{[Ww]ikisłownik\|([^\|\}]+?) - ([^\|]+?)([\|\}])/g, '{{Wikisłownik|$1 <%%%SDASH%%%> $2$3' );
				str = str.replace( /<ref name\s*=\s*"([^"]+?) - ([^"]+?)"/g, '<ref name="$1 <%%%SDASH%%%> $2"' );

				str = str.replace( /\[\[([^\]]+?) x ([^\]]+?)\]\]/g, '[[$1 <%%%X%%%> $2]]' );
			}
			// + escape w galeriach
			str = str.replace( /<gallery([\s\S]+?)<\/gallery>/gi, function( a, zaw ) {
				for ( i = 0; i < 5; i++ ) {
					zaw = zaw.replace( /(\s*[^\|\n]*) - ([^\|\n]*)\.(jpe?g|png|gif|tiff?|svg|webm|ogg)/gi, "$1 <%%%SDASH%%%> $2.$3" );
					zaw = zaw.replace( /(\s*[^\|\n]*) – ([^\|\n]*)\.(jpe?g|png|gif|tiff?|svg|webm|ogg)/gi, "$1 <%%%NDASH%%%> $2.$3" );
					zaw = zaw.replace( /(\s*[^\|\n]*) — ([^\|\n]*)\.(jpe?g|png|gif|tiff?|svg|webm|ogg)/gi, "$1 <%%%MDASH%%%> $2.$3" );
					zaw = zaw.replace( /(\s*[^\|\n]*)\.([^\|\n]*)\.(jpe?g|png|gif|tiff?|svg|webm|ogg)/gi, "$1<%%%DOT%%%>$2.$3" );
					zaw = zaw.replace( /(\s*[^\|\n]*\d)r([^\|\n]*)\.(jpe?g|png|gif|tiff?|svg|webm|ogg|JPE?G|PNG|GIF|TIFF?|SVG|WEBM|OGG)/g, "$1<%%%R%%%>$2.$3" );
					zaw = zaw.replace( /(\s*[^\[\|\n]*)\.(jpe?g|png|gif|tiff?|svg|webm|ogg)/gi, function( a, filename, ext ) {
						filename = filename.replace( /_/g, ' ' );
						filename = filename.substr( 0, 1 ).toUpperCase() + filename.substr( 1, filename.length );
						return filename + '.' + ext;
					} );
				}
				return "<gallery" + zaw + "</gallery>";
			} );
			// + escape i poprawki w {{Galeria}}
			str = str.replace( /\{\{Galeria(\s*\|[\s\S]+?)\}\}/gi, function( a, zaw ) {
				zaw = zaw.replace( /(File|Image|Plik|Grafika):/gi, "Plik:" );
				for ( i = 0; i < 5; i++ ) {
					zaw = zaw.replace( /:(\s*[^\|\n]*) - ([^\|\n]*)\.(jpe?g|png|gif|tiff?|svg|webm|ogg)/gi, ":$1 <%%%SDASH%%%> $2.$3" );
					zaw = zaw.replace( /:(\s*[^\|\n]*) – ([^\|\n]*)\.(jpe?g|png|gif|tiff?|svg|webm|ogg)/gi, ":$1 <%%%NDASH%%%> $2.$3" );
					zaw = zaw.replace( /:(\s*[^\|\n]*) — ([^\|\n]*)\.(jpe?g|png|gif|tiff?|svg|webm|ogg)/gi, ":$1 <%%%MDASH%%%> $2.$3" );
					zaw = zaw.replace( /:(\s*[^\|\n]*)\.([^\|\n]*)\.(jpe?g|png|gif|tiff?|svg|webm|ogg)/gi, ":$1<%%%DOT%%%>$2.$3" );
					zaw = zaw.replace( /:(\s*[^\|\n]*\d)r([^\|\n]*)\.(jpe?g|png|gif|tiff?|svg|webm|ogg|JPE?G|PNG|GIF|TIFF?|SVG|WEBM|OGG)/g, ":$1<%%%R%%%>$2.$3" );
					zaw = zaw.replace( /:(\s*[^\[\|\n]*)\.(jpe?g|png|gif|tiff?|svg|webm|ogg)/gi, function( a, filename, ext ) {
						filename = filename.replace( /_/g, ' ' );
						filename = filename.substr( 0, 1 ).toUpperCase() + filename.substr( 1, filename.length );
						return ':' + filename + '.' + ext;
					} );
				}
				return "{{Galeria" + zaw + "}}";
			} );
			return str;
		};

		wp_sk.unescapeString = function( str ) {
			str = str.replace( /<%%%SDASH%%%>/g, '-' );
			str = str.replace( /<%%%NDASH%%%>/g, '–' );
			str = str.replace( /<%%%MDASH%%%>/g, '—' );
			str = str.replace( /<%%%CROSS%%%>/g, '×' );
			str = str.replace( /<%%%X%%%>/g, 'x' );
			str = str.replace( /<%%%DOT%%%>/g, '.' );
			return str;
		};

		wp_sk.friendlierTemplate = function( a, nazwa, zaw ) {
			if ( zaw.indexOf( '=' ) == -1 && zaw.indexOf( '|' ) == -1 ) {
				return a;
			}
			// -dane teleadresowe w infoboksach
			zaw = zaw.replace( /(\s*)(\|\s*(email|mail|e-mail|mail_um|mail_ug|tel_ug|fax_ug|telefon|tel_um|fax_um)\s*=[\s\S]*?)+([\|\}])/g, '$1$4' );
			// escape
			zaw = wp_sk.escapeTemplate( zaw );
			zaw = zaw.replace( /\|(\s*[^\[\|\n]*)=(\s*[^\[\|\n]*)\.(jpe?g|png|gif|tiff?|svg|webm|ogg)/gi, function( a, param, filename, ext ) {
				spaces = '';
				if ( (filename.indexOf( '://' ) == -1) && (filename.indexOf( '%3a%2f%2f' ) == -1 ) ) {
					filename = filename.replace( /_/g, ' ' );
					while ( filename.charAt( 0 ) == ' ' ) {
						filename = filename.substr( 1, filename.length );
						spaces += ' ';
					}
					filename = filename.substr( 0, 1 ).toUpperCase() + filename.substr( 1, filename.length );
				}
				return '|' + param + '=' + spaces + filename + '.' + ext;
			} );
			return '{{' + nazwa + zaw + '}}';
		};

		wp_sk.cleaner_new = function( str ) {
			str = wp_sk.cleanerWikiVaria_old( str );

			// escape + podstawowe poprawki w linkach i szablonach
			str = wp_sk.cleanLinksAndTemplates( str );

			// brakująca kropka
			str = str.replace( /([IVX]+) *w([\),])/g, '$1 w.$2' );

			// ort.
			str = str.replace( /(północny|południowy|środkowy) *[–-] *(wschód|zachód)/g, '$1 $2' );
			str = str.replace( /(północnego|południowego|środkowego) *[ –-] *(wschodu|zachodu)/g, '$1 $2' );
			str = str.replace( /(północnym|południowym|środkowym) *[ –-] *(wschodzie|zachodzie)/g, '$1 $2' );
			str = str.replace( /(północno|południowo|środkowo) *[ –-] *(wschodni|zachodni)/g, '$1-$2' );

			// + escape w szablonach, przy okazji sprzątnięcie podkreśleń w nazwach plików i usunięcie zbędnych parametrów
			str = str.replace( /\{\{([^|}]+)((?:[^{}]|[^{}][{}][^{}]|\{\{(?:[^{}]|[^{}][{}][^{}]|\{\{[^{}]+\}\})+\}\})+)\}\}/g, wp_sk.friendlierTemplate );
			str = str.replace( /(\s)-(\s)/g, '$1–$2' );
			str = str.replace( /&ndash;/g, '–' );
			str = str.replace( /&mdash;/g, '—' );
			// str = str.replace(/ — /g, ' – ');
			// spacje w listach
			str = str.replace( /(\n[#\*:]+)([^ \t\n*#:\{])/g, '$1 $2' );
			str = str.replace( /(\n[#\*:]+)\{\{/g, '$1 {{' );

			// -zbędne spacje przed div
			str = str.replace( /\n\s*<(\/)?div/g, '\n<$1div' );
			// skale map
			str = str.replace( /(\D1): ?(\d) ?(\d{3}\D)/g, '$1:$2$3' );
			str = str.replace( /(\D1): ?(\d{2,3}) ?(\d{3}\D)/g, '$1:$2&nbsp;$3' );
			// liczby do 9999 bez spacji
			str = str.replace( /(\s\d)( |&nbsp;)(\d{3}(\s|&nbsp;)(\D))/g, '$1$3' );

			// optymistyczna heurystyka:
			// kropka na nbsp dla liczb pięciocyfrowych
			str = str.replace( /(\d{2})\.(\d{3})( |&nbsp;)?(m|cm|mm|ha|os\.|osób|km|°C|°F)(\W)/g, '$1&nbsp;$2&nbsp;$4$5' );
			// ludzie niepodzielni - ciach kropkę
//			str = str.replace( /(\d+)\.(\d+)( |&nbsp;)?(mieszk\.|mieszkańców|os\.|osób)(\W)/g, '$1$2&nbsp;$4$5' );
			str = str.replace( /(\d+)\.(\d+)( |&nbsp;)?(os\.)(\W)/g, '$1$2&nbsp;$4$5' );
			// spacja przed rokiem
			str = str.replace( /((\s([12])?\d{3})r\.)/g, '$2 r.' );
			str = str.replace( /<%%%R%%%>/g, 'r' );

			// encje, unicode od Malarza
			str = str.replace( /&bull;/g, '•' );
			str = str.replace( /\&\#8211;/g, '–' );
			str = str.replace( /\&\#8216;/g, '‘' );
			str = str.replace( /\&\#8217;/g, '’' );
			str = str.replace( /\&\#8221;/g, '”' );

			// int./ort.
			str = str.replace( /([ \(])m[\. ]+in\.? /g, '$1m.in. ' );
			str = str.replace( / np: /g, ' np. ' );

			// dwie kropki na końcu zdania
			str = str.replace( /\.\]\]\./g, '.]]' );
			// uproszczenie linku do n.p.m.
			str = str.replace( /\[\[(n\.p\.m\.\|)?m n\.p\.m\.\]\]/gi, 'm [[n.p.m.]]' );
			str = str.replace( /\[\[metr\|m\]\] \[\[n\.p\.m\.\]\]/gi, 'm [[n.p.m.]]' );

			// strzałki dla opisów szlaków
			str = str.replace( /↑ ?(\d)/g, '↑&nbsp;$1' );
			str = str.replace( /↓ ?(\d)/g, '↓&nbsp;$1' );

			str = str.replace( /_infobox/g, ' infobox' );

			str = str.replace( /\| wydawca=Wydaw\. Naukowe PWN/g, '| wydawca= Wydawnictwo Naukowe PWN' );

			// -puste parametry cytuj
			str = str.replace( /((\s*)\|\s*(imię\d|nazwisko\d|inni|autor link\d|autor\d)\s*=\s*)+([\|\}])/g, '$2$4' );

			// 1 x 2 -> odpowiedni krzyżyk + nbsp
			str = str.replace( /([0-9,\.]+) [x×] ([0-9,\.]+) /g, '$1&nbsp;×&nbsp;$2&nbsp;' );
			str = str.replace( /\{\{nowrap start\}\}(\s*)([0-9,\.]+) [x×] ([0-9,\.]+) /g, '{{nowrap start}}$1$2 × $3 ' );

			// przypisy dużą literą
			str = str.replace( / ?\{\{[pP]rzypisy/g, '{{Przypisy' );
			// kropka w języku do linka
			str = str.replace( /\[\[język ([a-ząćęłńóśźż]{2,5})([a-ząćęłńóśźż]+)\|\1\]\]\. ?/gi, '[[język $1$2|$1.]] ' );

			str = str.replace( /\{\{Seealso/gi, '{{Zobacz też' );

			// drobiazgi przy refach
			str = str.replace( /<\/ref>([\.,;])([a-zA-ZąćęłńóśźżĄĆĘŁŃÓŚŹŻ])/g, '</ref>$1 $2' );

			// sprzątanie wiosek
			str = str.replace( /Dane z \[\[30 czerwca\]\] \[\[2004\]\]\{\{ŹródłoPL\|2\}\}:/g, 'Dane z 30 czerwca 2004{{ŹródłoPL|2}}:' );
			str = str.replace( /\{\| border="1" cellpadding="4" cellspacing="0" style="margin: 0 0 1em 1em; background: #f9f9f9; border: 1px #aaaaaa solid; border-collapse: collapse; text-align:right;"/g, '{| class="wikitable"' );

			// wikizacja html
			str = str.replace( /<i>([\s\S]+?)<\/i>/gi, "''$1''" );
			str = str.replace( /<(b|strong)>([\s\S]+?)<\/\1>/gi, "'''$2'''" );
			str = str.replace( /<(h1|h2)>([\s\S]+?)<\/\1>/gi, "== $2 ==" );
			str = str.replace( /<h3>([\s\S]+?)<\/h3>/gi, "=== $1 ===" );
			str = str.replace( /<h4>([\s\S]+?)<\/h4>/gi, "==== $1 ====" );
			str = str.replace( /<h5>([\s\S]+?)<\/h5>/gi, "===== $1 =====" );
			// listy
			str = str.replace( /<([uo]l)>([\s\S]+?)<\/\1>/gi, function( a, ltype, zaw ) {
				var substBeg = ( ltype.toLowerCase().charAt( 0 ) == 'u' ) ? '*' : '#';
				zaw = zaw.replace( /<li>/gi, '\n<li>' );
				zaw = zaw.replace( /\n\n/gi, '\n' );
				zaw = zaw.replace( /<\/li>/gi, '' );
				zaw = zaw.replace( /<li>/gi, substBeg + ' ' );
				return zaw;
			} );

			// tabelki
			str = str.replace( /<table([\s\S]+?)>/gi, '{|$1' );
			str = str.replace( /<\/table>/gi, '|}' );
			str = str.replace( /<\/td>\n+\s*<td>/gi, '\n| ' );
			str = str.replace( /<\/th>\n+\s*<th>/gi, '\n! ' );
			str = str.replace( /<\/td>\n+\s*<td ([\s\S]+?)>/gi, '\n| $1 | ' );
			str = str.replace( /<\/th>\n+\s*<th ([\s\S]+?)>/gi, '\n! $1 | ' );
			str = str.replace( /<\/td>\s*<td>/gi, ' || ' );
			str = str.replace( /<\/th>\s*<th>/gi, ' !! ' );
			str = str.replace( /<\/td>\s*<td ([\s\S]+?)>/gi, ' || $1 | ' );
			str = str.replace( /<\/th>\s*<th ([\s\S]+?)>/gi, ' !! $1 | ' );
			str = str.replace( /(<\/t[dh]>)?\s*<\/tr>/gi, '' );
			str = str.replace( /\s*<tr([\s\S]*?)>\s*/gi, '\n|- $1\n' );
			str = str.replace( /\n\s*<td>/gi, '\n| ' );
			str = str.replace( /\n\s*<th>/gi, '\n! ' );
			str = str.replace( /\n\s*<td ([\s\S]+?)>/gi, '\n| $1 | ' );
			str = str.replace( /\n\s*<th ([\s\S]+?)>/gi, '\n! $1 | ' );
			str = str.replace( /<\/td>/gi, '' );

			// -> Plik: wszędzie, np. w parametrach infoboksów - rezygnacja, bo wycofujemy to z szablonów a generuje błędy
//			str = str.replace( /(= *|\[\[)(Grafika|File|Image):/gi, '$1Plik:' );

			// -spacje przy px
			str = str.replace( /\|(\d{2,4}) px\|/g, '|$1px|' );
			// centre -> center
			str = str.replace( /\|centre/g, '|center' );
			// |thumb|]] -> |thumb]]
			str = str.replace( /\|(center|thumb|left)\|\]\]/g, '|$1]]' );
			// thumb na początek, -right, od Matma Rexa
			str = str.replace( /\[\[(?:Plik):([^\|\]]+)\|((?:\[\[.+?\]\]|[^\]])+)\]\]/g, function( a, name, opts ) {
				o = opts.split( '|' );
				if ( o.indexOf( 'thumb' ) >= 0 ) {
					z = o.indexOf( 'right' );
					if ( z >= 0 ) o.splice( z, 1 );
					if ( o.indexOf( 'thumb' ) !== 0 ) {
						o.splice( o.indexOf( 'thumb' ), 1 );
						o.unshift( 'thumb' );
					}
				}
				res = '|' + o.join( '|' );
				res = res.replace( /\|\|/g, '|' );
				res = res.replace( /\|$/g, '' );
				return '[[Plik:' + name + res + ']]';
			} );
			// odescapujemy
			str = wp_sk.unescapeString( str );

			return str;
		};

		wp_sk.cleanerWikiVaria_old = wp_sk.cleanerWikiVaria;
		wp_sk.cleanerWikiVaria = wp_sk.cleaner_new;

});

//</nowiki>

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.
Kembali kehalaman sebelumnya