IndexFileAnalyzer.php 1.7 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 12 13
 */

namespace yii\apidoc\helpers;

use cebe\markdown\Markdown;

class IndexFileAnalyzer extends Markdown
{
14 15 16
    public $title;
    public $introduction;

17 18 19 20 21 22 23 24 25 26 27 28
    private $_chapter = 0;
    private $_chapters = [];

    public function analyze($text)
    {
        $this->parse($text);

        return $this->_chapters;
    }

    protected function renderHeadline($block)
    {
29 30 31 32 33 34 35 36 37 38 39
        if ($this->_chapter === 0) {
            $this->title = $block['content'];
            $this->introduction = '';
            $this->_chapter++;
        } else {
            $this->_chapter++;
            $this->_chapters[$this->_chapter] = [
                'headline' => $block['content'],
                'content' => [],
            ];
        }
40 41 42
        return parent::renderHeadline($block);
    }

43 44 45 46 47 48 49 50
    protected function renderParagraph($block)
    {
        if ($this->_chapter < 1) {
            $this->introduction .= implode("\n", $block['content']);
        }
        return parent::renderParagraph($block);
    }

51 52
    protected function renderList($block)
    {
53 54 55 56 57 58 59 60 61
        if ($this->_chapter > 0) {
            foreach ($block['items'] as $item => $itemLines) {
                if (preg_match('~\[([^\]]+)\]\(([^\)]+)\)(.*)~', implode("\n", $itemLines), $matches)) {
                    $this->_chapters[$this->_chapter]['content'][] = [
                        'headline' => $matches[1],
                        'file' => $matches[2],
                        'teaser' => $matches[3],
                    ];
                }
62 63 64 65
            }
        }
        return parent::renderList($block);
    }
66
}