-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
IndexFileAnalyzer.php
85 lines (75 loc) · 2.06 KB
/
IndexFileAnalyzer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/
namespace yii\apidoc\helpers;
use cebe\markdown\Markdown;
/**
* IndexFileAnalyzer analyzes index file with TOC. Typically README.md.
*/
class IndexFileAnalyzer extends Markdown
{
public $title;
public $introduction;
private $_chapter = 0;
private $_chapters = [];
/**
* Parses text and returns list of chapters got from it
* @param string $text
* @return array
*/
public function analyze($text)
{
$this->parse($text);
return $this->_chapters;
}
/**
* @inheritdoc
*/
protected function renderHeadline($block)
{
if ($this->_chapter === 0) {
$this->title = $this->renderAbsy($block['content']);
$this->introduction = '';
$this->_chapter++;
} else {
$this->_chapter++;
$this->_chapters[$this->_chapter] = [
'headline' => $this->renderAbsy($block['content']),
'content' => [],
];
}
return parent::renderHeadline($block);
}
/**
* @inheritdoc
*/
protected function renderParagraph($block)
{
if ($this->_chapter < 1) {
$this->introduction .= $this->renderAbsy($block['content']);
}
return parent::renderParagraph($block);
}
/**
* @inheritdoc
*/
protected function renderList($block)
{
if ($this->_chapter > 0) {
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'],
];
}
}
}
}
return parent::renderList($block);
}
}