<commit_before><?php

/**
 * Displays JSON preview as a table.
 *
 * @author <NAME>
 * @copyright 2014 Pematon, s.r.o. (http://www.pematon.com/)
 */
class AdminerJsonPreview
{
	const MAX_TEXT_LENGTH = 100;

	protected $maxLevel;

	protected $inTable;

	protected $inEdit;

	/**
	 * @param int Max. level in recursion. 0 means no limit.
	 * @param bool Whether apply JSON preview in selection table.
	 * @param bool Whether apply JSON preview in edit form.
	 */
	function AdminerJsonPreview($maxLevel = 0, $inTable = TRUE, $inEdit = TRUE)
	{
		$this->maxLevel = $maxLevel;
		$this->inTable = $inTable;
		$this->inEdit = $inEdit;
	}

	function head()
	{

		?>

		<style>
			/* Table */
			.json {
				width: auto;
				border-collapse: collapse;
				border-spacing: 0;
				margin: 4px 0;
				border: 1px solid #999;
			}

			.json tr {
				border-bottom: 1px solid #999;
			}

			.json tr:last-child {
				border-bottom: none;
			}

			.checkable .json .checked th, .checkable .json .checked td {
				background: transparent;
			}

			.json th {
				padding: 0;
				width: 1px;
				border-right: 1px solid #999;
				border-bottom: none;
			}

			.json td {
				padding: 0;
				border: 0;
			}

			.json code {
				display: block;
				background: transparent;
				padding: 2px 3px;
				white-space: normal;
			}

			.json .json {
				width: 100%;
				border: none;
				margin: 0;
			}

			/* Togglers */
			a.json-icon {
				display: inline-block;
				padding: 0;
				overflow: hidden;
				background-image: url("index.php?file=down.gif");
				background-position: center center;
				background-repeat: no-repeat;
				text-indent: -50px;
				vertical-align: middle;
			}

			a.json-link {
				width: auto;
				padding-left: 18px;
				background-position: left center;
				text-indent: 0;
			}

			a.json-link span {
				color: #fff;
				padding: 0 5px;
			}

			a.json-icon.json-up {
				background-image: url("index.php?file=up.gif");
			}

			/* No javascript support */
			.nojs .json-icon, .nojs .json-link {
				display: none;
			}

			.nojs .json {
				display: table !important;
			}
		</style>

		<script>
			function toggleJson(button, counter) {
				var obj = document.getElementById("json-code-" + counter);
				if (!obj)
					return;

				if (obj.style.display === "none") {
					button.className += " json-up";
					obj.style.display = "";
				} else {
					button.className = button.className.replace(" json-up", "");
					obj.style.display = "none";
				}
			}
		</script>

		<?php

	}

	function selectVal(&$val, $link, $field, $original)
	{
		static $counter = 1;

		if (!$this->inTable)
			return;

		if (is_string($original) && substr($original, 0, 1) == '{' && ($json = json_decode($original, TRUE))) {
			$val = "<a class='icon json-icon' href='#' onclick='toggleJson(this, $counter);return false;' title='JSON'>JSON</a> " . $val;
			$val .= $this->convertJson($json, 1, $counter++);
		}
	}

	function editInput($table, $field, $attrs, $value)
	{
		static $counter = 1;

		if (!$this->inEdit)
			return;

		if (is_string($value) && substr($value, 0, 1) == '{' && ($json = json_decode($value, TRUE))) {
			echo "<a class='icon json-icon json-link' href='#' onclick='toggleJson(this, $counter);return false;' title='JSON'><span>JSON</span></a><br/>";
			echo $this->convertJson($json, 1, $counter);
		}
	}

	function convertJson($json, $level = 1, $id = 0)
	{
		$value = "";

		$value .= "<table class='json'";
		if ($level === 1 && $id > 0) {
			$value .= "style='display: none' id='json-code-$id'";
		}
		$value .= ">";

		foreach ($json as $key => $val) {
			$value .= "<tr><th><code>" . h($key) . "</code>";
			$value .= "<td>";

			if (is_array($val) && ($this->maxLevel <= 0 || $level < $this->maxLevel)) {
				$value .= $this->convertJson($val, $level + 1);
			} elseif (is_array($val)) {
				$value .= "<code class='jush-js'>" . h(preg_replace('/([,:])([^\s])/', '$1 $2', json_encode($val))) . "</code>";
			} elseif (is_string($val)) {
				if (mb_strlen($val, "UTF-8") > self::MAX_TEXT_LENGTH)
					$val = mb_substr($val, 0, self::MAX_TEXT_LENGTH - 3, "UTF-8") . "...";

				$value .= "<code>" . nl2br(h($val)) . "</code>";
			} elseif (is_bool($val)) {
				$value .= "<code class='jush'>" . h($val ? "true" : "false") . "</code>";
			} elseif (is_null($val)) {
				$value .= "<code class='jush'>null</code>";
			} else {
				$value .= "<code class='jush'>" . h($val) . "</code>";
			}
		}

		$value .= "</table>";

		return $value;
	}
}<commit_msg>New line at the end of JSON string is visible now.<commit_after><?php

/**
 * Displays JSON preview as a table.
 *
 * @author <NAME>
 * @copyright 2014 Pematon, s.r.o. (http://www.pematon.com/)
 */
