-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathApiMarkdownTrait.php
278 lines (244 loc) · 8.54 KB
/
ApiMarkdownTrait.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/
namespace yii\apidoc\helpers;
use DOMDocument;
use yii\apidoc\models\ClassDoc;
use yii\apidoc\models\InterfaceDoc;
use yii\apidoc\models\MethodDoc;
use yii\apidoc\models\TypeDoc;
use yii\helpers\Markdown;
/**
* Class ApiMarkdownTrait
*
* @property TypeDoc $renderingContext
*/
trait ApiMarkdownTrait
{
/**
* @marker [[
*/
protected function parseApiLinks($text)
{
if (!preg_match('/^\[\[([\w\d\\\\():$]+)(\|[^]]*)?]]/', $text, $matches)) {
return [['text', '[['], 2];
}
$offset = strlen($matches[0]);
$object = $matches[1];
$title = (empty($matches[2]) || $matches[2] === '|') ? null : substr($matches[2], 1);
$title = $this->renderApiLinkText($title);
/** @var TypeDoc[] $contexts */
$contexts = [];
$this->_findContexts($this->renderingContext, $contexts);
$contexts = array_unique($contexts, SORT_REGULAR);
$contexts[] = null;
$e = null;
foreach ($contexts as $context) {
/** @var TypeDoc|null $context */
try {
return $this->parseApiLinkForContext($offset, $object, $title, $context);
} catch (BrokenLinkException $e) {
// Keep going if there are more contexts to check
}
}
// If we made it this far, there was a broken link
/** @var BrokenLinkException $e */
static::$renderer->apiContext->errors[] = [
'file' => ($e->context !== null) ? $e->context->sourceFile : null,
'message' => $e->getMessage(),
];
return [
['brokenApiLink', '<span class="broken-link">' . $object . '</span>'],
$offset
];
}
/**
* @param TypeDoc $type
* @param array $contexts
*/
private function _findContexts($type, &$contexts = [])
{
if ($type === null) {
return;
}
$contexts[] = $type;
if ($type instanceof ClassDoc) {
foreach ($type->traits as $trait) {
$this->_findContexts(static::$renderer->apiContext->getType($trait), $contexts);
}
foreach ($type->interfaces as $interface) {
$this->_findContexts(static::$renderer->apiContext->getType($interface), $contexts);
}
if ($type->parentClass) {
$this->_findContexts(static::$renderer->apiContext->getType($type->parentClass), $contexts);
}
} elseif ($type instanceof InterfaceDoc) {
foreach ($type->parentInterfaces as $interface) {
$this->_findContexts(static::$renderer->apiContext->getType($interface), $contexts);
}
}
}
/**
* Attempts to parse an API link for the given context.
*
* @param int $offset
* @param string $object
* @param string|null $title
* @param TypeDoc|null $context
* @return array
* @throws BrokenLinkException if the object can't be resolved
* @since 2.1.3
*/
protected function parseApiLinkForContext($offset, $object, $title, $context)
{
if (($pos = strpos($object, '::')) !== false) {
$typeName = substr($object, 0, $pos);
$subjectName = substr($object, $pos + 2);
if ($context !== null) {
if (!$context->phpDocContext) {
throw new BrokenLinkException($object, $context);
}
if (isset($context->phpDocContext->getNamespaceAliases()[$typeName])) {
$typeName = $context->phpDocContext->getNamespaceAliases()[$typeName];
} else {
$typeName = $context->phpDocContext->getNamespace() . '\\' . $typeName;
}
}
/** @var $type TypeDoc */
$type = static::$renderer->apiContext->getType($typeName);
if ($type === null || $subjectName === '') {
throw new BrokenLinkException($typeName . '::' . $subjectName, $context);
}
if (($subject = $type->findSubject($subjectName)) === null) {
throw new BrokenLinkException($type->name . '::' . $subjectName, $context);
}
if ($title === null) {
$title = $type->name . '::' . $subject->name;
if ($subject instanceof MethodDoc) {
$title .= '()';
}
}
return [
['apiLink', static::$renderer->createSubjectLink($subject, $title)],
$offset
];
}
if ($context !== null) {
if (($subject = $context->findSubject($object)) !== null) {
return [
['apiLink', static::$renderer->createSubjectLink($subject, $title)],
$offset
];
}
if (!$context->phpDocContext) {
throw new BrokenLinkException($object, $context);
}
if (isset($context->phpDocContext->getNamespaceAliases()[$object])) {
$object = $context->phpDocContext->getNamespaceAliases()[$object];
} else {
$object = $context->phpDocContext->getNamespace() . '\\' . $object;
}
}
if (($type = static::$renderer->apiContext->getType($object)) !== null) {
return [
['apiLink', static::$renderer->createTypeLink($type, null, $title)],
$offset
];
}
if (strpos($typeLink = static::$renderer->createTypeLink($object, null, $title), '<a href') !== false) {
return [
['apiLink', $typeLink],
$offset
];
}
throw new BrokenLinkException($object, $context);
}
/**
* Renders API link
* @param array $block
* @return string
*/
protected function renderApiLink($block)
{
return $block[1];
}
/**
* Renders API link that is broken i.e. points nowhere
* @param array $block
* @return string
*/
protected function renderBrokenApiLink($block)
{
return $block[1];
}
/**
* @param null|string $title
* @return null|string
*/
protected function renderApiLinkText($title)
{
if (!$title) {
return $title;
}
$title = Markdown::process($title);
$title = mb_convert_encoding($title, 'HTML-ENTITIES', 'UTF-8');
$doc = new DOMDocument();
$doc->loadHTML($title);
return $doc->getElementsByTagName('p')[0]->childNodes[0]->c14n();
}
/**
* Consume lines for a blockquote element
*/
protected function consumeQuote($lines, $current)
{
$block = parent::consumeQuote($lines, $current);
$blockTypes = [
'warning',
'note',
'info',
'tip',
];
// check whether this is a special Info, Note, Warning, Tip block
$content = $block[0]['content'];
$first = reset($content);
if (isset($first[0]) && $first[0] === 'paragraph') {
$parfirst = reset($first['content']);
if (isset($parfirst[0]) && $parfirst[0] === 'text') {
foreach ($blockTypes as $type) {
if (strncasecmp("$type: ", $parfirst[1], $len = strlen($type) + 2) === 0) {
// remove block indicator
$block[0]['content'][0]['content'][0][1] = substr($parfirst[1], $len);
// add translated block indicator as bold text
array_unshift($block[0]['content'][0]['content'], [
'strong',
[
['text', $this->translateBlockType($type)],
],
]);
$block[0]['blocktype'] = $type;
break;
}
}
}
}
return $block;
}
/**
* @since 2.0.5
*/
protected abstract function translateBlockType($type);
/**
* Renders a blockquote
*/
protected function renderQuote($block)
{
$class = '';
if (isset($block['blocktype'])) {
$class = ' class="' . $block['blocktype'] . '"';
}
return "<blockquote{$class}>" . $this->renderAbsy($block['content']) . "</blockquote>\n";
}
}