User:Connel MacKenzie/mess-with-popups.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

Previewmaker.prototype.firstBit = function() {
	try{
		if (mw.config.get("wgNamespaceNumber") !== 0){
			// dont't be givin' me no subsequent paragraphs, you hear me?
			/// first we "normalize" section headings, removing whitespace after, adding before
			var d = this.data;

			if (getValueOf('popupPreviewCutHeadings')) {
				this.data = this.data.replace(RegExp('\\s*(==+[^=]*==+)\\s*', 'g'), '\n\n$1 ');
				/// then we want to get rid of paragraph breaks whose text ends badly
				this.data = this.data.replace(RegExp('([:;]) *\\n{2,}', 'g'), '$1\n');

				this.data = this.data.replace(RegExp('^[\\s\\n]*'), '');
				stuff = (RegExp('^([^\\n]|\\n[^\\n\\s])*')).exec(this.data);
				if (stuff) {
					d = stuff[0];
				}
				if (!getValueOf('popupPreviewFirstParOnly')) {
					d = this.data;
				}

				/// now put \n\n after sections so that bullets and numbered lists work
				d = d.replace(RegExp('(==+[^=]*==+)\\s*', 'g'), '$1\n\n');
			}

			// superfluous sentences are RIGHT OUT.
			// note: exactly 1 set of parens here needed to make the slice work
			d = d.parenSplit(RegExp('([!?.]+["' + "'" + ']*\\s)', 'g'));
			// leading space is bad, mmkay?
			d[0] = d[0].replace(RegExp('^\\s*'), '');

			var notSentenceEnds = RegExp('([^.][a-z][.][a-z]|etc|sic|Dr|Mr|Mrs|Ms|St|no|\\[[^\\]]*|\\s[A-Zvclm])$', 'i');

			d = this.fixSentenceEnds(d, notSentenceEnds);

			var maxChars=getValueOf('popupMaxPreviewCharacters');
			var n = getValueOf('popupMaxPreviewSentences');
			var dd;

			do {
				dd = this.firstSentences(d, n);
				--n;
			}
			while (dd.length > maxChars && n > 0);

			this.data = dd;

		} else {

			//TODO: Limit this override to urls that start with http://*.wiktionary.org/
			//TODO: extend the above section to also preview Wikipedia links

			var charsWikt = 0;

			var linesWikt = this.data.split('\n');
			for (jWikt = 0; jWikt < linesWikt.length; jWikt++) {
				linesWikt[jWikt] = linesWikt[jWikt].replace(/\{\{[in]transitive}}/g, '');
				linesWikt[jWikt] = linesWikt[jWikt].replace(/\{\{[un]countable}}/g, '');
				linesWikt[jWikt] = linesWikt[jWikt].replace(/\{\{cattag\|(.*?)}}/g, "");
				linesWikt[jWikt] = linesWikt[jWikt].replace(/[\{\}]/g, "'");
				if (linesWikt[jWikt].search(/^[#]/) == -1) {
					if (linesWikt[jWikt].search(/^==[A-Z]/) == -1) {
						linesWikt[jWikt] = '';
					}
				}
				charsWikt += linesWikt[jWikt].length;
			}
			if (charsWikt < 2) {
				linesWikt[0] = "No properly formatted content";
			}

			var maxChars = getValueOf('popupMaxPreviewCharacters');
			var n = getValueOf('popupMaxPreviewSentences');
			var dd = '';

			for (jWikt = 0; jWikt < linesWikt.length; jWikt++) {
				if (linesWikt[jWikt] !== '') {
					dd += linesWikt[jWikt] + '\n';
				}
				if (dd.length < maxChars) {
					this.data = dd;
				}
			}
		}
	}
	catch (e) {
		console.log("error from mess with popups: ", e);
	}
};