GuideRenderer.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\pdf;
  8. use Yii;
  9. use yii\apidoc\helpers\ApiMarkdownLaTeX;
  10. use yii\helpers\Console;
  11. /**
  12. *
  13. * @author Carsten Brandt <mail@cebe.cc>
  14. * @since 2.0
  15. */
  16. class GuideRenderer extends \yii\apidoc\templates\html\GuideRenderer
  17. {
  18. /**
  19. * @inheritDoc
  20. */
  21. public function render($files, $targetDir)
  22. {
  23. // $types = array_merge($this->apiContext->classes, $this->apiContext->interfaces, $this->apiContext->traits);
  24. //
  25. // $extTypes = [];
  26. // foreach ($this->extensions as $k => $ext) {
  27. // $extType = $this->filterTypes($types, $ext);
  28. // if (empty($extType)) {
  29. // unset($this->extensions[$k]);
  30. // continue;
  31. // }
  32. // $extTypes[$ext] = $extType;
  33. // }
  34. $fileCount = count($files) + 1;
  35. if ($this->controller !== null) {
  36. Console::startProgress(0, $fileCount, 'Rendering markdown files: ', false);
  37. }
  38. $done = 0;
  39. $fileData = [];
  40. $chapters = $this->loadGuideStructure($files);
  41. foreach ($files as $file) {
  42. $fileData[basename($file)] = file_get_contents($file);
  43. // if (preg_match("/^(.*)\n=+/", $fileData[$file], $matches)) {
  44. // $headlines[$file] = $matches[1];
  45. // } else {
  46. // $headlines[$file] = basename($file);
  47. // }
  48. }
  49. $md = new ApiMarkdownLaTeX();
  50. $output = '';
  51. foreach ($chapters as $chapter) {
  52. if (isset($chapter['headline'])) {
  53. $output .= '\chapter{' . $chapter['headline'] . "}\n";
  54. }
  55. foreach($chapter['content'] as $content) {
  56. if (isset($fileData[$content['file']])) {
  57. $md->labelPrefix = $content['file'] . '#';
  58. $output .= '\label{'. $content['file'] . '}';
  59. $output .= $md->parse($fileData[$content['file']]) . "\n\n";
  60. } else {
  61. $output .= '\newpage';
  62. $output .= '\label{'. $content['file'] . '}';
  63. $output .= '\textbf{Error: not existing file: '.$content['file'].'}\newpage'."\n";
  64. }
  65. if ($this->controller !== null) {
  66. Console::updateProgress(++$done, $fileCount);
  67. }
  68. }
  69. }
  70. file_put_contents($targetDir . '/guide.tex', $output);
  71. copy(__DIR__ . '/main.tex', $targetDir . '/main.tex');
  72. copy(__DIR__ . '/title.tex', $targetDir . '/title.tex');
  73. copy(__DIR__ . '/Makefile', $targetDir . '/Makefile');
  74. if ($this->controller !== null) {
  75. Console::updateProgress(++$done, $fileCount);
  76. Console::endProgress(true);
  77. $this->controller->stdout('done.' . PHP_EOL, Console::FG_GREEN);
  78. }
  79. echo "\nnow run `make` in $targetDir (you need pdflatex to compile pdf file)\n\n";
  80. }
  81. }