Ejemplo n.º 1
0
 /**
  * Formats a plain-text post with HTML.
  *
  * Notes:
  * - cross-board or cross-thread links are not fixed, but instead left as
  *   deadlinks.
  * - <code>&lt;wbr&gt;</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] = '~&gt;&gt;([0-9]{1,9})~';
     // >>123 type post links
     $search[1] = "~^&gt;(.*)\$~m";
     // >greentext
     $search[2] = "~&gt;&gt;&gt;/([a-z]{1,4})/~";
     // >>>/board/ links
     $search[3] = "~&gt;&gt;&gt;/([a-z]{1,4})/([0-9]{1,9})~";
     // >>>/board/123 type post links
     $replace[0] = '<a href="" class="quotelink">&gt;&gt;$1</a>';
     $replace[1] = '<span class="quote">&gt;$1</span>';
     $replace[2] = '<a href="/$1/" class="quotelink">&gt;&gt;&gt;/$1/</a>';
     $replace[3] = '<span class="deadlink">&gt;&gt;&gt;/$1/$2</span>';
     $srch = array('&', "'", '<', '>', '"');
     $rpl = array("&amp;", '&#039;', '&lt;', '&gt;', "&quot;");
     $htmlSpecialCharComment = str_replace($srch, $rpl, $comment);
     $initialTagComment = preg_replace($search, $replace, $htmlSpecialCharComment);
     $formattedComment = preg_replace_callback('~<a href="" class="quotelink">&gt;&gt;([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] . '"' . '>&gt;&gt;' . $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] . '"' . '>&gt;&gt;' . $matches[1] . '</a>';
             } catch (\NotFoundException $ex) {
                 return '<span class="deadlink">&gt;&gt;' . $matches[1] . '</span>';
             }
         }
     }, $initialTagComment);
     $formattedComment = str_replace("\r", "", $formattedComment);
     $finalComment = str_replace("\n", "<br>", $formattedComment);
     return $finalComment;
 }