<commit_before>var postDivClass = "md";
var cardRegex = /\[\[(.+?)\]\]/ig;
var baseURL = 'http://www.hearthhead.com/card=';
var HS_CardObj = loadCardData();

(function()
{
	var postDivs = document.getElementsByClassName(postDivClass);
	var paragraphs = [];
	for (var i = postDivs.length - 1; i >= 0; i--) {
		Array.prototype.push.apply(paragraphs, postDivs[i].getElementsByTagName("p"));
	};

	for (var i = paragraphs.length - 1; i >= 0; i--) {
		var paragraph = paragraphs[i];
		var textNodes = findTextNodes(paragraph);
        textNodes.forEach(function(element, index, array) {
        		processTextNode(element, cardRegex, createCardLink);
        	});
	};

	// add HearthHead tooltips after we've linkified all the card names
	addTooltips();
}());

function findTextNodes(element)
{
	var child, tag;
	var nodes = [];

	for (var i = element.childNodes.length - 1; i >= 0; i--) {
		child = element.childNodes[i];
		if (child.nodeType === 3) // text node
		{
			nodes.push(child);
		}
	}

	return nodes;
}

function processTextNode(node, regex, replaceFn)
{
	var match, 
		matches = [];
	
	while (match = regex.exec(node.data)) {
		matches.push(match);
	}

	// process matches in reverse to avoid invalidating match indexes when splitting nodes
	for (var i = matches.length - 1; i >= 0; i--) {
		var match = matches[i];
		var newNode = replaceFn(match[1]);
		
		if (newNode == null) continue;

		// isolate matched text in its own node, replace that node with newNode
		node.splitText(match.index);
        node.nextSibling.splitText(match[0].length);
        node.parentNode.replaceChild(newNode, node.nextSibling);
	};
}

function createCardLink(cardName)
{
	// find card object with name of card text
	var card = HS_CardObj[cardName.toLowerCase()];
	if (card == null) return;

	// construct HearthHead link
	var link = document.createElement('a');
	link.href = baseURL + card.whid;
	link.target = '_blank';
	link.innerHTML = cardName;

	return link;	
}

function addTooltips()
{
	var whScriptElement = document.createElement('script');
	whScriptElement.type = 'text/javascript';
	whScriptElement.src = 'https://static.wowhead.com/widgets/power.js';
	document.getElementsByTagName('head')[0].appendChild(whScriptElement);

	var whVarsElement = document.createElement('script');
	var whVarsText = document.createTextNode('var wowhead_tooltips = { "colorlinks": true, "iconizelinks": true, "renamelinks": false }');
	whVarsElement.appendChild(whVarsText);
	document.getElementsByTagName('head')[0].appendChild(whVarsElement);
}

function loadCardData()
{
	return {
		"abomination": {
			"name": "abomination",
			"whid": 440
		},
		"abusive sergeant": {
			"name": "<NAME>",
			"whid": 242<commit_msg>More descriptive variable and function names.<commit_after>var postDivClass = "md";
var cardRegex = /\[\[(.+?)\]\]/ig;
var hheadCardUrl = 'http://www.hearthhead.com/card=';
var cardDict = loadCardData();

(function()
{
	var postDivs = document.getElementsByClassName(postDivClass);
	var paragraphs = [];
	for (var i = postDivs.length - 1; i >= 0; i--) {
		Array.prototype.push.apply(paragraphs, postDivs[i].getElementsByTagName("p"));
	};

	for (var i = paragraphs.length - 1; i >= 0; i--) {
		var paragraph = paragraphs[i];
		var textNodes = findTextNodes(paragraph);
        textNodes.forEach(function(element, index, array) {
        		linkifyTextNode(element, cardRegex, createCardLink);
        	});
	};

	// add HearthHead tooltips after we've linkified all the card names
	addTooltips();
}());

function findTextNodes(element)
{
	var child, tag;
	var nodes = [];

	for (var i = element.childNodes.length - 1; i >= 0; i--) {
		child = element.childNodes[i];
		if (child.nodeType === 3) // text node
		{
			nodes.push(child);
		}
	}

	return nodes;
}

function linkifyTextNode(node, regex, replaceFn)
{
	var match, 
		matches = [];
	
	while (match = regex.exec(node.data)) {
		matches.push(match);
	}

	// process matches in reverse to avoid invalidating match indexes when splitting nodes
	for (var i = matches.length - 1; i >= 0; i--) {
		var match = matches[i];
		var newNode = replaceFn(match[1]);
		
		if (newNode == null) continue;

		// isolate matched text in its own node, replace that node with newNode
		node.splitText(match.index);
        node.nextSibling.splitText(match[0].length);
        node.parentNode.replaceChild(newNode, node.nextSibling);
	};
}

function createCardLink(cardName)
{
	// find card object with name of card text
	var card = cardDict[cardName.toLowerCase()];
	if (card == null) return;

	// construct HearthHead link
	var link = document.createElement('a');
	link.href = hheadCardUrl + card.whid;
	link.target = '_blank';
	link.innerHTML = cardName;

	return link;	
}

function addTooltips()
{
	var whScriptElement = document.createElement('script');
	whScriptElement.type = 'text/javascript';
	whScriptElement.src = 'https://static.wowhead.com/widgets/power.js';
	document.getElementsByTagName('head')[0].appendChild(whScriptElement);

	var whVarsElement = document.createElement('script');
	var whVarsText = document.createTextNode('var wowhead_tooltips = { "colorlinks": true, "iconizelinks": true, "renamelinks": false }');
	whVarsElement.appendChild(whVarsText);
	document.getElementsByTagName('head')[0].appendChild(whVarsElement);
}

function loadCardData()
{
	return {
		"abomination": {
			"name": "abomination",
			"whid": 440
		},
		"abusive sergeant": {
			"name": "abusive sergeant",
			"whid": 242
{'fraction_non_alphanumeric': 0.1497434017595308, 'fraction_numerical': 0.006964809384164223, 'mean_word_length': 4.128759398496241, 'pattern_counts': {'":': 18, '<': 4, '<?xml version=': 0, '>': 12, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2102561', 'n_tokens_mistral': 1930, 'n_tokens_neox': 1803, 'n_words': 429}