match() public static method

Performs a regular expression match.
public static match ( $subject, $pattern, $flags, $offset ) : mixed
return mixed
Example #1
0
 /**
  * @return void
  */
 public function process(Texy\BlockParser $parser, $content, Texy\HtmlElement $el)
 {
     if ($parser->isIndented()) {
         $parts = preg_split('#(\\n(?! )|\\n{2,})#', $content, -1, PREG_SPLIT_NO_EMPTY);
     } else {
         $parts = preg_split('#(\\n{2,})#', $content, -1, PREG_SPLIT_NO_EMPTY);
     }
     foreach ($parts as $s) {
         $s = trim($s);
         if ($s === '') {
             continue;
         }
         // try to find modifier
         $mod = NULL;
         if ($mx = Regexp::match($s, '#' . Texy\Patterns::MODIFIER_H . '(?=\\n|\\z)#sUm', Regexp::OFFSET_CAPTURE)) {
             list($mMod) = $mx[1];
             $s = trim(substr_replace($s, '', $mx[0][1], strlen($mx[0][0])));
             if ($s === '') {
                 continue;
             }
             $mod = new Texy\Modifier();
             $mod->setProperties($mMod);
         }
         $res = $this->texy->invokeAroundHandlers('paragraph', $parser, [$s, $mod]);
         if ($res) {
             $el->insert(NULL, $res);
         }
     }
 }
Example #2
0
 /**
  * Callback for: {{...}}.
  *
  * @return Texy\HtmlElement|string|FALSE
  */
 public function pattern(Texy\LineParser $parser, array $matches)
 {
     list(, $mContent) = $matches;
     // [1] => ...
     $cmd = trim($mContent);
     if ($cmd === '') {
         return FALSE;
     }
     $raw = NULL;
     $args = [];
     // function (arg, arg, ...) or function: arg, arg
     if ($matches = Texy\Regexp::match($cmd, '#^([a-z_][a-z0-9_-]*)\\s*(?:\\(([^()]*)\\)|:(.*))$#iu')) {
         $cmd = $matches[1];
         $raw = isset($matches[3]) ? trim($matches[3]) : trim($matches[2]);
         if ($raw !== '') {
             $args = preg_split('#\\s*' . preg_quote($this->separator, '#') . '\\s*#u', $raw);
         }
     }
     // Texy 1.x way
     if ($this->handler) {
         if (is_callable([$this->handler, $cmd])) {
             array_unshift($args, $parser);
             return call_user_func_array([$this->handler, $cmd], $args);
         }
         if (is_callable($this->handler)) {
             return call_user_func_array($this->handler, [$parser, $cmd, $args, $raw]);
         }
     }
     // Texy 2 way
     return $this->texy->invokeAroundHandlers('script', $parser, [$cmd, $args, $raw]);
 }
Example #3
0
 /**
  * Parses image's syntax.
  * @param  string  input: small.jpg 80x13 | small-over.jpg | linked.jpg
  * @param  string
  * @param  bool
  * @return Image
  */
 public function factoryImage($content, $mod, $tryRef = TRUE)
 {
     $image = $tryRef ? $this->getReference(trim($content)) : FALSE;
     if (!$image) {
         $texy = $this->texy;
         $content = explode('|', $content);
         $image = new Image();
         // dimensions
         $matches = NULL;
         if ($matches = Texy\Regexp::match($content[0], '#^(.*) (\\d+|\\?) *(X|x) *(\\d+|\\?) *()$#U')) {
             $image->URL = trim($matches[1]);
             $image->asMax = $matches[3] === 'X';
             $image->width = $matches[2] === '?' ? NULL : (int) $matches[2];
             $image->height = $matches[4] === '?' ? NULL : (int) $matches[4];
         } else {
             $image->URL = trim($content[0]);
         }
         if (!$texy->checkURL($image->URL, $texy::FILTER_IMAGE)) {
             $image->URL = NULL;
         }
         // onmouseover image
         if (isset($content[1])) {
             $tmp = trim($content[1]);
             if ($tmp !== '' && $texy->checkURL($tmp, $texy::FILTER_IMAGE)) {
                 $image->overURL = $tmp;
             }
         }
         // linked image
         if (isset($content[2])) {
             $tmp = trim($content[2]);
             if ($tmp !== '' && $texy->checkURL($tmp, $texy::FILTER_ANCHOR)) {
                 $image->linkedURL = $tmp;
             }
         }
     }
     $image->modifier->setProperties($mod);
     return $image;
 }
