-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathApiMarkdown.php
217 lines (191 loc) · 5.8 KB
/
ApiMarkdown.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
<?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\GithubMarkdown;
use yii\apidoc\models\TypeDoc;
use yii\apidoc\renderers\BaseRenderer;
use yii\helpers\Html;
use yii\helpers\Inflector;
use yii\helpers\Markdown;
/**
* A Markdown helper with support for class reference links.
*
* @author Carsten Brandt <[email protected]>
* @since 2.0
*/
class ApiMarkdown extends GithubMarkdown
{
use ApiMarkdownTrait;
use MarkdownHighlightTrait;
/**
* @var BaseRenderer
*/
public static $renderer;
/**
* @var array translation for guide block types
* @since 2.0.5
*/
public static $blockTranslations = [];
protected $renderingContext;
protected $headings = [];
/**
* @return array the headlines of this document
* @since 2.0.5
*/
public function getHeadings()
{
return $this->headings;
}
/**
* @inheritDoc
*/
protected function prepare()
{
parent::prepare();
$this->headings = [];
}
public function parse($text)
{
$markup = parent::parse($text);
$markup = $this->applyToc($markup);
return $markup;
}
/**
* @since 2.0.5
*/
protected function applyToc($content)
{
// generate TOC if there is more than one headline
if (!empty($this->headings) && count($this->headings) > 1) {
$toc = [];
foreach ($this->headings as $heading) {
$toc[] = '<li>' . Html::a(strip_tags($heading['title']), '#' . $heading['id']) . '</li>';
}
$toc = '<div class="toc"><ol>' . implode("\n", $toc) . "</ol></div>\n";
$needle = '</h1>';
$pos = strpos($content, $needle);
if ($pos !== false) {
$content = substr_replace($content, "$needle\n$toc", $pos, strlen($needle));
} else {
$content = $toc . $content;
}
}
return $content;
}
/**
* @inheritDoc
*/
protected function renderHeadline($block)
{
$content = $this->renderAbsy($block['content']);
if (preg_match('~<span id="(.*?)"></span>~', $content, $matches)) {
$hash = $matches[1];
$content = preg_replace('~<span id=".*?"></span>~', '', $content);
} else {
$hash = Inflector::slug(strip_tags($content));
}
$hashLink = "<span id=\"$hash\"></span><a href=\"#$hash\" class=\"hashlink\">¶</a>";
if ($block['level'] == 2) {
$this->headings[] = [
'title' => trim($content),
'id' => $hash,
];
} elseif ($block['level'] > 2) {
if (end($this->headings)) {
$this->headings[key($this->headings)]['sub'][] = [
'title' => trim($content),
'id' => $hash,
];
}
}
$tag = 'h' . $block['level'];
return "<$tag>$content $hashLink</$tag>";
}
/**
* @inheritdoc
*/
protected function renderLink($block)
{
$url = $block['url'];
if (!$url) {
static::$renderer->apiContext->errors[] = [
'line' => null,
'file' => null,
'message' => "Using empty link.",
];
return parent::renderLink($block);
}
$linkHtml = parent::renderLink($block);
// add special syntax for linking to the guide
$guideLinkHtml = preg_replace_callback('/href="guide:([A-z0-9-.#]+)"/i', function ($matches) {
return 'href="' . static::$renderer->generateGuideUrl($matches[1]) . '"';
}, $linkHtml, 1);
if ($guideLinkHtml !== $linkHtml) {
return $guideLinkHtml;
}
if (!property_exists(static::$renderer, 'repoUrl')) {
return $linkHtml;
}
$repoUrl = static::$renderer->repoUrl;
if (!$repoUrl) {
static::$renderer->apiContext->errors[] = [
'line' => null,
'file' => null,
'message' => "Using relative link ($url) but repoUrl is not set.",
];
return $linkHtml;
}
return preg_replace_callback('/href="(.+)"/i', function ($matches) use ($repoUrl) {
return 'href="' . $repoUrl . '/' . $matches[1] . '"';
}, $linkHtml, 1);
}
/**
* @inheritdoc
* @since 2.0.5
*/
protected function translateBlockType($type)
{
$key = ucfirst($type) . ':';
if (isset(static::$blockTranslations[$key])) {
$translation = static::$blockTranslations[$key];
} else {
$translation = $key;
}
return "$translation ";
}
/**
* Converts markdown into HTML
*
* @param string $content
* @param TypeDoc $context
* @param bool $paragraph
* @return string
*/
public static function process($content, $context = null, $paragraph = false)
{
if (!isset(Markdown::$flavors['api'])) {
Markdown::$flavors['api'] = new static;
}
if (is_string($context)) {
$context = static::$renderer->apiContext->getType($context);
}
Markdown::$flavors['api']->renderingContext = $context;
if ($paragraph) {
return Markdown::processParagraph($content, 'api');
} else {
return Markdown::process($content, 'api');
}
}
/**
* Add bootstrap classes to tables.
* @inheritdoc
*/
public function renderTable($block)
{
return str_replace('<table>', '<table class="table table-bordered table-striped">', parent::renderTable($block));
}
}