IndexFileAnalyzer.php 2.06 KB
Newer Older
1 2
<?php
/**
3 4 5
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
6 7 8 9 10 11
 */

namespace yii\apidoc\helpers;

use cebe\markdown\Markdown;

12 13 14
/**
 * IndexFileAnalyzer analyzes index file with TOC. Typically README.md.
 */
15 16
class IndexFileAnalyzer extends Markdown
{
17 18 19
    public $title;
    public $introduction;

20 21 22
    private $_chapter = 0;
    private $_chapters = [];

23

24 25 26 27 28
    /**
     * Parses text and returns list of chapters got from it
     * @param string $text
     * @return array
     */
29 30 31 32 33 34 35
    public function analyze($text)
    {
        $this->parse($text);

        return $this->_chapters;
    }

36 37 38
    /**
     * @inheritdoc
     */
39 40
    protected function renderHeadline($block)
    {
41
        if ($this->_chapter === 0) {
42
            $this->title = $this->renderAbsy($block['content']);
43 44 45 46 47
            $this->introduction = '';
            $this->_chapter++;
        } else {
            $this->_chapter++;
            $this->_chapters[$this->_chapter] = [
48
                'headline' => $this->renderAbsy($block['content']),
49 50 51
                'content' => [],
            ];
        }
52 53 54
        return parent::renderHeadline($block);
    }

55 56 57
    /**
     * @inheritdoc
     */
58 59 60
    protected function renderParagraph($block)
    {
        if ($this->_chapter < 1) {
61
            $this->introduction .= $this->renderAbsy($block['content']);
62 63 64 65
        }
        return parent::renderParagraph($block);
    }

66 67 68
    /**
     * @inheritdoc
     */
69 70
    protected function renderList($block)
    {
71
        if ($this->_chapter > 0) {
Carsten Brandt committed
72 73 74 75 76 77 78 79
            foreach ($block['items'] as $item => $absyElements) {
                foreach($absyElements as $element) {
                    if ($element[0] === 'link') {
                        $this->_chapters[$this->_chapter]['content'][] = [
                            'headline' => $this->renderAbsy($element['text']),
                            'file' => $element['url'],
                        ];
                    }
80
                }
81 82 83 84
            }
        }
        return parent::renderList($block);
    }
85
}