MediaWiki:Gadget-edit-form-parser.js

Z Wikisłownika – wolnego słownika wielojęzycznego

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.
EParser = {
	getSections: function ( code, lang ) {
		var sections, reta, s, sec, section, id;

		if ( lang === undefined ) {
			code = code.replace( /^==([^=][^\n]+?)==\s*$/gm, '<BE>$1<EN>' );
		} else {
			switch ( lang ) {
				case 'ru':
					code = code.replace( /^(=([^=][^\n]+?)=)\s*$/gm, '<BE>$1<EN>' );
					break;
				case 'fr':
				case 'li':
				case 'nl':
				case 'oc':
					code = code.replace( /^((==\s*)?\{\{=([^=\-][^\n]*?)=\}\}(\s*==)?)\s*$/gm, '<BE>$1<EN>' );
					break;
				case 'lv':
					code = code.replace( /^(\{\{-([^=\-][^\n]*?)-\}\})\s*$/gm, '<BE>$1<EN>' );
					break;
				case 'co':
				case 'ga':
					code = code.replace( /^(\{\{-\w\w-\}\})\s*$/gm, '<BE>$1<EN>' );
					break;
				case 'is':
					code = code.replace( /^(\{\{-\w{2,3}-\}\})\s*$/gm, '<BE>$1<EN>' );
					break;
				case 'it':
					code = code.replace( /^(\{\{in\|[^\}]+\}\})\s*$/gm, '<BE>$1<EN>' );
					break;
				case 'es':
					code = code.replace( /^(\{\{[A-Z\-]{2,}\|[^\}]+\}\})\s*$/gm, '<BE>$1<EN>' );
					break;
				case 'af':
					code = code
						.replace( /^(\{\{-\w\w-\}\})\s*$/gm, '<BE>$1<EN>' )
						.replace( /^(==([^=][^\n]+?)==)\s*$/gm, '<BE>$1<EN>' );
					break;
				default:
					code = code.replace( /^(==([^=][^\n]+?)==)\s*$/gm, '<BE>$1<EN>' );
					break;
			}
		}
		
		sections = code.split( '<BE>' );
		reta = {};
		
		for ( s in sections ) {
			if ( sections.hasOwnProperty( s ) && sections[ s ].length ) {
				sec = sections[ s ].split( '<EN>' );

				if ( sec.length === 1 ) {
					// sekcja zerowa
					reta[ EConstants.SECTION_ID_INTRO ] = {
						content:     $.trim( sec[ 0 ] ),
						title:       '',
						id:          EConstants.SECTION_ID_INTRO,
						initcontent: $.trim( sec[ 0 ] )
					};
				} else if ( lang === undefined ) {
					// polski
					section = this.getSectionFromTitle( $.trim( sec[ 0 ] ) );
					id = section.id;
					reta[ id ] = section;
					reta[ id ].content = $.trim( sec[ 1 ] );
				} else {
					reta[ sec[ 0 ] ] = {
						title:   sec[ 0 ],
						content: $.trim( sec[ 1 ] )
					};
				}
			}
		}
		
		return reta;
	},

	getSectionFromTitle: function ( str ) {
		var template = this.insideTemplate( str );

		return {
			'title':       str,
			'short':       template.replace( /język /, '' ),
			'content':     '',
			'id':          this.langId( template ),
			'code':        this.langCode( template ),
			'initcontent': ''
		};
	},

	getTitleFromCode: function ( code ) {
		var lang,
		pagename = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' );

		if ( code === 'zh-char' || code === 'zh' ) {
			pagename = '{' + '{zh|' + pagename + '}}';
		} else if ( code === 'ja' || code === 'ko' ) {
			pagename = '{' + '{' + code + '|' + pagename + '}}';
		}
		
		lang = EConstants.CODE_TO_LANG[ code ] || code;
		return pagename + ' ({' + '{' + lang + '}})';
	},

	getSectionFromCodeAndLang: function ( code, lang ) {
		return {
			'title':       EParser.getTitleFromCode( code ),
			'short':       lang.replace( /język /, '' ),
			'content':     '',
			'id':          this.langId( lang ),
			'code':        code,
			'initcontent': ''
		};
	},

	langId: function ( langname ) {
		switch ( langname ) {
			case EStr.INTERNATIONAL_USAGE:
				return EConstants.SECTION_ID_INTERNATIONAL;
			case EStr.POLISH:
				return EConstants.SECTION_ID_POLISH;
			case EStr.CHINESE_SIGN:
				return EConstants.SECTION_ID_CHINESE_SIGN;
		}
		
		langname = langname
			.replace( /język /, '' ).replace( /[ąáåã]/g, 'azz' ).replace( /ć/g, 'czz' )
			.replace( /[ęè]/g, 'ezz' ).replace( /ł/g, 'lzz' ).replace( /ń/g, 'nzz' ).replace( /[óõ]/g, 'ozz' )
			.replace( /ś/g, 'szz' ).replace( /ü/g, 'uzz' ).replace( /ź/g, 'zzy' ).replace( /ż/g, 'zzz' )
			.replace( /[ \|!\(\)]/g, '_' );
		
		return langname;
	},

	getSectionFromInput: function ( str ) {
		var langname = EConstants.CODE_TO_LANG[ str ];

		if ( langname !== undefined ) {
			return this.getSectionFromCodeAndLang( str, langname );
		}

		var code = EConstants.LANG_CODES[ str ];
		
		if ( code !== undefined ) {
			if ( $.inArray( str, EConstants.SHORT_LANGS ) === -1 ) {
				str = 'język ' + str;
			}
			
			return this.getSectionFromCodeAndLang( code, str );
		}
		
		return this.getSectionFromTitle( str );
	},

	insideTemplate: function ( str ) {
		return str.replace( /.*\{\{(.*?)(\}\}|\|).*/g, '$1' );
	},

	langCode: function ( lang ) {
		lang = lang.replace( 'język ', '' );
		return EConstants.LANG_CODES[ lang ] || lang;
	},

	extractSubsections: function ( str, name, code ) {
		var sec, index, re,
		sections = EParser.getSections( str ),
		subsections = [];

		$.each( sections, function () {
			if (
				!this.content ||
				( code && code !== this.code )
			) {
				return true;
			}
			
			sec = this.content;
			index = sec.indexOf( '{' + '{' + name + '}}' );
			
			if ( index > -1 ) {
				sec = sec.substring( index + name.length + 4 );
				re = new RegExp( '\\{\\{(' + EConstants.SUBSECTIONS.ALL.join( '|' ) + ')[\\}\\|]' );
				subsections.push( sec.substring( 0, sec.search( re ) ) );
			}
		} );
		
		return subsections.join( '' );
	}
};

