Пример #1
0
 /**
  * Pass given text through the filter and return result.
  *
  * @see Filter::filter()
  * @param string $text
  * @return string $text
  */
 public function filter(Text $text)
 {
     $links = array();
     foreach ($text as $no => $line) {
         if (preg_match('/^ {0,3}[!]?\\[([\\w ]+)\\]:\\s+<?(.+?)>?(\\s+[\'"(].*?[\'")])?\\s*$/uS', $line, $match)) {
             $link =& $links[strtolower($match[1])];
             $link['href'] = $match[2];
             $link['title'] = null;
             if (isset($match[3])) {
                 $link['title'] = trim($match[3], ' \'"()');
             } else {
                 if (isset($text[$no + 1])) {
                     if (preg_match('/^ {0,3}[!]?[\'"(].*?[\'")]\\s*$/uS', $text[$no + 1], $match)) {
                         $link['title'] = trim($match[0], ' \'"()');
                         $text[$no + 1]->gist = '';
                     }
                 }
             }
             // erase line
             $line->gist = '';
         }
     }
     unset($link, $match, $no, $line);
     foreach ($text as $no => $line) {
         $line->gist = preg_replace_callback('/[!]?\\[(.*?)\\]\\((.*?)(\\s+"[\\w ]+")?\\)/uS', function ($match) {
             if (!isset($match[3])) {
                 $match[3] = null;
             }
             if (substr($match[0], 0, 1) == "!") {
                 return Link::buildImage($match[1], $match[2], $match[3]);
             } else {
                 return Link::buildHtml($match[1], $match[2], $match[3]);
             }
         }, $line->gist);
         if (preg_match_all('/\\[(.+?)\\] ?\\[([\\w ]*)\\]/uS', $line, $matches, PREG_SET_ORDER)) {
             foreach ($matches as &$match) {
                 $ref = !empty($match[2]) ? $match[2] : $match[1];
                 $ref = strtolower(trim($ref));
                 if (isset($links[$ref])) {
                     $link =& $links[$ref];
                     $html = Link::buildHtml($match[1], $link['href'], $link['title']);
                     $line->gist = str_replace($match[0], $html, $line);
                 }
             }
         }
     }
     return $text;
 }