<commit_before>var postForm = null;
var liveFeed = true;
var threads = {};
var dispatcher = {};
var THREAD = window.location.pathname.match(/\/(\d+)$/);
THREAD = THREAD ? parseInt(THREAD[1]) : 0;
var nameField = $('input[name=name]'), emailField = $('input[name=email]');
var ceiling = $('hr:first');

var socket = new io.Socket(window.location.domain, {
	port: PORT,
	transports: ['websocket', 'htmlfile', 'xhr-multipart', 'xhr-polling',
		'jsonp-polling']
});

if (!THREAD) {
	$('#sync').after($('<span id="live"><label for="live_check">' +
		'Real-time bump</label><input type="checkbox" ' +
		'id="live_check" checked /></span>'));
	$('#live_check').change(function () {
		liveFeed = $(this).attr('checked');
		if (liveFeed) {
			$('section').show();
			$('hr').show();
		}
	});
}

function send(msg) {
	socket.send(JSON.stringify(msg));
}

function make_reply_box() {
	var box = $('<aside><a>[Reply]</a></aside>');
	box.find('a').click(on_make_post);
	return box;
}

function insert_new_post_boxes() {
	make_reply_box().appendTo('section');
	if (!THREAD) {
		var box = $('<aside><a>[New thread]</a></aside>');
		box.find('a').click(on_make_post);
		ceiling.after(box);
	}
}

function on_make_post() {
	var link = $(this);
	postForm = new PostForm(link.parent(), link.parents('section'));
}

function make_link(num, op) {
	var p = {num: num, op: op};
	return safe('<a href="' + post_url(p, false) + '">&gt;&gt;'
			+ num + '</a>');
}

function format_link(num, env) {
	if (env.links && num in env.links)
		env.callback(make_link(num, env.links[num]));
	else
		env.callback('>>' + num);
}

function insert_formatted(text, buffer, state, env) {
	if (!env.format_link)
		env.format_link = format_link;
	env.callback = function (frag) {
		var dest = buffer;
		for (var i = 0; i < state[1]; i++)
			dest = dest.children('del:last');
		if (state[0] == 1)
			dest = dest.children('em:last');
		var out = null;
		if (frag.safe) {
			var m = frag.safe.match(/^<(\w+)>$/);
			if (m)
				out = document.createElement(m[1]);
			else if (frag.safe.match(/^<\/\w+>$/))
				out = '';
		}
		if (out === null)
			out = escape_fragment(frag);
		if (out)
			dest.append(out);
	};
	format_fragment(text, state, env);
}

function get_focus() {
	var focus = window.getSelection().focusNode;
	if (focus && focus.tagName && focus.tagName.match(/blockquote/i))
		return $(focus).find('textarea');
}

dispatcher[INSERT_POST] = function (msg) {
	msg = msg[0];
	if (postForm && msg.num == postForm.num)
		return true;
	var orig_focus = get_focus();
	msg.format_link = format_link;
	msg.dirs = DIRS;
	msg.image_view = function (img, imgnm, op) { return img; };
	var section, hr, bump = true;
	if (msg.op) {
		var post = $(gen_post_html(msg, msg));
		section = threads[msg.op];
		section.children('blockquote,form,article[id]:last'
				).last().after(post);
		if (THREAD || !liveFeed || msg.email == 'sage') {
			bump = false;
		}
		else {
			hr = section.next();
			section.detach();
			hr.detach();
		}
	}
	else {
		section = $(gen_thread(msg, msg).join(''));
		threads[msg.num] = section;
		hr = $('<hr/>');
		if (!postForm)
			section.append(make_reply_box());
		if (!liveFeed) {
			section.hide();
			hr.hide();
		}
	}
	if (bump) {
		var fencepost = $('body > aside');
		section.insertAfter(fencepost.length ? fencepost : ceiling
				).after(hr);
	}
	if (orig_focus)
		orig_focus.focus();
	return true;
};