Example #4
0
 /**
  * Callback for:.
  *
  * .(title)[class]{style}>
  * |------------------
  * | xxx | xxx | xxx | .(..){..}[..]
  * |------------------
  * | aa | bb | cc |
  *
  * @return HtmlElement|string|FALSE
  */
 public function patternTable(Texy\BlockParser $parser, array $matches)
 {
     if ($this->disableTables) {
         return FALSE;
     }
     list(, $mMod) = $matches;
     // [1] => .(title)[class]{style}<>_
     $texy = $this->texy;
     $el = new HtmlElement('table');
     $mod = new Modifier($mMod);
     $mod->decorate($texy, $el);
     $parser->moveBackward();
     if ($parser->next('#^\\|(\\#|\\=){2,}(?![|\\#=+])(.+)\\1*\\|? *' . Patterns::MODIFIER_H . '?()$#Um', $matches)) {
         list(, , $mContent, $mMod) = $matches;
         // [1] => # / =
         // [2] => ....
         // [3] => .(title)[class]{style}<>
         $caption = $el->create('caption');
         $mod = new Modifier($mMod);
         $mod->decorate($texy, $caption);
         $caption->parseLine($texy, $mContent);
     }
     $isHead = FALSE;
     $colModifier = [];
     $prevRow = [];
     // rowSpan building helper
     $rowCounter = 0;
     $colCounter = 0;
     $elPart = NULL;
     $lineMode = FALSE;
     // rows must be separated by lines
     while (TRUE) {
         if ($parser->next('#^\\|([=-])[+|=-]{2,}$#Um', $matches)) {
             // line
             if ($lineMode) {
                 if ($matches[1] === '=') {
                     $isHead = !$isHead;
                 }
             } else {
                 $isHead = !$isHead;
                 $lineMode = $matches[1] === '=';
             }
             $prevRow = [];
             continue;
         }
         if ($parser->next('#^\\|(.*)(?:|\\|\\ *' . Patterns::MODIFIER_HV . '?)()$#U', $matches)) {
             // smarter head detection
             if ($rowCounter === 0 && !$isHead && $parser->next('#^\\|[=-][+|=-]{2,}$#Um', $foo)) {
                 $isHead = TRUE;
                 $parser->moveBackward();
             }
             if ($elPart === NULL) {
                 $elPart = $el->create($isHead ? 'thead' : 'tbody');
             } elseif (!$isHead && $elPart->getName() === 'thead') {
                 $this->finishPart($elPart);
                 $elPart = $el->create('tbody');
             }
             // PARSE ROW
             list(, $mContent, $mMod) = $matches;
             // [1] => ....
             // [2] => .(title)[class]{style}<>_
             $elRow = new HtmlElement('tr');
             $mod = new Modifier($mMod);
             $mod->decorate($texy, $elRow);
             $rowClass = $rowCounter % 2 === 0 ? $this->oddClass : $this->evenClass;
             if ($rowClass && !isset($mod->classes[$this->oddClass]) && !isset($mod->classes[$this->evenClass])) {
                 $elRow->attrs['class'][] = $rowClass;
             }
             $col = 0;
             $elCell = NULL;
             // special escape sequence \|
             $mContent = str_replace('\\|', "", $mContent);
             $mContent = Regexp::replace($mContent, '#(\\[[^\\]]*)\\|#', "\$1");
             // HACK: support for [..|..]
             foreach (explode('|', $mContent) as $cell) {
                 $cell = strtr($cell, "", '|');
                 // rowSpan
                 if (isset($prevRow[$col]) && ($lineMode || ($matches = Regexp::match($cell, '#\\^\\ *$|\\*??(.*)\\ +\\^$#AU')))) {
                     $prevRow[$col]->rowSpan++;
                     if (!$lineMode) {
                         $cell = isset($matches[1]) ? $matches[1] : '';
                     }
                     $prevRow[$col]->text .= "\n" . $cell;
                     $col += $prevRow[$col]->colSpan;
                     $elCell = NULL;
                     continue;
                 }
                 // colSpan
                 if ($cell === '' && $elCell) {
                     $elCell->colSpan++;
                     unset($prevRow[$col]);
                     $col++;
                     continue;
                 }
                 // common cell
                 $matches = Regexp::match($cell, '#(\\*??)\\ *' . Patterns::MODIFIER_HV . '??(.*)' . Patterns::MODIFIER_HV . '?\\ *()$#AU');
                 if (!$matches) {
                     continue;
                 }
                 list(, $mHead, $mModCol, $mContent, $mMod) = $matches;
                 // [1] => * ^
                 // [2] => .(title)[class]{style}<>_
                 // [3] => ....
                 // [4] => .(title)[class]{style}<>_
                 if ($mModCol) {
                     $colModifier[$col] = new Modifier($mModCol);
                 }
                 if (isset($colModifier[$col])) {
                     $mod = clone $colModifier[$col];
                 } else {
                     $mod = new Modifier();
                 }
                 $mod->setProperties($mMod);
                 $elCell = new TableCellElement();
                 $elCell->setName($isHead || $mHead === '*' ? 'th' : 'td');
                 $mod->decorate($texy, $elCell);
                 $elCell->text = $mContent;
                 $elRow->add($elCell);
                 $prevRow[$col] = $elCell;
                 $col++;
             }
             // even up with empty cells
             while ($col < $colCounter) {
                 if (isset($prevRow[$col]) && $lineMode) {
                     $prevRow[$col]->rowSpan++;
                     $prevRow[$col]->text .= "\n";
                 } else {
                     $elCell = new TableCellElement();
                     $elCell->setName($isHead ? 'th' : 'td');
                     if (isset($colModifier[$col])) {
                         $colModifier[$col]->decorate($texy, $elCell);
                     }
                     $elRow->add($elCell);
                     $prevRow[$col] = $elCell;
                 }
                 $col++;
             }
             $colCounter = $col;
             if ($elRow->count()) {
                 $elPart->add($elRow);
                 $rowCounter++;
             } else {
                 // redundant row
                 foreach ($prevRow as $elCell) {
                     $elCell->rowSpan--;
                 }
             }
             continue;
         }
         break;
     }
     if ($elPart === NULL) {
         // invalid table
         return FALSE;
     }
     if ($elPart->getName() === 'thead') {
         // thead is optional, tbody is required
         $elPart->setName('tbody');
     }
     $this->finishPart($elPart);
     // event listener
     $texy->invokeHandlers('afterTable', [$parser, $el, $mod]);
     return $el;
 }