Inheritance: extends Symfony\Component\EventDispatcher\GenericEvent
Exemplo n.º 1
0
Arquivo: Toc.php Projeto: carew/carew
 public function onDocument(CarewEvent $event)
 {
     $document = $event->getSubject();
     if (Document::TYPE_API == $document->getType()) {
         return;
     }
     $extension = pathinfo($document->getPath(), PATHINFO_EXTENSION);
     if ('html' !== $extension) {
         return;
     }
     $urls = array();
     // hack to keep twig statements in local $urls variable because DOMDocument encode attributes value
     $body = preg_replace_callback('/(?P<attr>href|src)="(?P<url>[^"]*)"/', function ($matches) use(&$urls) {
         $urls[] = $matches['url'];
         return sprintf('%s="%s"', $matches['attr'], '%%%%%%%%%%%%%%%%%%%%');
     }, $document->getBody());
     try {
         $level = error_reporting(0);
         $document->setToc($this->htmlTools->buildTOC($document->getBody()));
         $body = $this->htmlTools->addHeadingsId($body, 'h1, h2, h3, h4, h5, h6', true);
         error_reporting($level);
     } catch (\Exception $e) {
         // TODO: add a message when failing.
         return;
     }
     // restore url value in href and src attribute
     $i = 0;
     $body = preg_replace_callback('/(?P<attr>href|src)="(?P<url>%%%%%%%%%%%%%%%%%%%%)"/', function ($matches) use(&$i, $urls) {
         return sprintf('%s="%s"', $matches['attr'], $urls[$i++]);
     }, $body);
     $document->setBody(html_entity_decode($body, ENT_QUOTES, 'UTF-8'));
 }
 public function onTerminate(CarewEvent $carewEvent)
 {
     foreach ($this->finder->in($carewEvent->getArgument('webDir')) as $file) {
         $asset = new FileAsset($file->getPathname(), array($this->getFilter()));
         file_put_contents($file->getPathname(), $asset->dump());
     }
 }
Exemplo n.º 3
0
 public function onDocument(CarewEvent $event)
 {
     $document = $event->getSubject();
     if (null === $document->getFile()) {
         return;
     }
     $extension = $document->getFile()->getExtension();
     if ('md' !== $extension) {
         if ('twig' !== $extension) {
             return;
         }
         $extension = pathinfo(str_replace('.twig', '', $document->getFilePath()), PATHINFO_EXTENSION);
         if ('md' !== $extension) {
             return;
         }
     }
     $twig = array();
     // hack to keep twig statements in local $twig variable because MD parser does not work with that
     $body = preg_replace_callback('/(?<twig>{{\\s*[^}}]*\\s*}})/', function ($matches) use(&$twig) {
         $twig[] = $matches['twig'];
         return 'http://%%%%%%%%%%%%%%%%%%%%';
     }, $document->getBody());
     $body = $this->markdownParser->text($body);
     $i = 0;
     $body = preg_replace_callback('/(http:\\/\\/%%%%%%%%%%%%%%%%%%%%)/', function ($matches) use(&$i, $twig) {
         return $twig[$i++];
     }, $body);
     $document->setBody($body);
 }
Exemplo n.º 4
0
 public function onTerminate(CarewEvent $event)
 {
     $output = $event->getArgument('webDir') . '/' . $this->output;
     $this->filesystem->mkdir(dirname($output));
     $this->less->importDir = dirname($this->input);
     file_put_contents($output, $this->less->parse(file_get_contents($this->input)));
 }
Exemplo n.º 5
0
 public function onDocument(CarewEvent $event)
 {
     $document = $event->getSubject();
     if (Document::TYPE_POST == $document->getType()) {
         $this->onPost($document);
     } elseif (Document::TYPE_API == $document->getType()) {
         $this->onApi($document);
     }
 }
 /**
  * @param Document $subject
  * @return CarewEvent
  */
 private function createEvent($subject = null)
 {
     if (null === $subject) {
         $subject = array(new Document());
     }
     $dispatcher = $this->getMock('Symfony\\Component\\EventDispatcher\\EventDispatcher');
     $event = new CarewEvent($subject);
     $event->setDispatcher($dispatcher);
     return $event;
 }
