IndexFileAnalyzer.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\helpers;
  8. use cebe\markdown\Markdown;
  9. /**
  10. * IndexFileAnalyzer analyzes index file with TOC. Typically README.md.
  11. */
  12. class IndexFileAnalyzer extends Markdown
  13. {
  14. public $title;
  15. public $introduction;
  16. private $_chapter = 0;
  17. private $_chapters = [];
  18. /**
  19. * Parses text and returns list of chapters got from it
  20. * @param string $text
  21. * @return array
  22. */
  23. public function analyze($text)
  24. {
  25. $this->parse($text);
  26. return $this->_chapters;
  27. }
  28. /**
  29. * @inheritdoc
  30. */
  31. protected function renderHeadline($block)
  32. {
  33. if ($this->_chapter === 0) {
  34. $this->title = $this->renderAbsy($block['content']);
  35. $this->introduction = '';
  36. $this->_chapter++;
  37. } else {
  38. $this->_chapter++;
  39. $this->_chapters[$this->_chapter] = [
  40. 'headline' => $this->renderAbsy($block['content']),
  41. 'content' => [],
  42. ];
  43. }
  44. return parent::renderHeadline($block);
  45. }
  46. /**
  47. * @inheritdoc
  48. */
  49. protected function renderParagraph($block)
  50. {
  51. if ($this->_chapter < 1) {
  52. $this->introduction .= $this->renderAbsy($block['content']);
  53. }
  54. return parent::renderParagraph($block);
  55. }
  56. /**
  57. * @inheritdoc
  58. */
  59. protected function renderList($block)
  60. {
  61. if ($this->_chapter > 0) {
  62. foreach ($block['items'] as $item => $absyElements) {
  63. foreach($absyElements as $element) {
  64. if ($element[0] === 'link') {
  65. $this->_chapters[$this->_chapter]['content'][] = [
  66. 'headline' => $this->renderAbsy($element['text']),
  67. 'file' => $element['url'],
  68. ];
  69. }
  70. }
  71. }
  72. }
  73. return parent::renderList($block);
  74. }
  75. }