public function onDocument(CarewEvent $event)
{
$document = $event->getSubject();
$file = $document->getFile();
$document->setPath($this->path->generatePath($file->getRelativePathName()));
preg_match('#^---\\n(.+)---\\n(.+)$#sU', $document->getBody(), $matches);
if ($matches) {
list(, $metadatas, $body) = $matches;
$metadatas = Yaml::parse($metadatas);
$document->setLayout('default');
foreach ($metadatas as $key => $value) {
$method = 'set' . ucfirst($key);
if (method_exists($document, $method)) {
$document->{$method}($value);
unset($metadatas[$key]);
}
}
if (isset($metadatas['permalink'])) {
$document->setPath($this->path->generatePath($metadatas['permalink']));
}
$document->addMetadatas($metadatas);
$document->setBody($body);
}
}