Skip to content

Commit

Permalink
Add global contents index
Browse files Browse the repository at this point in the history
  • Loading branch information
Webkadabra committed Nov 28, 2018
1 parent 3072347 commit 0ec776e
Showing 1 changed file with 94 additions and 6 deletions.
100 changes: 94 additions & 6 deletions controllers/DocsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,103 @@ public function behaviors()
*/
public function actionIndex($page=null)
{
$viewPath = \Yii::getAlias($this->module->docsPath);

// show simple global index page
if (!$page || $page == 'index')
{
$dirsLists = \yii\helpers\FileHelper::findDirectories($viewPath,['recursive'=>false]);
$links = [];
$content = [];
foreach ($dirsLists as $value) {
$view_id = str_ireplace($viewPath, '', $value);
$view_id = str_ireplace(DIRECTORY_SEPARATOR, '/', $view_id);
$view_id = trim($view_id, DIRECTORY_SEPARATOR);
$view_id = str_replace(['.php', '.md', '.txt'], ['','',''], $view_id);
$view_id = ltrim($view_id, '/');
if ($view_id) {
$fileLists = \yii\helpers\FileHelper::findFiles($value,['only'=>[
'*.md',
'*.txt'
],
]);
if ($fileLists) {
$content[] = '## [' . mb_ucfirst($view_id).']('.urldecode(Yii::$app->urlManager->createAbsoluteUrl(['/docs/docs/index', 'page' => $view_id], 1)).')';
$links = [];
foreach ($fileLists as $fileName) {
$view_id2 = str_ireplace($viewPath, '', $fileName);
$view_id2 = str_ireplace(DIRECTORY_SEPARATOR, '/', $view_id2);
$view_id2 = trim($view_id2, DIRECTORY_SEPARATOR);
$view_id2 = str_replace(['.php', '.md', '.txt'], ['','',''], $view_id2);
$view_id2 = ltrim($view_id2, '/');
$resolveTitle = str_ireplace($view_id, '', $view_id2);
$resolveTitle = ltrim($resolveTitle, '/');

$link = '['.$resolveTitle.']('.urldecode(Yii::$app->urlManager->createAbsoluteUrl(['/docs/docs/index', 'page' => $view_id2], 1)).')';
$links[$resolveTitle] = $link;
}
if ($links) {
sort($links);
$content[] = '* '.implode("\n\n* ", $links);
$content[] = '';
}
}
}

}
$tpl = Yii::t('app', 'Index') . "\n\n" . implode("\n\n", $content);
$content = Markdown::process($tpl, 'extra');
return $this->render('page', [
'content' => $content,
]);
}

if (!$page) {
$dirsLists = \yii\helpers\FileHelper::findDirectories($viewPath,['only'=>[
'*.md',
'*.txt'
]]);
foreach ($dirsLists as $value) {
$view_id = str_ireplace($viewPath, '', $value);
$view_id = str_ireplace(DIRECTORY_SEPARATOR, '/', $view_id);
$view_id = trim($view_id, DIRECTORY_SEPARATOR);
$view_id = str_replace(['.php', '.md', '.txt'], ['','',''], $view_id);
$view_id = ltrim($view_id, '/');
if ($view_id && $view_id == $page) {
$fileLists = \yii\helpers\FileHelper::findFiles($value,['only'=>[
'*.md',
'*.txt'
]]);
$links = [];
foreach ($fileLists as $fileName) {
$view_id2 = str_ireplace($viewPath, '', $fileName);
$view_id2 = str_ireplace(DIRECTORY_SEPARATOR, '/', $view_id2);
$view_id2 = trim($view_id2, DIRECTORY_SEPARATOR);
$view_id2 = str_replace(['.php', '.md', '.txt'], ['','',''], $view_id2);
$view_id2 = ltrim($view_id2, '/');
$resolveTitle = str_ireplace($view_id, '', $view_id2);
$resolveTitle = ltrim($resolveTitle, '/');
$links[] = '['.$resolveTitle.']('.urldecode(Yii::$app->urlManager->createAbsoluteUrl(['/docs/docs/index', 'page' => $view_id2], 1)).')';
}
$tpl = Yii::t('app', 'Index') . "\n\n* " . implode("\n* ", $links);
$content = Markdown::process($tpl, 'extra');
return $this->render('page', [
'content' => $content,
]);


}
}
} else {
$page = trim($page, '/');
$fileLists = \yii\helpers\FileHelper::findFiles($viewPath,['only'=>[
'*.md',
'*.txt'
]]);
}
if (!$fileLists) {
throw new NotFoundHttpException();
}
$page = trim($page, '/');
$viewPath = \Yii::getAlias($this->module->docsPath);
$fileLists = \yii\helpers\FileHelper::findFiles($viewPath,['only'=>[
'*.md',
'*.txt'
]]);
foreach ($fileLists as $value) {
$view_id = str_ireplace($viewPath, '', $value);
$view_id = str_ireplace(DIRECTORY_SEPARATOR, '/', $view_id);
Expand Down

0 comments on commit 0ec776e

Please sign in to comment.