GuideRenderer.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\apidoc\templates\html;
  8. use yii\apidoc\helpers\ApiMarkdown;
  9. use yii\console\Controller;
  10. use yii\helpers\Console;
  11. use yii\apidoc\renderers\GuideRenderer as BaseGuideRenderer;
  12. use Yii;
  13. use yii\helpers\Html;
  14. use yii\helpers\Markdown;
  15. use yii\web\AssetManager;
  16. use yii\web\View;
  17. /**
  18. *
  19. * @property View $view The view instance. This property is read-only.
  20. *
  21. * @author Carsten Brandt <mail@cebe.cc>
  22. * @since 2.0
  23. */
  24. abstract class GuideRenderer extends BaseGuideRenderer
  25. {
  26. public $layout;
  27. /**
  28. * @var View
  29. */
  30. private $_view;
  31. private $_targetDir;
  32. /**
  33. * @inheritdoc
  34. */
  35. public function init()
  36. {
  37. parent::init();
  38. if ($this->pageTitle === null) {
  39. $this->pageTitle = 'The Definitive Guide to Yii 2.0';
  40. }
  41. }
  42. /**
  43. * @return View the view instance
  44. */
  45. public function getView()
  46. {
  47. if ($this->_view === null) {
  48. $this->_view = new View();
  49. $assetPath = Yii::getAlias($this->_targetDir) . '/assets';
  50. if (!is_dir($assetPath)) {
  51. mkdir($assetPath);
  52. }
  53. $this->_view->assetManager = new AssetManager([
  54. 'basePath' => $assetPath,
  55. 'baseUrl' => './assets',
  56. ]);
  57. }
  58. return $this->_view;
  59. }
  60. /**
  61. * Renders a set of files given into target directory.
  62. *
  63. * @param array $files
  64. * @param string $targetDir
  65. */
  66. public function render($files, $targetDir)
  67. {
  68. $this->_targetDir = $targetDir;
  69. $fileCount = count($files) + 1;
  70. if ($this->controller !== null) {
  71. Console::startProgress(0, $fileCount, 'Rendering markdown files: ', false);
  72. }
  73. $done = 0;
  74. $fileData = [];
  75. $chapters = $this->loadGuideStructure($files);
  76. foreach ($files as $file) {
  77. $fileData[$file] = file_get_contents($file);
  78. if (basename($file) == 'README.md') {
  79. continue; // to not add index file to nav
  80. }
  81. }
  82. foreach ($fileData as $file => $content) {
  83. $output = ApiMarkdown::process($content); // TODO generate links to yiiframework.com by default
  84. $output = $this->afterMarkdownProcess($file, $output, Markdown::$flavors['api']);
  85. if ($this->layout !== false) {
  86. $params = [
  87. 'chapters' => $chapters,
  88. 'currentFile' => $file,
  89. 'content' => $output,
  90. ];
  91. $output = $this->getView()->renderFile($this->layout, $params, $this);
  92. }
  93. $fileName = $this->generateGuideFileName($file);
  94. file_put_contents($targetDir . '/' . $fileName, $output);
  95. if ($this->controller !== null) {
  96. Console::updateProgress(++$done, $fileCount);
  97. }
  98. }
  99. if ($this->controller !== null) {
  100. Console::updateProgress(++$done, $fileCount);
  101. Console::endProgress(true);
  102. $this->controller->stdout('done.' . PHP_EOL, Console::FG_GREEN);
  103. }
  104. }
  105. /**
  106. * Callback that is called after markdown is processed.
  107. *
  108. * You may override it to do some post processing.
  109. * The default implementation fixes some markdown links using [[fixMarkdownLinks]] on the output.
  110. *
  111. * @param string $file the file that has been processed.
  112. * @param string $output the rendered HTML output.
  113. * @param ApiMarkdown $renderer the renderer instance.
  114. * @return string
  115. * @since 2.0.5
  116. */
  117. protected function afterMarkdownProcess($file, $output, $renderer)
  118. {
  119. return $this->fixMarkdownLinks($output);
  120. }
  121. /**
  122. * Given markdown file name generates resulting html file name
  123. * @param string $file markdown file name
  124. * @return string
  125. */
  126. protected function generateGuideFileName($file)
  127. {
  128. return $this->guidePrefix . basename($file, '.md') . '.html';
  129. }
  130. public function getGuideReferences()
  131. {
  132. // TODO implement for api docs
  133. // $refs = [];
  134. // foreach ($this->markDownFiles as $file) {
  135. // $refName = 'guide-' . basename($file, '.md');
  136. // $refs[$refName] = ['url' => $this->generateGuideFileName($file)];
  137. // }
  138. // return $refs;
  139. }
  140. /**
  141. * Adds guide name to link URLs in markdown
  142. * @param string $content
  143. * @return string
  144. */
  145. protected function fixMarkdownLinks($content)
  146. {
  147. $content = preg_replace('/href\s*=\s*"([^"\/]+)\.md(#.*)?"/i', 'href="' . $this->guidePrefix . '\1.html\2"', $content);
  148. return $content;
  149. }
  150. /**
  151. * @inheritdoc
  152. */
  153. protected function generateLink($text, $href, $options = [])
  154. {
  155. $options['href'] = $href;
  156. return Html::a($text, null, $options);
  157. }
  158. /**
  159. * @inheritdoc
  160. */
  161. public function generateApiUrl($typeName)
  162. {
  163. return rtrim($this->apiUrl, '/') . '/' . strtolower(str_replace('\\', '-', $typeName)) . '.html';
  164. }
  165. }