/**
  * Generate HTML for the content of a tag.
  *
  * @param   string  $tag    Name of the tag without @
  * @param   string  $text   Content of the tag
  * @return  string  HTML
  */
 public static function format_tag($tag, $text)
 {
     if ($tag === 'license') {
         if (strpos($text, '://') !== FALSE) {
             return HTML::anchor($text);
         }
     } elseif ($tag === 'link') {
         $split = preg_split('/\\s+/', $text, 2);
         return HTML::anchor($split[0], isset($split[1]) ? $split[1] : $split[0]);
     } elseif ($tag === 'copyright') {
         // Convert the copyright symbol
         return str_replace('(c)', '©', $text);
     } elseif ($tag === 'throws') {
         $route = Route::get('docs/api');
         if (preg_match('/^(\\w+)\\W(.*)$/D', $text, $matches)) {
             return HTML::anchor($route->uri(array('class' => $matches[1])), $matches[1]) . ' ' . $matches[2];
         }
         return HTML::anchor($route->uri(array('class' => $text)), $text);
     } elseif ($tag === 'see' or $tag === 'uses') {
         if (preg_match('/^' . Kodoc::$regex_class_member . '/', $text, $matches)) {
             return Kodoc::link_class_member($matches);
         }
     }
     return $text;
 }
Ejemplo n.º 2
0
 /**
  * Parse a comment to extract the description and the tags
  *
  * @param   string  the comment retreived using ReflectionClass->getDocComment()
  * @return  array   array(string $description, array $tags)
  */
 public static function parse($comment)
 {
     // Normalize all new lines to \n
     $comment = str_replace(array("\r\n", "\n"), "\n", $comment);
     // Remove the phpdoc open/close tags and split
     $comment = array_slice(explode("\n", $comment), 1, -1);
     // Tag content
     $tags = array();
     foreach ($comment as $i => $line) {
         // Remove all leading whitespace
         $line = preg_replace('/^\\s*\\* ?/m', '', $line);
         // Search this line for a tag
         if (preg_match('/^@(\\S+)(?:\\s*(.+))?$/', $line, $matches)) {
             // This is a tag line
             unset($comment[$i]);
             $name = $matches[1];
             $text = isset($matches[2]) ? trim($matches[2]) : '';
             switch ($name) {
                 case 'license':
                     if (strpos($text, '://') !== FALSE) {
                         // Convert the lincense into a link
                         $text = HTML::anchor($text);
                     }
                     break;
                 case 'link':
                     $text = preg_split('/\\s+/', $text, 2);
                     $text = HTML::anchor($text[0], isset($text[1]) ? $text[1] : $text[0]);
                     break;
                 case 'copyright':
                     if (strpos($text, '(c)') !== FALSE) {
                         // Convert the copyright sign
                         $text = str_replace('(c)', '©', $text);
                     }
                     break;
                 case 'throws':
                     if (preg_match('/^(\\w+)\\W(.*)$/', $text, $matches)) {
                         $text = HTML::anchor(Route::get('docs/api')->uri(array('class' => $matches[1])), $matches[1]) . ' ' . $matches[2];
                     } else {
                         $text = HTML::anchor(Route::get('docs/api')->uri(array('class' => $text)), $text);
                     }
                     break;
                 case 'uses':
                     if (preg_match('/^' . Kodoc::$regex_class_member . '$/i', $text, $matches)) {
                         $text = Kodoc::link_class_member($matches);
                     }
                     break;
                     // Don't show @access lines, they are shown elsewhere
                 // Don't show @access lines, they are shown elsewhere
                 case 'access':
                     continue 2;
             }
             // Add the tag
             $tags[$name][] = $text;
         } else {
             // Overwrite the comment line
             $comment[$i] = (string) $line;
         }
     }
     // Concat the comment lines back to a block of text
     if ($comment = trim(implode("\n", $comment))) {
         // Parse the comment with Markdown
         $comment = Markdown($comment);
     }
     return array($comment, $tags);
 }