/** * @param Text $text * * @return array */ protected function parseAttributes(Text $text) { $patterns = ['id' => '/^#([a-zA-Z0-9_-]+)/', 'class' => '/^\\.([a-zA-Z0-9_-]+)/', 'attr' => '/^\\[([^\\]]+)\\]/', 'ident' => '/^(.)/']; $tokens = ['id' => [], 'class' => [], 'attr' => [], 'ident' => []]; while (!$text->isEmpty()) { foreach ($patterns as $name => $pattern) { if ($text->match($pattern, $matches)) { $tokens[$name][] = $matches[1]; $text->setString(substr($text->getString(), strlen($matches[0]))); break; } } } $attributes = array(); if (count($tokens['id'])) { $attributes['id'] = array_pop($tokens['id']); } if (count($tokens['class'])) { $attributes['class'] = implode(' ', $tokens['class']); } if (count($tokens['attr'])) { foreach ($tokens['attr'] as $raw) { $items = explode(' ', $raw); foreach ($items as $item) { if (strpos($item, '=') !== false) { list($key, $value) = explode('=', $item); $attributes[$key] = trim($value, '"'); } else { $attributes[$item] = $item; } } } } return $attributes; }
/** * Handle reference-style links: [link text] [id] * * @param Text $text * @param array $options */ public function processReferencedLink(Text $text, array $options = array()) { if (!$text->contains('[')) { return; } /** @noinspection PhpUnusedParameterInspection */ $text->replace('{ #( # wrap whole match in $1 \\[ (' . $this->getNestedBrackets() . ') # link text = $2 \\] [ ]? # one optional space (?:\\n[ ]*)? # one optional newline followed by spaces \\[ (.*?) # id = $3 \\] #) }xs', function (Text $whole, Text $linkText, Text $id = null) use($options) { if (is_null($id) || (string) $id == '') { $id = new Text($linkText); } $id->lower(); if ($this->markdown->getUrlRegistry()->exists($id)) { $url = new Text($this->markdown->getUrlRegistry()->get($id)); $url->escapeHtml(); $linkOptions = ['href' => $url->getString()]; if ($this->markdown->getTitleRegistry()->exists($id)) { $title = new Text($this->markdown->getTitleRegistry()->get($id)); $linkOptions['title'] = $title->escapeHtml()->getString(); } return $this->getRenderer()->renderLink($linkText->getString(), $linkOptions); } else { if ($options['strict']) { throw new SyntaxError(sprintf('Unable to find id "%s" in Reference-style link', $id), $this, $whole, $this->markdown); } return $whole; } }); }