/** * @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; }
/** * @param Text $text */ public function buildParagraph(Text $text) { $parts = $text->replace('/\\A\\n+/', '')->replace('/\\n+\\z/', '')->split('/\\n{2,}/', PREG_SPLIT_NO_EMPTY); $parts->apply(function (Text $part) { if (!$this->markdown->getHashRegistry()->exists($part)) { $this->markdown->emit('inline', array($part)); $part->replace('/^([ \\t]*)/', ''); $part->setString($this->getRenderer()->renderParagraph((string) $part)); } return $part; }); $parts->apply(function (Text $part) { if ($this->markdown->getHashRegistry()->exists($part)) { $part->setString(trim($this->markdown->getHashRegistry()->get($part))); } return $part; }); $text->setString($parts->join("\n\n")); }