Exemplo n.º 7
0
Arquivo: Feed.php Projeto: carew/carew
 public function onDocuments(CarewEvent $event)
 {
     $document = new Document();
     $document->setLayout('index.atom.twig')->setPath('feed/atom.xml')->setFilePath('feed/atom');
     $documents = $event->getSubject();
     $documents[$document->getFilePath()] = $document;
     $event->setSubject($documents);
     $globals = $event['globals'];
     $globals->documents = array_replace($globals->documents, $documents);
 }
Exemplo n.º 8
0
 public function onDocuments(CarewEvent $event)
 {
     $documents = $event->getSubject();
     $globals = $event['globals'];
     foreach ($globals->tags as $tagName => $documentList) {
         $documentList = array_filter($documentList, function (Document $document) {
             return $document->isTypePost();
         });
         $document = new Document();
         $document->setLayout('index.atom.twig')->setPath(sprintf('tags/%s/feed/atom.xml', $this->inflector->urlize($tagName)))->setFilePath(sprintf('tags/%s/feed/atom', $tagName))->setVar('post', $documentList);
         $documents[$document->getFilePath()] = $document;
     }
     $event->setSubject($documents);
     $globals->documents = array_replace($globals->documents, $documents);
 }
Exemplo n.º 9
0
Arquivo: Tags.php Projeto: carew/carew
 public function onDocuments(CarewEvent $event)
 {
     $documents = $event->getSubject();
     $globals = $event['globals'];
     $tags = array();
     foreach ($globals->tags as $tagName => $documentList) {
         $tagDocument = new Document();
         $tagDocument->setLayout('default.html.twig')->setBody('{{ render_documents(carew.documents) }}')->setPath(sprintf('tags/%s.html', $this->inflector->urlize($tagName)))->setTitle('Tag #' . $tagName)->setFilePath('tags/' . $tagName)->setNavigations(array('tags', 'sub-tags'))->setVar('documents', $documentList);
         $documents[$tagDocument->getFilePath()] = $tagDocument;
         $tags[$tagDocument->getFilePath()] = $tagDocument;
     }
     $tagsDocument = new Document();
     $tagsDocument->setLayout('default.html.twig')->setBody('{{ render_documents(carew.tags) }}')->setPath('tags/index.html')->setTitle('Tags')->setFilePath('tags')->setNavigations('tags')->setVar('tags', $tags);
     $documents[$tagsDocument->getFilePath()] = $tagsDocument;
     $globals->documents = array_replace($globals->documents, $documents);
     $event->setSubject($documents);
 }
 /**
  * @param CarewEvent $event
  */
 public function onIndexes(CarewEvent $event)
 {
     /** @var array|Document[] $index */
     $indexes = $event->getSubject();
     $docs = $this->getDocumentsFromIndexes($indexes);
     foreach ($docs as $doc) {
         $data = $doc->getMetadatas();
         if (isset($data['author'])) {
             $author = $this->authors->getAuthor($data['author']);
             $author->addDocument($doc);
             $data['author'] = $author;
             $doc->setMetadatas($data);
         }
     }
     $authorsEvent = new CarewEvent(array());
     $event->getDispatcher()->dispatch(AuthorEvents::AUTHORS, $authorsEvent);
     $event->setSubject(array_merge($indexes, $authorsEvent->getSubject()));
 }
Exemplo n.º 11
0
 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);
     }
 }
Exemplo n.º 12
0
Arquivo: Twig.php Projeto: carew/carew
 public function postRender(CarewEvent $event)
 {
     $documents = $event->getSubject();
     foreach ($documents as $document) {
         if (false === $document->getLayout()) {
             $document->setBodyDecorated($document->getBody());
             continue;
         }
         $this->setTwigGlobals($document);
         $layout = $document->getLayout();
         if (false === strpos($layout, '.twig')) {
             $layout .= '.html.twig';
         }
         $document->setBodyDecorated($this->twig->render($layout));
     }
     $event->setSubject($documents);
 }