Skip to content

Commit

Permalink
Merge pull request #26 from doctrine/fix-glob-toc
Browse files Browse the repository at this point in the history
Toc tree with globs adds non *.rst files to toc tree
  • Loading branch information
jwage authored Nov 1, 2018
2 parents b95e501 + bf225c5 commit b1c205f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/Directives/Toctree.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
use Doctrine\RST\Environment;
use Doctrine\RST\Nodes\Node;
use Doctrine\RST\Parser;
use Symfony\Component\Finder\Finder;
use function array_merge;
use function count;
use function explode;
use function glob;
use function implode;
use function is_dir;
use function realpath;
use function rtrim;
use function str_repeat;
Expand Down Expand Up @@ -95,12 +94,15 @@ private function globSearch(Environment $environment, string $globPattern) : arr

$allFiles = [];

$files = glob($globPatternPath);
$finder = new Finder();
$finder->in(rtrim($globPatternPath, '*'))
->name('*.rst')
->files();

foreach ($files as $file) {
if (is_dir($file)) {
foreach ($finder as $file) {
if ($file->isDir()) {
// remove the root directory so it is a relative path from the root
$relativePath = str_replace($rootDocPath . '/', '', $file);
$relativePath = str_replace($rootDocPath . '/', '', (string) $file->getRealPath());

// recursively search in this directory
$dirFiles = $this->globSearch($environment, $relativePath . '/*');
Expand All @@ -109,7 +111,7 @@ private function globSearch(Environment $environment, string $globPattern) : arr
} else {
// Trim the root path and the .rst extension. This is what the
// RST parser requires to add a dependency.
$file = str_replace([$rootDocPath . '/', '.rst'], '', $file);
$file = str_replace([$rootDocPath . '/', '.rst'], '', (string) $file->getRealPath());

$allFiles[] = $file;
}
Expand Down
46 changes: 46 additions & 0 deletions tests/BuilderIncludeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\RST;

use Doctrine\RST\Builder;
use PHPUnit\Framework\TestCase;
use function file_exists;
use function file_get_contents;
use function shell_exec;

/**
* Unit testing build with ".. include::" directive
*/
class BuilderIncludeTest extends TestCase
{
public function testTocTreeGlob() : void
{
shell_exec('rm -rf ' . $this->targetFile());
$builder = new Builder();
$builder->setUseRelativeUrls(true);
$builder->build($this->sourceFile(), $this->targetFile(), false);

self::assertTrue(file_exists($this->targetFile('index.html')));
self::assertContains('This file is included', file_get_contents($this->targetFile('index.html')));

foreach ($builder->getDocuments() as $document) {
foreach ($document->getEnvironment()->getMetas()->getAll() as $meta) {
foreach ($meta->getTocs() as $toc) {
self::assertNotContains('include.inc', $toc);
}
}
}
}

private function sourceFile(string $file = '') : string
{
return __DIR__ . '/builder-include/input/' . $file;
}

private function targetFile(string $file = '') : string
{
return __DIR__ . '/builder-include/output/' . $file;
}
}
1 change: 1 addition & 0 deletions tests/builder-include/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
output
4 changes: 4 additions & 0 deletions tests/builder-include/input/file.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
File
====

a file
1 change: 1 addition & 0 deletions tests/builder-include/input/include.rst.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file is included
9 changes: 9 additions & 0 deletions tests/builder-include/input/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Test include
============

.. include:: include.rst.inc

.. toctree::
:glob:

*

0 comments on commit b1c205f

Please sign in to comment.