<commit_before><?php

namespace TeamTNT\TNTSearch\Support;

class Highlighter
{
    protected $options = [
        'simple'        => false,
        'wholeWord'     => true,
        'caseSensitive' => false,
        'stripLinks'    => false,
    ];

	/**
	 * @param        $text
	 * @param        $needle
	 * @param string $tag
	 * @param array  $options
	 *
	 * @return string
	 */
    public function highlight($text, $needle, $tag = 'em', $options = [])
    {
        $this->options = array_merge($this->options, $options);

        $highlight = '<' . $tag . '>\1</' . $tag . '>';
        $needle    = preg_split('/\PL+/u', $needle, -1, PREG_SPLIT_NO_EMPTY);

        // Select pattern to use
        if ($this->options['simple']) {
            $pattern    = '#(%s)#';
            $sl_pattern = '#(%s)#';
        } else {
            $pattern    = '#(?!<.*?)(%s)(?![^<>]*?>)#';
            $sl_pattern = '#<a\s(?:.*?)>(%s)</a>#';
        }

	    // Add Forgotten Unicode
	    $pattern .= 'u';

        // Case sensitivity<commit_msg>This branch allows you to specify attributes to be added to the tag used to highlight search terms. So besides the tag to be used (<em> by default) you can specify attributes like class="term", title="You search for this." or style="color: red;" all in array format, where the key is the attribute name and the value is the attribute value.<commit_after><?php

namespace TeamTNT\TNTSearch\Support;

class Highlighter
{
    protected $options = [
        'simple'        => false,
        'wholeWord'     => true,
        'caseSensitive' => false,
        'stripLinks'    => false,
        'tagOptions' => [
            // 'class' => 'search-term',             // Example
            // 'title' => 'You searched for this.',  // Example
            // 'data-toggle' => 'tooltip',           // Example
        ]
    ];

	/**
	 * @param        $text
	 * @param        $needle
	 * @param string $tag
	 * @param array  $options
	 *
	 * @return string
	 */
    public function highlight($text, $needle, $tag = 'em', $options = [])
    {
        $this->options = array_merge($this->options, $options);

        $tagAttributes = ' ';
        if (count($this->options['tagOptions'])) {
            foreach ($this->options['tagOptions'] as $attr => $value) {
                $tagAttributes .= $attr . '="' . $value . '" ';
            }
        }

        $highlight = '<' . $tag . $tagAttributes .'>\1</' . $tag . '>';
        $needle    = preg_split('/\PL+/u', $needle, -1, PREG_SPLIT_NO_EMPTY);

        // Select pattern to use
        if ($this->options['simple']) {
            $pattern    = '#(%s)#';
            $sl_pattern = '#(%s)#';
        } else {
            $pattern    = '#(?!<.*?)(%s)(?![^<>]*?>)#';
            $sl_pattern = '#<a\s(?:.*?)>(%s)</a>#';
        }

	    // Add Forgotten Unicode
	    $pattern .= 'u';

        // Case sensitivity
{'fraction_non_alphanumeric': 0.18960138648180241, 'fraction_numerical': 0.0013864818024263432, 'mean_word_length': 1.8630952380952381, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 37, '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': '2983987', 'n_tokens_mistral': 923, 'n_tokens_neox': 886, 'n_words': 218}