<commit_before><?php

namespace WEEEOpen\Tarallo\SSRv1;

use League\Plates\Engine;
use League\Plates\Extension\ExtensionInterface;
use WEEEOpen\Tarallo\BaseFeature;
use WEEEOpen\Tarallo\Feature;

class TemplateUtilities implements ExtensionInterface {
	public $template;

	public function __construct() {

	}

	public function register(Engine $engine) {
		$engine->registerFunction('u', 'rawurlencode');
		$engine->registerFunction('getUltraFeatures', [$this, 'getUltraFeatures']);
		$engine->registerFunction('getGroupedFeatures', [$this, 'getGroupedFeatures']);
		$engine->registerFunction('printFeature', [$this, 'printFeature']);
		$engine->registerFunction('printExplanation', [$this, 'printExplanation']);
		$engine->registerFunction('colorToHtml', [$this, 'colorToHtml']);
		$engine->registerFunction('contentEditableWrap', [$this, 'contentEditableWrap']);
		$engine->registerFunction('getOptions', [$this, 'getOptions']);
		$engine->registerFunction('asTextContent', [$this, 'asTextContent']);
		$engine->registerFunction('prettyPrintJson', [$this, 'prettyPrintJson']);
	}

	/**
	 * @param Feature[] $features
	 *
	 * @return UltraFeature[]
	 */
	public function getUltraFeatures(array $features) {
		$result = [];

		foreach($features as $feature) {
			$ultra = UltraFeature::fromFeature($feature, $this->template->data()['lang'] ?? 'en');
			$result[] = $ultra;
		}
		return $result;

	}

	/**
	 * @param UltraFeature[] $ultraFeatures
	 * @return UltraFeature[][] Translated group name => [UltraFeature, UltraFeature, ...]
	 */
	public function getGroupedFeatures(array $ultraFeatures) {
		$groups = [];
		$groupsPrintable = [];

		// $group has the group ID as key, the human-radable name comes later
		foreach($ultraFeatures as $ultra) {
			$groups[BaseFeature::getGroup($ultra->name)][] = $ultra;
		}

		// Group IDs are numbered, that's the order, so it has to be sorted HERE
		// rather than later, when FeaturePrinter::printableGroup has translated
		// IDs to human-readable names
		ksort($groups);

		foreach($groups as $groupId => &$ultraFeatures) {
			usort($ultraFeatures, [TemplateUtilities::class, 'featureNameSort']);
			$groupsPrintable[FeaturePrinter::printableGroup($groupId)] = $ultraFeatures;
		}

		return $groupsPrintable;
	}

	private static function featureNameSort(UltraFeature $a, UltraFeature $b) {
		return $a->pname <=> $b->pname;
	}

	/**
	 * Print a single feature, if you have its parts (useful for statistics).
	 * Use UltraFeature::printableValue directly if you have the entire feature.
	 *
	 * @see UltraFeature::printableValue
	 *
	 * @param string $feature Feature name
	 * @param int|double|string $value Feature value
	 * @param string|null $lang Page language code
	 *
	 * @return string nice printable value
	 */
	public function printFeature(string $feature, $value, ?string $lang = null): string {
		return UltraFeature::printableValue(new Feature($feature, $value), $lang ?? 'en');
	}

	public function printExplanation(UltraFeature $ultra, ?string $lang = null): string {
		return $ultra->printableExplanation($lang ?? 'en') ?? '';
	}

	public function colorToHtml(string $color): string {
		$from = ['sip-brown', 'brown', 'orange', 'golden', 'yellowed', '-'];
		$to   = ['#CB8', 'saddlebrown', 'darkorange', 'gold', 'lightyellow', ''];
		return str_replace($from, $to, $color);
	}

	/**
	 * Wrap text into paragraphs ("div" tags), to be used in contenteditable elements
	 *
	 * @param string $html
	 *
	 * @return string
	 */
	public function contentEditableWrap(string $html): string {
		$paragraphed = '<div>' . str_replace(["\r\n", "\r", "\n"], '</div><div>', $html) . '</div>';
		// According to the HTML spec, <div></div> should be ignored by browser.
		// Firefox used to insert <p><br></p> for empty lines, for <div>s it does absolutely nothing but still displays them, soooo...
		return str_replace('<div></div>', '<div><br></div>', $paragraphed);
		// Or replace with '' to remove empty lines: cool, huh?
		//return $paragraphed;
	}

