/** * Formats a plain-text post with HTML. * * Notes: * - cross-board or cross-thread links are not fixed, but instead left as * deadlinks. * - <code><wbr></code> tags are not replaced. * * @param Post $post * @return string HTML-formatted comment */ public static function toHtml(Post $post) { $comment = $post->getComment(); $posts = $post->hasThread() ? $post->getThread()->getPostIds() : []; $search[0] = '~>>([0-9]{1,9})~'; // >>123 type post links $search[1] = "~^>(.*)\$~m"; // >greentext $search[2] = "~>>>/([a-z]{1,4})/~"; // >>>/board/ links $search[3] = "~>>>/([a-z]{1,4})/([0-9]{1,9})~"; // >>>/board/123 type post links $replace[0] = '<a href="" class="quotelink">>>$1</a>'; $replace[1] = '<span class="quote">>$1</span>'; $replace[2] = '<a href="/$1/" class="quotelink">>>>/$1/</a>'; $replace[3] = '<span class="deadlink">>>>/$1/$2</span>'; $srch = array('&', "'", '<', '>', '"'); $rpl = array("&", ''', '<', '>', """); $htmlSpecialCharComment = str_replace($srch, $rpl, $comment); $initialTagComment = preg_replace($search, $replace, $htmlSpecialCharComment); $formattedComment = preg_replace_callback('~<a href="" class="quotelink">>>([0-9]{1,9})</a>~', function ($matches) use($posts, $post) { if (in_array($matches[1], $posts)) { return '<a href="#p' . $matches[1] . '" class="quotelink" ' . 'data-board="' . $post->getBoard()->getName() . '"' . 'data-thread="' . $post->getThreadId() . '"' . 'data-post="' . $matches[1] . '"' . '>>>' . $matches[1] . '</a>'; } else { try { $threadId = Model::get()->getThreadId($post->getBoard(), $matches[1]); return '<a href="' . $threadId . '#p' . $matches[1] . '" class="quotelink" ' . 'data-board="' . $post->getBoard()->getName() . '"' . 'data-thread="' . $threadId . '"' . 'data-post="' . $matches[1] . '"' . '>>>' . $matches[1] . '</a>'; } catch (\NotFoundException $ex) { return '<span class="deadlink">>>' . $matches[1] . '</span>'; } } }, $initialTagComment); $formattedComment = str_replace("\r", "", $formattedComment); $finalComment = str_replace("\n", "<br>", $formattedComment); return $finalComment; }
public static function fixHTML(Post $p) : string { if ($p->getBoard()->isArchive()) { return self::transform4chanHtml($p->getComment(), $p->getBoard(), $p->getThreadId()); } else { return self::transformHtml($p); } }