diff --git a/README.md b/README.md index 86301423..91b3e7d9 100644 --- a/README.md +++ b/README.md @@ -240,6 +240,18 @@ Special Markdown Syntax We have a special Syntax for linking to a class in the API documentation. See the [code style guide](https://github.com/yiisoft/yii2/blob/master/docs/internals/core-code-style.md#markdown) for details. +Generating documentation for your own project +--------------------------------------------- + +To generate API documentation for your own project, use the "project" template and specify the README.md of your repository using the "readmeUrl" parameter + + apidoc api YOUR_REPO_PATH OUTPUT_PATH --exclude="vendor" --template="project" --readmeUrl="file://YOUR_REPO_PATH/README.md" --pageTitle="TITLE_OF_YOUR_DOCS" + +To also include links to the Yii2 documentation, you can do + + apidoc api "YOUR_REPO_PATH,vendor/yiisoft/yii2" OUTPUT_PATH --exclude="vendor" --template="project" --readmeUrl="file://YOUR_REPO_PATH/README.md" --pageTitle="TITLE_OF_YOUR_DOCS" + + Creating your own templates --------------------------- diff --git a/commands/ApiController.php b/commands/ApiController.php index bd7d6425..3a544dd9 100644 --- a/commands/ApiController.php +++ b/commands/ApiController.php @@ -38,6 +38,12 @@ class ApiController extends BaseController */ public $repoUrl; + /** + * @var string URL for the README to use for the index of the guide. + * @since 2.0.7 + */ + public $readmeUrl = "https://raw.github.com/yiisoft/yii2-framework/master/README.md"; + // TODO add force update option /** @@ -56,6 +62,7 @@ public function actionIndex(array $sourceDirs, $targetDir) $renderer->apiUrl = './'; $renderer->guidePrefix = $this->guidePrefix; + $renderer->readmeUrl = $this->readmeUrl; if ($this->pageTitle !== null) { $renderer->pageTitle = $this->pageTitle; @@ -195,6 +202,6 @@ protected function findRenderer($template) */ public function options($actionID) { - return array_merge(parent::options($actionID), ['guide', 'guidePrefix', 'repoUrl']); + return array_merge(parent::options($actionID), ['guide', 'guidePrefix', 'readmeUrl', 'repoUrl']); } } diff --git a/renderers/BaseRenderer.php b/renderers/BaseRenderer.php index fa5c2fa0..370907bd 100644 --- a/renderers/BaseRenderer.php +++ b/renderers/BaseRenderer.php @@ -52,6 +52,12 @@ abstract class BaseRenderer extends Component public $controller; public $guideUrl; + /** + * @var string URL for the README to use for the index of the guide. + * @since 2.0.7 + */ + public $readmeUrl; + /** * @var string[] */ @@ -87,7 +93,6 @@ abstract class BaseRenderer extends Component 'int' => 'integer', ]; - public function init() { ApiMarkdown::$renderer = $this; diff --git a/templates/project/ApiRenderer.php b/templates/project/ApiRenderer.php new file mode 100644 index 00000000..7b9be1a5 --- /dev/null +++ b/templates/project/ApiRenderer.php @@ -0,0 +1,53 @@ + + * @since 2.0 + */ +class ApiRenderer extends \yii\apidoc\templates\bootstrap\ApiRenderer +{ + use \yii\apidoc\templates\bootstrap\RendererTrait; + + public $layout = '@yii/apidoc/templates/bootstrap/layouts/api.php'; + public $indexView = '@yii/apidoc/templates/bootstrap/views/index.php'; + + /** + * @inheritdoc + */ + public function render($context, $targetDir) + { + // render view files + parent::render($context, $targetDir); + + if ($this->controller !== null) { + $this->controller->stdout('generating extension index files...'); + } + + $types = array_merge($context->classes, $context->interfaces, $context->traits); + + $appTypes = $this->filterTypes($types, 'app'); + + // It's a hack, but we'll go with it for now. + $readme = @file_get_contents($this->readmeUrl); + $indexFileContent = $this->renderWithLayout($this->indexView, [ + 'docContext' => $context, + 'types' => $appTypes ?: null, + 'readme' => $readme ?: null, + ]); + + file_put_contents($targetDir . '/index.html', $indexFileContent); + } +} diff --git a/templates/project/GuideRenderer.php b/templates/project/GuideRenderer.php new file mode 100644 index 00000000..016f425c --- /dev/null +++ b/templates/project/GuideRenderer.php @@ -0,0 +1,23 @@ + + * @since 2.0 + */ +class GuideRenderer extends \yii\apidoc\templates\bootstrap\GuideRenderer +{ + use \yii\apidoc\templates\bootstrap\RendererTrait; +}