Commit 75b1f481 by Carsten Brandt

improved HTML guide navigation

parent 713f73e6
...@@ -81,7 +81,7 @@ class ApiMarkdown extends GithubMarkdown ...@@ -81,7 +81,7 @@ class ApiMarkdown extends GithubMarkdown
{ {
$content = $this->parseInline($block['content']); $content = $this->parseInline($block['content']);
$hash = Inflector::slug(strip_tags($content)); $hash = Inflector::slug(strip_tags($content));
$hashLink = "<a href=\"#$hash\" name=\"$hash\">&para;</a>"; $hashLink = "<a href=\"#$hash\" name=\"$hash\" class=\"hashlink\">&para;</a>";
$tag = 'h' . $block['level']; $tag = 'h' . $block['level'];
return "<$tag>$content $hashLink</$tag>"; return "<$tag>$content $hashLink</$tag>";
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace yii\apidoc\renderers; namespace yii\apidoc\renderers;
use Yii; use Yii;
use yii\apidoc\helpers\IndexFileAnalyzer;
/** /**
* Base class for all Guide documentation renderers * Base class for all Guide documentation renderers
...@@ -25,4 +26,24 @@ abstract class GuideRenderer extends BaseRenderer ...@@ -25,4 +26,24 @@ abstract class GuideRenderer extends BaseRenderer
*/ */
abstract public function render($files, $targetDir); abstract public function render($files, $targetDir);
protected function loadGuideStructure($files)
{
$chapters = [];
foreach ($files as $file) {
$contents = file_get_contents($file);
if (basename($file) == 'README.md') {
$indexAnalyzer = new IndexFileAnalyzer();
$chapters = $indexAnalyzer->analyze($contents);
break;
}
if (preg_match("/^(.*)\n=+/", $contents, $matches)) {
$headlines[$file] = $matches[1];
} else {
$headlines[$file] = basename($file);
}
}
return $chapters;
}
} }
...@@ -110,6 +110,35 @@ table.summary-table .col-defined { width: 15%; } ...@@ -110,6 +110,35 @@ table.summary-table .col-defined { width: 15%; }
text-decoration: none; text-decoration: none;
} }
.hashlink {
display: none;
}
h1:hover .hashlink, h2:hover .hashlink, h3:hover .hashlink, h4:hover .hashlink, h5:hover .hashlink {
display: inline;
}
.toplink {
position: fixed;
bottom: 50px;
right: 50px;
padding: 5px 8px 0 5px;
border: solid 1px #ddd;
border-radius: 5px;
background: #fff;
}
.toplink a {
color: #bbb;
text-decoration: none;
}
.toplink a:hover {
color: #999;
text-decoration: none;
}
#search-resultbox { #search-resultbox {
position: fixed; position: fixed;
......
...@@ -5,6 +5,7 @@ use yii\apidoc\templates\bootstrap\SideNavWidget; ...@@ -5,6 +5,7 @@ use yii\apidoc\templates\bootstrap\SideNavWidget;
/** /**
* @var yii\web\View $this * @var yii\web\View $this
* @var string $content * @var string $content
* @var array $chapters
*/ */
$this->beginContent('@yii/apidoc/templates/bootstrap/layouts/main.php'); ?> $this->beginContent('@yii/apidoc/templates/bootstrap/layouts/main.php'); ?>
...@@ -12,23 +13,20 @@ $this->beginContent('@yii/apidoc/templates/bootstrap/layouts/main.php'); ?> ...@@ -12,23 +13,20 @@ $this->beginContent('@yii/apidoc/templates/bootstrap/layouts/main.php'); ?>
<div class="row"> <div class="row">
<div class="col-md-2"> <div class="col-md-2">
<?php <?php
asort($headlines);
$nav = []; $nav = [];
foreach ($headlines as $file => $headline) { foreach ($chapters as $chapter) {
if (basename($file) == 'README.md') { $items = [];
$nav[] = [ foreach($chapter['content'] as $chContent) {
'label' => $headline, $items[] = [
'url' => $this->context->generateGuideUrl($file), 'label' => $chContent['headline'],
'active' => isset($currentFile) && ($file == $currentFile), 'url' => $this->context->generateGuideUrl($chContent['file']),
'active' => isset($currentFile) && ($chContent['file'] == basename($currentFile)),
]; ];
unset($headlines[$file]);
}
} }
foreach ($headlines as $file => $headline) {
$nav[] = [ $nav[] = [
'label' => $headline, 'label' => $chapter['headline'],
'url' => $this->context->generateGuideUrl($file), // 'url' => $this->context->generateGuideUrl($file),
'active' => isset($currentFile) && ($file == $currentFile), 'items' => $items,
]; ];
} ?> } ?>
<?= SideNavWidget::widget([ <?= SideNavWidget::widget([
...@@ -39,6 +37,7 @@ $this->beginContent('@yii/apidoc/templates/bootstrap/layouts/main.php'); ?> ...@@ -39,6 +37,7 @@ $this->beginContent('@yii/apidoc/templates/bootstrap/layouts/main.php'); ?>
</div> </div>
<div class="col-md-9 guide-content" role="main"> <div class="col-md-9 guide-content" role="main">
<?= $content ?> <?= $content ?>
<div class="toplink"><a href="#" class="h1" title="go to top"><span class="glyphicon glyphicon-arrow-up"></a></div>
</div> </div>
</div> </div>
......
...@@ -78,17 +78,12 @@ abstract class GuideRenderer extends BaseGuideRenderer ...@@ -78,17 +78,12 @@ abstract class GuideRenderer extends BaseGuideRenderer
} }
$done = 0; $done = 0;
$fileData = []; $fileData = [];
$headlines = []; $chapters = $this->loadGuideStructure($files);
foreach ($files as $file) { foreach ($files as $file) {
$fileData[$file] = file_get_contents($file); $fileData[$file] = file_get_contents($file);
if (basename($file) == 'README.md') { if (basename($file) == 'README.md') {
continue; // to not add index file to nav continue; // to not add index file to nav
} }
if (preg_match("/^(.*)\n=+/", $fileData[$file], $matches)) {
$headlines[$file] = $matches[1];
} else {
$headlines[$file] = basename($file);
}
} }
foreach ($fileData as $file => $content) { foreach ($fileData as $file => $content) {
...@@ -96,7 +91,7 @@ abstract class GuideRenderer extends BaseGuideRenderer ...@@ -96,7 +91,7 @@ abstract class GuideRenderer extends BaseGuideRenderer
$output = $this->fixMarkdownLinks($output); $output = $this->fixMarkdownLinks($output);
if ($this->layout !== false) { if ($this->layout !== false) {
$params = [ $params = [
'headlines' => $headlines, 'chapters' => $chapters,
'currentFile' => $file, 'currentFile' => $file,
'content' => $output, 'content' => $output,
]; ];
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
namespace yii\apidoc\templates\pdf; namespace yii\apidoc\templates\pdf;
use cebe\markdown\latex\GithubMarkdown;
use Yii; use Yii;
use yii\apidoc\helpers\ApiMarkdownLaTeX; use yii\apidoc\helpers\ApiMarkdownLaTeX;
use yii\apidoc\helpers\IndexFileAnalyzer; use yii\apidoc\helpers\IndexFileAnalyzer;
...@@ -43,13 +42,8 @@ class GuideRenderer extends \yii\apidoc\templates\html\GuideRenderer ...@@ -43,13 +42,8 @@ class GuideRenderer extends \yii\apidoc\templates\html\GuideRenderer
} }
$done = 0; $done = 0;
$fileData = []; $fileData = [];
$chapters = []; $chapters = $this->loadGuideStructure($files);
foreach ($files as $file) { foreach ($files as $file) {
if (basename($file) == 'README.md') {
$indexAnalyzer = new IndexFileAnalyzer();
$chapters = $indexAnalyzer->analyze(file_get_contents($file));
continue; // to not add index file to nav
}
if (basename($file) == 'tutorial-i18n.md') { if (basename($file) == 'tutorial-i18n.md') {
continue; // TODO avoid i18n tut because of non displayable characters right now. need to fix it. continue; // TODO avoid i18n tut because of non displayable characters right now. need to fix it.
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment