1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* README plugin for HiDev |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hidev-readme |
6
|
|
|
* @package hidev-readme |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hidev\readme\components; |
12
|
|
|
|
13
|
|
|
use hidev\helpers\FileHelper; |
14
|
|
|
use hidev\helpers\Helper; |
15
|
|
|
use Yii; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* README generation component. |
19
|
|
|
* @author Andrii Vasyliev <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Readme extends \hidev\base\Component |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var \Twig\Environment |
25
|
|
|
*/ |
26
|
|
|
protected $_twig; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array list of sections to render |
30
|
|
|
*/ |
31
|
|
|
protected $_sections; |
32
|
|
|
|
33
|
|
|
public $knownBadges = []; |
34
|
|
|
|
35
|
|
|
public $badges; |
36
|
|
|
|
37
|
|
|
public $forceRewrite = false; |
38
|
|
|
|
39
|
|
|
public function save($path, $type) |
40
|
|
|
{ |
41
|
|
|
if ($this->forceRewrite || !file_exists($path)) { |
42
|
|
|
FileHelper::write($path, $this->render($this->getTemplate($type))); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getTemplate($type) |
47
|
|
|
{ |
48
|
|
|
return 'readme-' . $type; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get charset. |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function getCharset() |
56
|
|
|
{ |
57
|
|
|
return (isset(Yii::$app->charset) ? Yii::$app->charset : null) ?: mb_internal_encoding(); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
public function renderH($title, $prefix) |
61
|
|
|
{ |
62
|
2 |
|
return $prefix . ' ' . $title . "\n"; |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
public function renderH1($title) |
66
|
|
|
{ |
67
|
1 |
|
return $this->renderH($title, '#'); |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
public function renderH2($title) |
71
|
|
|
{ |
72
|
1 |
|
return $this->renderH($title, '##'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function renderH3($title) |
76
|
|
|
{ |
77
|
|
|
return $this->renderH($title, '###'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function renderText($text) |
81
|
|
|
{ |
82
|
|
|
$text = trim($text); |
83
|
|
|
|
84
|
|
|
return $text ? "\n$text\n" : ''; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function renderBold($text) |
88
|
|
|
{ |
89
|
|
|
$text = trim($text); |
90
|
|
|
|
91
|
|
|
return $this->renderText('**' . $text . '**'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function renderSection($section, $default = null) |
95
|
|
|
{ |
96
|
|
|
$file = 'readme/' . str_replace(' ', '', $section); |
97
|
|
|
$path = Yii::getAlias("@root/docs/$file.md"); |
98
|
|
|
$text = file_exists($path) ? file_get_contents($path) : $this->getSection($file, $default); |
99
|
|
|
$text = trim($text); |
100
|
|
|
|
101
|
|
|
return $text ? "\n## $section\n\n$text\n" : ''; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function getSection($file, $default = null) |
105
|
|
|
{ |
106
|
|
|
$tpl = '@hidev/views/' . Helper::file2template($file); |
107
|
|
|
try { |
108
|
|
|
$res = $this->render($tpl); |
109
|
|
|
} catch (\Exception $e) { |
110
|
|
|
$res = ''; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $res ?: $default; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set sections list. |
118
|
|
|
* @param array $value |
119
|
|
|
*/ |
120
|
|
|
public function setSections($value) |
121
|
|
|
{ |
122
|
|
|
$this->_sections = (array) $value; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns sections list. Returns default list if not set. |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
public function getSections() |
130
|
|
|
{ |
131
|
|
|
if (empty($this->_sections)) { |
132
|
|
|
$this->_sections = ['Requirements', 'Installation', 'Idea', 'Configuration', 'Basic Usage', 'Usage', 'Support', 'Disclaimer', 'License', 'Acknowledgements', 'Acknowledgments']; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return array_unique($this->_sections); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Render all configured sections. |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
public function renderSections($sections = null) |
143
|
|
|
{ |
144
|
|
|
if ($sections === null) { |
145
|
|
|
$sections = $this->getSections(); |
146
|
|
|
} |
147
|
|
|
$res = ''; |
148
|
|
|
foreach ($sections as $section) { |
149
|
|
|
$res .= $this->renderSection($section); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $res; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Render all configured badges. |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public function renderBadges() |
160
|
|
|
{ |
161
|
|
|
$badges = $this->badges; |
162
|
|
|
if (!$badges) { |
163
|
|
|
return ''; |
164
|
|
|
} |
165
|
|
|
$pm = $this->take('package')->getPackageManager(); |
|
|
|
|
166
|
|
|
$res = ''; |
167
|
|
|
foreach ($badges as $badge => $tpl) { |
168
|
|
|
if (!$tpl) { |
169
|
|
|
$tpl = $this->knownBadges[$badge]; |
170
|
|
|
} |
171
|
|
|
if ($tpl === 'disabled') { |
172
|
|
|
continue; |
173
|
|
|
} |
174
|
|
|
$res .= $this->renderBadge($tpl) . "\n"; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $res ? "\n$res" : ''; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Render badge by given template. |
182
|
|
|
* @param string $template string to render |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
public function renderBadge($template) |
186
|
|
|
{ |
187
|
|
|
$tpl = $this->getTwig()->createTemplate($template); |
188
|
|
|
|
189
|
|
|
return $tpl->render(['app' => Yii::$app]); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Twig getter. |
194
|
|
|
* @return \Twig\Environment |
195
|
|
|
*/ |
196
|
|
|
public function getTwig() |
197
|
|
|
{ |
198
|
|
|
if ($this->_twig === null) { |
199
|
|
|
$this->_twig = new \Twig\Environment(new \Twig\Loader\ArrayLoader()); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $this->_twig; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|