dispatcher[IMAGE_STATUS] = function (msg) {<commit_msg>Clean up client post insertion a little<commit_after>var postForm = null;
var liveFeed = true;
var threads = {};
var dispatcher = {};
var THREAD = window.location.pathname.match(/\/(\d+)$/);
THREAD = THREAD ? parseInt(THREAD[1]) : 0;
var nameField = $('input[name=name]'), emailField = $('input[name=email]');
var ceiling = $('hr:first');

var socket = new io.Socket(window.location.domain, {
	port: PORT,
	transports: ['websocket', 'htmlfile', 'xhr-multipart', 'xhr-polling',
		'jsonp-polling']
});

if (!THREAD) {
	$('#sync').after($('<span id="live"><label for="live_check">' +
		'Real-time bump</label><input type="checkbox" ' +
		'id="live_check" checked /></span>'));
	$('#live_check').change(function () {
		liveFeed = $(this).attr('checked');
		if (liveFeed) {
			$('section').show();
			$('hr').show();
		}
	});
}

function send(msg) {
	socket.send(JSON.stringify(msg));
}

function make_reply_box() {
	var box = $('<aside><a>[Reply]</a></aside>');
	box.find('a').click(on_make_post);
	return box;
}

function insert_new_post_boxes() {
	make_reply_box().appendTo('section');
	if (!THREAD) {
		var box = $('<aside><a>[New thread]</a></aside>');
		box.find('a').click(on_make_post);
		ceiling.after(box);
	}
}

function on_make_post() {
	var link = $(this);
	postForm = new PostForm(link.parent(), link.parents('section'));
}

function make_link(num, op) {
	var p = {num: num, op: op};
	return safe('<a href="' + post_url(p, false) + '">&gt;&gt;'
			+ num + '</a>');
}

function format_link(num, env) {
	if (env.links && num in env.links)
		env.callback(make_link(num, env.links[num]));
	else
		env.callback('>>' + num);
}

function insert_formatted(text, buffer, state, env) {
	if (!env.format_link)
		env.format_link = format_link;
	env.callback = function (frag) {
		var dest = buffer;
		for (var i = 0; i < state[1]; i++)
			dest = dest.children('del:last');
		if (state[0] == 1)
			dest = dest.children('em:last');
		var out = null;
		if (frag.safe) {
			var m = frag.safe.match(/^<(\w+)>$/);
			if (m)
				out = document.createElement(m[1]);
			else if (frag.safe.match(/^<\/\w+>$/))
				out = '';
		}
		if (out === null)
			out = escape_fragment(frag);
		if (out)
			dest.append(out);
	};
	format_fragment(text, state, env);
}

function get_focus() {
	var focus = window.getSelection().focusNode;
	if (focus && focus.tagName && focus.tagName.match(/blockquote/i))
		return $(focus).find('textarea');
}

dispatcher[INSERT_POST] = function (msg) {
	msg = msg[0];
	if (postForm && msg.num == postForm.num)
		return true;
	var orig_focus = get_focus();
	var env = {format_link: format_link, dirs: DIRS, links: msg.links,
			image_view: function (img, imgnm, op) { return img; }};
	var section, hr, bump = true;
	if (msg.op) {
		var post = $(gen_post_html(msg, env));
		section = threads[msg.op];
		section.children('blockquote,form,article[id]:last'
				).last().after(post);
		if (THREAD || !liveFeed || msg.email == 'sage') {
			bump = false;
		}
		else {
			hr = section.next();
			section.detach();
			hr.detach();
		}
	}
	else {
		section = $(gen_thread(msg, env).join(''));
		threads[msg.num] = section;
		hr = $('<hr/>');
		if (!postForm)
			section.append(make_reply_box());
		if (!liveFeed) {
			section.hide();
			hr.hide();
		}
	}
	if (bump) {
		var fencepost = $('body > aside');
		section.insertAfter(fencepost.length ? fencepost : ceiling
				).after(hr);
	}
	if (orig_focus)
		orig_focus.focus();
	return true;
};

dispatcher[IMAGE_STATUS] = function (msg) {
{'fraction_non_alphanumeric': 0.213893703061814, 'fraction_numerical': 0.002310803004043905, 'mean_word_length': 4.09941089837997, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7272884', 'n_tokens_mistral': 2805, 'n_tokens_neox': 2599, 'n_words': 546}