منځپانگې ته ورتلل

ميډياويکي:Gadget-Global-WikitextParser.js

د AfghaniPedia|افغانيپېډيا لخوا

يادښت: د غوره توبونو د خوندي کولو وروسته، خپل د کتنمل (بروزر) ساتل شوې حافظه تازه کړی.

  • فايرفاکس/ سفري: په دې کتنمل کې د Reload د ټکوهلو په وخت د Shift تڼۍ نيولې وساتی، او يا هم Ctrl-F5 يا Ctrl-Rتڼۍ کېښکاږۍ (په Apple Mac کمپيوټر باندې ⌘-R کېښکاږۍ)
  • گووگل کروم: په دې کتنمل کې د Ctrl-Shift-R تڼۍ کېښکاږۍ (د مک لپاره ⌘-Shift-R)
  • انټرنټ اېکسپلورر: په دې کتنمل کې د Refresh د ټکوهلو په وخت کې د Ctrl تڼۍ کېښکاږلې ونيسۍ، او يا هم د Ctrl-F5 تڼۍ کېښکاږۍ
  • اوپرا: په دې کتنمل کې د خپل براوزر ساتل شوې حافظه پدې توگه سپينولی شی Tools→Preferences
/**
 * WikitextParser.js is a set of methods for parsing wikitext
 * Documentation: https://www.mediawiki.org/wiki/WikitextParser.js
 * License: GNU General Public License 3 or later (http://www.gnu.org/licenses/gpl-3.0.html)
 */
// <nowiki>
window.WikitextParser = {

	replacedElements: new Map(),

	replaceElements: function ( wikitext, elements ) {
		for ( const element of elements ) {
			if ( wikitext.includes( element ) ) {
				const index = this.replacedElements.size;
				wikitext = wikitext.replace( element, '@@@' + index + '@@@' );
				this.replacedElements.set( index, element );
			}
		}
		return wikitext;
	},

	restoreElements: function ( wikitext ) {
		const matches = wikitext.matchAll( /@@@(\d+)@@@/g );
		for ( const match of matches ) {
			const placeholder = match[0];
			const index = Number( match[1] );
			const element = this.replacedElements.get( index );
			wikitext = wikitext.replace( placeholder, element );
			this.replacedElements.delete( index );
		}
		return wikitext;
	},

	getElements: function ( wikitext, prefix, suffix ) {
		const elements = [];
		let start = wikitext.indexOf( prefix );
		while ( start > -1 ) {
			let depth = 0;
			let position;
			for ( position = start; position < wikitext.length; position++ ) {
				if ( wikitext.substr( position, prefix.length ) === prefix ) {
					position += prefix.length - 1;
					depth++;
				}
				if ( wikitext.substr( position, suffix.length ) === suffix ) {
					position += suffix.length - 1;
					depth--;
				}
				if ( !depth ) {
					break;
				}
			}
			const end = position - start + 1;
			const element = wikitext.substr( start, end );
			elements.push( element );
			start = wikitext.indexOf( prefix, start + 1 );
		}
		return elements;
	},

	getTemplates: function ( wikitext ) {
		return this.getElements( wikitext, '{{', '}}' );
	},

	getTables: function ( wikitext ) {
		return this.getElements( wikitext, '{|', '|}' );
	},

	getComments: function ( wikitext ) {
		return this.getElements( wikitext, '<!--', '-->' );
	},

	getLinks: function ( wikitext ) {
		return this.getElements( wikitext, '[[', ']]' );
	},

	getLinkTitle: function ( wikitext ) {
		wikitext = wikitext.trim();
		wikitext = wikitext.replace( '[[', '' );
		wikitext = wikitext.replace( /]]$/, '' );
		const parts = wikitext.split( '|' );
		const first = parts.shift();
		const title = first.trim();
		return title;
	},

	getFiles: function ( wikitext, namespace ) {
		const files = [];
		const links = this.getLinks( wikitext );
		for ( const link of links ) {
			const title = this.getLinkTitle( link );
			if ( title.toLowerCase().startsWith( 'file:' ) ) {
				files.push( link );
			} else if ( namespace && title.toLowerCase().startsWith( namespace.toLowerCase() + ':' ) ) {
				files.push( link );
			}
		}
		return files;
	},

	getFileName: function ( wikitext ) {
		const title = this.getLinkTitle( wikitext );
		const parts = title.split( ':' );
		const name = parts[1];
		return name;
	},

	getFileExtension: function ( wikitext ) {
		const name = this.getFileName( wikitext );
		const extension = name.split( '.' ).pop();
		return extension;
	},

	getFileParameters: function ( wikitext ) {
		// Remove the outer square braces
		wikitext = wikitext.trim();
		wikitext = wikitext.replace( '[[', '' );
		wikitext = wikitext.replace( /]]$/, '' );

		// Temporarily replace every subelement that may contain pipes to prevent absolute chaos
		const templates = this.getTemplates( wikitext );
		const links = this.getLinks( wikitext );
		const tables = this.getTables( wikitext );
		const elements = [ ...templates, ...links, ...tables ];
		wikitext = this.replaceElements( wikitext, elements );

		// Parse the template params
		const parts = wikitext.split( '|' );
		parts.shift(); // The first part is the file name, so remove it
		const params = {};
		let anonymousParams = 0;
		for ( const part of parts ) {
			const pair = part.split( '=' );
			let param = pair[0];
			let value = pair[1];
			if ( value === undefined ) {
				value = param;
				anonymousParams++;
				param = anonymousParams.toString();
			}
			param = param.trim();
			value = value.trim();
			value = this.restoreElements( value );
			params[ param ] = value;
		}

		return params;
	},

	getTemplateName: function ( wikitext ) {
		wikitext = wikitext.trim();
		wikitext = wikitext.replace( '{{', '' );
		wikitext = wikitext.replace( /}}$/, '' );
		const parts = wikitext.split( '|' );
		const first = parts.shift();
		const name = first.trim();
		return name;
	},

	getTemplateNames: function ( wikitext ) {
		let names = [];
		const templates = this.getTemplates( wikitext );
		for ( const template of templates ) {
			const name = this.getTemplateName( template );
			names.push( name );
		}
		return names;
	},

	getTemplateParameters: function ( wikitext ) {
		// Remove the outer curly braces
		wikitext = wikitext.trim();
		wikitext = wikitext.replace( '{{', '' );
		wikitext = wikitext.replace( /}}$/, '' );

		// Temporarily replace every subelement that may contain pipes to prevent absolute chaos
		const templates = this.getTemplates( wikitext );
		const links = this.getLinks( wikitext );
		const tables = this.getTables( wikitext );
		const elements = [ ...templates, ...links, ...tables ];
		wikitext = this.replaceElements( wikitext, elements );

		// Parse the template params
		const parts = wikitext.split( '|' );
		parts.shift(); // The first part is the template name, so remove it
		const params = {};
		let anonymousParams = 0;
		for ( const part of parts ) {
			const pair = part.split( '=' );
			let param = pair[0];
			let value = pair[1];
			if ( value === undefined ) {
				value = param;
				anonymousParams++;
				param = anonymousParams.toString();
			}
			param = param.trim();
			value = value.trim();
			value = this.restoreElements( value );
			params[ param ] = value;
		}

		return params;
	},

	/**
	 * Get the wikitext of the first template with the given name
 	 */
	getTemplate: function ( wikitext, templateName ) {
		const templates = this.getTemplates( wikitext );
		for ( const template of templates ) {
			const name = this.getTemplateName( template );
			if ( name === templateName ) {
				return template;
			}
		}
	}
};
// </nowiki>