class AdminerJsonPreview
{
	const MAX_TEXT_LENGTH = 100;

	protected $maxLevel;

	protected $inTable;

	protected $inEdit;

	/**
	 * @param int Max. level in recursion. 0 means no limit.
	 * @param bool Whether apply JSON preview in selection table.
	 * @param bool Whether apply JSON preview in edit form.
	 */
	function AdminerJsonPreview($maxLevel = 0, $inTable = TRUE, $inEdit = TRUE)
	{
		$this->maxLevel = $maxLevel;
		$this->inTable = $inTable;
		$this->inEdit = $inEdit;
	}

	function head()
	{

		?>

		<style>
			/* Table */
			.json {
				width: auto;
				border-collapse: collapse;
				border-spacing: 0;
				margin: 4px 0;
				border: 1px solid #999;
			}

			.json tr {
				border-bottom: 1px solid #999;
			}

			.json tr:last-child {
				border-bottom: none;
			}

			.checkable .json .checked th, .checkable .json .checked td {
				background: transparent;
			}

			.json th {
				padding: 0;
				width: 1px;
				border-right: 1px solid #999;
				border-bottom: none;
			}

			.json td {
				padding: 0;
				border: 0;
			}

			.json code {
				display: block;
				background: transparent;
				padding: 2px 3px;
				white-space: normal;
			}

			.json .json {
				width: 100%;
				border: none;
				margin: 0;
			}

			/* Togglers */
			a.json-icon {
				display: inline-block;
				padding: 0;
				overflow: hidden;
				background-image: url("index.php?file=down.gif");
				background-position: center center;
				background-repeat: no-repeat;
				text-indent: -50px;
				vertical-align: middle;
			}

			a.json-link {
				width: auto;
				padding-left: 18px;
				background-position: left center;
				text-indent: 0;
			}

			a.json-link span {
				color: #fff;
				padding: 0 5px;
			}

			a.json-icon.json-up {
				background-image: url("index.php?file=up.gif");
			}

			/* No javascript support */
			.nojs .json-icon, .nojs .json-link {
				display: none;
			}

			.nojs .json {
				display: table !important;
			}
		</style>

		<script>
			function toggleJson(button, counter) {
				var obj = document.getElementById("json-code-" + counter);
				if (!obj)
					return;

				if (obj.style.display === "none") {
					button.className += " json-up";
					obj.style.display = "";
				} else {
					button.className = button.className.replace(" json-up", "");
					obj.style.display = "none";
				}
			}
		</script>

		<?php

	}

	function selectVal(&$val, $link, $field, $original)
	{
		static $counter = 1;

		if (!$this->inTable)
			return;

		if (is_string($original) && substr($original, 0, 1) == '{' && ($json = json_decode($original, TRUE))) {
			$val = "<a class='icon json-icon' href='#' onclick='toggleJson(this, $counter);return false;' title='JSON'>JSON</a> " . $val;
			$val .= $this->convertJson($json, 1, $counter++);
		}
	}

	function editInput($table, $field, $attrs, $value)
	{
		static $counter = 1;

		if (!$this->inEdit)
			return;

		if (is_string($value) && substr($value, 0, 1) == '{' && ($json = json_decode($value, TRUE))) {
			echo "<a class='icon json-icon json-link' href='#' onclick='toggleJson(this, $counter);return false;' title='JSON'><span>JSON</span></a><br/>";
			echo $this->convertJson($json, 1, $counter);
		}
	}

	function convertJson($json, $level = 1, $id = 0)
	{
		$value = "";

		$value .= "<table class='json'";
		if ($level === 1 && $id > 0) {
			$value .= "style='display: none' id='json-code-$id'";
		}
		$value .= ">";

		foreach ($json as $key => $val) {
			$value .= "<tr><th><code>" . h($key) . "</code>";
			$value .= "<td>";

			if (is_array($val) && ($this->maxLevel <= 0 || $level < $this->maxLevel)) {
				$value .= $this->convertJson($val, $level + 1);
			} elseif (is_array($val)) {
				$value .= "<code class='jush-js'>" . h(preg_replace('/([,:])([^\s])/', '$1 $2', json_encode($val))) . "</code>";
			} elseif (is_string($val)) {
				// Shorten string to max. length.
				if (mb_strlen($val, "UTF-8") > self::MAX_TEXT_LENGTH)
					$val = mb_substr($val, 0, self::MAX_TEXT_LENGTH - 3, "UTF-8") . "...";

				// Add extra new line to make it visible in HTML output.
				if (preg_match("@\n$@", $val))
					$val .= "\n";

				$value .= "<code>" . nl2br(h($val)) . "</code>";
			} elseif (is_bool($val)) {
				// Handle boolean values.
				$value .= "<code class='jush'>" . h($val ? "true" : "false") . "</code>";
			} elseif (is_null($val)) {
				// Handle null value.
				$value .= "<code class='jush'>null</code>";
			} else {
				$value .= "<code class='jush'>" . h($val) . "</code>";
			}
		}

		$value .= "</table>";

		return $value;
	}
}
{'fraction_non_alphanumeric': 0.19873817034700317, 'fraction_numerical': 0.013706080713586425, 'mean_word_length': 2.7162489894907034, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 89, 'https://': 0, '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': '2737841', 'n_tokens_mistral': 4097, 'n_tokens_neox': 3293, 'n_words': 791}