User:Ungoliant MMDCCLXIV/cathoister.js

var langcode_index = []; var langname_index = []; var langname_to_langcode = []; var all_langnames = []; var langname_regex = '';

var cur_langcode = ''; var cur_langname = ''; var has_cat_block = false;

function add_cat_block(element_after) { if (has_cat_block === true || cur_langname.length === 0 || cur_langcode.length === 0) return;

var new_block = $(' ') .attr('id', cur_langcode + '-catlinks') .addClass('catlinks') .attr('data-mw', 'interface') .append(			$(' ')				.attr('id', cur_langcode + '-mw-normal-catlinks')				.hide				.addClass('mw-normal-catlinks')				.append( $(' ')						.addClass('category-link-container') .html('Categories:') .css('font-weight', 'bold') )		)		.append(			$(' ')				.attr('id', cur_langcode + '-mw-topic-catlinks')				.hide				.addClass('mw-topic-catlinks')				.append( $(' ')						.addClass('category-link-container') .html('Topics:') .css('font-weight', 'bold') )		)		.append(			$(' ')				.attr('id', cur_langcode + '-mw-hidden-catlinks')				.hide				.addClass('mw-hidden-catlinks')				.append( $(' ')						.addClass('category-link-container') .html('Hidden categories:') .css('font-weight', 'bold') )		);

if (element_after === null) { new_block = new_block.insertAfter($('#mw-content-text .mw-parser-output').children(':last')); } else { new_block = new_block.insertBefore(element_after); }

langcode_index[cur_langcode] = new_block; langname_index[cur_langname] = new_block; langname_to_langcode[cur_langname] = cur_langcode; all_langnames.push(cur_langname); cur_langcode = ''; cur_langname = ''; }

function create_cat_link(container, catname, href, langcode, catname_without_lang, red) {

container.show;

$(container).append(		$(' ')			.addClass('category-link-container')			.addClass(langcode + '-category')			.addClass(catname_without_lang.trim.replace(/\s/g, '-').toLowerCase + '-category')			.append( $('') .addClass('category-link') .addClass(red? 'new': '') .attr('href', href) .html(catname) )	); }

function loop_through_cats(hidden_cats) { var cat; var match; var normal_selector = '.mw-normal-catlinks'; var topics_selector = '.mw-topic-catlinks'; if (hidden_cats === true) { normal_selector = '.mw-hidden-catlinks'; topics_selector = '.mw-hidden-catlinks'; }	$(hidden_cats? '#mw-hidden-catlinks li a' : '#mw-normal-catlinks li a').each(function(index, catlink) {	   cat = $(catlink).html;

match = cat.match(/^([a-z|\u002D]+):(.+)/);

if (match !== null) { create_cat_link(	   		$(langcode_index[match[1]].children(topics_selector)),	    		cat,	    		$(catlink).attr('href'),	    		match[1],	    		match[2],	    		$(catlink).hasClass('new')	    	); $(catlink).parent.hide;

} else { match = cat.match(langname_regex);

if (match !== null) { create_cat_link(		   		$(langname_index[match[1]].children(normal_selector)),		    		cat,		    		$(catlink).attr('href'),		    		langname_to_langcode[match[1]],		    		cat.replace(match[1], ''),	    			$(catlink).hasClass('new')		    	); $(catlink).parent.hide; } else {

console.log('Category language not identified: ' + cat); }

}

});

}

$.when($.ready, mw.loader.using('mediawiki.cookie')).done(function{   if (mw.config.get('wgNamespaceNumber') === 0) {	$('#mw-content-text .mw-parser-output').children('h2, p, hr').each(function(index, val) { switch (val.tagName) { case 'HR': add_cat_block(val); has_cat_block = true; break; case 'H2': add_cat_block(val); has_cat_block = false; cur_langname = $(val).children('.mw-headline').html; cur_langcode = ''; break; case 'P': cur_langcode = $(val).children('.headword').attr('lang') || cur_langcode; }	});	add_cat_block(null);	all_langnames.sort(function (a, b) { return a.length - b.length; });	var langname_regex_str = '('; for (var c1 = 0; c1 < all_langnames.length; c1++) { if (c1 !== 0) { langname_regex_str += '|'; }		langname_regex_str += '^' + all_langnames[c1] + '|' + all_langnames[c1] + '$'; }	langname_regex_str += ')';	langname_regex = new RegExp(langname_regex_str);	loop_through_cats(false);	loop_through_cats(true);	} });