generateFromTokens() 공개 메소드

Generates HTML from an array of tokens.
public generateFromTokens ( HTMLPurifier_Token[] $tokens ) : string
$tokens HTMLPurifier_Token[] Array of HTMLPurifier_Token
리턴 string Generated HTML
예제 #1
0
 protected static function truncateHtml($string, $count, $suffix, $wordsPerLine, $encoding)
 {
     $config = \HTMLPurifier_Config::create(null);
     $config->set('Cache.SerializerPath', \Yii::$app->getRuntimePath());
     $lexer = \HTMLPurifier_Lexer::create($config);
     $tokens = $lexer->tokenizeHTML($string, $config, null);
     $openTokens = 0;
     $totalCount = 0;
     $truncated = [];
     foreach ($tokens as $token) {
         if ($token instanceof \HTMLPurifier_Token_Start) {
             //Tag begins
             $openTokens++;
             $truncated[] = $token;
         } elseif ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) {
             //Text
             if (false === $encoding) {
                 $token->data = self::truncateWords($token->data, ($count - $totalCount) * $wordsPerLine, '');
                 $currentWords = str_word_count($token->data);
             } else {
                 $token->data = self::truncate($token->data, ($count - $totalCount) * $wordsPerLine, '', $encoding) . ' ';
                 $currentWords = mb_strlen($token->data, $encoding);
             }
             //$totalCount += $currentWords;
             if (!$token->is_whitespace) {
                 $totalCount += intval(ceil($currentWords / $wordsPerLine));
             }
             //turn into lines
             if (1 === $currentWords) {
                 $token->data = ' ' . $token->data;
             }
             $truncated[] = $token;
         } elseif ($token instanceof \HTMLPurifier_Token_End) {
             //Tag ends
             $openTokens--;
             $truncated[] = $token;
         } elseif ($token instanceof \HTMLPurifier_Token_Empty) {
             //Self contained tags, i.e. <img/> etc.
             if ($token->name == 'img') {
                 //filter img tag
             } else {
                 $truncated[] = $token;
             }
         }
         if (0 === $openTokens && $totalCount >= $count) {
             break;
         }
     }
     $context = new \HTMLPurifier_Context();
     $generator = new \HTMLPurifier_Generator($config, $context);
     return $generator->generateFromTokens($truncated) . $suffix;
 }
예제 #2
0
 protected function assertGeneration($tokens, $expect)
 {
     $generator = new HTMLPurifier_Generator($this->config, $this->context);
     $result = $generator->generateFromTokens($tokens);
     $this->assertIdentical($expect, $result);
 }
예제 #3
0
 /**
  * Truncate a string while preserving the HTML.
  * 
  * @param string $string The string to truncate
  * @param integer $count
  * @param string $suffix String to append to the end of the truncated string.
  * @param string|boolean $encoding
  * @return string
  * @since 2.0.1
  */
 protected static function truncateHtml($string, $count, $suffix, $encoding = false)
 {
     $config = \HTMLPurifier_Config::create(null);
     $lexer = \HTMLPurifier_Lexer::create($config);
     $tokens = $lexer->tokenizeHTML($string, $config, null);
     $openTokens = 0;
     $totalCount = 0;
     $truncated = [];
     foreach ($tokens as $token) {
         if ($token instanceof \HTMLPurifier_Token_Start) {
             //Tag begins
             $openTokens++;
             $truncated[] = $token;
         } else {
             if ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) {
                 //Text
                 if (false === $encoding) {
                     $token->data = self::truncateWords($token->data, $count - $totalCount, '');
                     $currentCount = str_word_count($token->data);
                 } else {
                     $token->data = self::truncate($token->data, $count - $totalCount, '', $encoding) . ' ';
                     $currentCount = mb_strlen($token->data, $encoding);
                 }
                 $totalCount += $currentCount;
                 if (1 === $currentCount) {
                     $token->data = ' ' . $token->data;
                 }
                 $truncated[] = $token;
             } else {
                 if ($token instanceof \HTMLPurifier_Token_End) {
                     //Tag ends
                     $openTokens--;
                     $truncated[] = $token;
                 } else {
                     if ($token instanceof \HTMLPurifier_Token_Empty) {
                         //Self contained tags, i.e. <img/> etc.
                         $truncated[] = $token;
                     }
                 }
             }
         }
         if (0 === $openTokens && $totalCount >= $count) {
             break;
         }
     }
     $context = new \HTMLPurifier_Context();
     $generator = new \HTMLPurifier_Generator($config, $context);
     return $generator->generateFromTokens($truncated) . $suffix;
 }
예제 #4
0
 /**
  * Generate textual HTML from tokens
  */
 protected function generate($tokens)
 {
     $generator = new HTMLPurifier_Generator($this->config, $this->context);
     return $generator->generateFromTokens($tokens);
 }
예제 #5
0
 /**
  * Truncate a string while preserving the HTML.
  *
  * @param string $string The string to truncate
  * @param int $count
  * @param string $suffix String to append to the end of the truncated string.
  * @param string|bool $encoding
  * @return string
  * @since 2.0.1
  */
 protected static function truncateHtml($string, $count, $suffix, $encoding = false)
 {
     $config = \HTMLPurifier_Config::create(null);
     $config->set('Cache.SerializerPath', \Yii::$app->getRuntimePath());
     $lexer = \HTMLPurifier_Lexer::create($config);
     $tokens = $lexer->tokenizeHTML($string, $config, null);
     $openTokens = 0;
     $totalCount = 0;
     $truncated = [];
     foreach ($tokens as $token) {
         if ($token instanceof \HTMLPurifier_Token_Start) {
             //Tag begins
             $openTokens++;
             $truncated[] = $token;
         } elseif ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) {
             //Text
             if (false === $encoding) {
                 preg_match('/^(\\s*)/um', $token->data, $prefixSpace) ?: ($prefixSpace = ['', '']);
                 $token->data = $prefixSpace[1] . self::truncateWords(ltrim($token->data), $count - $totalCount, '');
                 $currentCount = self::countWords($token->data);
             } else {
                 $token->data = self::truncate($token->data, $count - $totalCount, '', $encoding);
                 $currentCount = mb_strlen($token->data, $encoding);
             }
             $totalCount += $currentCount;
             $truncated[] = $token;
         } elseif ($token instanceof \HTMLPurifier_Token_End) {
             //Tag ends
             $openTokens--;
             $truncated[] = $token;
         } elseif ($token instanceof \HTMLPurifier_Token_Empty) {
             //Self contained tags, i.e. <img/> etc.
             $truncated[] = $token;
         }
         if (0 === $openTokens && $totalCount >= $count) {
             break;
         }
     }
     $context = new \HTMLPurifier_Context();
     $generator = new \HTMLPurifier_Generator($config, $context);
     return $generator->generateFromTokens($truncated) . ($totalCount >= $count ? $suffix : '');
 }