Skip to content

Commit

Permalink
Use a more reliable way to detect the current page, fixes #420
Browse files Browse the repository at this point in the history
  • Loading branch information
onigoetz committed Jan 9, 2017
1 parent cf05db0 commit e831e75
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
2 changes: 2 additions & 0 deletions libs/Format/HTML/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ function () use ($node, $output_dir, $key, $params, $index_pages) {
return;
}

$this->daux->tree->setActiveNode($node);

$generated = $this->generateOne($node, $params);
file_put_contents($output_dir . DIRECTORY_SEPARATOR . $key, $generated->getContent());
if ($index_pages) {
Expand Down
6 changes: 2 additions & 4 deletions libs/Format/HTML/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,16 @@ private function buildNavigation(Directory $tree, $path, $current_url, $base_pag
$nav[] = [
'title' => $node->getTitle(),
'href' => $base_page . $link,
'class' => $current_url === $link ? 'Nav__item--active' : '',
'class' => $node->isHotPath() ? 'Nav__item--active' : '',
];
} elseif ($node instanceof Directory) {
if (!$node->hasContent()) {
continue;
}

$link = ($path === '') ? $url : $path . '/' . $url;

$folder = [
'title' => $node->getTitle(),
'class' => strpos($current_url, $link) === 0 ? 'Nav__item--open' : '',
'class' => $node->isHotPath() ? 'Nav__item--open' : '',
];

if ($index = $node->getIndexPage()) {
Expand Down
2 changes: 2 additions & 0 deletions libs/Server/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ private function getPage($request)
throw new NotFoundException('The Page you requested is yet to be made. Try again later.');
}

$this->daux->tree->setActiveNode($file);

$generator = $this->daux->getGenerator();

if (!$generator instanceof LiveGenerator) {
Expand Down
4 changes: 4 additions & 0 deletions libs/Tree/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,8 @@ public function dump()
'path' => $this->path,
];
}

public function isHotPath(Entry $node = null) {
return $this->parent->isHotPath($node ?: $this);
}
}
29 changes: 29 additions & 0 deletions libs/Tree/Root.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class Root extends Directory
/** @var Config */
protected $config;

/** @var Entry */
protected $activeNode;

/**
* The root doesn't have a parent
*/
Expand All @@ -33,4 +36,30 @@ public function setConfig($config)
{
$this->config = $config;
}

public function isHotPath(Entry $node = null) {
if ($node == null) {
return true;
}

if ($this->activeNode == null) {
return false;
}

if ($node == $this->activeNode) {
return true;
}

foreach ($this->activeNode->getParents() as $parent) {
if ($node == $parent) {
return true;
}
}

return false;
}

public function setActiveNode(Entry $node) {
$this->activeNode = $node;
}
}

0 comments on commit e831e75

Please sign in to comment.