ESectionParser = {
	parse: function ( section ) {
		var i, targetSubsections,
		subsections = [],
		mode = null,
		code = section.code;

		if ( !section.title ) {
			mode = 'INTRO';
		} else {
			switch ( code ) {
				case 'pl':
					mode = 'POLISH';
					break;
				case 'zh-char':
					mode = 'CHINESE_SIGN';
					break;
				case 'zh':
					mode = 'CHINESE';
					break;
				case 'egy':
					mode = 'EGYPTIAN';
					break;
				case 'ko':
					mode = 'KOREAN';
					break;
				case 'ja':
					mode = 'JAPANESE';
					break;
				case 'eo':
					mode = 'ESPERANTO';
					break;
				case 'eom':
					mode = 'ESPERANTO_M';
					break;
				case 'inter':
					mode = 'INTERNATIONAL';
					break;
				default:
					mode = [];
					
					if ( $.inArray( code, EConstants.DOUBLE_LANGS ) !== -1 ) {
						mode.push( 'DOUBLE' );
					}
					
					if ( $.inArray( code, EConstants.NON_LATIN_LANGS ) !== -1 ) {
						mode.push( 'NON_LATIN' );
					} else {
						mode.push( 'LATIN' );
					}
					
					if ( mode.length === 1 ) {
						mode = mode[ 0 ];
					}
					
					break;
			}
		}
		
		subsections.push( {
			title:     '',
			content:   '',
			shortened: false,
			active:    true
		} );
		
		for ( i in EConstants.SUBSECTIONS.ALL ) {
			if ( EConstants.SUBSECTIONS.ALL.hasOwnProperty( i ) ) {
				subsections.push( {
					title:         EConstants.SUBSECTIONS.ALL[ i ],
					content:       '',
					shortened:     true,
					initcontent:   '',
					initmultiline: false,
					active:        true
				} );
			}
		}

		targetSubsections = Array.isArray( mode )
			? ESectionParser.combineModes( mode )
			: ( EConstants.SUBSECTIONS[ mode ] || [] );
		
		section.subsections = subsections;
		section.mode = mode;
		
		ESectionParser.parsePreparedSubsections( section, targetSubsections );
	},
	
	combineModes: function ( modes ) {
		var hset = [], combined = [];
		
		$.each( modes, function ( i, mode ) {
			$.each( EConstants.SUBSECTIONS[ mode ], function ( j, value ) {
				hset[ EConstants.SUBSECTIONS.ALL.indexOf( value ) ] = true;
			} );
		} );
		
		$.each( hset, function ( i, v ) {
			if ( v ) {
				combined.push( EConstants.SUBSECTIONS.ALL[ i ] );
			}
		} );
		
		return combined;
	},

	alternateTitle: function ( title ) {
		switch ( title ) {
			case 'transliteracja':
				return '|trans';
			case 'transkrypcja':
				return '|transkr';
			case 'kreski':
				return '|hanja-kreski';
			case 'przykłady':
				return '|użycie';
			default:
				return '';
		}
	},

	parsePreparedSubsections: function ( section, targetSubsections ) {
		var i, j, title, alt, regex, sub, pos, repl, changed, firstbreak,
		str = section.content,
		subsections = section.subsections,
		positions = [];

		for ( i in subsections ) {
			if ( subsections.hasOwnProperty( i ) ) {
				title = subsections[ i ].title;
				alt = ESectionParser.alternateTitle( title );
				regex = new RegExp( '\\{\\{(' + title + alt + ')\\s*[\\|\\}]', 'g' );
				
				positions.push( {
					index: title === '' ? 0 : str.search( regex ),
					title: title
				} );
			}
		}
		
		positions.sort( function ( a, b ) {
			if ( !a.index && !b.index ) {
				return a.title ? 1 : -1;
			}
			
			return a.index - b.index;
		} );

		for ( i in subsections ) {
			if ( subsections.hasOwnProperty( i ) ) {
				sub = subsections[ i ];
				
				for ( j in positions ) {
					if ( positions.hasOwnProperty( j ) ) {
						j = parseInt( j, 10 );
						pos = positions[ j ];
						
						if ( pos.title === sub.title ) {
							if ( pos.index !== -1 ) {
								sub.content = ( j < positions.length - 1 )
									? $.trim( str.substring( pos.index, positions[ j + 1 ].index ) )
									: $.trim( str.substring( pos.index ) );
								
								alt = ESectionParser.alternateTitle( sub.title );
								repl = new RegExp( '\\{\\{(' + sub.title + alt + ')\\}\\}' );
								changed = sub.content.replace( repl, '' );

								if ( changed !== sub.content ) {
									firstbreak = changed.search( /\n/ );
									
									if (
										firstbreak !== -1 &&
										firstbreak < changed.search( /\S/ )
									) {
										sub.initmultiline = true;
									}
									
									sub.content = $.trim( changed );
								} else if (
									sub.content !== '' ||
									sub.title === ''
								) {
									sub.shortened = false;
								}
								
								if (
									targetSubsections.indexOf( pos.title ) === -1 &&
									sub.content === '' &&
									sub.title !== ''
								) {
									sub.active = false;
								} else {
									sub.initcontent = sub.content;
								}
								
								break;
							} else if ( targetSubsections.indexOf( pos.title ) === -1 ) {
								sub.active = false;
							}
						}
					}
				}
			}
		}
	},

	obligatorySubsection: function ( subsection, section ) {
		return (
			subsection.title === 'znaczenia' &&
			section.mode !== 'CHINESE_SIGN'
		);
	},

	botSubsection: function ( subsection, section ) {
		return (
			subsection.title === 'wymowa' &&
			section.mode === 'POLISH' &&
			!subsection.content
		);
	}
};