	/**
	 * Get all options for an enum feature
	 *
	 * @param string $name Feature name
	 *
	 * @return string[] Internal feature name => translated feature name
	 */
	public function getOptions(string $name) {
		$options = BaseFeature::getOptions($name);
		foreach($options as $value => &$translated) {<commit_msg>Darker grey<commit_after><?php

namespace WEEEOpen\Tarallo\SSRv1;

use League\Plates\Engine;
use League\Plates\Extension\ExtensionInterface;
use WEEEOpen\Tarallo\BaseFeature;
use WEEEOpen\Tarallo\Feature;

class TemplateUtilities implements ExtensionInterface {
	public $template;

	public function __construct() {

	}

	public function register(Engine $engine) {
		$engine->registerFunction('u', 'rawurlencode');
		$engine->registerFunction('getUltraFeatures', [$this, 'getUltraFeatures']);
		$engine->registerFunction('getGroupedFeatures', [$this, 'getGroupedFeatures']);
		$engine->registerFunction('printFeature', [$this, 'printFeature']);
		$engine->registerFunction('printExplanation', [$this, 'printExplanation']);
		$engine->registerFunction('colorToHtml', [$this, 'colorToHtml']);
		$engine->registerFunction('contentEditableWrap', [$this, 'contentEditableWrap']);
		$engine->registerFunction('getOptions', [$this, 'getOptions']);
		$engine->registerFunction('asTextContent', [$this, 'asTextContent']);
		$engine->registerFunction('prettyPrintJson', [$this, 'prettyPrintJson']);
	}

	/**
	 * @param Feature[] $features
	 *
	 * @return UltraFeature[]
	 */
	public function getUltraFeatures(array $features) {
		$result = [];

		foreach($features as $feature) {
			$ultra = UltraFeature::fromFeature($feature, $this->template->data()['lang'] ?? 'en');
			$result[] = $ultra;
		}
		return $result;

	}

	/**
	 * @param UltraFeature[] $ultraFeatures
	 * @return UltraFeature[][] Translated group name => [UltraFeature, UltraFeature, ...]
	 */
	public function getGroupedFeatures(array $ultraFeatures) {
		$groups = [];
		$groupsPrintable = [];

		// $group has the group ID as key, the human-radable name comes later
		foreach($ultraFeatures as $ultra) {
			$groups[BaseFeature::getGroup($ultra->name)][] = $ultra;
		}

		// Group IDs are numbered, that's the order, so it has to be sorted HERE
		// rather than later, when FeaturePrinter::printableGroup has translated
		// IDs to human-readable names
		ksort($groups);

		foreach($groups as $groupId => &$ultraFeatures) {
			usort($ultraFeatures, [TemplateUtilities::class, 'featureNameSort']);
			$groupsPrintable[FeaturePrinter::printableGroup($groupId)] = $ultraFeatures;
		}

		return $groupsPrintable;
	}

	private static function featureNameSort(UltraFeature $a, UltraFeature $b) {
		return $a->pname <=> $b->pname;
	}

	/**
	 * Print a single feature, if you have its parts (useful for statistics).
	 * Use UltraFeature::printableValue directly if you have the entire feature.
	 *
	 * @see UltraFeature::printableValue
	 *
	 * @param string $feature Feature name
	 * @param int|double|string $value Feature value
	 * @param string|null $lang Page language code
	 *
	 * @return string nice printable value
	 */
	public function printFeature(string $feature, $value, ?string $lang = null): string {
		return UltraFeature::printableValue(new Feature($feature, $value), $lang ?? 'en');
	}

	public function printExplanation(UltraFeature $ultra, ?string $lang = null): string {
		return $ultra->printableExplanation($lang ?? 'en') ?? '';
	}

	public function colorToHtml(string $color): string {
		$from = ['sip-brown', 'brown', 'darkgrey', 'orange', 'golden', 'yellowed', '-'];
		$to   = ['#CB8', 'saddlebrown', 'dimgrey', 'darkorange', 'gold', 'lightyellow', ''];
		return str_replace($from, $to, $color);
	}

	/**
	 * Wrap text into paragraphs ("div" tags), to be used in contenteditable elements
	 *
	 * @param string $html
	 *
	 * @return string
	 */
	public function contentEditableWrap(string $html): string {
		$paragraphed = '<div>' . str_replace(["\r\n", "\r", "\n"], '</div><div>', $html) . '</div>';
		// According to the HTML spec, <div></div> should be ignored by browser.
		// Firefox used to insert <p><br></p> for empty lines, for <div>s it does absolutely nothing but still displays them, soooo...
		return str_replace('<div></div>', '<div><br></div>', $paragraphed);
		// Or replace with '' to remove empty lines: cool, huh?
		//return $paragraphed;
	}

	/**
	 * Get all options for an enum feature
	 *
	 * @param string $name Feature name
	 *
	 * @return string[] Internal feature name => translated feature name
	 */
	public function getOptions(string $name) {
		$options = BaseFeature::getOptions($name);
		foreach($options as $value => &$translated) {
{'fraction_non_alphanumeric': 0.1593984962406015, 'fraction_numerical': 0.0004626951995373048, 'mean_word_length': 4.905737704918033, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 75, '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': '2113089', 'n_tokens_mistral': 2849, 'n_tokens_neox': 2556, 'n_words': 703}