public function testOnDocuments() { $documents = array(); for ($i = 1; $i <= 20; ++$i) { $document = new Document(null, null, Document::TYPE_POST); $document->setTitle('Post #' . $i); $document->setTags('Tag #' . $i % 5); $documents[] = $document; } for ($i = 1; $i <= 20; ++$i) { $document = new Document(null, null, Document::TYPE_PAGE); $document->setTitle('Post #' . $i); $document->setTags('Tag #' . $i % 5); $documents[] = $document; } $processor = new Processor(); $globals = $processor->processGlobals($documents); $event = new CarewEvent($documents, array('globals' => $globals)); $tagFeed = new TagsFeed(); $tagFeed->onDocuments($event); $documents = $event->getSubject(); // 20 posts, 20 pages, 5 tags index $this->assertCount(45, $documents); $this->assertSame('tags/tag-1/feed/atom.xml', $documents['tags/Tag #1/feed/atom']->getPath()); }
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); }
/** * @dataProvider getOnPostTests */ public function testOnPost($expected, $format) { $file = $this->prophesize('Symfony\\Component\\Finder\\SplFileInfo'); $file->getBasename('.md')->willReturn('2010-09-09-foobar-bar'); $file->__toString()->willReturn(''); $document = new Document($file->reveal(), null, Document::TYPE_POST); $event = new CarewEvent($document); $extraction = new Optimization($format); $extraction->onDocument($event); $this->assertSame($expected, $document->getPath()); }
public function testOnDocumentDoesNotLinkWithDoubleQuote() { $document = new Document(); $document->setPath('index.html'); $body = '<a href="https://google.com">"google.com"</a>'; $document->setBody($body); $event = new CarewEvent($document); $toc = new Toc(); $toc->onDocument($event); $this->assertSame($body, $document->getBody()); }
/** * @dataProvider getContentTypes * * @param string $contentType */ public function testOnIndexesAddsDocumentsToAuthors($contentType) { $author = new Author('seiffert'); $authors = new AuthorRegistry(); $authors->addAuthor($author); $subscriber = new IndexesEventSubscriber($authors); $document = new Document(); $document->setMetadatas(array('author' => 'seiffert')); $index = new Document(); $index->setVars(array($contentType => array($document))); $subscriber->onIndexes($this->createEvent(array($index))); $document->setMetadatas(array('author' => $author), true); $this->assertSame(array($document), $author->getDocuments()); }
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); }
/** * @param Author $author * @return Document[] */ public function generateAuthorIndices(Author $author) { $indexes = array(); foreach ($this->getLayouts() as $layout) { $file = $layout->getBasename(); preg_match('#authors\\.(.+?)\\.twig$#', $file, $match); $format = $match[1]; $document = new Document(); $document->setLayout((string) $file); $document->setPath(sprintf('authors/%s.%s', $author->getHandle(), $format)); $document->setTitle(sprintf('Author: %s (%s)', $author->get('name'), $author->getHandle())); $document->setVars(array('author' => $author)); $indexes[] = $document; } return $indexes; }
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); }
public function testOnDocuments() { $tags = new Tags(); $documents = array(); for ($i = 1; $i <= 20; ++$i) { $document = new Document(); $document->setTitle('Post #' . $i); $document->setTags('Tag #' . $i % 5); $documents[] = $document; } $processor = new Processor(); $globals = $processor->processGlobals($documents); $event = new CarewEvent($documents, array('globals' => $globals)); $tags->onDocuments($event); $documents = $event->getSubject(); // 20 post, 1 page for all tags, 5 tags $this->assertCount(26, $documents); $this->assertSame('tags/tag-1.html', $documents['tags/Tag #1']->getPath()); }
public function testGetSetMetadata() { $document = new Document(); $this->assertSame(null, $document->getMetadata('foo')); $this->assertSame('default', $document->getMetadata('foo', 'default')); $document->setMetadata('foo', 'bar'); $this->assertSame('bar', $document->getMetadata('foo')); $this->assertSame('bar', $document->getMetadata('foo', 'default')); }
public function setUp() { $this->container = new \Pimple(); $this->extension = new CarewExtension($this->container); $this->twig = new \Twig_Environment(new \Twig_Loader_Filesystem(array(__DIR__ . '/fixtures/theme1', __DIR__ . '/fixtures/theme2', __DIR__ . '/../../Twig/Resources/layouts'))); $documents = array(); for ($i = 1; $i <= 20; ++$i) { $document = new Document(); $document->setTitle('Doc #' . $i); $document->setTags('Tag #' . $i % 5); $document->setFilePath('pages/page-' . $i); $document->setPath('pages/page-' . $i . '.html'); $documents[$document->getFilePath()] = $document; } $processor = new Processor(); $this->globals = $processor->processGlobals($documents); $this->documents = $documents; $this->twig->addGlobal('carew', $this->globals); $this->twig->addExtension($this->extension); }
public function testWrite() { $tmp = sys_get_temp_dir() . '/carew-test'; static::deleteDir($tmp); $document = new Document(new SplFileInfo(__FILE__, '', '')); $document->setPath('foo/bar/file.html'); $this->processor->write($document, $tmp); $this->assertFileEquals(__FILE__, $tmp . '/foo/bar/file.html'); static::deleteDir($tmp); }
/** * @expectedException RuntimeException * @expectedExceptionMessage Unexpected "}" in a string template line 1. */ public function testPreRenderThrowExceptionOnRender() { $document = new Document(); $document->setLayout('default'); $document->setBody(<<<EOL Hello {{ foobar }} {% set text = '{{ foobar }' %} {{ template_from_string(text) }} EOL ); $event = new CarewEvent(array($document)); $this->twigListenner->preRender($event); $this->assertSame('foobar', $document->getBody()); }
private function setTwigGlobals(Document $document) { $twigGlobals = $this->twig->getGlobals(); $globals = $twigGlobals['carew']; $globals->fromArray($document->getVars()); $globals->relativeRoot = $document->getRootPath(); $globals->currentPath = $document->getPath(); $globals->document = $document; return $this; }
public function write(Document $document, $webDir) { $target = $webDir . '/' . $document->getPath(); $this->filesystem->mkdir(dirname($target)); file_put_contents($target, $document->getBodyDecorated()); }
private function onApi(Document $document) { $document->setPath(sprintf('api/%s', $document->getPath())); }