1
0
Fork 1
mirror of https://github.com/pixelfed/pixelfed.git synced 2024-12-27 10:16:47 +00:00
pixelfed/app/Util/Lexer/HitHighlighter.php

200 lines
6.8 KiB
PHP
Raw Normal View History

2018-06-09 03:31:42 +00:00
<?php
/**
* @author Nick Pope <nick@nickpope.me.uk>
* @copyright Copyright © 2010, Nick Pope
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0
*/
namespace App\Util\Lexer;
/**
2018-08-28 03:07:36 +00:00
* Twitter HitHighlighter Class.
2018-06-09 03:31:42 +00:00
*
* Performs "hit highlighting" on tweets that have been auto-linked already.
* Useful with the results returned from the search API.
*
* Originally written by {@link http://github.com/mikenz Mike Cochrane}, this
* is based on code by {@link http://github.com/mzsanford Matt Sanford} and
* heavily modified by {@link http://github.com/ngnpope Nick Pope}.
*
* @author Nick Pope <nick@nickpope.me.uk>
* @copyright Copyright © 2010, Nick Pope
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0
*/
class HitHighlighter extends Regex
{
/**
* The tag to surround hits with.
*
2018-08-28 03:07:36 +00:00
* @var string
2018-06-09 03:31:42 +00:00
*/
protected $tag = 'em';
/**
* Provides fluent method chaining.
*
2018-08-28 03:07:36 +00:00
* @param string $tweet The tweet to be hit highlighted.
* @param bool $full_encode Whether to encode all special characters.
2018-06-09 03:31:42 +00:00
*
* @see __construct()
*
2018-08-28 03:07:36 +00:00
* @return HitHighlighter
2018-06-09 03:31:42 +00:00
*/
public static function create($tweet = null, $full_encode = false)
{
return new self($tweet, $full_encode);
}
/**
* Reads in a tweet to be parsed and hit highlighted.
*
* We take this opportunity to ensure that we escape user input.
*
* @see htmlspecialchars()
*
2018-08-28 03:07:36 +00:00
* @param string $tweet The tweet to be hit highlighted.
* @param bool $escape Whether to escape the tweet (default: true).
* @param bool $full_encode Whether to encode all special characters.
2018-06-09 03:31:42 +00:00
*/
public function __construct($tweet = null, $escape = true, $full_encode = false)
{
if (!empty($tweet) && $escape) {
if ($full_encode) {
parent::__construct(htmlentities($tweet, ENT_QUOTES, 'UTF-8', false));
} else {
parent::__construct(htmlspecialchars($tweet, ENT_QUOTES, 'UTF-8', false));
}
} else {
parent::__construct($tweet);
}
}
/**
* Set the highlighting tag to surround hits with. The default tag is 'em'.
*
2018-08-28 03:07:36 +00:00
* @return string The tag name.
2018-06-09 03:31:42 +00:00
*/
public function getTag()
{
return $this->tag;
}
/**
* Set the highlighting tag to surround hits with. The default tag is 'em'.
*
2018-08-28 03:07:36 +00:00
* @param string $v The tag name.
2018-06-09 03:31:42 +00:00
*
2018-08-28 03:07:36 +00:00
* @return HitHighlighter Fluid method chaining.
2018-06-09 03:31:42 +00:00
*/
public function setTag($v)
{
$this->tag = $v;
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $this;
}
/**
* Hit highlights the tweet.
*
2018-08-28 03:07:36 +00:00
* @param string $tweet The tweet to be hit highlighted.
* @param array $hits An array containing the start and end index pairs
* for the highlighting.
2018-06-09 03:31:42 +00:00
* @param bool $escape Whether to escape the tweet (default: true).
2018-08-28 03:07:36 +00:00
* @param bool $full_encode Whether to encode all special characters.
2018-06-09 03:31:42 +00:00
*
2018-08-28 03:07:36 +00:00
* @return string The hit highlighted tweet.
2018-06-09 03:31:42 +00:00
*/
public function highlight($tweet = null, array $hits = null)
{
if (is_null($tweet)) {
$tweet = $this->tweet;
}
if (empty($hits)) {
return $tweet;
}
$highlightTweet = '';
2018-08-28 03:07:36 +00:00
$tags = ['<'.$this->tag.'>', '</'.$this->tag.'>'];
// Check whether we can simply replace or whether we need to chunk...
2018-06-09 03:31:42 +00:00
if (strpos($tweet, '<') === false) {
$ti = 0; // tag increment (for added tags)
$highlightTweet = $tweet;
foreach ($hits as $hit) {
$highlightTweet = StringUtils::substrReplace($highlightTweet, $tags[0], $hit[0] + $ti, 0);
$ti += StringUtils::strlen($tags[0]);
$highlightTweet = StringUtils::substrReplace($highlightTweet, $tags[1], $hit[1] + $ti, 0);
$ti += StringUtils::strlen($tags[1]);
}
} else {
$chunks = preg_split('/[<>]/iu', $tweet);
$chunk = $chunks[0];
$chunk_index = 0;
$chunk_cursor = 0;
$offset = 0;
$start_in_chunk = false;
2018-08-28 03:07:36 +00:00
// Flatten the multidimensional hits array:
$hits_flat = [];
2018-06-09 03:31:42 +00:00
foreach ($hits as $hit) {
$hits_flat = array_merge($hits_flat, $hit);
}
2018-08-28 03:07:36 +00:00
// Loop over the hit indices:
2018-06-09 03:31:42 +00:00
for ($index = 0; $index < count($hits_flat); $index++) {
$hit = $hits_flat[$index];
$tag = $tags[$index % 2];
$placed = false;
while ($chunk !== null && $hit >= ($i = $offset + StringUtils::strlen($chunk))) {
$highlightTweet .= StringUtils::substr($chunk, $chunk_cursor);
if ($start_in_chunk && $hit === $i) {
$highlightTweet .= $tag;
$placed = true;
}
if (isset($chunks[$chunk_index + 1])) {
2018-08-28 03:07:36 +00:00
$highlightTweet .= '<'.$chunks[$chunk_index + 1].'>';
2018-06-09 03:31:42 +00:00
}
$offset += StringUtils::strlen($chunk);
$chunk_cursor = 0;
$chunk_index += 2;
$chunk = (isset($chunks[$chunk_index]) ? $chunks[$chunk_index] : null);
$start_in_chunk = false;
}
if (!$placed && $chunk !== null) {
$hit_spot = $hit - $offset;
2018-08-28 03:07:36 +00:00
$highlightTweet .= StringUtils::substr($chunk, $chunk_cursor, $hit_spot - $chunk_cursor).$tag;
2018-06-09 03:31:42 +00:00
$chunk_cursor = $hit_spot;
$start_in_chunk = ($index % 2 === 0);
$placed = true;
}
2018-08-28 03:07:36 +00:00
// Ultimate fallback - hits that run off the end get a closing tag:
2018-06-09 03:31:42 +00:00
if (!$placed) {
$highlightTweet .= $tag;
}
}
if ($chunk !== null) {
if ($chunk_cursor < StringUtils::strlen($chunk)) {
$highlightTweet .= StringUtils::substr($chunk, $chunk_cursor);
}
for ($index = $chunk_index + 1; $index < count($chunks); $index++) {
2018-08-28 03:07:36 +00:00
$highlightTweet .= ($index % 2 === 0 ? $chunks[$index] : '<'.$chunks[$index].'>');
2018-06-09 03:31:42 +00:00
}
}
}
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $highlightTweet;
}
/**
* Hit highlights the tweet.
*
2018-08-28 03:07:36 +00:00
* @param array $hits An array containing the start and end index pairs
* for the highlighting.
*
* @return string The hit highlighted tweet.
2018-06-09 03:31:42 +00:00
*
* @deprecated since version 1.1.0
*/
public function addHitHighlighting(array $hits)
{
return $this->highlight($this->tweet, $hits);
}
}